Search in sources :

Example 1 with RepositoryFileInputStream

use of org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream in project pentaho-platform by pentaho.

the class MondrianCatalogRepositoryHelper method getModrianSchemaFiles.

public Map<String, InputStream> getModrianSchemaFiles(String catalogName) {
    Map<String, InputStream> values = new HashMap<String, InputStream>();
    RepositoryFile catalogFolder = repository.getFile(ETC_MONDRIAN_JCR_FOLDER + RepositoryFile.SEPARATOR + catalogName);
    if (catalogFolder == null) {
        logger.warn("Catalog " + catalogName + " not found");
        throw new RepositoryException("Catalog " + catalogName + " not found");
    }
    for (RepositoryFile repoFile : repository.getChildren(catalogFolder.getId())) {
        RepositoryFileInputStream is;
        if (repoFile.getName().equals("metadata")) {
            continue;
        }
        try {
            is = new RepositoryFileInputStream(repoFile, repository);
        } catch (FileNotFoundException e) {
            throw new RepositoryException(e);
        }
        values.put(repoFile.getName(), is);
    }
    if (values.containsKey(ANNOTATIONS_XML) && values.containsKey(SCHEMA_XML)) {
        return includeAnnotatedSchema(values);
    }
    return values;
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) RepositoryFileInputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) RepositoryException(org.pentaho.platform.api.repository.RepositoryException) RepositoryFileInputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream)

Example 2 with RepositoryFileInputStream

use of org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream in project pentaho-platform by pentaho.

the class FileServiceTest method testDoGetFileOrDir.

@Test
public void testDoGetFileOrDir() throws Exception {
    RepositoryFile file = mock(RepositoryFile.class);
    doReturn("file.txt").when(file).getName();
    when(fileService.repository.getFile(anyString())).thenReturn(file);
    RepositoryFileInputStream mockInputStream = mock(RepositoryFileInputStream.class);
    doReturn(1).when(fileService).copy(any(java.io.InputStream.class), any(java.io.OutputStream.class));
    doReturn(mockInputStream).when(fileService).getRepositoryFileInputStream(any(RepositoryFile.class));
    String pathId = "/usr/folder/file.txt";
    FileService.RepositoryFileToStreamWrapper wrapper = fileService.doGetFileOrDir(pathId);
    assertEquals("file.txt", wrapper.getRepositoryFile().getName());
}
Also used : RepositoryFileInputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream) InputStream(java.io.InputStream) RepositoryFileOutputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileOutputStream) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) Matchers.anyString(org.mockito.Matchers.anyString) RepositoryFileInputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream) Test(org.junit.Test)

Example 3 with RepositoryFileInputStream

use of org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream in project pentaho-platform by pentaho.

the class ActionSequenceAction method execute.

public void execute() throws Exception {
    IOutputHandler outputHandler = null;
    if (xactionResultsOutputStream instanceof RepositoryFileOutputStream) {
        outputHandler = new RepositoryFileOutputHandler(((RepositoryFileOutputStream) xactionResultsOutputStream));
    } else {
        outputHandler = new SimpleOutputHandler(xactionResultsOutputStream, false);
    }
    IRuntimeContext rt = null;
    try {
        ISolutionEngine solutionEngine = PentahoSystem.get(ISolutionEngine.class, null);
        solutionEngine.setCreateFeedbackParameterCallback(null);
        solutionEngine.setLoggingLevel(ILogger.DEBUG);
        solutionEngine.setForcePrompt(false);
        ArrayList messages = new ArrayList();
        HashMap<String, Object> parameterProviders = new HashMap<String, Object>();
        parameterProviders.put(IParameterProvider.SCOPE_REQUEST, new SimpleParameterProvider(xActionInputParams));
        parameterProviders.put(IParameterProvider.SCOPE_SESSION, new PentahoSessionParameterProvider(PentahoSessionHolder.getSession()));
        String xactionPath = null;
        if (xactionDefInputStream instanceof RepositoryFileInputStream) {
            xactionPath = ((RepositoryFileInputStream) xactionDefInputStream).getFile().getPath();
        }
        rt = solutionEngine.execute(xactionPath, this.getClass().getName(), false, true, null, true, parameterProviders, outputHandler, null, null, messages);
        if (!outputHandler.contentDone()) {
            if ((rt != null) && (rt.getStatus() == IRuntimeContext.RUNTIME_STATUS_SUCCESS)) {
                // set content which generated by sequence for pass it to caller
                List<IContentItem> components = new ArrayList<IContentItem>(rt.getOutputContentItems());
                setActionOutputContents(components);
                boolean isFlushed = false;
                boolean isEmpty;
                if (xactionResultsOutputStream instanceof RepositoryFileOutputStream) {
                    RepositoryFileOutputStream repositoryFileOutputStream = (RepositoryFileOutputStream) xactionResultsOutputStream;
                    isFlushed = repositoryFileOutputStream.isFlushed();
                    isEmpty = repositoryFileOutputStream.size() > 0 ? false : true;
                    String extension = RepositoryFilenameUtils.getExtension(repositoryFileOutputStream.getFilePath());
                    String mimeTypeFromExtension = MimeHelper.getMimeTypeFromExtension("." + extension);
                    if (mimeTypeFromExtension == null) {
                        // unknown type, treat it not as an extension but part of the name
                        extension = "";
                    }
                    if (extension.isEmpty() && xactionResultsOutputStream.toString().isEmpty()) {
                        repositoryFileOutputStream.setFilePath(repositoryFileOutputStream.getFilePath() + ".html");
                    }
                } else {
                    isEmpty = xactionResultsOutputStream.toString().isEmpty();
                }
                if (!isFlushed) {
                    if (isEmpty) {
                        StringBuffer buffer = new StringBuffer();
                        // $NON-NLS-1$
                        MessageFormatUtils.formatSuccessMessage("text/html", rt, buffer, false);
                        xactionResultsOutputStream.write(buffer.toString().getBytes(LocaleHelper.getSystemEncoding()));
                    }
                    xactionResultsOutputStream.close();
                }
            } else {
                // we need an error message...
                StringBuffer buffer = new StringBuffer();
                // $NON-NLS-1$
                MessageFormatUtils.formatFailureMessage("text/html", rt, buffer, messages);
                xactionResultsOutputStream.write(buffer.toString().getBytes(LocaleHelper.getSystemEncoding()));
                xactionResultsOutputStream.close();
            }
        }
    } finally {
        if (rt != null) {
            rt.dispose();
        }
    }
}
Also used : ISolutionEngine(org.pentaho.platform.api.engine.ISolutionEngine) RepositoryFileOutputHandler(org.pentaho.platform.repository2.unified.fileio.RepositoryFileOutputHandler) HashMap(java.util.HashMap) PentahoSessionParameterProvider(org.pentaho.platform.engine.core.solution.PentahoSessionParameterProvider) ArrayList(java.util.ArrayList) SimpleOutputHandler(org.pentaho.platform.engine.core.output.SimpleOutputHandler) RepositoryFileInputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream) RepositoryFileOutputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileOutputStream) IOutputHandler(org.pentaho.platform.api.engine.IOutputHandler) IContentItem(org.pentaho.platform.api.repository.IContentItem) IRuntimeContext(org.pentaho.platform.api.engine.IRuntimeContext) SimpleParameterProvider(org.pentaho.platform.engine.core.solution.SimpleParameterProvider)

