Search in sources :

Example 6 with IRepositoryContentConverterHandler

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

use of org.pentaho.platform.api.repository2.unified.IRepositoryContentConverterHandler 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)

Example 8 with IRepositoryContentConverterHandler

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

the class FileResource method checkCorrectExtension.

private void checkCorrectExtension(String fileName) {
    IRepositoryContentConverterHandler handler = PentahoSystem.get(IRepositoryContentConverterHandler.class);
    String ext = RepositoryFilenameUtils.getExtension(fileName);
    if (handler != null && handler.getConverter(ext) == null) {
        throw new IllegalArgumentException(Messages.getInstance().getString("FileResource.INCORRECT_EXTENSION", fileName));
    }
}
Also used : IRepositoryContentConverterHandler(org.pentaho.platform.api.repository2.unified.IRepositoryContentConverterHandler)

Aggregations

IRepositoryContentConverterHandler (org.pentaho.platform.api.repository2.unified.IRepositoryContentConverterHandler)8 Converter (org.pentaho.platform.api.repository2.unified.Converter)6 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 IMimeType (org.pentaho.platform.api.mimetype.IMimeType)3 Log4JRepositoryImportLogger (org.pentaho.platform.plugin.services.importexport.Log4JRepositoryImportLogger)3 File (java.io.File)2 Log (org.apache.commons.logging.Log)2 Before (org.junit.Before)2 IPlatformMimeResolver (org.pentaho.platform.api.mimetype.IPlatformMimeResolver)2 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)2 MimeType (org.pentaho.platform.core.mimetype.MimeType)2 StreamConverter (org.pentaho.platform.plugin.services.importexport.StreamConverter)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 BeforeClass (org.junit.BeforeClass)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1 ISolutionEngine (org.pentaho.platform.api.engine.ISolutionEngine)1