Search in sources :

Example 1 with SimpleContentItem

use of org.pentaho.platform.engine.core.output.SimpleContentItem in project pentaho-platform by pentaho.

the class FileOutputHandler method getFileOutputContentItem.

@Override
public IContentItem getFileOutputContentItem() {
    String contentRef = getContentRef();
    File file = new File(contentRef);
    File dir = file.getParentFile();
    if ((dir != null) && !dir.exists()) {
        boolean result = dir.mkdirs();
        if (!result) {
            try {
                URI uri = new URI(contentRef);
                file = new File(uri);
                dir = file.getParentFile();
            } catch (URISyntaxException e) {
                logger.error(Messages.getInstance().getErrorString("FileOutputHandler.ERROR_0001_COULD_NOT_CREATE_DIRECTORY", // $NON-NLS-1$
                dir.getAbsolutePath()));
                return null;
            }
        }
    }
    try {
        SimpleContentItem content = new FileContentItem(file);
        return content;
    } catch (FileNotFoundException e) {
        logger.error(Messages.getInstance().getErrorString("FileOutputHandler.ERROR_0002_COULD_NOT_CREATE_OUTPUT_FILE", file.getAbsolutePath()), // $NON-NLS-1$
        e);
    }
    return null;
}
Also used : SimpleContentItem(org.pentaho.platform.engine.core.output.SimpleContentItem) FileContentItem(org.pentaho.platform.engine.core.output.FileContentItem) FileNotFoundException(java.io.FileNotFoundException) URISyntaxException(java.net.URISyntaxException) File(java.io.File) URI(java.net.URI)

Example 2 with SimpleContentItem

use of org.pentaho.platform.engine.core.output.SimpleContentItem in project pentaho-platform by pentaho.

the class ZipOutputHandler method getFileOutputContentItem.