Example 4 with RepositoryFileInputStream

use of org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream in project pentaho-platform by pentaho.

the class FileService method doGetFileOrDir.

/**
 * Takes a pathId and returns a response object with the output stream based on the file located at the pathID
 *
 * @param pathId pathId to the file
 * @return Response object containing the file stream for the file located at the pathId, along with the mimetype,
 * and file name.
 * @throws FileNotFoundException, IllegalArgumentException
 */
public RepositoryFileToStreamWrapper doGetFileOrDir(String pathId) throws FileNotFoundException {
    String path = idToPath(pathId);
    if (!isPathValid(path)) {
        throw new IllegalArgumentException();
    }
    RepositoryFile repoFile = getRepository().getFile(path);
    if (repoFile == null) {
        // file does not exist or is not readable but we can't tell at this point
        throw new FileNotFoundException();
    }
    // check whitelist acceptance of file (based on extension)
    if (!getWhitelist().accept(repoFile.getName())) {
        // if whitelist check fails, we can still inline if you have PublishAction, otherwise we're FORBIDDEN
        if (!getPolicy().isAllowed(PublishAction.NAME)) {
            throw new IllegalArgumentException();
        }
    }
    final RepositoryFileInputStream is = getRepositoryFileInputStream(repoFile);
    StreamingOutput streamingOutput = getStreamingOutput(is);
    RepositoryFileToStreamWrapper wrapper = new RepositoryFileToStreamWrapper();
    wrapper.setOutputStream(streamingOutput);
    wrapper.setRepositoryFile(repoFile);
    wrapper.setMimetype(is.getMimeType());
    return wrapper;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) StreamingOutput(javax.ws.rs.core.StreamingOutput) RepositoryFileInputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream)

Example 5 with RepositoryFileInputStream

use of org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream in project pentaho-platform by pentaho.

the class RepositoryFileStreamProvider method getInputStream.

public InputStream getInputStream() throws Exception {
    IUnifiedRepository repository = PentahoSystem.get(IUnifiedRepository.class);
    RepositoryFile repositoryFile = repository.getFile(inputFilePath);
    if ((repositoryFile == null) || repositoryFile.isFolder()) {
        throw new FileNotFoundException();
    }
    return new RepositoryFileInputStream(repositoryFile);
}
Also used : FileNotFoundException(java.io.FileNotFoundException) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) RepositoryFileInputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream) IUnifiedRepository(org.pentaho.platform.api.repository2.unified.IUnifiedRepository)

Aggregations

RepositoryFileInputStream (org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream)7 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)6 FileNotFoundException (java.io.FileNotFoundException)4 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)3 RepositoryFileOutputStream (org.pentaho.platform.repository2.unified.fileio.RepositoryFileOutputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Test (org.junit.Test)2 Matchers.anyString (org.mockito.Matchers.anyString)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 StreamingOutput (javax.ws.rs.core.StreamingOutput)1 DomainAlreadyExistsException (org.pentaho.metadata.repository.DomainAlreadyExistsException)1 DomainIdNullException (org.pentaho.metadata.repository.DomainIdNullException)1 DomainStorageException (org.pentaho.metadata.repository.DomainStorageException)1 IOutputHandler (org.pentaho.platform.api.engine.IOutputHandler)1 IRuntimeContext (org.pentaho.platform.api.engine.IRuntimeContext)1 ISolutionEngine (org.pentaho.platform.api.engine.ISolutionEngine)1 PentahoAccessControlException (org.pentaho.platform.api.engine.PentahoAccessControlException)1 IContentItem (org.pentaho.platform.api.repository.IContentItem)1