Search in sources :

Example 16 with Converter

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

the class SolutionImportHandlerNamingIT method init.

@Before
public void init() throws IOException, PlatformInitializationException, PlatformImportException, DomainIdNullException, DomainAlreadyExistsException, DomainStorageException {
    // repository
    File repoDir = new File(tempDir.getAbsolutePath() + REPO_PATH);
    FileUtils.forceMkdir(repoDir);
    FileUtils.cleanDirectory(repoDir);
    repoRoot = repoDir;
    repo = new FileSystemBackedUnifiedRepository();
    repo = Mockito.spy(repo);
    repo.setRootDir(repoRoot);
    // mimeResolver
    final Converter defaultConverter = new StreamConverter();
    final List<IMimeType> solutionMimeList = java.util.Collections.singletonList(MIME_SOLUTION);
    final List<IMimeType> contentMimeList = java.util.Arrays.asList(new IMimeType[] { MIME_PRPT, MIME_XML });
    final List<IMimeType> allMimeTypes = new ArrayList<IMimeType>(solutionMimeList.size() + contentMimeList.size());
    {
        allMimeTypes.addAll(solutionMimeList);
        allMimeTypes.addAll(contentMimeList);
        for (IMimeType mimeType : allMimeTypes) {
            mimeType.setConverter(defaultConverter);
        }
    }
    final IPlatformMimeResolver mimeResolver = new NameBaseMimeResolver();
    for (IMimeType mimeType : allMimeTypes) {
        mimeResolver.addMimeType(mimeType);
    }
    // platform, import handlers
    PentahoSystem.clearObjectFactory();
    microPlatform = new MicroPlatform(getSolutionPath());
    microPlatform.defineInstance(IUnifiedRepository.class, repo);
    microPlatform.defineInstance(IPlatformMimeResolver.class, mimeResolver);
    microPlatform.defineInstance(ISolutionEngine.class, Mockito.mock(SolutionEngine.class));
    microPlatform.defineInstance(IDatasourceMgmtService.class, Mockito.mock(IDatasourceMgmtService.class));
    IRepositoryContentConverterHandler converterHandler = new DefaultRepositoryContentConverterHandler(new HashMap<String, Converter>());
    RepositoryFileImportFileHandler contentImportFileHandler = new RepositoryFileImportFileHandler(contentMimeList);
    contentImportFileHandler.setRepository(repo);
    solutionImportHandler = new SolutionImportHandler(solutionMimeList);
    List<IPlatformImportHandler> handlers = new ArrayList<IPlatformImportHandler>();
    handlers.add(contentImportFileHandler);
    handlers.add(solutionImportHandler);
    PentahoPlatformImporter importer = new PentahoPlatformImporter(handlers, converterHandler);
    importer.setDefaultHandler(contentImportFileHandler);
    importer.setRepositoryImportLogger(new Log4JRepositoryImportLogger());
    microPlatform.defineInstance(IPlatformImporter.class, importer);
    microPlatform.start();
}
Also used : ArrayList(java.util.ArrayList) ISolutionEngine(org.pentaho.platform.api.engine.ISolutionEngine) SolutionEngine(org.pentaho.platform.engine.services.solution.SolutionEngine) IDatasourceMgmtService(org.pentaho.platform.api.repository.datasource.IDatasourceMgmtService) IMimeType(org.pentaho.platform.api.mimetype.IMimeType) IRepositoryContentConverterHandler(org.pentaho.platform.api.repository2.unified.IRepositoryContentConverterHandler) IPlatformMimeResolver(org.pentaho.platform.api.mimetype.IPlatformMimeResolver) Log4JRepositoryImportLogger(org.pentaho.platform.plugin.services.importexport.Log4JRepositoryImportLogger) StreamConverter(org.pentaho.platform.plugin.services.importexport.StreamConverter) MicroPlatform(org.pentaho.test.platform.engine.core.MicroPlatform) Converter(org.pentaho.platform.api.repository2.unified.Converter) StreamConverter(org.pentaho.platform.plugin.services.importexport.StreamConverter) File(java.io.File) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) FileSystemBackedUnifiedRepository(org.pentaho.platform.repository2.unified.fs.FileSystemBackedUnifiedRepository) Before(org.junit.Before)

Example 17 with Converter

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

the class ActionSequenceResource method getInputStream.

