Search in sources :

Example 1 with ExportManifest

use of org.pentaho.platform.plugin.services.importexport.exportManifest.ExportManifest in project pentaho-platform by pentaho.

the class SolutionImportHandlerTest method testImportMetaStore_nullMetastoreManifest.

@Test
public void testImportMetaStore_nullMetastoreManifest() throws Exception {
    ExportManifest manifest = Mockito.spy(new ExportManifest());
    importHandler.cachedImports = new HashMap<String, RepositoryFileImportBundle.Builder>();
    importHandler.importMetaStore(manifest, true);
    Assert.assertEquals(0, importHandler.cachedImports.size());
}
Also used : ExportManifest(org.pentaho.platform.plugin.services.importexport.exportManifest.ExportManifest) Test(org.junit.Test)

Example 2 with ExportManifest

use of org.pentaho.platform.plugin.services.importexport.exportManifest.ExportManifest in project pentaho-platform by pentaho.

the class SolutionImportHandler method importFile.

@Override
public void importFile(IPlatformImportBundle bundle) throws PlatformImportException, DomainIdNullException, DomainAlreadyExistsException, DomainStorageException, IOException {
    RepositoryFileImportBundle importBundle = (RepositoryFileImportBundle) bundle;
    ZipInputStream zipImportStream = new ZipInputStream(bundle.getInputStream());
    SolutionRepositoryImportSource importSource = new SolutionRepositoryImportSource(zipImportStream);
    LocaleFilesProcessor localeFilesProcessor = new LocaleFilesProcessor();
    setOverwriteFile(bundle.overwriteInRepository());
    // importSession.set(ImportSession.getSession());
    IPlatformImporter importer = PentahoSystem.get(IPlatformImporter.class);
    cachedImports = new HashMap<String, RepositoryFileImportBundle.Builder>();
    // Process Manifest Settings
    ExportManifest manifest = getImportSession().getManifest();
    String manifestVersion = null;
    if (manifest != null) {
        manifestVersion = manifest.getManifestInformation().getManifestVersion();
    }
    // Process Metadata
    if (manifest != null) {
        // import the users
        Map<String, List<String>> roleToUserMap = importUsers(manifest.getUserExports());
        // import the roles
        importRoles(manifest.getRoleExports(), roleToUserMap);
        List<ExportManifestMetadata> metadataList = manifest.getMetadataList();
        for (ExportManifestMetadata exportManifestMetadata : metadataList) {
            String domainId = exportManifestMetadata.getDomainId();
            if (domainId != null && !domainId.endsWith(XMI_EXTENSION)) {
                domainId = domainId + XMI_EXTENSION;
            }
            boolean overWriteInRepository = isOverwriteFile();
            RepositoryFileImportBundle.Builder bundleBuilder = new RepositoryFileImportBundle.Builder().charSet("UTF-8").hidden(RepositoryFile.HIDDEN_BY_DEFAULT).schedulable(RepositoryFile.SCHEDULABLE_BY_DEFAULT).preserveDsw(bundle.isPreserveDsw()).overwriteFile(overWriteInRepository).mime("text/xmi+xml").withParam("domain-id", domainId);
            cachedImports.put(exportManifestMetadata.getFile(), bundleBuilder);
        }
        // Process Mondrian
        List<ExportManifestMondrian> mondrianList = manifest.getMondrianList();
        for (ExportManifestMondrian exportManifestMondrian : mondrianList) {
            String catName = exportManifestMondrian.getCatalogName();
            Parameters parametersMap = exportManifestMondrian.getParameters();
            StringBuilder parametersStr = new StringBuilder();
            for (String s : parametersMap.keySet()) {
                parametersStr.append(s).append("=").append(parametersMap.get(s)).append(sep);
            }
            RepositoryFileImportBundle.Builder bundleBuilder = new RepositoryFileImportBundle.Builder().charSet("UTF_8").hidden(RepositoryFile.HIDDEN_BY_DEFAULT).schedulable(RepositoryFile.SCHEDULABLE_BY_DEFAULT).name(catName).overwriteFile(isOverwriteFile()).mime("application/vnd.pentaho.mondrian+xml").withParam("parameters", parametersStr.toString()).withParam("domain-id", // TODO: this is
            catName);
            // definitely
            // named wrong
            // at the very
            // least.
            // pass as param if not in parameters string
            String xmlaEnabled = "" + exportManifestMondrian.isXmlaEnabled();
            bundleBuilder.withParam("EnableXmla", xmlaEnabled);
            cachedImports.put(exportManifestMondrian.getFile(), bundleBuilder);
            String annotationsFile = exportManifestMondrian.getAnnotationsFile();
            if (annotationsFile != null) {
                RepositoryFileImportBundle.Builder annotationsBundle = new RepositoryFileImportBundle.Builder().path(MondrianCatalogRepositoryHelper.ETC_MONDRIAN_JCR_FOLDER + RepositoryFile.SEPARATOR + catName).name("annotations.xml").charSet("UTF_8").overwriteFile(isOverwriteFile()).mime("text/xml").hidden(RepositoryFile.HIDDEN_BY_DEFAULT).schedulable(RepositoryFile.SCHEDULABLE_BY_DEFAULT).withParam("domain-id", catName);
                cachedImports.put(annotationsFile, annotationsBundle);
            }
        }
    }
    importMetaStore(manifest, bundle.overwriteInRepository());
    for (IRepositoryFileBundle fileBundle : importSource.getFiles()) {
        String fileName = fileBundle.getFile().getName();
        String actualFilePath = fileBundle.getPath();
        if (manifestVersion != null) {
            fileName = ExportFileNameEncoder.decodeZipFileName(fileName);
            actualFilePath = ExportFileNameEncoder.decodeZipFileName(actualFilePath);
        }
        String repositoryFilePath = RepositoryFilenameUtils.concat(PentahoPlatformImporter.computeBundlePath(actualFilePath), fileName);
        if (this.cachedImports.containsKey(repositoryFilePath)) {
            byte[] bytes = IOUtils.toByteArray(fileBundle.getInputStream());
            RepositoryFileImportBundle.Builder builder = cachedImports.get(repositoryFilePath);
            builder.input(new ByteArrayInputStream(bytes));
            importer.importFile(build(builder));
            continue;
        }
        RepositoryFileImportBundle.Builder bundleBuilder = new RepositoryFileImportBundle.Builder();
        InputStream bundleInputStream = null;
        String decodedFilePath = fileBundle.getPath();
        RepositoryFile decodedFile = fileBundle.getFile();
        if (manifestVersion != null) {
            decodedFile = new RepositoryFile.Builder(decodedFile).path(decodedFilePath).name(fileName).title(fileName).build();
            decodedFilePath = ExportFileNameEncoder.decodeZipFileName(fileBundle.getPath());
        }
        if (fileBundle.getFile().isFolder()) {
            bundleBuilder.mime("text/directory");
            bundleBuilder.file(decodedFile);
            fileName = repositoryFilePath;
            repositoryFilePath = importBundle.getPath();
        } else {
            byte[] bytes = IOUtils.toByteArray(fileBundle.getInputStream());
            bundleInputStream = new ByteArrayInputStream(bytes);
            // If is locale file store it for later processing.
            if (localeFilesProcessor.isLocaleFile(fileBundle, importBundle.getPath(), bytes)) {
                log.trace("Skipping [" + repositoryFilePath + "], it is a locale property file");
                continue;
            }
            bundleBuilder.input(bundleInputStream);
            bundleBuilder.mime(solutionHelper.getMime(fileName));
            String filePath = (decodedFilePath.equals("/") || decodedFilePath.equals("\\")) ? "" : decodedFilePath;
            repositoryFilePath = RepositoryFilenameUtils.concat(importBundle.getPath(), filePath);
        }
        bundleBuilder.name(fileName);
        bundleBuilder.path(repositoryFilePath);
        String sourcePath;
        if (fileBundle.getFile().isFolder()) {
            sourcePath = fileName;
        } else {
            sourcePath = RepositoryFilenameUtils.concat(PentahoPlatformImporter.computeBundlePath(actualFilePath), fileName);
        }
        // may not have rights to such as /home or /public
        if (manifest != null && manifest.getExportManifestEntity(sourcePath) == null && fileBundle.getFile().isFolder()) {
            continue;
        }
        getImportSession().setCurrentManifestKey(sourcePath);
        bundleBuilder.charSet(bundle.getCharset());
        bundleBuilder.overwriteFile(bundle.overwriteInRepository());
        bundleBuilder.applyAclSettings(bundle.isApplyAclSettings());
        bundleBuilder.retainOwnership(bundle.isRetainOwnership());
        bundleBuilder.overwriteAclSettings(bundle.isOverwriteAclSettings());
        bundleBuilder.acl(getImportSession().processAclForFile(sourcePath));
        RepositoryFile file = getFile(importBundle, fileBundle);
        ManifestFile manifestFile = getImportSession().getManifestFile(sourcePath, file != null);
        bundleBuilder.hidden(isFileHidden(file, manifestFile, sourcePath));
        boolean isSchedulable = isSchedulable(file, manifestFile);
        if (isSchedulable) {
            bundleBuilder.schedulable(isSchedulable);
        } else {
            bundleBuilder.schedulable(fileIsScheduleInputSource(manifest, sourcePath));
        }
        IPlatformImportBundle platformImportBundle = build(bundleBuilder);
        importer.importFile(platformImportBundle);
        if (bundleInputStream != null) {
            bundleInputStream.close();
            bundleInputStream = null;
        }
    }
    if (manifest != null) {
        importSchedules(manifest.getScheduleList());
        // Add Hitachi Vantara Connections
        List<org.pentaho.database.model.DatabaseConnection> datasourceList = manifest.getDatasourceList();
        if (datasourceList != null) {
            IDatasourceMgmtService datasourceMgmtSvc = PentahoSystem.get(IDatasourceMgmtService.class);
            for (org.pentaho.database.model.DatabaseConnection databaseConnection : datasourceList) {
                if (databaseConnection.getDatabaseType() == null) {
                    // don't try to import the connection if there is no type it will cause an error
                    // However, if this is the DI Server, and the connection is defined in a ktr, it will import automatically
                    log.warn("Can't import connection " + databaseConnection.getName() + " because it doesn't have a databaseType");
                    continue;
                }
                try {
                    IDatabaseConnection existingDBConnection = datasourceMgmtSvc.getDatasourceByName(databaseConnection.getName());
                    if (existingDBConnection != null && existingDBConnection.getName() != null) {
                        if (isOverwriteFile()) {
                            databaseConnection.setId(existingDBConnection.getId());
                            datasourceMgmtSvc.updateDatasourceByName(databaseConnection.getName(), databaseConnection);
                        }
                    } else {
                        datasourceMgmtSvc.createDatasource(databaseConnection);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    // Process locale files.
    localeFilesProcessor.processLocaleFiles(importer);
}
Also used : ExportManifestMetadata(org.pentaho.platform.plugin.services.importexport.exportManifest.bindings.ExportManifestMetadata) ExportManifest(org.pentaho.platform.plugin.services.importexport.exportManifest.ExportManifest) IDatasourceMgmtService(org.pentaho.platform.api.repository.datasource.IDatasourceMgmtService) IPlatformImportBundle(org.pentaho.platform.api.repository2.unified.IPlatformImportBundle) List(java.util.List) ArrayList(java.util.ArrayList) ExportManifestMondrian(org.pentaho.platform.plugin.services.importexport.exportManifest.bindings.ExportManifestMondrian) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) IRepositoryFileBundle(org.pentaho.platform.plugin.services.importexport.ImportSource.IRepositoryFileBundle) Parameters(org.pentaho.platform.plugin.services.importexport.exportManifest.Parameters) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ManifestFile(org.pentaho.platform.plugin.services.importexport.ImportSession.ManifestFile) AlreadyExistsException(org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException) DomainStorageException(org.pentaho.metadata.repository.DomainStorageException) DomainIdNullException(org.pentaho.metadata.repository.DomainIdNullException) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) IOException(java.io.IOException) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection)

Example 3 with ExportManifest

use of org.pentaho.platform.plugin.services.importexport.exportManifest.ExportManifest in project pentaho-platform by pentaho.

the class PentahoPlatformExporterTest method testExportUsersAndRoles.

@Test
public void testExportUsersAndRoles() {
    IUserRoleListService mockDao = mock(IUserRoleListService.class);
    IAnyUserSettingService userSettingService = mock(IAnyUserSettingService.class);
    UserDetailsService userDetailsService = mock(UserDetailsService.class);
    PentahoSystem.registerObject(mockDao);
    PentahoSystem.registerObject(userSettingService);
    PentahoSystem.registerObject(userDetailsService);
    IRoleAuthorizationPolicyRoleBindingDao roleBindingDao = mock(IRoleAuthorizationPolicyRoleBindingDao.class);
    PentahoSystem.registerObject(roleBindingDao);
    String tenantPath = "path";
    when(session.getAttribute(IPentahoSession.TENANT_ID_KEY)).thenReturn(tenantPath);
    List<String> userList = new ArrayList<String>();
    String user = "testUser";
    String role = "testRole";
    userList.add(user);
    when(mockDao.getAllUsers(any(ITenant.class))).thenReturn(userList);
    List<String> roleList = new ArrayList<String>();
    roleList.add(role);
    when(mockDao.getAllRoles()).thenReturn(roleList);
    Map<String, List<String>> map = new HashMap<String, List<String>>();
    List<String> permissions = new ArrayList<String>();
    permissions.add("read");
    map.put("testRole", permissions);
    RoleBindingStruct struct = mock(RoleBindingStruct.class);
    struct.bindingMap = map;
    when(roleBindingDao.getRoleBindingStruct(anyString())).thenReturn(struct);
    ArgumentCaptor<UserExport> userCaptor = ArgumentCaptor.forClass(UserExport.class);
    ArgumentCaptor<RoleExport> roleCaptor = ArgumentCaptor.forClass(RoleExport.class);
    ExportManifest manifest = mock(ExportManifest.class);
    exporter.setExportManifest(manifest);
    List<IUserSetting> settings = new ArrayList<>();
    IUserSetting setting = mock(IUserSetting.class);
    settings.add(setting);
    when(userSettingService.getUserSettings(user)).thenReturn(settings);
    when(userSettingService.getGlobalUserSettings()).thenReturn(settings);
    List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>();
    UserDetails userDetails = new User("testUser", "testPassword", true, true, true, true, authList);
    when(userDetailsService.loadUserByUsername(anyString())).thenReturn(userDetails);
    exporter.exportUsersAndRoles();
    verify(manifest).addUserExport(userCaptor.capture());
    verify(manifest).addRoleExport(roleCaptor.capture());
    verify(userSettingService).getGlobalUserSettings();
    verify(manifest).addGlobalUserSetting(any(ExportManifestUserSetting.class));
    assertEquals(settings.size(), userCaptor.getValue().getUserSettings().size());
    UserExport userExport = userCaptor.getValue();
    assertEquals("testUser", userExport.getUsername());
    RoleExport roleExport = roleCaptor.getValue();
    assertEquals("testRole", roleExport.getRolename());
}
Also used : IRoleAuthorizationPolicyRoleBindingDao(org.pentaho.platform.security.policy.rolebased.IRoleAuthorizationPolicyRoleBindingDao) User(org.springframework.security.core.userdetails.User) HashMap(java.util.HashMap) RoleBindingStruct(org.pentaho.platform.security.policy.rolebased.RoleBindingStruct) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) ExportManifestUserSetting(org.pentaho.platform.plugin.services.importexport.ExportManifestUserSetting) Mockito.anyString(org.mockito.Mockito.anyString) ExportManifest(org.pentaho.platform.plugin.services.importexport.exportManifest.ExportManifest) IUserSetting(org.pentaho.platform.api.usersettings.pojo.IUserSetting) IAnyUserSettingService(org.pentaho.platform.api.usersettings.IAnyUserSettingService) ITenant(org.pentaho.platform.api.mt.ITenant) UserDetails(org.springframework.security.core.userdetails.UserDetails) UserExport(org.pentaho.platform.plugin.services.importexport.UserExport) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) List(java.util.List) ArrayList(java.util.ArrayList) IUserRoleListService(org.pentaho.platform.api.engine.IUserRoleListService) RoleExport(org.pentaho.platform.plugin.services.importexport.RoleExport) Test(org.junit.Test)

