Search in sources :

Example 1 with Converter

use of org.pentaho.platform.api.repository2.unified.Converter in project pentaho-platform by pentaho.

the class PlatformImporterTest method testNoMatchingMime.

@Test
public void testNoMatchingMime() throws Exception {
    IPlatformImportHandler mockImportHandler = mock(IPlatformImportHandler.class);
    when(mockImportHandler.getMimeTypes()).thenReturn(Collections.<IMimeType>emptyList());
    List<IPlatformImportHandler> handlers = new ArrayList<IPlatformImportHandler>();
    handlers.add(mockImportHandler);
    NameBaseMimeResolver nameResolver = new NameBaseMimeResolver();
    PentahoSystem.registerObject(nameResolver);
    PentahoPlatformImporter importer = new PentahoPlatformImporter(handlers, new DefaultRepositoryContentConverterHandler(new HashMap<String, Converter>()));
    importer.setDefaultHandler(mockImportHandler);
    FileInputStream in = new FileInputStream(new File(TestResourceLocation.TEST_RESOURCES + "/ImportTest/steel-wheels.xmi"));
    Log4JRepositoryImportLogger importLogger = new Log4JRepositoryImportLogger();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    importLogger.startJob(outputStream, "", Level.DEBUG);
    // With custom domain id
    final IPlatformImportBundle bundle1 = (new RepositoryFileImportBundle.Builder().input(in).charSet("UTF-8").hidden(false).overwriteFile(true).name("steel-wheels.xmi").comment("Test Metadata Import").withParam("domain-id", "parameterized-domain-id")).build();
    try {
        importer.setRepositoryImportLogger(importLogger);
        importer.importFile(bundle1);
        String result = new String(outputStream.toByteArray());
        assertTrue(result.contains("Error computing or retrieving mime-type"));
    } catch (PlatformImportException e) {
        e.printStackTrace();
        return;
    }
    importLogger.endJob();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileInputStream(java.io.FileInputStream) IPlatformImportBundle(org.pentaho.platform.api.repository2.unified.IPlatformImportBundle) Log4JRepositoryImportLogger(org.pentaho.platform.plugin.services.importexport.Log4JRepositoryImportLogger) File(java.io.File) Test(org.junit.Test)

Example 2 with Converter

use of org.pentaho.platform.api.repository2.unified.Converter in project pentaho-platform by pentaho.

the class PlatformImporterTest method testMatchingMimeAndHandler.

@Test
public void testMatchingMimeAndHandler() throws Exception {
    List<IMimeType> mimeList = Collections.singletonList((IMimeType) new MimeType("text/xmi+xml", "xmi"));
    IPlatformImportHandler mockImportHandler = mock(IPlatformImportHandler.class);
    when(mockImportHandler.getMimeTypes()).thenReturn(mimeList);
    List<IPlatformImportHandler> handlers = Collections.singletonList(mockImportHandler);
    NameBaseMimeResolver nameResolver = new NameBaseMimeResolver();
    PentahoSystem.registerObject(nameResolver);
    // mock logger to prevent npe
    IRepositoryImportLogger importLogger = new Log4JRepositoryImportLogger();
    PentahoPlatformImporter importer = new PentahoPlatformImporter(handlers, new DefaultRepositoryContentConverterHandler(new HashMap<String, Converter>()));
    importer.setRepositoryImportLogger(importLogger);
    FileInputStream in = new FileInputStream(new File(TestResourceLocation.TEST_RESOURCES + "/ImportTest/steel-wheels.xmi"));
    // With custom domain id
    final IPlatformImportBundle bundle1 = (new RepositoryFileImportBundle.Builder().input(in).charSet("UTF-8").hidden(false).mime("text/xmi+xml").name("steel-wheels.xmi").comment("Test Metadata Import").withParam("domain-id", "parameterized-domain-id")).build();
    importer.importFile(bundle1);
    verify(mockImportHandler, times(1)).importFile(bundle1);
}
Also used : HashMap(java.util.HashMap) IRepositoryImportLogger(org.pentaho.platform.plugin.services.importexport.IRepositoryImportLogger) IMimeType(org.pentaho.platform.api.mimetype.IMimeType) MimeType(org.pentaho.platform.core.mimetype.MimeType) FileInputStream(java.io.FileInputStream) IPlatformImportBundle(org.pentaho.platform.api.repository2.unified.IPlatformImportBundle) IMimeType(org.pentaho.platform.api.mimetype.IMimeType) Log4JRepositoryImportLogger(org.pentaho.platform.plugin.services.importexport.Log4JRepositoryImportLogger) File(java.io.File) Test(org.junit.Test)

Example 3 with Converter

use of org.pentaho.platform.api.repository2.unified.Converter in project pentaho-platform by pentaho.

the class RepositoryFileOutputStream method flush.

@Override
public void flush() throws IOException {
    if (closed) {
        return;
    }
    super.flush();
    ByteArrayInputStream bis = new ByteArrayInputStream(toByteArray());
    // make an effort to determine the correct mime type, default to application/octet-stream
    String extension = RepositoryFilenameUtils.getExtension(path);
    // $NON-NLS-1$
    String mimeType = "application/octet-stream";
    if (extension != null) {
        // $NON-NLS-1$
        String tempMimeType = MimeHelper.getMimeTypeFromExtension("." + extension);
        if (tempMimeType != null) {
            mimeType = tempMimeType;
        }
    }
    if (charsetName == null) {
        charsetName = MimeHelper.getDefaultCharset(mimeType);
    }
    // FIXME: not a good idea that we assume we are dealing with text. Best if this is somehow moved to the
    // RepositoryFileWriter
    // but I couldn't figure out a clean way to do that. For now, charsetName is passed in here and we use it if
    // available.
    final IRepositoryFileData payload;
    Converter converter;
    if (TRANS_EXT.equalsIgnoreCase(extension)) {
        converter = PentahoSystem.get(Converter.class, null, Collections.singletonMap("name", "PDITransformationStreamConverter"));
        mimeType = "application/vnd.pentaho.transformation";
    } else if (JOB_EXT.equalsIgnoreCase(extension)) {
        converter = PentahoSystem.get(Converter.class, null, Collections.singletonMap("name", "PDIJobStreamConverter"));
        mimeType = "application/vnd.pentaho.job";
    } else {
        converter = null;
    }
    payload = convert(converter, bis, mimeType);
    if (!flushed) {
        RepositoryFile file = repository.getFile(path);
        RepositoryFile parentFolder = getParent(path);
        String baseFileName = RepositoryFilenameUtils.getBaseName(path);
        if (file == null) {
            if (autoCreateDirStructure) {
                ArrayList<String> foldersToCreate = new ArrayList<>();
                String parentPath = RepositoryFilenameUtils.getFullPathNoEndSeparator(path);
                // Make sure the parent path isn't the root
                while ((parentPath != null) && (parentPath.length() > 0 && !path.equals(parentPath)) && (repository.getFile(parentPath) == null)) {
                    foldersToCreate.add(RepositoryFilenameUtils.getName(parentPath));
                    parentPath = RepositoryFilenameUtils.getFullPathNoEndSeparator(parentPath);
                }
                Collections.reverse(foldersToCreate);
                parentFolder = ((parentPath != null) && (parentPath.length() > 0)) ? repository.getFile(parentPath) : repository.getFile("/");
                if (!parentFolder.isFolder()) {
                    throw new FileNotFoundException();
                }
                for (String folderName : foldersToCreate) {
                    parentFolder = repository.createFolder(parentFolder.getId(), new RepositoryFile.Builder(folderName).folder(true).build(), null);
                }
            } else {
                if (parentFolder == null) {
                    throw new FileNotFoundException();
                }
            }
            file = buildRepositoryFile(RepositoryFilenameUtils.getName(path), extension, baseFileName);
            repository.createFile(parentFolder.getId(), file, payload, // $NON-NLS-1$
            "commit from " + RepositoryFileOutputStream.class.getName());
            for (IStreamListener listener : listeners) {
                listener.fileCreated(path);
            }
        } else if (file.isFolder()) {
            throw new FileNotFoundException(MessageFormat.format("Repository file {0} is a directory", file.getPath()));
        } else {
            if (autoCreateUniqueFileName) {
                int nameCount = 1;
                String newFileName = null;
                String newBaseFileName = null;
                List<RepositoryFile> children = repository.getChildren(parentFolder.getId());
                boolean hasFile = true;
                while (hasFile) {
                    hasFile = false;
                    nameCount++;
                    newBaseFileName = baseFileName + "(" + nameCount + ")";
                    if ((extension != null) && (extension.length() > 0)) {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        newFileName = newBaseFileName + "." + extension;
                    } else {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        newFileName = newBaseFileName;
                    }
                    for (RepositoryFile child : children) {
                        if (child.getPath().equals(parentFolder.getPath() + "/" + newFileName)) {
                            hasFile = true;
                            break;
                        }
                    }
                }
                file = buildRepositoryFile(newFileName, extension, newBaseFileName);
                // $NON-NLS-1$
                file = repository.createFile(parentFolder.getId(), file, payload, "New File");
                path = file.getPath();
                for (IStreamListener listener : listeners) {
                    listener.fileCreated(path);
                }
            } else {
                // $NON-NLS-1$
                repository.updateFile(file, payload, "New File");
                path = file.getPath();
                for (IStreamListener listener : listeners) {
                    listener.fileCreated(path);
                }
            }
        }
    } else {
        RepositoryFile file = repository.getFile(path);
        // $NON-NLS-1$
        repository.updateFile(file, payload, "New File");
    }
    flushed = true;
}
Also used : IRepositoryFileData(org.pentaho.platform.api.repository2.unified.IRepositoryFileData) IStreamListener(org.pentaho.platform.api.repository2.unified.IStreamListener) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) Converter(org.pentaho.platform.api.repository2.unified.Converter) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with Converter

