Search in sources :

Example 1 with IPlatformImportBundle

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

the class LocaleFilesProcessor method proceed.

private void proceed(IPlatformImporter importer, RepositoryFileImportBundle.Builder bundleBuilder, String mimeType, LocaleFileDescriptor localeFile) throws PlatformImportException {
    bundleBuilder.name(localeFile.getName());
    bundleBuilder.comment(localeFile.getDescription());
    bundleBuilder.path(localeFile.getPath());
    bundleBuilder.file(localeFile.getFile());
    bundleBuilder.input(localeFile.getInputStream());
    bundleBuilder.mime(mimeType);
    IPlatformImportBundle platformImportBundle = bundleBuilder.build();
    importer.importFile(platformImportBundle);
}
Also used : IPlatformImportBundle(org.pentaho.platform.api.repository2.unified.IPlatformImportBundle)

Example 2 with IPlatformImportBundle

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

the class PentahoPlatformImporter method importFile.

/**
 * this is the main method that uses the mime time (from Spring) to determine which handler to invoke.
 */
public void importFile(IPlatformImportBundle file) throws PlatformImportException {
    String mime = file.getMimeType() != null ? file.getMimeType() : mimeResolver.resolveMimeForBundle(file);
    try {
        if (mime == null) {
            log.trace(messages.getString("PentahoPlatformImporter.ERROR_0001_INVALID_MIME_TYPE") + file.getName());
            repositoryImportLogger.error(messages.getString("PentahoPlatformImporter.ERROR_0001_INVALID_MIME_TYPE") + file.getName());
            return;
        }
        IPlatformImportHandler handler = (importHandlers.containsKey(mime) == false) ? defaultHandler : importHandlers.get(mime);
        if (handler == null) {
            throw new PlatformImportException(messages.getString("PentahoPlatformImporter.ERROR_0002_MISSING_IMPORT_HANDLER"), PlatformImportException.PUBLISH_GENERAL_ERROR);
        // replace with default handler?
        }
        try {
            logImportFile(file);
            handler.importFile(file);
        } catch (DomainIdNullException e1) {
            throw new PlatformImportException(messages.getString("PentahoPlatformImporter.ERROR_0004_PUBLISH_TO_SERVER_FAILED"), PlatformImportException.PUBLISH_TO_SERVER_FAILED, e1);
        } catch (DomainAlreadyExistsException e1) {
            throw new PlatformImportException(messages.getString("PentahoPlatformImporter.ERROR_0007_PUBLISH_SCHEMA_EXISTS_ERROR"), PlatformImportException.PUBLISH_SCHEMA_EXISTS_ERROR, e1);
        } catch (DomainStorageException e1) {
            throw new PlatformImportException(messages.getString("PentahoPlatformImporter.ERROR_0004_PUBLISH_TO_SERVER_FAILED"), PlatformImportException.PUBLISH_DATASOURCE_ERROR, e1);
        } catch (IOException e1) {
            throw new PlatformImportException(messages.getString("PentahoPlatformImporter.ERROR_0005_PUBLISH_GENERAL_ERRORR", e1.getLocalizedMessage()), PlatformImportException.PUBLISH_GENERAL_ERROR, e1);
        } catch (PlatformImportException pe) {
            // if already converted - just rethrow
            throw pe;
        } catch (Exception e1) {
            throw new PlatformImportException(messages.getString("PentahoPlatformImporter.ERROR_0005_PUBLISH_GENERAL_ERRORR", e1.getLocalizedMessage()), PlatformImportException.PUBLISH_GENERAL_ERROR, e1);
        }
    } catch (Exception e) {
        e.printStackTrace();
        // If we are doing a logged import then we do not want to fail on a single file
        // so log the error and keep going.
        RepositoryFileImportBundle bundle = (RepositoryFileImportBundle) file;
        String repositoryFilePath = RepositoryFilenameUtils.concat(bundle.getPath(), bundle.getName());
        if (repositoryImportLogger.hasLogger() && repositoryFilePath != null && repositoryFilePath.length() > 0) {
            repositoryImportLogger.error(e);
        } else {
            if (e instanceof PlatformImportException) {
                throw (PlatformImportException) e;
            } else {
                // shouldn't happen but just in case
                throw new PlatformImportException(e.getMessage());
            }
        }
        if (e.getCause() instanceof UnifiedRepositoryAccessDeniedException) {
            throw new UnifiedRepositoryAccessDeniedException();
        }
    }
}
Also used : UnifiedRepositoryAccessDeniedException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryAccessDeniedException) DomainStorageException(org.pentaho.metadata.repository.DomainStorageException) DomainIdNullException(org.pentaho.metadata.repository.DomainIdNullException) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) IOException(java.io.IOException) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) DomainStorageException(org.pentaho.metadata.repository.DomainStorageException) IOException(java.io.IOException) DomainIdNullException(org.pentaho.metadata.repository.DomainIdNullException) UnifiedRepositoryAccessDeniedException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryAccessDeniedException)