public IContentItem getFileOutputContentItem() {
    String contentRef = getContentRef();
    File file = new File(contentRef);
    File dir = file.getParentFile();
    if (!dir.exists()) {
        dir.mkdirs();
    }
    try {
        FileOutputStream outputStream = new FileOutputStream(file);
        SimpleContentItem content = new SimpleContentItem(outputStream);
        return content;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : SimpleContentItem(org.pentaho.platform.engine.core.output.SimpleContentItem) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File)

Example 3 with SimpleContentItem

use of org.pentaho.platform.engine.core.output.SimpleContentItem in project pentaho-platform by pentaho.

the class ActionDelegate method executeAction.

/**
 * Wires up inputs outputs and resources to an Action and executes it.
 */
@Override
protected boolean executeAction() throws Throwable {
    // 
    // Set inputs
    // 
    InputErrorCallback errorCallback = new InputErrorCallback();
    for (IActionInput input : getActionDefinition().getInputs()) {
        Object inputValue = input.getValue();
        if (input instanceof ActionInputConstant) {
            // if the input is coming from the component definition section,
            // do parameter replacement on the string and the result of that
            // is the input value
            inputValue = input.getStringValue(true);
        }
        errorCallback.setValue(inputValue);
        actionHarness.setValue(input.getName(), inputValue, errorCallback, COMPATIBILITY_FORMATTER, ALTERNATE_INDEX_FORMATTER);
    }
    // 
    // Set resources
    // 
    ResourceCallback resourceCallback = new ResourceCallback();
    for (IActionResource res : getActionDefinition().getResources()) {
        actionHarness.setValue(res.getName(), res.getInputStream(), resourceCallback, COMPATIBILITY_FORMATTER, ALTERNATE_INDEX_FORMATTER);
    }
    // 
    // Provide output stream for the streaming action. We are going to look for all outputs where
    // type = "content", and derive output streams to hand to the IStreamingAction.
    // 
    Map<String, IContentItem> outputContentItems = new HashMap<String, IContentItem>();
    StreamOutputErrorCallback streamingOutputCallback = new StreamOutputErrorCallback();
    OuputStreamGenerator outputStreamGenerator = new OuputStreamGenerator(outputContentItems);
    IActionOutput[] contentOutputs = getActionDefinition().getOutputs(ActionSequenceDocument.CONTENT_TYPE);
    if (contentOutputs.length > 0) {
        for (IActionOutput contentOutput : contentOutputs) {
            outputStreamGenerator.setContentOutput(contentOutput);
            actionHarness.setValue(contentOutput.getName(), outputStreamGenerator, streamingOutputCallback, STREAM_APPENDER_FORMATTER, COMPATIBILITY_FORMATTER, ALTERNATE_INDEX_FORMATTER);
        }
    }
    // 
    if (actionBean instanceof IAction) {
        ((IAction) actionBean).execute();
    }
    // 
    for (IActionOutput output : actionDefintionOutputs) {
        String outputName = output.getName();
        outputName = COMPATIBILITY_FORMATTER.format(outputName);
        // if streaming output, add it to the context and don't try to get it from the Action bean
        if (outputContentItems.containsKey(outputName)) {
            IContentItem contentItem = outputContentItems.get(outputName);
            if (!(contentItem instanceof SimpleContentItem)) {
                // this is a special output for streaming actions and does not require a bean accessor
                output.setValue(contentItem);
            }
        } else if (actionHarness.isReadable(outputName)) {
            Object outputVal = actionHarness.getValue(outputName);
            output.setValue(outputVal);
        } else {
            if (loggingLevel <= ILogger.WARN) {
                warn(// $NON-NLS-1$
                Messages.getInstance().getString(// $NON-NLS-1$
                "ActionDelegate.WARN_OUTPUT_NOT_READABLE", outputName, output.getType(), actionBean.getClass().getSimpleName()));
            }
        }
    }
    return true;
}
Also used : IAction(org.pentaho.platform.api.action.IAction) HashMap(java.util.HashMap) ActionInputConstant(org.pentaho.actionsequence.dom.ActionInputConstant) IActionOutput(org.pentaho.actionsequence.dom.IActionOutput) SimpleContentItem(org.pentaho.platform.engine.core.output.SimpleContentItem) IActionResource(org.pentaho.actionsequence.dom.IActionResource) IActionInput(org.pentaho.actionsequence.dom.IActionInput) IContentItem(org.pentaho.platform.api.repository.IContentItem)

Example 4 with SimpleContentItem

use of org.pentaho.platform.engine.core.output.SimpleContentItem in project pentaho-platform by pentaho.

the class PojoComponent method executeAction.

@SuppressWarnings({ "unchecked" })
@Override
protected boolean executeAction() throws Throwable {
    Set<?> inputNames = getInputNames();
    Element defnNode = (Element) getComponentDefinition();
    // if( pojo instanceof IConfiguredPojo ) {
    if (getMethods.containsKey("CONFIGSETTINGSPATHS") && configureMethod != null) {
        // $NON-NLS-1$
        // $NON-NLS-1$
        Method method = getMethods.get("CONFIGSETTINGSPATHS");
        Set<String> settingsPaths = (Set<String>) method.invoke(pojo, new Object[] {});
        Iterator<String> keys = settingsPaths.iterator();
        Map<String, String> settings = new HashMap<String, String>();
        SystemSettingsParameterProvider params = new SystemSettingsParameterProvider();
        while (keys.hasNext()) {
            String path = keys.next();
            String value = params.getStringParameter(path, null);
            if (value != null) {
                settings.put(path, value);
            }
        }
        configureMethod.invoke(pojo, new Object[] { settings });
    }
    // set the PentahoSession
    if (sessionMethod != null) {
        callMethods(Arrays.asList(new Method[] { sessionMethod }), getSession());
    }
    // set the logger
    if (loggerMethod != null) {
        callMethods(Arrays.asList(new Method[] { loggerMethod }), getLogger());
    }
    Map<String, Object> inputMap = new HashMap<String, Object>();
    // look at the component settings
    // $NON-NLS-1$
    List<?> nodes = defnNode.selectNodes("*");
    for (int idx = 0; idx < nodes.size(); idx++) {
        Element node = (Element) nodes.get(idx);
        // inputs may typically contain a dash in them, such as
        // something like "report-definition" and we should expect
        // a setter as setReportDefinition, so we will remove the
        // dashes and everything should proceed as expected
        // $NON-NLS-1$ //$NON-NLS-2$
        String name = node.getName().replace("-", "").toUpperCase();
        if (!name.equals("CLASS") && !name.equals("OUTPUTSTREAM")) {
            // $NON-NLS-1$ //$NON-NLS-2$
            String value = node.getText();
            List<Method> method = setMethods.get(name);
            if (method != null) {
                callMethodWithString(method, value);
            } else if (runtimeInputsMethod != null) {
                inputMap.put(name, value);
            } else {
                // Supress error (For string/value replacement)
                // $NON-NLS-1$
                getLogger().warn(Messages.getInstance().getString("PojoComponent.UNUSED_INPUT", name));
            }
        }
    }
    Iterator<?> it = null;
    // now process all of the resources and see if we can call them as setters
    Set<?> resourceNames = getResourceNames();
    Map<String, IActionSequenceResource> resourceMap = new HashMap<String, IActionSequenceResource>();
    if (resourceNames != null && resourceNames.size() > 0) {
        it = resourceNames.iterator();
        while (it.hasNext()) {
            String name = (String) it.next();
            IActionSequenceResource resource = getResource(name);
            // $NON-NLS-1$ //$NON-NLS-2$
            name = name.replace("-", "");
            resourceMap.put(name, resource);
            List<Method> methods = setMethods.get(name.toUpperCase());
            if (methods != null) {
                for (Method method : methods) {
                    Class<?>[] paramTypes = method.getParameterTypes();
                    if (paramTypes.length == 1) {
                        Object value = null;
                        if (paramTypes[0] == InputStream.class) {
                            value = resource.getInputStream(RepositoryFilePermission.READ, LocaleHelper.getLocale());
                        } else if (paramTypes[0] == IActionSequenceResource.class) {
                            value = resource;
                        } else if (paramTypes[0] == String.class) {
                            value = getRuntimeContext().getResourceAsString(resource);
                        } else if (paramTypes[0] == Document.class) {
                            value = getRuntimeContext().getResourceAsDocument(resource);
                        }
                        callMethod(method, value);
                    }
                }
            // CHECKSTYLE IGNORE EmptyBlock FOR NEXT 3 LINES
            } else {
            // BISERVER-2715 we should ignore this as the resource might be meant for another component
            }
        }
    }
    // now process all of the inputs, overriding the component settings
    it = inputNames.iterator();
    while (it.hasNext()) {
        String name = (String) it.next();
        Object value = getInputValue(name);
        // now that we have the value, we can fix the name
        // $NON-NLS-1$ //$NON-NLS-2$
        name = name.replace("-", "");
        List<Method> methods = setMethods.get(name.toUpperCase());
        if (methods != null) {
            callMethods(methods, value);
        } else if (runtimeInputsMethod != null) {
            inputMap.put(name, value);
        } else {
            // Supress error (For string/value replacement)
            // $NON-NLS-1$
            getLogger().warn(Messages.getInstance().getString("PojoComponent.UNUSED_INPUT", name));
        }
    }
    if (resourceMap.size() > 0 && resourcesMethod != null) {
        // call the resources setter
        resourcesMethod.invoke(pojo, new Object[] { resourceMap });
    }
    if (inputMap.size() > 0 && runtimeInputsMethod != null) {
        // call the generic input setter
        runtimeInputsMethod.invoke(pojo, new Object[] { inputMap });
    }
    if (// $NON-NLS-1$ //$NON-NLS-2$
    getOutputNames().contains("outputstream") && setMethods.containsKey("OUTPUTSTREAM") && getMethods.containsKey("MIMETYPE")) {
        // $NON-NLS-1$
        // get the mime-type
        // Get the first method to match
        // $NON-NLS-1$
        Method method = getMethods.get("MIMETYPE");
        String mimeType = (String) method.invoke(pojo, new Object[] {});
        // $NON-NLS-1$
        String mappedOutputName = "outputstream";
        if ((getActionDefinition() != null) && (getActionDefinition().getOutput("outputstream") != null)) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            mappedOutputName = getActionDefinition().getOutput("outputstream").getPublicName();
        }
        // this marks the HttpOutputHandler as contentDone=true, causing the MessageFormatter to not print an error
        IContentItem contentItem = getOutputContentItem(mappedOutputName, mimeType);
        if (!(contentItem instanceof SimpleContentItem)) {
            // SimpleContentItem can't handle being added to outputs because it
            // doesn't have a getInputStream(), and the path used to return
            // null.
            // $NON-NLS-1$
            setOutputValue("outputstream", contentItem);
        }
        // set the output stream
        OutputStream out = contentItem.getOutputStream(getActionName());
        // $NON-NLS-1$
        method = setMethods.get("OUTPUTSTREAM").get(0);
        method.invoke(pojo, new Object[] { out });
    }
    if (validateMethod != null) {
        Object obj = validateMethod.invoke(pojo, (Object[]) null);
        if (obj instanceof Boolean) {
            Boolean ok = (Boolean) obj;
            if (!ok) {
                return false;
            }
        }
    }
    // now execute the pojo
    Boolean result = Boolean.FALSE;
    if (executeMethod != null) {
        result = (Boolean) executeMethod.invoke(pojo, new Object[] {});
    } else {
        // we can only assume we are ok so far
        result = Boolean.TRUE;
    }
    // now handle outputs
    Set<?> outputNames = getOutputNames();
    // first get the runtime outputs
    Map<String, Object> outputMap = new HashMap<String, Object>();
    if (runtimeOutputsMethod != null) {
        outputMap = (Map<String, Object>) runtimeOutputsMethod.invoke(pojo, new Object[] {});
    }
    it = outputNames.iterator();
    while (it.hasNext()) {
        String name = (String) it.next();
        // CHECKSTYLE IGNORE EmptyBlock FOR NEXT 3 LINES
        if (name.equals("outputstream")) {
        // $NON-NLS-1$
        // we should be done
        } else {
            IActionParameter param = getOutputItem(name);
            Method method = getMethods.get(name.toUpperCase());
            if (method != null) {
                Object value = method.invoke(pojo, new Object[] {});
                param.setValue(value);
            } else {
                Object value = outputMap.get(name);
                if (value != null) {
                    param.setValue(value);
                } else {
                    throw new NoSuchMethodException(name);
                }
            }
        }
    }
    return result.booleanValue();
}
Also used : Set(java.util.Set) IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) HashMap(java.util.HashMap) Element(org.dom4j.Element) OutputStream(java.io.OutputStream) Document(org.dom4j.Document) IActionSequenceResource(org.pentaho.platform.api.engine.IActionSequenceResource) SimpleContentItem(org.pentaho.platform.engine.core.output.SimpleContentItem) Method(java.lang.reflect.Method) IContentItem(org.pentaho.platform.api.repository.IContentItem) SystemSettingsParameterProvider(org.pentaho.platform.engine.core.solution.SystemSettingsParameterProvider) IActionParameter(org.pentaho.platform.api.engine.IActionParameter)

