KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27 import org.pentaho.core.repository.IContentItem;
28 import org.pentaho.core.services.HttpContentItem;
29
30 public class HttpOutputHandler implements IOutputHandler {
31
32     private HttpServletResponse JavaDoc response;
33
34     private IContentItem outputContent;
35
36     private IContentItem feedbackContent;
37
38     boolean allowFeedback;
39
40     private boolean contentGenerated;
41
42     private int outputType = OUTPUT_TYPE_DEFAULT;
43
44     public HttpOutputHandler(HttpServletResponse JavaDoc response, OutputStream JavaDoc outputStream, boolean allowFeedback) {
45         this.response = response;
46
47         outputContent = new HttpContentItem(outputStream, this);
48         feedbackContent = new HttpContentItem(outputStream, this);
49
50         this.allowFeedback = allowFeedback;
51         contentGenerated = false;
52     }
53
54     public void setOutputPreference(int outputType) {
55         this.outputType = outputType;
56     }
57
58     public int getOutputPreference() {
59         return outputType;
60     }
61
62     public void setMimeType(String JavaDoc mimeType) {
63         response.setContentType(mimeType);
64     }
65
66     public boolean contentDone() {
67         return contentGenerated;
68
69     }
70
71     public boolean allowFeedback() {
72         return allowFeedback;
73     }
74
75     /*
76      * (non-Javadoc)
77      *
78      * @see org.pentaho.core.solution.IOutputHandler#getOutputDefs()
79      */

80     public Map JavaDoc getOutputDefs() {
81         // TODO Auto-generated method stub
82
return null;
83     }
84
85     /*
86      * (non-Javadoc)
87      *
88      * @see org.pentaho.core.solution.IOutputHandler#getOutputDef(java.lang.String)
89      */

90     public IOutputDef getOutputDef(String JavaDoc name) {
91         // TODO Auto-generated method stub
92
return null;
93     }
94
95     /*
96      * (non-Javadoc)
97      *
98      * @see org.pentaho.core.solution.IOutputHandler#getFeedbackStream()
99      */

100     public IContentItem getFeedbackContentItem() {
101         if (allowFeedback) {
102             // assume that content is generated becuase of this
103
contentGenerated = true;
104             return feedbackContent;
105         }
106         return null;
107     }
108
109     /*
110      * (non-Javadoc)
111      *
112      * @see org.pentaho.core.solution.IOutputHandler#getContentStream()
113      */

114     public IContentItem getOutputContentItem(String JavaDoc objectName, String JavaDoc contentName) {
115         if (IOutputHandler.FILE.equals(objectName)) {
116             return FileOutputHandler.getFileOutputContentItem(contentName);
117         }
118         if (objectName.equals(IOutputHandler.RESPONSE) && contentName.equals(IOutputHandler.CONTENT)) {
119             // assume that content is generated becuase of this
120
contentGenerated = true;
121             return outputContent;
122         } else {
123             return null;
124         }
125     }
126
127     public IContentItem getOutputContentItem(String JavaDoc objectName, String JavaDoc contentName, String JavaDoc title, String JavaDoc url) {
128             return getOutputContentItem( objectName, contentName );
129     }
130
131     public void setContentItem(IContentItem content, String JavaDoc objectName, String JavaDoc contentName) {
132         if (objectName.equals(IOutputHandler.RESPONSE) && contentName.equals(IOutputHandler.CONTENT)) {
133             response.setContentType(content.getMimeType());
134         }
135     }
136
137     public void setOutput(String JavaDoc name, Object JavaDoc value) {
138         if ( value == null ) {
139             return;
140         }
141         
142         if ("redirect".equalsIgnoreCase(name)) { //$NON-NLS-1$
143
try {
144                 response.sendRedirect( value.toString() );
145             } catch (Exception JavaDoc e) {
146                 
147             }
148         }
149         else if ("header".equalsIgnoreCase(name)) { //$NON-NLS-1$
150
try {
151                 if ( value instanceof Map JavaDoc ) {
152                     for ( Iterator JavaDoc it = ((Map JavaDoc)value).entrySet().iterator(); it.hasNext(); ) {
153                         Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
154                         response.addHeader( entry.getKey().toString(), entry.getValue().toString() );
155                     }
156                 }
157                 else {
158                     String JavaDoc strVal = value.toString();
159                     int i = strVal.indexOf( '=' );
160                     String JavaDoc headerName = strVal.substring( 0, i );
161                     String JavaDoc headerValue = strVal.substring( i+1 );
162                     response.addHeader( headerName, headerValue );
163                 }
164             } catch (Exception JavaDoc e) {
165                 // todo Error message perhaps
166
}
167         }
168         
169         else if (IOutputHandler.CONTENT.equalsIgnoreCase(name)) {
170             try {
171                 if ( value instanceof IContentItem ) {
172                     IContentItem content = (IContentItem)value;
173                     if ( (response.getContentType() == null) || (!response.getContentType().equalsIgnoreCase( content.getMimeType() ) ) ) {
174                         response.setContentType( content.getMimeType() );
175                     }
176                     content.getMimeType();
177                     InputStream JavaDoc inStr = content.getInputStream();
178                     try {
179                       OutputStream JavaDoc outStr = response.getOutputStream();
180                       int inCnt = 0;
181                       byte[] buf = new byte[4096];
182                       while (-1 != (inCnt = inStr.read(buf))) {
183                           outStr.write(buf, 0, inCnt);
184                       }
185                     } finally {
186                       try {
187                         inStr.close();
188                       } catch (Exception JavaDoc ignored) {}
189                     }
190                     contentGenerated = true;
191                 }
192                 else {
193                     if ( response.getContentType() == null ) {
194                         response.setContentType( "text/html" ); //$NON-NLS-1$
195
}
196                     
197                     response.getOutputStream().write( value.toString().getBytes() );
198                     contentGenerated = true;
199                 }
200             } catch (Exception JavaDoc e) {
201
202             }
203         }
204     }
205
206 }
207
Popular Tags