KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > core > ContentOutputComponent


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  *
14  * This component lets me take an input variable (CONTENTOUTPUT), and write it to
15  * the default output.
16  *
17  *
18  * Created Dec 20, 2005
19  * @author mbatchel
20  */

21 package org.pentaho.plugin.core;
22
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.pentaho.core.repository.IContentItem;
29 import org.pentaho.messages.Messages;
30 import org.pentaho.messages.util.LocaleHelper;
31 import org.pentaho.plugin.ComponentBase;
32
33 /**
34  * This component takes an input, and writes the output into the current
35  * output handler. Future extensions to this component will allow writing
36  * the input variable to a file within the solution.
37  *
38  * <p><b>Inputs</b>
39  * <table>
40  * <tr><th align=left>Input Name</th><th>&nbsp</th><th align=left>Description</th></tr>
41  * <tr>
42  * <td>CONTENTOUTPUT</td>
43  * <td />
44  * <td>The name of the input that contains the content to output</td>
45  * </tr>
46  * <tr>
47  * <td>mime-type</td>
48  * <td />
49  * <td>The mime type to output.</td>
50  * </tr>
51  * </table>
52  * <p>
53  * <b>Outputs</b>
54  * <p>None
55  * @author mbatchel
56  */

57 public class ContentOutputComponent extends ComponentBase {
58
59     private static final long serialVersionUID = -6300339081029611956L;
60
61     private static final String JavaDoc INPUT_NAME_EXPECTED = "CONTENTOUTPUT"; //$NON-NLS-1$
62

63     private static final String JavaDoc COMPONENT_SETTING_MIME_TYPE = "mime-type"; //$NON-NLS-1$
64

65     /**
66      * Validates that the input called CONTENTOUTPUT has been provided, and that the
67      * Mime type of the input is also set.
68      */

69     protected boolean validateAction() {
70         if (!isDefinedInput(INPUT_NAME_EXPECTED)) {
71             error(Messages.getErrorString("ContentOutputComponent.ERROR_0001_CONTENTOUTPUT_NOT_DEFINED")); //$NON-NLS-1$
72
return false;
73         }
74         if (!isDefinedInput(COMPONENT_SETTING_MIME_TYPE)) {
75             error(Messages.getErrorString("ContentOutputComponent.ERROR_0006_MIME_TYPE_REQUIRED")); //$NON-NLS-1$
76
return false;
77         }
78         return true;
79     }
80
81     protected boolean validateSystemSettings() {
82         // No system settings to validate
83
return true;
84     }
85
86     public void done() {
87         // No cleanup necessary
88
}
89
90     public Log getLogger() {
91         return LogFactory.getLog(ContentOutputComponent.class);
92     }
93
94     protected boolean executeAction() throws Throwable JavaDoc {
95         String JavaDoc mimeType = getInputStringValue(COMPONENT_SETTING_MIME_TYPE);
96         Object JavaDoc dataToOutput = getInputValue(INPUT_NAME_EXPECTED);
97         
98         if (dataToOutput != null) {
99             IContentItem outputContentItem = getOutputContentItem();
100             if (outputContentItem != null) {
101                 outputContentItem.setMimeType(mimeType);
102                 OutputStream JavaDoc outputStream = null;
103                 if (dataToOutput instanceof String JavaDoc) {
104                     String JavaDoc theOutput = (String JavaDoc) dataToOutput;
105                     if (theOutput.length() > 0) {
106                         try {
107                             outputStream = outputContentItem.getOutputStream(getActionName());
108                             outputStream.write(theOutput.getBytes(LocaleHelper.getSystemEncoding()));
109                         } catch (Exception JavaDoc e) {
110                             error(Messages.getErrorString("ContentOutputComponent.ERROR_0003_WRITING_OUTPUT"), e); //$NON-NLS-1$
111
return false;
112                         }
113                         trace(theOutput);
114                         return true;
115                     } else {
116                         error(Messages.getErrorString("ContentOutputComponent.ERROR_0002_EMPTY_OUTPUT")); //$NON-NLS-1$
117
return false;
118                     }
119                 } else if (dataToOutput instanceof InputStream JavaDoc) {
120                     InputStream JavaDoc is = (InputStream JavaDoc) dataToOutput;
121                     byte[] buff = new byte[1024];
122                     int len;
123                     outputStream = outputContentItem.getOutputStream(null);
124                     while ((len = is.read(buff)) >= 0) {
125                         outputStream.write(buff, 0, len);
126                     }
127                     return true;
128                 } else if (dataToOutput instanceof ByteArrayOutputStream JavaDoc) {
129                     ByteArrayOutputStream JavaDoc baos = (ByteArrayOutputStream JavaDoc) dataToOutput;
130                     outputStream = outputContentItem.getOutputStream(null);
131                     outputStream.write(baos.toByteArray());
132                     return true;
133                 } else {
134                     error(Messages.getErrorString("ContentOutputComponent.ERROR_0007_UNKNOWN_TYPE", dataToOutput.getClass().getName())); //$NON-NLS-1$
135
return false;
136                 }
137
138             } else {
139                 error(Messages.getErrorString("ContentOutputComponent.ERROR_0005_OUTPUT_CONTENT_ITEM")); //$NON-NLS-1$
140
return false;
141             }
142         }
143         error(Messages.getErrorString("ContentOutputComponent.ERROR_0004_CONTENTOUTPUT_NULL")); //$NON-NLS-1$
144
return false;
145     }
146
147     // Nothing to do here.
148
public boolean init() {
149         return true;
150     }
151
152 }
153
Popular Tags