Example 4 with ExportManifest

use of org.pentaho.platform.plugin.services.importexport.exportManifest.ExportManifest in project pentaho-platform by pentaho.

the class PentahoPlatformExporterTest method testExportMetaStore.

@Test
public void testExportMetaStore() throws Exception {
    exporterSpy.zos = mock(ZipOutputStream.class);
    IMetaStore metastore = mock(IMetaStore.class);
    exporterSpy.setRepoMetaStore(metastore);
    ExportManifest manifest = mock(ExportManifest.class);
    exporterSpy.setExportManifest(manifest);
    exporterSpy.exportMetastore();
    verify(exporterSpy.zos).putNextEntry(any(ZipEntry.class));
    verify(manifest).setMetaStore(any(ExportManifestMetaStore.class));
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) IMetaStore(org.pentaho.metastore.api.IMetaStore) ExportManifest(org.pentaho.platform.plugin.services.importexport.exportManifest.ExportManifest) ExportManifestMetaStore(org.pentaho.platform.plugin.services.importexport.exportManifest.bindings.ExportManifestMetaStore) Test(org.junit.Test)

Example 5 with ExportManifest

use of org.pentaho.platform.plugin.services.importexport.exportManifest.ExportManifest in project pentaho-platform by pentaho.

the class RepositoryFileImportFileHandlerTest method createExportManifest.

