Search in sources :

Example 41 with IContentItem

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

the class RepositoryFileOutputHandler method setOutput.

public void setOutput(final String name, final Object value) throws IOException {
    if (value == null) {
        return;
    }
    if (IOutputHandler.CONTENT.equalsIgnoreCase(name)) {
        // $NON-NLS-1$
        IContentItem response = getOutputContentItem("response", IOutputHandler.CONTENT, null, null);
        if (response != null) {
            if (!(value instanceof IContentItem)) {
                if (response.getMimeType() == null) {
                    // $NON-NLS-1$
                    response.setMimeType("text/html");
                }
                response.getOutputStream(null).write(value.toString().getBytes());
                contentGenerated = true;
            }
        }
    }
}
Also used : IContentItem(org.pentaho.platform.api.repository.IContentItem)

Example 42 with IContentItem

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

the class SimpleOutputHandlerTest method test2.

public void test2() throws Exception {
    StandaloneSession session = new StandaloneSession();
    OutputStream out = new MockExceptionOutputStream();
    IContentItem content = new SimpleContentItem(out);
    SimpleOutputHandler handler = new SimpleOutputHandler(content, true);
    assertTrue(handler.allowFeedback());
    assertFalse(handler.contentDone());
    assertNotNull(handler.getFeedbackContentItem());
    assertTrue(handler.contentDone());
    IContentItem content2 = handler.getFeedbackContentItem();
    assertEquals(content.getOutputStream(null), content2.getOutputStream(null));
    IContentItem content3 = new BufferedContentItem(null);
    content3.getOutputStream(null).write("test data".getBytes());
    content3.closeOutputStream();
    try {
        handler.setOutput(IOutputHandler.CONTENT, content3);
        fail("Exception not detected.");
    } catch (Exception ex) {
    // Test passed.
    }
}
Also used : SimpleContentItem(org.pentaho.platform.engine.core.output.SimpleContentItem) 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) BufferedContentItem(org.pentaho.platform.engine.core.output.BufferedContentItem)

Example 43 with IContentItem

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

the class SimpleOutputHandlerTest method test4.

public void test4() throws Exception {
    StandaloneSession session = new StandaloneSession();
    OutputStream out = new ByteArrayOutputStream();
    SimpleOutputHandler handler = new SimpleOutputHandler(out, true);
    assertTrue(handler.allowFeedback());
    assertNotNull(handler.getFeedbackContentItem());
    IContentItem content2 = handler.getFeedbackContentItem();
    assertEquals(out, content2.getOutputStream(null));
    OutputStream out3 = new ByteArrayOutputStream();
    handler.setOutputStream(out3, IOutputHandler.RESPONSE, IOutputHandler.CONTENT);
    IContentItem content3 = handler.getOutputContentItem(IOutputHandler.RESPONSE, IOutputHandler.CONTENT, null, null);
    assertEquals(out3, content3.getOutputStream(null));
}
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) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 44 with IContentItem

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

the class SimpleOutputHandlerTest method test1.

