Search in sources :

Example 1 with RepositoryFileOutputStream

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

the class FileServiceTest method testDoCreateFile.

@Test
public void testDoCreateFile() throws Exception {
    RepositoryFileOutputStream mockOutputStream = mock(RepositoryFileOutputStream.class);
    doReturn(mockOutputStream).when(fileService).getRepositoryFileOutputStream(anyString());
    InputStream mockInputStream = mock(InputStream.class);
    doReturn(1).when(fileService).copy(mockInputStream, mockOutputStream);
    String charsetName = "test";
    fileService.createFile(charsetName, "testString", mockInputStream);
    verify(mockOutputStream, times(1)).setCharsetName(eq(charsetName));
    verify(mockOutputStream, times(1)).close();
    verify(mockInputStream, times(1)).close();
}
Also used : RepositoryFileInputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream) InputStream(java.io.InputStream) Matchers.anyString(org.mockito.Matchers.anyString) RepositoryFileOutputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileOutputStream) Test(org.junit.Test)

Example 2 with RepositoryFileOutputStream

use of org.pentaho.platform.repository2.unified.fileio.RepositoryFileOutputStream 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 3 with RepositoryFileOutputStream

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

the class FileService method createFile.

/**
 * Creates a new file with the provided contents at a given path
 *
 * @param pathId       (colon separated path for the repository file)
 * @param fileContents (content of the file)
 * @return
 * @throws IOException
 */
public void createFile(String charsetName, String pathId, InputStream fileContents) throws Exception {
    try {
        String idToPath = idToPath(pathId);
        RepositoryFileOutputStream rfos = getRepositoryFileOutputStream(idToPath);
        rfos.setCharsetName(charsetName);
        rfos.setAutoCreateDirStructure(true);
        copy(fileContents, rfos);
        rfos.close();
        fileContents.close();
    } catch (Exception e) {
        logger.error(Messages.getInstance().getString("SystemResource.GENERAL_ERROR"), e);
        throw e;
    }
}
Also used : RepositoryFileOutputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileOutputStream) GeneralSecurityException(java.security.GeneralSecurityException) InvalidParameterException(java.security.InvalidParameterException) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) UnifiedRepositoryAccessDeniedException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryAccessDeniedException) FileNotFoundException(java.io.FileNotFoundException) PlatformImportException(org.pentaho.platform.plugin.services.importer.PlatformImportException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ExportException(org.pentaho.platform.plugin.services.importexport.ExportException) IllegalSelectorException(java.nio.channels.IllegalSelectorException) IOException(java.io.IOException)

Example 4 with RepositoryFileOutputStream

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

the class RepositoryFileStreamProvider method getOutputStream.

public OutputStream getOutputStream() throws Exception {
    String tempOutputFilePath = outputFilePath;
    String extension = RepositoryFilenameUtils.getExtension(tempOutputFilePath);
    if ("*".equals(extension)) {
        // $NON-NLS-1$
        tempOutputFilePath = tempOutputFilePath.substring(0, tempOutputFilePath.lastIndexOf('.'));
        if (appendDateFormat != null) {
            try {
                LocalDateTime now = LocalDateTime.now();
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern(appendDateFormat);
                String formattedDate = now.format(formatter);
                tempOutputFilePath += formattedDate;
            } catch (Exception e) {
                logger.warn("Unable to calculate current date: " + e.getMessage());
            }
        }
        if (streamingAction != null) {
            String mimeType = streamingAction.getMimeType(null);
            if (mimeType != null && MimeHelper.getExtension(mimeType) != null) {
                tempOutputFilePath += MimeHelper.getExtension(mimeType);
            }
        }
    }
    RepositoryFileOutputStream outputStream = new RepositoryFileOutputStream(tempOutputFilePath, autoCreateUniqueFilename, true);
    outputStream.addListener(this);
    return outputStream;
}
Also used : LocalDateTime(java.time.LocalDateTime) DateTimeFormatter(java.time.format.DateTimeFormatter) RepositoryFileOutputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileOutputStream) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

RepositoryFileOutputStream (org.pentaho.platform.repository2.unified.fileio.RepositoryFileOutputStream)4 FileNotFoundException (java.io.FileNotFoundException)2 RepositoryFileInputStream (org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 IllegalSelectorException (java.nio.channels.IllegalSelectorException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 InvalidParameterException (java.security.InvalidParameterException)1 LocalDateTime (java.time.LocalDateTime)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)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