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);
}
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();
}
}
}
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();
}
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);
}
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());
}
Aggregations