Search in sources :

Example 6 with IContentItem

use of org.pentaho.platform.api.repository.IContentItem in project pentaho-platform by pentaho.

the class ComponentImplementationExample method setOutputMimeType.

protected void setOutputMimeType(final String mimeType) {
    IContentItem outputContentItem = runtimeContext.getOutputContentItem(mimeType);
    outputContentItem.setMimeType(mimeType);
}
Also used : IContentItem(org.pentaho.platform.api.repository.IContentItem)

Example 7 with IContentItem

use of org.pentaho.platform.api.repository.IContentItem 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 8 with IContentItem

use of org.pentaho.platform.api.repository.IContentItem in project pentaho-platform by pentaho.

the class TemplateComponent method executeAction.

@Override
protected boolean executeAction() {
    try {
        TemplateMsgAction actionDefinition = (TemplateMsgAction) getActionDefinition();
        String template = null;
        template = actionDefinition.getTemplate().getStringValue();
        if ((null == template) && isDefinedResource(TemplateComponent.TEMPLATE)) {
            // $NON-NLS-1$
            IActionSequenceResource resource = getResource("template");
            template = getResourceAsString(resource);
        }
        String outputName = (String) getOutputNames().iterator().next();
        IActionParameter outputParam = getOutputItem(outputName);
        if (outputParam.getType().equals(IActionParameter.TYPE_CONTENT)) {
            String mimeType = actionDefinition.getMimeType().getStringValue();
            String extension = actionDefinition.getExtension().getStringValue();
            // This would prevent null values being passed as parameters to getOutputItem
            if (mimeType == null) {
                // $NON-NLS-1$
                mimeType = "";
            }
            if (extension == null) {
                // $NON-NLS-1$
                extension = "";
            }
            // Removing the null check here because if we avoid null exception it gives misleading hibernate
            // stale data exception which has nothing to do with a report that simply reads data.
            IContentItem outputItem = getOutputContentItem(outputName, mimeType);
            // IContentItem outputItem = getOutputItem(outputName, mimeType, extension);
            OutputStream outputStream = outputItem.getOutputStream(getActionName());
            outputStream.write(applyInputsToFormat(template).getBytes(LocaleHelper.getSystemEncoding()));
            outputItem.closeOutputStream();
            return true;
        } else {
            setOutputValue(outputName, applyInputsToFormat(template));
        }
        return true;
    } catch (Exception e) {
        // $NON-NLS-1$
        error(Messages.getInstance().getString("Template.ERROR_0004_COULD_NOT_FORMAT_TEMPLATE"), e);
        return false;
    }
}
Also used : OutputStream(java.io.OutputStream) IContentItem(org.pentaho.platform.api.repository.IContentItem) TemplateMsgAction(org.pentaho.actionsequence.dom.actions.TemplateMsgAction) IActionParameter(org.pentaho.platform.api.engine.IActionParameter) IActionSequenceResource(org.pentaho.platform.api.engine.IActionSequenceResource)

Example 9 with IContentItem

use of org.pentaho.platform.api.repository.IContentItem in project pentaho-platform by pentaho.

the class RepositoryFileOutputHandler method getOutputContentItem.

