use of org.pentaho.platform.engine.core.output.SimpleContentItem in project pentaho-platform by pentaho.
the class FileOutputHandler method getFileOutputContentItem.
@Override
public IContentItem getFileOutputContentItem() {
String contentRef = getContentRef();
File file = new File(contentRef);
File dir = file.getParentFile();
if ((dir != null) && !dir.exists()) {
boolean result = dir.mkdirs();
if (!result) {
try {
URI uri = new URI(contentRef);
file = new File(uri);
dir = file.getParentFile();
} catch (URISyntaxException e) {
logger.error(Messages.getInstance().getErrorString("FileOutputHandler.ERROR_0001_COULD_NOT_CREATE_DIRECTORY", // $NON-NLS-1$
dir.getAbsolutePath()));
return null;
}
}
}
try {
SimpleContentItem content = new FileContentItem(file);
return content;
} catch (FileNotFoundException e) {
logger.error(Messages.getInstance().getErrorString("FileOutputHandler.ERROR_0002_COULD_NOT_CREATE_OUTPUT_FILE", file.getAbsolutePath()), // $NON-NLS-1$
e);
}
return null;
}
use of org.pentaho.platform.engine.core.output.SimpleContentItem in project pentaho-platform by pentaho.
the class ZipOutputHandler method getFileOutputContentItem.
public IContentItem getFileOutputContentItem() {
String contentRef = getContentRef();
File file = new File(contentRef);
File dir = file.getParentFile();
if (!dir.exists()) {
dir.mkdirs();
}
try {
FileOutputStream outputStream = new FileOutputStream(file);
SimpleContentItem content = new SimpleContentItem(outputStream);
return content;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
use of org.pentaho.platform.engine.core.output.SimpleContentItem 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;
}
use of org.pentaho.platform.engine.core.output.SimpleContentItem 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();
}
use of org.pentaho.platform.engine.core.output.SimpleContentItem in project pentaho-platform by pentaho.
the class OutputTest method testMultiContentItem.
public void testMultiContentItem() {
ByteArrayOutputStream out1 = new ByteArrayOutputStream();
ByteArrayOutputStream out2 = new ByteArrayOutputStream();
SimpleContentItem item1 = new SimpleContentItem(out1);
SimpleContentItem item2 = new SimpleContentItem(out2);
MultiContentItem multiContent = new MultiContentItem();
multiContent.addContentItem(item1);
multiContent.addContentItem(item2);
byte[] in = "abcd".getBytes();
String outStr1 = "";
String outStr2 = "";
try {
OutputStream multi = multiContent.getOutputStream("");
multi.write('a');
multi.write(in, 1, 2);
multi.write(in);
multiContent.closeOutputStream();
} catch (IOException e) {
// we should not get here
assertEquals("IOException", null, e);
}
outStr1 = new String(out1.toByteArray());
outStr2 = new String(out2.toByteArray());
assertEquals("Output stream 1", "abcabcd", outStr1);
assertEquals("Output stream 2", "abcabcd", outStr2);
}
Aggregations