Search in sources :

Example 11 with Converter

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;
    }
}
Also used : IRepositoryFileData(org.pentaho.platform.api.repository2.unified.IRepositoryFileData) Converter(org.pentaho.platform.api.repository2.unified.Converter) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) IOException(java.io.IOException)

Example 12 with Converter

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());
}
Also used : IRepositoryContentConverterHandler(org.pentaho.platform.api.repository2.unified.IRepositoryContentConverterHandler) InputStream(java.io.InputStream) Converter(org.pentaho.platform.api.repository2.unified.Converter)

Example 13 with Converter

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());
}
Also used : Log(org.apache.commons.logging.Log) ArrayList(java.util.ArrayList) IMimeType(org.pentaho.platform.api.mimetype.IMimeType) MimeType(org.pentaho.platform.core.mimetype.MimeType) IPlatformImportBundle(org.pentaho.platform.api.repository2.unified.IPlatformImportBundle) 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) Test(org.junit.Test)

Example 14 with Converter

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);
}
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 15 with Converter

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);
}
Also used : IRepositoryFileData(org.pentaho.platform.api.repository2.unified.IRepositoryFileData) ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) NodeRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.node.NodeRepositoryFileData) Converter(org.pentaho.platform.api.repository2.unified.Converter) Test(org.junit.Test)

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