Search in sources :

Example 16 with IContentItem

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

the class RuntimeContext method getDataSource.

public IPentahoStreamSource getDataSource(final String parameterName) {
    IPentahoStreamSource dataSource = null;
    // TODO Temp workaround for content repos bug
    IActionParameter actionParameter = paramManager.getCurrentInput(parameterName);
    if (actionParameter == null) {
        throw new InvalidParameterException(Messages.getInstance().getErrorString("RuntimeContext.ERROR_0019_INVALID_INPUT_REQUEST", parameterName, // $NON-NLS-1$
        actionSequence.getSequenceName()));
    }
    Object locObj = actionParameter.getValue();
    if (locObj != null) {
        if (locObj instanceof IContentItem) {
            // At this point we have an IContentItem so why do anything else?
            dataSource = ((IContentItem) locObj).getDataSource();
        }
    }
    // This will return null if the locObj is null
    return dataSource;
}
Also used : InvalidParameterException(org.pentaho.platform.api.engine.InvalidParameterException) IContentItem(org.pentaho.platform.api.repository.IContentItem) IPentahoStreamSource(org.pentaho.commons.connection.IPentahoStreamSource) IActionParameter(org.pentaho.platform.api.engine.IActionParameter)

Example 17 with IContentItem

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

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

the class SoapHelper method createSoapResponseDocument.

public static Document createSoapResponseDocument(IRuntimeContext context, IOutputHandler outputHandler, OutputStream contentStream, List messages) {
    Document document = createSoapDocument();
    if ((context == null) || (context.getStatus() != IRuntimeContext.RUNTIME_STATUS_SUCCESS)) {
        // $NON-NLS-1$
        document.getRootElement().element("SOAP-ENV:Body").add(createSoapFaultElement(messages));
    } else {
        Element activityResponse = createActivityResponseElement();
        // $NON-NLS-1$
        document.getRootElement().element("SOAP-ENV:Body").add(activityResponse);
        IContentItem contentItem = outputHandler.getFeedbackContentItem();
        // hmm do we need this to be ordered?
        Set outputNames = context.getOutputNames();
        Iterator outputNameIterator = outputNames.iterator();
        while (outputNameIterator.hasNext()) {
            String outputName = (String) outputNameIterator.next();
            contentItem = outputHandler.getOutputContentItem(IOutputHandler.RESPONSE, IOutputHandler.CONTENT, context.getInstanceId(), // $NON-NLS-1$
            "text/xml");
            if ((outputNames.size() == 1) && (contentItem != null)) {
                String mimeType = contentItem.getMimeType();
                if ((mimeType != null) && mimeType.startsWith("text/")) {
                    // $NON-NLS-1$
                    if (mimeType.equals("text/xml")) {
                        // $NON-NLS-1$
                        activityResponse.addElement(outputName).setText(contentStream.toString());
                    } else if (mimeType.startsWith("text/")) {
                        // $NON-NLS-1$
                        activityResponse.addElement(outputName).addCDATA(contentStream.toString());
                    }
                } else {
                    Object value = context.getOutputParameter(outputName).getValue();
                    if (value == null) {
                        // $NON-NLS-1$
                        value = "";
                    }
                    activityResponse.add(createSoapElement(outputName, value));
                }
            } else {
                Object value = context.getOutputParameter(outputName).getValue();
                if (value == null) {
                    // $NON-NLS-1$
                    value = "";
                }
                activityResponse.add(createSoapElement(outputName, value));
            }
        }
    }
    return document;
}
Also used : Set(java.util.Set) IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) DefaultElement(org.dom4j.tree.DefaultElement) Element(org.dom4j.Element) IContentItem(org.pentaho.platform.api.repository.IContentItem) Iterator(java.util.Iterator) Document(org.dom4j.Document)

Example 19 with IContentItem

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

the class ComponentBase method setOutputMimeType.

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

Example 20 with IContentItem

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

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