001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: *
014: * This component lets me take an input variable (CONTENTOUTPUT), and write it to
015: * the default output.
016: *
017: *
018: * Created Dec 20, 2005
019: * @author mbatchel
020: */
021: package org.pentaho.plugin.core;
022:
023: import java.io.ByteArrayOutputStream;
024: import java.io.InputStream;
025: import java.io.OutputStream;
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.pentaho.core.repository.IContentItem;
029: import org.pentaho.messages.Messages;
030: import org.pentaho.messages.util.LocaleHelper;
031: import org.pentaho.plugin.ComponentBase;
032:
033: /**
034: * This component takes an input, and writes the output into the current
035: * output handler. Future extensions to this component will allow writing
036: * the input variable to a file within the solution.
037: *
038: * <p><b>Inputs</b>
039: * <table>
040: * <tr><th align=left>Input Name</th><th> </th><th align=left>Description</th></tr>
041: * <tr>
042: * <td>CONTENTOUTPUT</td>
043: * <td />
044: * <td>The name of the input that contains the content to output</td>
045: * </tr>
046: * <tr>
047: * <td>mime-type</td>
048: * <td />
049: * <td>The mime type to output.</td>
050: * </tr>
051: * </table>
052: * <p>
053: * <b>Outputs</b>
054: * <p>None
055: * @author mbatchel
056: */
057: public class ContentOutputComponent extends ComponentBase {
058:
059: private static final long serialVersionUID = -6300339081029611956L;
060:
061: private static final String INPUT_NAME_EXPECTED = "CONTENTOUTPUT"; //$NON-NLS-1$
062:
063: private static final String OUTPUT_NAME = "content"; //$NON-NLS-1$
064:
065: private static final String COMPONENT_SETTING_MIME_TYPE = "mime-type"; //$NON-NLS-1$
066:
067: /**
068: * Validates that the input called CONTENTOUTPUT has been provided, and that the
069: * Mime type of the input is also set.
070: */
071: protected boolean validateAction() {
072: if (!isDefinedInput(INPUT_NAME_EXPECTED)) {
073: error(Messages
074: .getErrorString("ContentOutputComponent.ERROR_0001_CONTENTOUTPUT_NOT_DEFINED")); //$NON-NLS-1$
075: return false;
076: }
077: if (!isDefinedInput(COMPONENT_SETTING_MIME_TYPE)) {
078: error(Messages
079: .getErrorString("ContentOutputComponent.ERROR_0006_MIME_TYPE_REQUIRED")); //$NON-NLS-1$
080: return false;
081: }
082: if (!isDefinedOutput(OUTPUT_NAME)) {
083: error(Messages
084: .getErrorString("ContentOutputComponent.ERROR_0008_CONTENT_OUTPUT_REQUIRED")); //$NON-NLS-1$
085: return false;
086: }
087: return true;
088: }
089:
090: protected boolean validateSystemSettings() {
091: // No system settings to validate
092: return true;
093: }
094:
095: public void done() {
096: // No cleanup necessary
097: }
098:
099: public Log getLogger() {
100: return LogFactory.getLog(ContentOutputComponent.class);
101: }
102:
103: protected boolean executeAction() throws Throwable {
104: String mimeType = getInputStringValue(COMPONENT_SETTING_MIME_TYPE);
105: Object dataToOutput = getInputValue(INPUT_NAME_EXPECTED);
106:
107: if (dataToOutput != null) {
108: IContentItem outputContentItem = getOutputContentItem(
109: OUTPUT_NAME, mimeType);
110: if (outputContentItem != null) {
111: outputContentItem.setMimeType(mimeType);
112: OutputStream outputStream = null;
113: if (dataToOutput instanceof String) {
114: String theOutput = (String) dataToOutput;
115: if (theOutput.length() > 0) {
116: try {
117: outputStream = outputContentItem
118: .getOutputStream(getActionName());
119: outputStream.write(theOutput
120: .getBytes(LocaleHelper
121: .getSystemEncoding()));
122: outputStream.flush();
123: outputStream.close();
124: outputContentItem.closeOutputStream();
125: } catch (Exception e) {
126: error(
127: Messages
128: .getErrorString("ContentOutputComponent.ERROR_0003_WRITING_OUTPUT"), e); //$NON-NLS-1$
129: return false;
130: }
131: trace(theOutput);
132: return true;
133: } else {
134: error(Messages
135: .getErrorString("ContentOutputComponent.ERROR_0002_EMPTY_OUTPUT")); //$NON-NLS-1$
136: return false;
137: }
138: } else if (dataToOutput instanceof InputStream) {
139: InputStream is = (InputStream) dataToOutput;
140: byte[] buff = new byte[1024];
141: int len;
142: outputStream = outputContentItem
143: .getOutputStream(null);
144: while ((len = is.read(buff)) >= 0) {
145: outputStream.write(buff, 0, len);
146: }
147: outputStream.flush();
148: outputStream.close();
149: outputContentItem.closeOutputStream();
150: return true;
151: } else if (dataToOutput instanceof ByteArrayOutputStream) {
152: ByteArrayOutputStream baos = (ByteArrayOutputStream) dataToOutput;
153: outputStream = outputContentItem
154: .getOutputStream(null);
155: outputStream.write(baos.toByteArray());
156: outputStream.flush();
157: outputStream.close();
158: outputContentItem.closeOutputStream();
159: return true;
160: } else {
161: error(Messages
162: .getErrorString(
163: "ContentOutputComponent.ERROR_0007_UNKNOWN_TYPE", dataToOutput.getClass().getName())); //$NON-NLS-1$
164: return false;
165: }
166:
167: } else {
168: error(Messages
169: .getErrorString("ContentOutputComponent.ERROR_0005_OUTPUT_CONTENT_ITEM")); //$NON-NLS-1$
170: return false;
171: }
172: }
173: error(Messages
174: .getErrorString("ContentOutputComponent.ERROR_0004_CONTENTOUTPUT_NULL")); //$NON-NLS-1$
175: return false;
176: }
177:
178: // Nothing to do here.
179: public boolean init() {
180: return true;
181: }
182:
183: }
|