private ExportManifest createExportManifest(RepositoryFile repoFile, RepositoryFileAcl acl) throws Exception {
    String path = repoFile.getPath();
    String parentFolder = path.substring(0, path.lastIndexOf("/"));
    ExportManifest manifest = new ExportManifest();
    manifest.getManifestInformation().setManifestVersion("2");
    manifest.getManifestInformation().setRootFolder(parentFolder);
    manifest.add(repoFile, acl);
    return manifest;
}
Also used : ExportManifest(org.pentaho.platform.plugin.services.importexport.exportManifest.ExportManifest)

Aggregations

ExportManifest (org.pentaho.platform.plugin.services.importexport.exportManifest.ExportManifest)9 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)3 List (java.util.List)2 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)2 ExportManifestMetaStore (org.pentaho.platform.plugin.services.importexport.exportManifest.bindings.ExportManifestMetaStore)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 Mockito.anyString (org.mockito.Mockito.anyString)1 IDatabaseConnection (org.pentaho.database.model.IDatabaseConnection)1 DomainAlreadyExistsException (org.pentaho.metadata.repository.DomainAlreadyExistsException)1 DomainIdNullException (org.pentaho.metadata.repository.DomainIdNullException)1 DomainStorageException (org.pentaho.metadata.repository.DomainStorageException)1 IMetaStore (org.pentaho.metastore.api.IMetaStore)1 IUserRoleListService (org.pentaho.platform.api.engine.IUserRoleListService)1