@SuppressWarnings({ "resource", "deprecation" })
public static InputStream getInputStream(String filePath, Locale locale) {
    InputStream inputStream = null;
    if (filePath.startsWith("system")) {
        File file = null;
        filePath = PentahoSystem.getApplicationContext().getSolutionPath(filePath);
        if (locale == null) {
            file = new File(filePath);
        } else {
            String extension = FilenameUtils.getExtension(filePath);
            String baseName = FilenameUtils.removeExtension(filePath);
            if (extension.length() > 0) {
                // $NON-NLS-1$
                extension = "." + extension;
            }
            String language = locale.getLanguage();
            String country = locale.getCountry();
            String variant = locale.getVariant();
            if (!variant.equals("")) {
                // $NON-NLS-1$
                // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                file = new File(baseName + "_" + language + "_" + country + "_" + variant + extension);
            }
            if ((file == null) || !file.exists()) {
                // $NON-NLS-1$//$NON-NLS-2$
                file = new File(baseName + "_" + language + "_" + country + extension);
            }
            if ((file == null) || !file.exists()) {
                // $NON-NLS-1$
                file = new File(baseName + "_" + language + extension);
            }
            if ((file == null) || !file.exists()) {
                file = new File(filePath);
            }
        }
        if (file != null) {
            try {
                inputStream = new FileInputStream(file);
            } catch (FileNotFoundException ex) {
            // Do nothing we'll just return a null input stream;
            }
        }
    } else {
        // This is not a file from the system folder. User is trying to access a resource in the repository.
        // Get the RepositoryContentConverterHandler
        IRepositoryContentConverterHandler converterHandler = PentahoSystem.get(IRepositoryContentConverterHandler.class);
        RepositoryFile repositoryFile = null;
        if (locale == null) {
            repositoryFile = getRepository().getFile(filePath);
            String extension = FilenameUtils.getExtension(filePath);
            try {
                // assume simple type and will get the data that way
                if (converterHandler != null) {
                    Converter converter = converterHandler.getConverter(extension);
                    if (converter != null) {
                        inputStream = converter.convert(repositoryFile.getId());
                    }
                }
                if (inputStream == null) {
                    inputStream = getRepository().getDataForRead(repositoryFile.getId(), SimpleRepositoryFileData.class).getStream();
                }
            } catch (UnifiedRepositoryException ure) {
            // ignored
            }
        } else {
            String extension = FilenameUtils.getExtension(filePath);
            String baseName = FilenameUtils.removeExtension(filePath);
            if (extension.length() > 0) {
                // $NON-NLS-1$
                extension = "." + extension;
            }
            String language = locale.getLanguage();
            String country = locale.getCountry();
            String variant = locale.getVariant();
            if (!variant.equals("")) {
                // $NON-NLS-1$
                repositoryFile = // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                getRepository().getFile(baseName + "_" + language + "_" + country + "_" + variant + extension);
                try {
                    if (repositoryFile != null) {
                        // assume simple type and will get the data that way
                        if (converterHandler != null) {
                            Converter converter = converterHandler.getConverter(FilenameUtils.getExtension(filePath));
                            if (converter != null) {
                                inputStream = converter.convert(repositoryFile.getId());
                            }
                        }
                        if (inputStream == null) {
                            inputStream = getRepository().getDataForRead(repositoryFile.getId(), SimpleRepositoryFileData.class).getStream();
                        }
                    }
                } catch (UnifiedRepositoryException ure) {
                // ignored
                }
            }
            if (inputStream == null) {
                // $NON-NLS-1$//$NON-NLS-2$
                repositoryFile = getRepository().getFile(baseName + "_" + language + "_" + country + extension);
                try {
                    if (repositoryFile != null) {
                        // assume simple type and will get the data that way
                        if (converterHandler != null) {
                            Converter converter = converterHandler.getConverter(FilenameUtils.getExtension(filePath));
                            if (converter != null) {
                                inputStream = converter.convert(repositoryFile.getId());
                            }
                        }
                        if (inputStream == null) {
                            inputStream = getRepository().getDataForRead(repositoryFile.getId(), SimpleRepositoryFileData.class).getStream();
                        }
                    }
                } catch (UnifiedRepositoryException ure) {
                // ignored
                }
            }
            if (inputStream == null) {
                // $NON-NLS-1$
                repositoryFile = getRepository().getFile(baseName + "_" + language + extension);
                try {
                    if (repositoryFile != null) {
                        // assume simple type and will get the data that way
                        if (converterHandler != null) {
                            Converter converter = converterHandler.getConverter(FilenameUtils.getExtension(filePath));
                            if (converter != null) {
                                inputStream = converter.convert(repositoryFile.getId());
                            }
                        }
                        if (inputStream == null) {
                            inputStream = getRepository().getDataForRead(repositoryFile.getId(), SimpleRepositoryFileData.class).getStream();
                        }
                    }
                } catch (UnifiedRepositoryException ure) {
                // ignored
                }
            }
            if (inputStream == null) {
                repositoryFile = getRepository().getFile(filePath);
                try {
                    if (repositoryFile != null) {
                        // assume simple type and will get the data that way
                        if (converterHandler != null) {
                            Converter converter = converterHandler.getConverter(FilenameUtils.getExtension(filePath));
                            if (converter != null) {
                                inputStream = converter.convert(repositoryFile.getId());
                            }
                        }
                        if (inputStream == null) {
                            inputStream = getRepository().getDataForRead(repositoryFile.getId(), SimpleRepositoryFileData.class).getStream();
                        }
                    }
                } catch (UnifiedRepositoryException ure) {
                // ignored
                }
            }
        }
    }
    return inputStream;
}
Also used : IRepositoryContentConverterHandler(org.pentaho.platform.api.repository2.unified.IRepositoryContentConverterHandler) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) Converter(org.pentaho.platform.api.repository2.unified.Converter) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) File(java.io.File) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) FileInputStream(java.io.FileInputStream)

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