public void test1() throws Exception {
    StandaloneObjectFactory factory = new StandaloneObjectFactory();
    TestOutputHandler.contentItem = new SimpleContentItem();
    factory.defineObject("testoutut", TestOutputHandler.class.getName(), StandaloneObjectFactory.Scope.LOCAL);
    PentahoSystem.registerObjectFactory(factory);
    StandaloneSession session = new StandaloneSession();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    SimpleOutputHandler handler = new SimpleOutputHandler(out, false);
    MimeTypeListener listener = new MimeTypeListener();
    handler.setMimeTypeListener(listener);
    assertEquals(listener, handler.getMimeTypeListener());
    assertFalse(handler.allowFeedback());
    handler.setSession(session);
    assertEquals(session, handler.getSession());
    handler.setContentItem(new SimpleContentItem(), null, null);
    IContentItem content2 = handler.getOutputContentItem("bogus", "testoutut:bogus", null, null);
    assertEquals(TestOutputHandler.contentItem, content2);
    content2 = handler.getOutputContentItem("bogus", "bogus", null, null);
    assertNull(content2);
    content2 = handler.getOutputContentItem(IOutputHandler.RESPONSE, IOutputHandler.CONTENT, null, null);
    assertEquals(out, content2.getOutputStream(null));
    assertFalse(handler.contentDone());
    assertNull(handler.getFeedbackContentItem());
    assertFalse(handler.contentDone());
    handler.setOutputPreference(-1);
    assertEquals(-1, handler.getOutputPreference());
    handler.setMimeType("test/test");
    assertEquals("test/test", handler.getMimeType());
    handler.setOutput("bogus", null);
    handler.setOutput("file:bogus", null);
    IContentItem content3 = new BufferedContentItem(null);
    OutputStream os = content3.getOutputStream(null);
    os.write("test data".getBytes());
    content3.closeOutputStream();
    handler.setOutput(IOutputHandler.CONTENT, content3);
    assertEquals("test data", new String(out.toByteArray()));
    handler.setOutput(IOutputHandler.CONTENT, "+test data2");
    assertEquals("test data+test data2", new String(out.toByteArray()));
}
Also used : SimpleContentItem(org.pentaho.platform.engine.core.output.SimpleContentItem) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) StandaloneObjectFactory(org.pentaho.platform.engine.core.system.objfac.StandaloneObjectFactory) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IContentItem(org.pentaho.platform.api.repository.IContentItem) SimpleOutputHandler(org.pentaho.platform.engine.core.output.SimpleOutputHandler) BufferedContentItem(org.pentaho.platform.engine.core.output.BufferedContentItem) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 45 with IContentItem

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

the class ContentOutputComponent method executeAction.

@Override
protected boolean executeAction() throws Throwable {
    String mimeType = getInputStringValue(ContentOutputComponent.COMPONENT_SETTING_MIME_TYPE);
    Object dataToOutput = getInputValue(ContentOutputComponent.INPUT_NAME_EXPECTED);
    if (dataToOutput != null) {
        IContentItem outputContentItem = getOutputContentItem(ContentOutputComponent.OUTPUT_NAME, mimeType);
        if (outputContentItem != null) {
            outputContentItem.setMimeType(mimeType);
            OutputStream outputStream = null;
            if (dataToOutput instanceof String) {
                String theOutput = (String) dataToOutput;
                if (theOutput.length() > 0) {
                    try {
                        outputStream = outputContentItem.getOutputStream(getActionName());
                        outputStream.write(theOutput.getBytes(LocaleHelper.getSystemEncoding()));
                        outputStream.flush();
                        outputStream.close();
                        outputContentItem.closeOutputStream();
                    } catch (Exception e) {
                        // $NON-NLS-1$
                        error(Messages.getInstance().getErrorString("ContentOutputComponent.ERROR_0003_WRITING_OUTPUT"), e);
                        return false;
                    }
                    trace(theOutput);
                    return true;
                } else {
                    // $NON-NLS-1$
                    error(Messages.getInstance().getErrorString("ContentOutputComponent.ERROR_0002_EMPTY_OUTPUT"));
                    return false;
                }
            } else if (dataToOutput instanceof InputStream) {
                InputStream is = (InputStream) dataToOutput;
                byte[] buff = new byte[1024];
                int len;
                outputStream = outputContentItem.getOutputStream(null);
                while ((len = is.read(buff)) >= 0) {
                    outputStream.write(buff, 0, len);
                }
                outputStream.flush();
                outputStream.close();
                outputContentItem.closeOutputStream();
                return true;
            } else if (dataToOutput instanceof ByteArrayOutputStream) {
                ByteArrayOutputStream baos = (ByteArrayOutputStream) dataToOutput;
                outputStream = outputContentItem.getOutputStream(null);
                outputStream.write(baos.toByteArray());
                outputStream.flush();
                outputStream.close();
                outputContentItem.closeOutputStream();
                return true;
            } else {
                error(Messages.getInstance().getErrorString("ContentOutputComponent.ERROR_0007_UNKNOWN_TYPE", // $NON-NLS-1$
                dataToOutput.getClass().getName()));
                return false;
            }
        } else {
            // $NON-NLS-1$
            error(Messages.getInstance().getErrorString("ContentOutputComponent.ERROR_0005_OUTPUT_CONTENT_ITEM"));
            return false;
        }
    }
    // $NON-NLS-1$
    error(Messages.getInstance().getErrorString("ContentOutputComponent.ERROR_0004_CONTENTOUTPUT_NULL"));
    return false;
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IContentItem(org.pentaho.platform.api.repository.IContentItem) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

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