Search in sources :

Example 26 with IContentItem

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

the class SoapHelperTest method testCreateSoapResponseDocumentFromContext.

/**
 * Tests for SoapHelper.createSoapResponseDocument(IRuntimeContext, IOutputHandler, OutputStream, List)
 */
public void testCreateSoapResponseDocumentFromContext() {
    // Set up test data
    Set<Object> outputNames = new HashSet<>();
    IActionParameter actionParameter = mock(IActionParameter.class);
    when(actionParameter.getValue()).thenReturn("testValue");
    IRuntimeContext context = mock(IRuntimeContext.class);
    when(context.getOutputNames()).thenReturn(outputNames);
    when(context.getStatus()).thenReturn(IRuntimeContext.RUNTIME_STATUS_SUCCESS);
    when(context.getOutputParameter(anyString())).thenReturn(actionParameter);
    List messages = new ArrayList();
    IOutputHandler outputHandler = mock(IOutputHandler.class);
    IContentItem contentItem = mock(IContentItem.class);
    when(outputHandler.getOutputContentItem(anyString(), anyString(), anyString(), anyString())).thenReturn(contentItem);
    when(contentItem.getMimeType()).thenReturn("text/xml");
    OutputStream contentStream = mock(OutputStream.class);
    when(contentStream.toString()).thenReturn("contentStreamTestString");
    // Tests for document with two output names
    outputNames.add("outputName1");
    outputNames.add("outputName2");
    Document d1 = SoapHelper.createSoapResponseDocument(context, outputHandler, contentStream, messages);
    Element activityResponse1 = d1.getRootElement().element("SOAP-ENV:Body").element("ExecuteActivityResponse");
    assertEquals(activityResponse1.elements().size(), 2);
    assertNotNull(activityResponse1.element("outputName1"));
    assertNotNull(activityResponse1.element("outputName2"));
    // Test for document with one output name (different branch in createSoapResponseDocument())
    outputNames.clear();
    outputNames.add("outputName1");
    Document d2 = SoapHelper.createSoapResponseDocument(context, outputHandler, contentStream, messages);
    Element activityResponse2 = d2.getRootElement().element("SOAP-ENV:Body").element("ExecuteActivityResponse");
    assertEquals(activityResponse2.elements().size(), 1);
    assertNotNull(activityResponse2.element("outputName1"));
    // Tests for document with no output names
    outputNames.clear();
    Document d3 = SoapHelper.createSoapResponseDocument(context, outputHandler, contentStream, messages);
    Element activityResponse3 = d3.getRootElement().element("SOAP-ENV:Body").element("ExecuteActivityResponse");
    assertTrue(activityResponse3.elements().isEmpty());
}
Also used : IOutputHandler(org.pentaho.platform.api.engine.IOutputHandler) OutputStream(java.io.OutputStream) DefaultElement(org.dom4j.tree.DefaultElement) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) IContentItem(org.pentaho.platform.api.repository.IContentItem) ArrayList(java.util.ArrayList) List(java.util.List) Document(org.dom4j.Document) IActionParameter(org.pentaho.platform.api.engine.IActionParameter) IRuntimeContext(org.pentaho.platform.api.engine.IRuntimeContext) HashSet(java.util.HashSet)

Example 27 with IContentItem

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

the class SimpleOutputHandlerTest method testGetOutputContentItemObjectNameImportant.