Example 5 with SimpleContentItem

use of org.pentaho.platform.engine.core.output.SimpleContentItem in project pentaho-platform by pentaho.

the class OutputTest method testMultiContentItem.

public void testMultiContentItem() {
    ByteArrayOutputStream out1 = new ByteArrayOutputStream();
    ByteArrayOutputStream out2 = new ByteArrayOutputStream();
    SimpleContentItem item1 = new SimpleContentItem(out1);
    SimpleContentItem item2 = new SimpleContentItem(out2);
    MultiContentItem multiContent = new MultiContentItem();
    multiContent.addContentItem(item1);
    multiContent.addContentItem(item2);
    byte[] in = "abcd".getBytes();
    String outStr1 = "";
    String outStr2 = "";
    try {
        OutputStream multi = multiContent.getOutputStream("");
        multi.write('a');
        multi.write(in, 1, 2);
        multi.write(in);
        multiContent.closeOutputStream();
    } catch (IOException e) {
        // we should not get here
        assertEquals("IOException", null, e);
    }
    outStr1 = new String(out1.toByteArray());
    outStr2 = new String(out2.toByteArray());
    assertEquals("Output stream 1", "abcabcd", outStr1);
    assertEquals("Output stream 2", "abcabcd", outStr2);
}
Also used : SimpleContentItem(org.pentaho.platform.engine.core.output.SimpleContentItem) MultiContentItem(org.pentaho.platform.engine.core.output.MultiContentItem) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MultiOutputStream(org.pentaho.platform.engine.core.output.MultiOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

SimpleContentItem (org.pentaho.platform.engine.core.output.SimpleContentItem)12 OutputStream (java.io.OutputStream)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 IContentItem (org.pentaho.platform.api.repository.IContentItem)5 MultiOutputStream (org.pentaho.platform.engine.core.output.MultiOutputStream)3 SimpleOutputHandler (org.pentaho.platform.engine.core.output.SimpleOutputHandler)3 StandaloneObjectFactory (org.pentaho.platform.engine.core.system.objfac.StandaloneObjectFactory)3 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 BufferedContentItem (org.pentaho.platform.engine.core.output.BufferedContentItem)2 MultiContentItem (org.pentaho.platform.engine.core.output.MultiContentItem)2 StandaloneSession (org.pentaho.platform.engine.core.system.StandaloneSession)2 FileOutputStream (java.io.FileOutputStream)1 Method (java.lang.reflect.Method)1 BigDecimal (java.math.BigDecimal)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Set (java.util.Set)1