Search in sources :

Example 1 with BufferedContentItem

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

the class MemoryOutputHandler method getFileOutputContentItem.

public IContentItem getFileOutputContentItem() {
    path = getContentRef();
    IContentItem item = new BufferedContentItem(this);
    item.setName(path);
    item.setMimeType(getMimeType());
    return item;
}
Also used : IContentItem(org.pentaho.platform.api.repository.IContentItem) BufferedContentItem(org.pentaho.platform.engine.core.output.BufferedContentItem)

Example 2 with BufferedContentItem

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

the class OutputTest method testBufferedContentItem.

public void testBufferedContentItem() throws Exception {
    TestContentListener listener = new TestContentListener();
    BufferedContentItem content = new BufferedContentItem(listener);
    OutputStream out = content.getOutputStream(null);
    content.setMimeType("text/test");
    out.write("test sting".getBytes());
    content.closeOutputStream();
    InputStream in = content.getInputStream();
    byte[] b = new byte[100];
    int n = in.read(b);
    assertEquals("test sting", new String(b, 0, n));
    assertEquals("text/test", listener.mimeType);
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MultiOutputStream(org.pentaho.platform.engine.core.output.MultiOutputStream) BufferedContentItem(org.pentaho.platform.engine.core.output.BufferedContentItem)

Example 3 with BufferedContentItem

use of org.pentaho.platform.engine.core.output.BufferedContentItem 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 4 with BufferedContentItem

use of org.pentaho.platform.engine.core.output.BufferedContentItem 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 5 with BufferedContentItem

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

the class JcrCmsOutputHandler method getFileOutputContentItem.

@Override
public IContentItem getFileOutputContentItem() {
    String contentName = getContentRef();
    try {
        Repository repository = getRepository();
        if (repository == null) {
            Logger.error(JcrCmsOutputHandler.class.getName(), Messages.getInstance().getString(// $NON-NLS-1$
            "JcrCmsOutputHandler.ERROR_0001_GETTING_CMSREPO"));
            return null;
        }
        Session jcrSession = getJcrSession(repository);
        if (jcrSession == null) {
            Logger.error(JcrCmsOutputHandler.class.getName(), Messages.getInstance().getString(// $NON-NLS-1$
            "JcrCmsOutputHandler.ERROR_0002_GETTING_SESSION"));
            return null;
        }
        // Use the root node as a starting point
        Node root = jcrSession.getRootNode();
        if (root == null) {
            Logger.error(JcrCmsOutputHandler.class.getName(), Messages.getInstance().getString(// $NON-NLS-1$
            "JcrCmsOutputHandler.ERROR_0003_GETTING_ROOT"));
            return null;
        }
        Node node = root;
        // parse the path
        // $NON-NLS-1$
        StringTokenizer tokenizer = new StringTokenizer(contentName, "/");
        int levels = tokenizer.countTokens();
        for (int idx = 0; idx < levels - 1; idx++) {
            String folder = tokenizer.nextToken();
            if (!node.hasNode(folder)) {
                // Create an unstructured node under which to import the XML
                // $NON-NLS-1$
                node = node.addNode(folder, "nt:folder");
            } else {
                node = node.getNodes(folder).nextNode();
            }
        }
        // we should be at the right level now
        String fileName = tokenizer.nextToken();
        Node fileNode = null;
        Node contentNode = null;
        Version version = null;
        if (node.hasNode(fileName)) {
            fileNode = node.getNode(fileName);
            // $NON-NLS-1$
            contentNode = fileNode.getNode("jcr:content");
            if (contentNode.isLocked()) {
                JcrCmsOutputHandler.logger.warn(Messages.getInstance().getString("JcrCmsOutputHandler.ERROR_0004_NODE_LOCKED", // $NON-NLS-1$
                contentName));
                return null;
            }
            if (contentNode.isCheckedOut()) {
                JcrCmsOutputHandler.logger.warn(Messages.getInstance().getString("JcrCmsOutputHandler.ERROR_0005_NODE_CHECKED_OUT", // $NON-NLS-1$
                contentName));
                return null;
            }
            contentNode.checkout();
            VersionHistory history = contentNode.getVersionHistory();
            VersionIterator iterator = history.getAllVersions();
            while (iterator.hasNext()) {
                version = iterator.nextVersion();
                JcrCmsOutputHandler.logger.trace(version.getPath() + "," + version.getName() + "," + version.getIndex() + "," + // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                version.getCreated().toString());
            }
        } else {
            // $NON-NLS-1$
            fileNode = node.addNode(fileName, "nt:file");
            // $NON-NLS-1$
            fileNode.addMixin("mix:versionable");
            // create the mandatory child node - jcr:content
            // $NON-NLS-1$ //$NON-NLS-2$
            contentNode = fileNode.addNode("jcr:content", "nt:resource");
            // $NON-NLS-1$
            contentNode.addMixin("mix:versionable");
            // $NON-NLS-1$
            contentNode.addMixin("mix:filename");
            // $NON-NLS-1$
            contentNode.setProperty("jcr:mimeType", getMimeType());
            // $NON-NLS-1$
            contentNode.setProperty("jcr:name", fileName);
            // $NON-NLS-1$
            contentNode.setProperty("jcr:encoding", LocaleHelper.getSystemEncoding());
        }
        CmsContentListener listener = new CmsContentListener(contentNode, jcrSession);
        BufferedContentItem contentItem = new BufferedContentItem(listener);
        listener.setContentItem(contentItem);
        if (false) {
            // Disable faked search for now
            // $NON-NLS-1$
            search("test", jcrSession);
        }
        return contentItem;
    } catch (LockException le) {
        Logger.error(JcrCmsOutputHandler.class.getName(), Messages.getInstance().getString("JcrCmsOutputHandler.ERROR_0006_GETTING_OUTPUTHANDLER") + contentName, // $NON-NLS-1$
        le);
    } catch (NestableRuntimeException nre) {
        Logger.error(JcrCmsOutputHandler.class.getName(), Messages.getInstance().getString("JcrCmsOutputHandler.ERROR_0006_GETTING_OUTPUTHANDLER") + contentName, // $NON-NLS-1$
        nre);
    } catch (RepositoryException re) {
        Logger.error(JcrCmsOutputHandler.class.getName(), Messages.getInstance().getString("JcrCmsOutputHandler.ERROR_0006_GETTING_OUTPUTHANDLER") + contentName, // $NON-NLS-1$
        re);
    }
    return null;
}
Also used : NestableRuntimeException(org.apache.commons.lang.exception.NestableRuntimeException) Node(javax.jcr.Node) BufferedContentItem(org.pentaho.platform.engine.core.output.BufferedContentItem) RepositoryException(javax.jcr.RepositoryException) VersionHistory(javax.jcr.version.VersionHistory) Repository(javax.jcr.Repository) StringTokenizer(java.util.StringTokenizer) Version(javax.jcr.version.Version) LockException(javax.jcr.lock.LockException) VersionIterator(javax.jcr.version.VersionIterator) Session(javax.jcr.Session)

Aggregations

BufferedContentItem (org.pentaho.platform.engine.core.output.BufferedContentItem)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 OutputStream (java.io.OutputStream)3 IContentItem (org.pentaho.platform.api.repository.IContentItem)3 SimpleContentItem (org.pentaho.platform.engine.core.output.SimpleContentItem)2 SimpleOutputHandler (org.pentaho.platform.engine.core.output.SimpleOutputHandler)2 StandaloneSession (org.pentaho.platform.engine.core.system.StandaloneSession)2 InputStream (java.io.InputStream)1 StringTokenizer (java.util.StringTokenizer)1 Node (javax.jcr.Node)1 Repository (javax.jcr.Repository)1 RepositoryException (javax.jcr.RepositoryException)1 Session (javax.jcr.Session)1 LockException (javax.jcr.lock.LockException)1 Version (javax.jcr.version.Version)1 VersionHistory (javax.jcr.version.VersionHistory)1 VersionIterator (javax.jcr.version.VersionIterator)1 NestableRuntimeException (org.apache.commons.lang.exception.NestableRuntimeException)1 MultiOutputStream (org.pentaho.platform.engine.core.output.MultiOutputStream)1 StandaloneObjectFactory (org.pentaho.platform.engine.core.system.objfac.StandaloneObjectFactory)1