use of org.pentaho.platform.api.repository2.unified.Converter in project pentaho-platform by pentaho.

the class FileResourceIT method testBrowserDownload.

// This is testing the Rest end points, we should instead be testing the underlying functionality in unit tests
@Test
public void testBrowserDownload() {
    final String text = "abcdefg";
    // mock converters map
    StreamConverter streamConverter = new StreamConverter(repo);
    Map<String, Converter> converterMap = new HashMap<String, Converter>();
    converterMap.put("txt", streamConverter);
    // stub DefaultExportProcessor
    DefaultExportHandler defaultExportHandler = new DefaultExportHandler();
    defaultExportHandler.setConverters(converterMap);
    defaultExportHandler.setRepository(repo);
    loginAsRepositoryAdmin();
    ITenant systemTenant = tenantManager.createTenant(null, ServerRepositoryPaths.getPentahoRootFolderName(), adminAuthorityName, authenticatedAuthorityName, "Anonymous");
    userRoleDao.createUser(systemTenant, sysAdminUserName, "password", "", new String[] { adminAuthorityName });
    login(sysAdminUserName, systemTenant, new String[] { adminAuthorityName, authenticatedAuthorityName });
    ITenant mainTenant_1 = tenantManager.createTenant(systemTenant, MAIN_TENANT_1, adminAuthorityName, authenticatedAuthorityName, "Anonymous");
    userRoleDao.createUser(mainTenant_1, "admin", "password", "", new String[] { adminAuthorityName });
    try {
        login("admin", mainTenant_1, new String[] { authenticatedAuthorityName, adminAuthorityName });
        mp.defineInstance(IUnifiedRepository.class, repo);
        mp.defineInstance(DefaultExportHandler.class, defaultExportHandler);
        final String fileName = "file.txt";
        createTestFile("/public".replaceAll("/", ":") + ":" + fileName, text);
        // test download of file
        WebResource webResource = resource();
        webResource.path("repo/files/public:file.txt/download").get(ClientResponse.class);
        // test download of dir as a zip file
        ClientResponse r2 = webResource.path("repo/files/public:file.txt/download").get(ClientResponse.class);
        assertResponse(r2, Status.OK);
        JerseyTestUtil.assertResponseIsZip(r2);
    } catch (Throwable ex) {
        TestCase.fail();
    } finally {
        cleanupUserAndRoles(mainTenant_1);
        cleanupUserAndRoles(systemTenant);
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) ITenant(org.pentaho.platform.api.mt.ITenant) HashMap(java.util.HashMap) StreamConverter(org.pentaho.platform.plugin.services.importexport.StreamConverter) StreamConverter(org.pentaho.platform.plugin.services.importexport.StreamConverter) Converter(org.pentaho.platform.api.repository2.unified.Converter) WebResource(com.sun.jersey.api.client.WebResource) DefaultExportHandler(org.pentaho.platform.plugin.services.importexport.DefaultExportHandler) JerseyTest(com.sun.jersey.test.framework.JerseyTest) Test(org.junit.Test)

Example 5 with Converter

use of org.pentaho.platform.api.repository2.unified.Converter in project pentaho-platform by pentaho.

the class LocaleImportHandlerTest method setUp.

@Before
public void setUp() throws Exception {
    NameBaseMimeResolver nameResolver = new NameBaseMimeResolver();
    PentahoSystem.registerObject(nameResolver);
    IRepositoryContentConverterHandler converterHandler = new DefaultRepositoryContentConverterHandler(new HashMap<String, Converter>());
    List<IMimeType> localeMimeList = new ArrayList<IMimeType>();
    localeMimeList.add(new MimeType("text/locale", "locale"));
    nameResolver.addMimeType(new MimeType("text/prptMimeType", "prpt"));
    nameResolver.addMimeType(new MimeType("text/xactionMimeType", "xaction"));
    MimeType mimeType = new MimeType("text/xml", "xml");
    mimeType.setHidden(true);
    nameResolver.addMimeType(mimeType);
    mimeType = new MimeType("image/png", "png");
    mimeType.setHidden(true);
    nameResolver.addMimeType(mimeType);
    List<String> allowedArtifacts = new ArrayList<String>();
    allowedArtifacts.add("xaction");
    allowedArtifacts.add("url");
    LocaleImportHandler localeImportHandler = new LocaleImportHandler(localeMimeList, allowedArtifacts);
    LocaleImportHandler spylocaleImportHandler = spy(localeImportHandler);
    Log log = mock(Log.class);
    doReturn(log).when(spylocaleImportHandler).getLogger();
    List<IPlatformImportHandler> handlers = new ArrayList<IPlatformImportHandler>();
    handlers.add(spylocaleImportHandler);
    importer = new PentahoPlatformImporter(handlers, converterHandler);
    importer.setRepositoryImportLogger(new Log4JRepositoryImportLogger());
    localeFilesProcessor = new LocaleFilesProcessor();
}
Also used : Log(org.apache.commons.logging.Log) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) IMimeType(org.pentaho.platform.api.mimetype.IMimeType) MimeType(org.pentaho.platform.core.mimetype.MimeType) IRepositoryContentConverterHandler(org.pentaho.platform.api.repository2.unified.IRepositoryContentConverterHandler) IMimeType(org.pentaho.platform.api.mimetype.IMimeType) Log4JRepositoryImportLogger(org.pentaho.platform.plugin.services.importexport.Log4JRepositoryImportLogger) Converter(org.pentaho.platform.api.repository2.unified.Converter) Before(org.junit.Before)

Aggregations

Converter (org.pentaho.platform.api.repository2.unified.Converter)13 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)7 IMimeType (org.pentaho.platform.api.mimetype.IMimeType)7 Log4JRepositoryImportLogger (org.pentaho.platform.plugin.services.importexport.Log4JRepositoryImportLogger)7 IRepositoryContentConverterHandler (org.pentaho.platform.api.repository2.unified.IRepositoryContentConverterHandler)6 MimeType (org.pentaho.platform.core.mimetype.MimeType)6 File (java.io.File)5 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)5 FileInputStream (java.io.FileInputStream)4 HashMap (java.util.HashMap)4 IPlatformImportBundle (org.pentaho.platform.api.repository2.unified.IPlatformImportBundle)4 IRepositoryFileData (org.pentaho.platform.api.repository2.unified.IRepositoryFileData)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 Before (org.junit.Before)3 FileNotFoundException (java.io.FileNotFoundException)2 Log (org.apache.commons.logging.Log)2 IPlatformMimeResolver (org.pentaho.platform.api.mimetype.IPlatformMimeResolver)2 IRepositoryImportLogger (org.pentaho.platform.plugin.services.importexport.IRepositoryImportLogger)2