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: * @created Jul 11, 2005
014: * @author James Dixon
015: *
016: */
017:
018: package org.pentaho.core.solution;
019:
020: import java.io.InputStream;
021: import java.io.OutputStream;
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: import javax.servlet.http.HttpServletResponse;
026:
027: import org.pentaho.core.output.IContentOutputHandler;
028: import org.pentaho.core.repository.IContentItem;
029: import org.pentaho.core.runtime.IRuntimeContext;
030: import org.pentaho.core.services.HttpContentItem;
031: import org.pentaho.core.session.IPentahoSession;
032: import org.pentaho.core.system.PentahoSystem;
033:
034: public class HttpOutputHandler implements IOutputHandler,
035: MimeTypeListener {
036:
037: private HttpServletResponse response;
038:
039: private IContentItem outputContent;
040:
041: private IContentItem feedbackContent;
042:
043: boolean allowFeedback;
044:
045: private boolean contentGenerated;
046:
047: private IPentahoSession session;
048:
049: private MimeTypeListener mimeTypeListener;
050:
051: protected IRuntimeContext runtimeContext;
052:
053: private int outputType = OUTPUT_TYPE_DEFAULT;
054:
055: public HttpOutputHandler(HttpServletResponse response,
056: OutputStream outputStream, boolean allowFeedback) {
057: this .response = response;
058:
059: outputContent = new HttpContentItem(outputStream, this );
060: ((HttpContentItem) outputContent).setMimeTypeListener(this );
061: feedbackContent = new HttpContentItem(outputStream, this );
062:
063: this .allowFeedback = allowFeedback;
064: contentGenerated = false;
065: }
066:
067: public void setSession(IPentahoSession session) {
068: this .session = session;
069: }
070:
071: public IPentahoSession getSession() {
072: return session;
073: }
074:
075: public void setOutputPreference(int outputType) {
076: this .outputType = outputType;
077: }
078:
079: public int getOutputPreference() {
080: return outputType;
081: }
082:
083: public void setMimeType(String mimeType) {
084:
085: if (mimeTypeListener != null) {
086: mimeTypeListener.setMimeType(mimeType);
087: }
088: response.setContentType(mimeType);
089: }
090:
091: public boolean contentDone() {
092: return contentGenerated;
093:
094: }
095:
096: public boolean allowFeedback() {
097: return allowFeedback;
098: }
099:
100: /*
101: * (non-Javadoc)
102: *
103: * @see org.pentaho.core.solution.IOutputHandler#getOutputDefs()
104: */
105: public Map getOutputDefs() {
106: // TODO Auto-generated method stub
107: return null;
108: }
109:
110: /*
111: * (non-Javadoc)
112: *
113: * @see org.pentaho.core.solution.IOutputHandler#getOutputDef(java.lang.String)
114: */
115: public IOutputDef getOutputDef(String name) {
116: // TODO Auto-generated method stub
117: return null;
118: }
119:
120: /*
121: * (non-Javadoc)
122: *
123: * @see org.pentaho.core.solution.IOutputHandler#getFeedbackStream()
124: */
125: public IContentItem getFeedbackContentItem() {
126: if (allowFeedback) {
127: // assume that content is generated becuase of this
128: contentGenerated = true;
129: return feedbackContent;
130: }
131: return null;
132: }
133:
134: /*
135: * (non-Javadoc)
136: *
137: * @see org.pentaho.core.solution.IOutputHandler#getContentStream()
138: */
139: public IContentItem getOutputContentItem(String objectName,
140: String contentName, String solution, String instanceId,
141: String mimeType) {
142: if (objectName.equals(IOutputHandler.RESPONSE)
143: && contentName.equals(IOutputHandler.CONTENT)) {
144: // assume that content is generated becuase of this
145: // change the content type if necessary
146: outputContent.setMimeType(mimeType);
147: contentGenerated = true;
148:
149: return outputContent;
150: } else {
151: IContentOutputHandler output = null;
152:
153: // this code allows us to stay backwards compatible
154: if (contentName != null && contentName.indexOf(":") == -1) { //$NON-NLS-1$
155: output = PentahoSystem
156: .getOutputDestinationFromContentRef(objectName
157: + ":" + contentName, session); //$NON-NLS-1$
158: } else {
159: output = PentahoSystem
160: .getOutputDestinationFromContentRef(
161: contentName, session);
162: if (output == null) {
163: output = PentahoSystem
164: .getOutputDestinationFromContentRef(
165: objectName + ":" + contentName, session); //$NON-NLS-1$
166: }
167: }
168: if (output != null) {
169: output.setInstanceId(instanceId);
170: output.setMimeType(mimeType);
171: output.setSolutionName(solution);
172: return output.getFileOutputContentItem();
173: }
174: }
175: return null;
176: }
177:
178: public IContentItem getOutputContentItem(String objectName,
179: String contentName, String title, String url,
180: String solution, String instanceId, String mimeType) {
181: return getOutputContentItem(objectName, contentName, solution,
182: instanceId, mimeType);
183: }
184:
185: public void setContentItem(IContentItem content, String objectName,
186: String contentName) {
187: if (objectName.equals(IOutputHandler.RESPONSE)
188: && contentName.equals(IOutputHandler.CONTENT)) {
189: setMimeType(content.getMimeType());
190: }
191: }
192:
193: public void setOutput(String name, Object value) {
194: if (value == null) {
195: return;
196: }
197:
198: if ("redirect".equalsIgnoreCase(name)) { //$NON-NLS-1$
199: try {
200: response.sendRedirect(value.toString());
201: } catch (Exception e) {
202:
203: }
204: } else if ("header".equalsIgnoreCase(name)) { //$NON-NLS-1$
205: try {
206: if (value instanceof Map) {
207: for (Iterator it = ((Map) value).entrySet()
208: .iterator(); it.hasNext();) {
209: Map.Entry entry = (Map.Entry) it.next();
210: response.addHeader(entry.getKey().toString(),
211: entry.getValue().toString());
212: }
213: } else {
214: String strVal = value.toString();
215: int i = strVal.indexOf('=');
216: String headerName = strVal.substring(0, i);
217: String headerValue = strVal.substring(i + 1);
218: response.addHeader(headerName, headerValue);
219: }
220: } catch (Exception e) {
221: // todo Error message perhaps
222: }
223: }
224:
225: else if (IOutputHandler.CONTENT.equalsIgnoreCase(name)) {
226: try {
227: if (value instanceof IContentItem) {
228: IContentItem content = (IContentItem) value;
229: if ((response.getContentType() == null)
230: || (!response.getContentType()
231: .equalsIgnoreCase(
232: content.getMimeType()))) {
233: // response.setContentType( content.getMimeType() );
234: setMimeType(content.getMimeType());
235: }
236: InputStream inStr = content.getInputStream();
237: try {
238: OutputStream outStr = response
239: .getOutputStream();
240: int inCnt = 0;
241: byte[] buf = new byte[4096];
242: while (-1 != (inCnt = inStr.read(buf))) {
243: outStr.write(buf, 0, inCnt);
244: }
245: } finally {
246: try {
247: inStr.close();
248: } catch (Exception ignored) {
249: }
250: }
251: contentGenerated = true;
252: } else {
253: if (response.getContentType() == null) {
254: setMimeType("text/html"); //$NON-NLS-1$
255: }
256:
257: response.getOutputStream().write(
258: value.toString().getBytes());
259: contentGenerated = true;
260: }
261: } catch (Exception e) {
262:
263: }
264: }
265: }
266:
267: public MimeTypeListener getMimeTypeListener() {
268: return mimeTypeListener;
269: }
270:
271: public void setMimeTypeListener(MimeTypeListener mimeTypeListener) {
272: this .mimeTypeListener = mimeTypeListener;
273: }
274:
275: public IContentItem getOutputContent() {
276: return outputContent;
277: }
278:
279: public void setOutputContent(IContentItem outputContent) {
280: this .outputContent = outputContent;
281: }
282:
283: public HttpServletResponse getResponse() {
284: return response;
285: }
286:
287: public void setResponse(HttpServletResponse response) {
288: this .response = response;
289: }
290:
291: public void setRuntimeContext(IRuntimeContext runtimeContext) {
292: this.runtimeContext = runtimeContext;
293: }
294:
295: }
|