KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > core > solution > SimpleOutputHandler


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  * @created Jul 11, 2005
14  * @author James Dixon
15  *
16  */

17
18 package org.pentaho.core.solution;
19
20 import java.io.InputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.pentaho.core.repository.IContentItem;
26 import org.pentaho.core.services.SimpleContentItem;
27
28 public class SimpleOutputHandler implements IOutputHandler {
29
30     private Map JavaDoc responseAttributes;
31
32     private IContentItem feedbackContent;
33
34     boolean allowFeedback;
35
36     private String JavaDoc mimeType;
37
38     private int outputType = OUTPUT_TYPE_DEFAULT;
39
40     private boolean contentGenerated;
41
42     private Map JavaDoc outputs;
43
44     public SimpleOutputHandler(OutputStream JavaDoc outputStream, boolean allowFeedback) {
45
46         this.allowFeedback = allowFeedback;
47         if (allowFeedback) {
48             feedbackContent = new SimpleContentItem(outputStream);
49         }
50         responseAttributes = new HashMap JavaDoc();
51         contentGenerated = false;
52         outputs = new HashMap JavaDoc();
53         setOutputStream(outputStream, IOutputHandler.RESPONSE, IOutputHandler.CONTENT);
54     }
55
56     public void setOutputStream(OutputStream JavaDoc outputStream, String JavaDoc outputName, String JavaDoc contentName) {
57         String JavaDoc key = outputName + "." + contentName; //$NON-NLS-1$
58
SimpleContentItem item = new SimpleContentItem(outputStream);
59         outputs.put(key, item);
60     }
61
62     public void setOutputPreference(int outputType) {
63         this.outputType = outputType;
64     }
65
66     public boolean contentDone() {
67         return contentGenerated;
68
69     }
70
71     public int getOutputPreference() {
72         return outputType;
73     }
74
75     public void setMimeType(String JavaDoc mimeType) {
76         this.mimeType = mimeType;
77     }
78
79     public String JavaDoc getMimeType() {
80         return mimeType;
81     }
82
83     public boolean allowFeedback() {
84         return allowFeedback;
85     }
86
87     /*
88      * (non-Javadoc)
89      *
90      * @see org.pentaho.core.solution.IOutputHandler#getOutputDefs()
91      */

92     public Map JavaDoc getOutputDefs() {
93         // TODO Auto-generated method stub
94
return null;
95     }
96
97     /*
98      * (non-Javadoc)
99      *
100      * @see org.pentaho.core.solution.IOutputHandler#getOutputDef(java.lang.String)
101      */

102     public IOutputDef getOutputDef(String JavaDoc name) {
103         // TODO Auto-generated method stub
104
return null;
105     }
106
107     /*
108      * (non-Javadoc)
109      *
110      * @see org.pentaho.core.solution.IOutputHandler#getFeedbackStream()
111      */

112     public IContentItem getFeedbackContentItem() {
113         if (allowFeedback) {
114             contentGenerated = true;
115             return feedbackContent;
116         }
117         return null;
118     }
119
120     /*
121      * (non-Javadoc)
122      *
123      * @see org.pentaho.core.solution.IOutputHandler#getContentStream()
124      */

125     public IContentItem getOutputContentItem(String JavaDoc outputName, String JavaDoc contentName) {
126         if (IOutputHandler.FILE.equals(outputName)) {
127             return FileOutputHandler.getFileOutputContentItem(contentName);
128         }
129         String JavaDoc key = outputName + "." + contentName; //$NON-NLS-1$
130
return (SimpleContentItem) outputs.get(key);
131     }
132
133     public IContentItem getOutputContentItem(String JavaDoc objectName, String JavaDoc contentName, String JavaDoc title, String JavaDoc url) {
134         return getOutputContentItem( objectName, contentName );
135     }
136     
137     public void setContentItem(IContentItem content, String JavaDoc objectName, String JavaDoc contentName) {
138         mimeType = content.getMimeType();
139     }
140
141     public void setOutput(String JavaDoc name, Object JavaDoc value) {
142       if (IOutputHandler.CONTENT.equalsIgnoreCase(name)) {
143           IContentItem response = getOutputContentItem( "response", IOutputHandler.CONTENT ); //$NON-NLS-1$
144
if ( response != null ) {
145             try {
146                 if ( value instanceof IContentItem ) {
147                     IContentItem content = (IContentItem)value;
148                     if ( (response.getMimeType() == null) || (!response.getMimeType().equalsIgnoreCase( content.getMimeType() ) ) ) {
149                         response.setMimeType( content.getMimeType() );
150                     }
151
152                     InputStream JavaDoc inStr = content.getInputStream();
153                     try {
154                       OutputStream JavaDoc outStr = response.getOutputStream( response.getActionName() );
155                       int inCnt = 0;
156                       byte[] buf = new byte[4096];
157                       while (-1 != (inCnt = inStr.read(buf))) {
158                           outStr.write(buf, 0, inCnt);
159                       }
160                     } finally {
161                       try {
162                         inStr.close();
163                       } catch (Exception JavaDoc ignored) {}
164                     }
165                     contentGenerated = true;
166                 }
167                 else {
168                     if ( response.getMimeType() == null ) {
169                         response.setMimeType( "text/html" ); //$NON-NLS-1$
170
}
171                     
172                     response.getOutputStream( response.getActionName() ).write( value.toString().getBytes() );
173                     contentGenerated = true;
174                 }
175             } catch (Exception JavaDoc e) {
176
177             }
178         }
179       }
180           else {
181               responseAttributes.put(name, value);
182           }
183         
184     }
185
186     public Map JavaDoc getResponseArrtibutes() {
187         return responseAttributes;
188     }
189 }
190
Popular Tags