Example 3 with IPlatformImportBundle

use of org.pentaho.platform.api.repository2.unified.IPlatformImportBundle 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 4 with IPlatformImportBundle

use of org.pentaho.platform.api.repository2.unified.IPlatformImportBundle 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 5 with IPlatformImportBundle

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

the class RepositoryPublishResourceIT method testImportsSuccessfully.

private void testImportsSuccessfully(String path, String filename) throws Exception {
    String full = path + '/' + filename;
    FormDataMultiPart part = new FormDataMultiPart();
    part.field("importPath", URLEncoder.encode(full, "UTF-8"), MULTIPART_FORM_DATA_TYPE);
    part.field("fileUpload", new ByteArrayInputStream(new byte[0]), MULTIPART_FORM_DATA_TYPE);
    part.field("overwriteFile", "true", MULTIPART_FORM_DATA_TYPE);
    part.getField("fileUpload").setContentDisposition(FormDataContentDisposition.name("fileUpload").fileName(URLEncoder.encode(filename, "UTF-8")).build());
    ClientResponse response = resource().path("repo/publish/file").type(MediaType.MULTIPART_FORM_DATA).accept(TEXT_PLAIN).post(ClientResponse.class, part);
    assertResponse(response, ClientResponse.Status.OK, MediaType.TEXT_PLAIN);
    ArgumentCaptor<IPlatformImportBundle> captor = ArgumentCaptor.forClass(IPlatformImportBundle.class);
    verify(importer).importFile(captor.capture());
    IPlatformImportBundle bundle = captor.getValue();
    assertEquals(path, bundle.getPath());
    assertEquals(filename, bundle.getName());
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) IPlatformImportBundle(org.pentaho.platform.api.repository2.unified.IPlatformImportBundle) ByteArrayInputStream(java.io.ByteArrayInputStream) FormDataMultiPart(com.sun.jersey.multipart.FormDataMultiPart)

Aggregations

IPlatformImportBundle (org.pentaho.platform.api.repository2.unified.IPlatformImportBundle)34 Test (org.junit.Test)21 InputStream (java.io.InputStream)15 FileInputStream (java.io.FileInputStream)14 IPlatformImporter (org.pentaho.platform.plugin.services.importer.IPlatformImporter)13 File (java.io.File)12 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)10 RepositoryFileImportBundle (org.pentaho.platform.plugin.services.importer.RepositoryFileImportBundle)8 ArrayList (java.util.ArrayList)7 Expectations (org.jmock.Expectations)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 IOException (java.io.IOException)5 Response (javax.ws.rs.core.Response)5 Matchers.anyString (org.mockito.Matchers.anyString)5 FileResource (org.pentaho.platform.web.http.api.resources.FileResource)5 FormDataContentDisposition (com.sun.jersey.core.header.FormDataContentDisposition)4 Domain (org.pentaho.metadata.model.Domain)4 XmiParser (org.pentaho.metadata.util.XmiParser)4