use of org.pentaho.platform.api.repository2.unified.Converter in project pentaho-platform by pentaho.
the class RepositoryFileImportFileHandler method copyFileToRepository.
/**
* Copies the file bundle into the repository
*
* @param bundle
* @param repositoryPath
* @param file
*/
protected boolean copyFileToRepository(final RepositoryFileImportBundle bundle, final String repositoryPath, final RepositoryFile file) throws PlatformImportException {
// Compute the file extension
final String name = bundle.getName();
final String ext = RepositoryFilenameUtils.getExtension(name);
if (StringUtils.isEmpty(ext)) {
getLogger().debug("Skipping file without extension: " + name);
return false;
}
// Check the mime type
final String mimeType = bundle.getMimeType();
if (mimeType == null) {
getLogger().debug("Skipping file without mime-type: " + name);
return false;
}
// Copy the file into the repository
try {
getLogger().trace("copying file to repository: " + name);
if (getMimeTypeMap().get(mimeType) == null) {
getLogger().debug("Skipping file - mime type of " + mimeType + " is not registered :" + name);
}
Converter converter = getMimeTypeMap().get(mimeType).getConverter();
if (converter == null) {
getLogger().debug("Skipping file without converter: " + name);
return false;
}
RepositoryFile repositoryFile;
IRepositoryFileData data = converter.convert(bundle.getInputStream(), bundle.getCharset(), mimeType);
if (null == file) {
repositoryFile = createFile(bundle, repositoryPath, data);
if (repositoryFile != null) {
updateAclFromBundle(true, bundle, repositoryFile);
}
} else {
repositoryFile = updateFile(bundle, file, data);
updateAclFromBundle(false, bundle, repositoryFile);
}
converter.convertPostRepoSave(repositoryFile);
if (repositoryFile != null) {
getImportSession().addImportedRepositoryFile(repositoryFile);
}
return true;
} catch (IOException e) {
// TODO make sure
getLogger().warn(messages.getString("DefaultImportHandler.WARN_0003_IOEXCEPTION", name), e);
// string exists
return false;
}
}
use of org.pentaho.platform.api.repository2.unified.Converter in project pentaho-platform by pentaho.
the class DefaultExportHandler method doExport.
/**
* Perform export with registered handlers
*/
@Override
public InputStream doExport(RepositoryFile repositoryFile, String filePath) throws ExportException, IOException {
InputStream is = null;
// Compute the file extension
final String name = repositoryFile.getName();
final String ext = RepositoryFilenameUtils.getExtension(name);
if (StringUtils.isEmpty(ext)) {
log.debug("Skipping file without extension: " + name);
}
// Find the converter - defined in spring xml by import handlers
if (converters == null) {
IRepositoryContentConverterHandler converterHandler = PentahoSystem.get(IRepositoryContentConverterHandler.class);
converters = converterHandler.getConverters();
}
final Converter converter = converters.get(ext);
if (converter == null) {
log.debug("Skipping file without converter: " + name);
return null;
}
// since it is already based on the file extension
return converter.convert(repositoryFile.getId());
}
use of org.pentaho.platform.api.repository2.unified.Converter in project pentaho-platform by pentaho.
the class LocaleFilesProcessorTest method testProcessLocaleFilesTwoLocaleFiles.
@Test
public void testProcessLocaleFilesTwoLocaleFiles() throws Exception {
IRepositoryContentConverterHandler converterHandler = new DefaultRepositoryContentConverterHandler(new HashMap<String, Converter>());
List<IMimeType> localeMimeList = new ArrayList<IMimeType>();
localeMimeList.add(new MimeType("text/locale", "locale"));
LocaleImportHandler localeImportHandler = new LocaleImportHandler(localeMimeList, null);
LocaleImportHandler localeImportHandlerSpy = spy(localeImportHandler);
Log log = mock(Log.class);
doReturn(log).when(localeImportHandlerSpy).getLogger();
String localeFileName = "SampleTransformationWithParameters";
List<IPlatformImportHandler> handlers = new ArrayList<IPlatformImportHandler>();
handlers.add(localeImportHandlerSpy);
importer = new PentahoPlatformImporter(handlers, converterHandler);
importer.setRepositoryImportLogger(new Log4JRepositoryImportLogger());
localeFilesProcessor = new LocaleFilesProcessor();
StringBuffer localePropertiesContent = new StringBuffer();
localePropertiesContent.append("description=This runs a simple Kettle transformation filtering records from the Quandrant_Actuals sample data " + "table, and sending a Hello message to each position.\n");
localePropertiesContent.append("name=1. Hello ETL");
assertTrue(processIsLocalFile("SampleTransformation.properties", localePropertiesContent));
StringBuffer localeContent = new StringBuffer();
localeContent.append("file.title=" + localeFileName + "\n");
localeContent.append("title=SampleTransformation\n");
localeContent.append("file.description=");
assertTrue(processIsLocalFile("SampleTransformation.xaction.locale", localeContent));
localeFilesProcessor.processLocaleFiles(importer);
// verify that in case of both .properties and .locale files are at input the only one proceeded is .locale
Mockito.verify(localeImportHandlerSpy, times(1)).importFile(any(IPlatformImportBundle.class));
ArgumentCaptor<IPlatformImportBundle> argument = ArgumentCaptor.forClass(IPlatformImportBundle.class);
Mockito.verify(localeImportHandlerSpy).importFile(argument.capture());
assertEquals(localeFileName, argument.getValue().getName());
}
use of org.pentaho.platform.api.repository2.unified.Converter in project pentaho-platform by pentaho.
the class PlatformImporterTest method testUseDefaultHandler.
@Test
public void testUseDefaultHandler() throws Exception {
List<IMimeType> mimeList = Collections.singletonList((IMimeType) new MimeType("text/html", "html"));
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>()));
IPlatformImportHandler mockDefaultImportHandler = mock(IPlatformImportHandler.class);
importer.setDefaultHandler(mockDefaultImportHandler);
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(mockDefaultImportHandler, times(1)).importFile(bundle1);
}
use of org.pentaho.platform.api.repository2.unified.Converter in project pentaho-platform by pentaho.
the class RepositoryFileOutputStreamTest method convertTest.
@Test
public void convertTest() throws Exception {
RepositoryFileOutputStream spy = Mockito.spy(new RepositoryFileOutputStream("1.ktr", "UTF-8"));
Converter converter = Mockito.mock(Converter.class);
ByteArrayInputStream bis = Mockito.mock(ByteArrayInputStream.class);
Mockito.doReturn(Mockito.mock(NodeRepositoryFileData.class)).when(converter).convert(bis, "UTF-8", "");
IRepositoryFileData data = spy.convert(null, bis, "");
Assert.assertTrue(data instanceof SimpleRepositoryFileData);
data = spy.convert(converter, bis, "");
Assert.assertTrue(data instanceof NodeRepositoryFileData);
}
Aggregations