public void testGetOutputContentItemObjectNameImportant() throws Exception {
    OutputStream out = new ByteArrayOutputStream();
    TestOutputHandler.contentItem = new SimpleContentItem(out);
    SimpleOutputHandler handler = new SimpleOutputHandler(out, false);
    StandaloneObjectFactory factory = new StandaloneObjectFactory();
    factory.defineObject("contentrepo", TestOutputHandler.class.getName(), StandaloneObjectFactory.Scope.GLOBAL);
    PentahoSystem.registerObjectFactory(factory);
    IContentItem contentItem = handler.getOutputContentItem("contentrepo", "myreport:contentrepo", null, null);
    assertNotNull(contentItem);
    assertEquals(out, contentItem.getOutputStream(null));
    contentItem = handler.getOutputContentItem("", "myreport:contentrepo", null, null);
    assertNull(contentItem);
}
Also used : SimpleContentItem(org.pentaho.platform.engine.core.output.SimpleContentItem) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StandaloneObjectFactory(org.pentaho.platform.engine.core.system.objfac.StandaloneObjectFactory) IContentItem(org.pentaho.platform.api.repository.IContentItem) SimpleOutputHandler(org.pentaho.platform.engine.core.output.SimpleOutputHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 28 with IContentItem

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

the class SimpleOutputHandlerTest method test3.

public void test3() throws Exception {
    StandaloneSession session = new StandaloneSession();
    OutputStream out = new MockExceptionOutputStream();
    SimpleOutputHandler handler = new SimpleOutputHandler(out, false);
    assertFalse(handler.allowFeedback());
    handler.setSession(session);
    assertEquals(session, handler.getSession());
    IContentItem content2 = handler.getOutputContentItem(IOutputHandler.RESPONSE, IOutputHandler.CONTENT, null, null);
    assertEquals(out, content2.getOutputStream(null));
    assertNull(handler.getFeedbackContentItem());
    try {
        handler.setOutput(IOutputHandler.CONTENT, "test data");
        fail("Exception not detected.");
    } catch (Exception ex) {
    // Test passed.
    }
}
Also used : StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IContentItem(org.pentaho.platform.api.repository.IContentItem) SimpleOutputHandler(org.pentaho.platform.engine.core.output.SimpleOutputHandler)

Example 29 with IContentItem

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

the class XactionUtil method postExecute.

public static String postExecute(IRuntimeContext runtime, boolean debugMessages, boolean doWrapper, IOutputHandler outputHandler, Map<String, IParameterProvider> parameterProviders, HttpServletRequest request, HttpServletResponse response, List<?> messages, boolean deleteGeneratedFiles) throws Exception {
    StringBuffer buffer = new StringBuffer();
    boolean hasResponse = outputHandler.isResponseExpected();
    IContentItem responseContentItem = outputHandler.getOutputContentItem(IOutputHandler.RESPONSE, IOutputHandler.CONTENT, null, null);
    boolean success = (runtime != null && runtime.getStatus() == IRuntimeContext.RUNTIME_STATUS_SUCCESS);
    boolean printSuccess = (runtime != null) && success && (!hasResponse || debugMessages);
    boolean printError = (runtime != null) && !success && !response.isCommitted();
    if (printSuccess || printError) {
        // $NON-NLS-1$
        final String htmlMimeType = "text/html";
        responseContentItem.setMimeType(htmlMimeType);
        response.setContentType(htmlMimeType);
        if (printSuccess) {
            MessageFormatUtils.formatSuccessMessage(htmlMimeType, runtime, buffer, debugMessages, doWrapper);
        } else {
            response.resetBuffer();
            MessageFormatUtils.formatFailureMessage(htmlMimeType, runtime, buffer, messages);
        }
    }
    // clear files which was generated during action execution
    // http://jira.pentaho.com/browse/BISERVER-12639
    IUnifiedRepository unifiedRepository = PentahoSystem.get(IUnifiedRepository.class, null);
    if (unifiedRepository != null) {
        for (IContentItem contentItem : runtime.getOutputContentItems()) {
            if (contentItem != null) {
                try {
                    contentItem.closeOutputStream();
                    if (deleteGeneratedFiles) {
                        deleteContentItem(contentItem, unifiedRepository);
                    }
                } catch (Exception e) {
                    logger.warn(Messages.getInstance().getString("XactionUtil.CANNOT_REMOVE_OUTPUT_FILE", contentItem.getPath()), e);
                }
            }
        }
    }
    return buffer.toString();
}
Also used : IContentItem(org.pentaho.platform.api.repository.IContentItem) ServletException(javax.servlet.ServletException) ObjectFactoryException(org.pentaho.platform.api.engine.ObjectFactoryException) IOException(java.io.IOException) IUnifiedRepository(org.pentaho.platform.api.repository2.unified.IUnifiedRepository)

Example 30 with IContentItem

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

the class HttpOutputHandler method setOutput.

public void setOutput(final String name, final Object value) throws IOException {
    if (value == null) {
        // $NON-NLS-1$
        HttpOutputHandler.logger.warn(Messages.getInstance().getString("HttpOutputHandler.WARN_0001_VALUE_IS_NULL"));
        return;
    }
    /*
     * if setOutput is called, it means someone intends to write some data to the response stream, which they may not
     * have gotten a handle to earlier through getOutputContentItem*. So we need to set the flag here.
     */
    responseExpected = true;
    if ("redirect".equalsIgnoreCase(name)) {
        // $NON-NLS-1$
        try {
            response.sendRedirect(value.toString());
        } catch (IOException ioe) {
            HttpOutputHandler.logger.error(Messages.getInstance().getString("HttpOutputHandler.ERROR_0001_REDIRECT_FAILED", value.toString()), // $NON-NLS-1$
            ioe);
        }
    } else if ("header".equalsIgnoreCase(name)) {
        // $NON-NLS-1$
        try {
            if (value instanceof Map) {
                for (Iterator it = ((Map) value).entrySet().iterator(); it.hasNext(); ) {
                    Map.Entry entry = (Map.Entry) it.next();
                    response.addHeader(entry.getKey().toString(), entry.getValue().toString());
                }
            } else {
                String strVal = value.toString();
                int i = strVal.indexOf('=');
                String headerName = strVal.substring(0, i);
                String headerValue = strVal.substring(i + 1);
                response.addHeader(headerName, headerValue);
            }
        } catch (IndexOutOfBoundsException e) {
            HttpOutputHandler.logger.error(e.getLocalizedMessage());
        }
    } else if (IOutputHandler.CONTENT.equalsIgnoreCase(name)) {
        if (value instanceof IContentItem) {
            IContentItem content = (IContentItem) value;
            // See if we should process the input stream. If it's from
            // the content repository, then there's an input stream.
            // SimpleContentItem and HttpContentItem both return null from
            // getInputStream().
            InputStream inStr = content.getInputStream();
            if (inStr != null) {
                if ((response.getContentType() == null) || (!response.getContentType().equalsIgnoreCase(content.getMimeType()))) {
                    // response.setContentType( content.getMimeType() );
                    setMimeType(content.getMimeType());
                }
                try {
                    // Bad idea to write to the output stream directly. This prevents us from
                    // auditing writes. Use the managed destinationOutputStream instead.
                    // OutputStream outStr = response.getOutputStream();
                    int inCnt = 0;
                    byte[] buf = new byte[4096];
                    while (-1 != (inCnt = inStr.read(buf))) {
                        destinationOutputStream.write(buf, 0, inCnt);
                    }
                } finally {
                    try {
                        inStr.close();
                    } catch (IOException ignored) {
                    // ignored
                    }
                }
                contentGenerated = true;
            }
        } else {
            if (response.getContentType() == null) {
                // $NON-NLS-1$
                setMimeType("text/html");
            }
            destinationOutputStream.write(value.toString().getBytes());
            contentGenerated = true;
        }
    }
}
Also used : InputStream(java.io.InputStream) Iterator(java.util.Iterator) IContentItem(org.pentaho.platform.api.repository.IContentItem) IOException(java.io.IOException) Map(java.util.Map)

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