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;
}
}
}
}
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.
}
}
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));
}
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()));
}
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;
}
Aggregations