public IContentItem getOutputContentItem(final String outputName, final String contentName, final String instanceId, final String localMimeType) {
    IContentItem outputContentItem = null;
    if (outputName.equals(IOutputHandler.RESPONSE) && contentName.equals(IOutputHandler.CONTENT)) {
        String requestedFileExtension = MimeHelper.getExtension(localMimeType);
        String currentExtension = FilenameUtils.getExtension(outputStream.getFilePath());
        if (requestedFileExtension == null) {
            if (currentExtension != null) {
                String tempFilePath = FilenameUtils.getFullPathNoEndSeparator(outputStream.getFilePath()) + "/" + FilenameUtils.getBaseName(outputStream.getFilePath());
                outputStream.setFilePath(tempFilePath);
                outputContentItem = new RepositoryFileContentItem(outputStream);
            } else {
                outputContentItem = new RepositoryFileContentItem(outputStream);
            }
        } else if (!requestedFileExtension.substring(1).equals(currentExtension.toLowerCase())) {
            String tempFilePath = FilenameUtils.getFullPathNoEndSeparator(outputStream.getFilePath()) + "/" + FilenameUtils.getBaseName(outputStream.getFilePath()) + requestedFileExtension;
            outputStream.setFilePath(tempFilePath);
            outputContentItem = new RepositoryFileContentItem(outputStream);
        } else {
            outputContentItem = new RepositoryFileContentItem(outputStream);
        }
        responseExpected = true;
    } else {
        IContentOutputHandler output = PentahoSystem.getOutputDestinationFromContentRef(contentName, session);
        // (This mirrors HttpOutputHandler's lookup logic)
        if (output == null) {
            // $NON-NLS-1$
            output = PentahoSystem.getOutputDestinationFromContentRef(outputName + ":" + contentName, session);
        }
        if (output != null) {
            output.setSession(PentahoSessionHolder.getSession());
            output.setInstanceId(instanceId);
            // $NON-NLS-1$
            String filePath = "~/workspace/" + contentName;
            if (contentName.startsWith("/")) {
                filePath = contentName;
            }
            output.setSolutionPath(filePath);
            output.setMimeType(localMimeType);
            outputContentItem = output.getFileOutputContentItem();
        }
    }
    return outputContentItem;
}
Also used : IContentOutputHandler(org.pentaho.platform.api.engine.IContentOutputHandler) IContentItem(org.pentaho.platform.api.repository.IContentItem)

Example 10 with IContentItem

use of org.pentaho.platform.api.repository.IContentItem in project pentaho-platform by pentaho.

the class RepositoryContentOutputHandler method getFileOutputContentItem.

public IContentItem getFileOutputContentItem() {
    String filePath = getSolutionPath();
    if (StringUtils.isEmpty(filePath)) {
        filePath = getContentRef();
    }
    if (filePath.startsWith("~/") || filePath.startsWith("~\\") || filePath.equals("~")) {
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        // $NON-NLS-1$
        filePath = ClientRepositoryPaths.getUserHomeFolderPath(getSession().getName()) + "/";
        filePath = filePath + (getSolutionPath().length() > 1 ? getSolutionPath().substring(2) : getSolutionPath().substring(1));
    }
    filePath = replaceIllegalChars(filePath);
    IContentItem contentItem = null;
    String requestedFileExtension = MimeHelper.getExtension(getMimeType());
    if (requestedFileExtension == null) {
        contentItem = new RepositoryFileContentItem(filePath);
    } else {
        String tempFilePath = FilenameUtils.getFullPathNoEndSeparator(filePath) + "/" + FilenameUtils.getBaseName(filePath) + requestedFileExtension;
        contentItem = new RepositoryFileContentItem(tempFilePath);
    }
    return contentItem;
}
Also used : IContentItem(org.pentaho.platform.api.repository.IContentItem)

Aggregations

IContentItem (org.pentaho.platform.api.repository.IContentItem)49 OutputStream (java.io.OutputStream)20 Test (org.junit.Test)8 IActionParameter (org.pentaho.platform.api.engine.IActionParameter)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 SimpleOutputHandler (org.pentaho.platform.engine.core.output.SimpleOutputHandler)6 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 Iterator (java.util.Iterator)5 List (java.util.List)5 Element (org.dom4j.Element)5 SimpleContentItem (org.pentaho.platform.engine.core.output.SimpleContentItem)5 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 Document (org.dom4j.Document)4 IPentahoResultSet (org.pentaho.commons.connection.IPentahoResultSet)4 IRuntimeContext (org.pentaho.platform.api.engine.IRuntimeContext)4 InvalidParameterException (org.pentaho.platform.api.engine.InvalidParameterException)4 Set (java.util.Set)3 IActionSequenceResource (org.pentaho.platform.api.engine.IActionSequenceResource)3