KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > myvietnam > mvncore > servlet > filter > CompressionServletResponseWrapper


1 /*
2  * CompressionServletResponseWrapper.java
3  * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/servlet/filter/CompressionServletResponseWrapper.java,v 1.6 2004/12/10 04:29:05 minhnn Exp $
4  * $Revision: 1.6 $
5  * $Date: 2004/12/10 04:29:05 $
6  *
7  * ====================================================================
8  *
9  * The Apache Software License, Version 1.1
10  *
11  * Copyright (c) 1999 The Apache Software Foundation. All rights
12  * reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  *
18  * 1. Redistributions of source code must retain the above copyright
19  * notice, this list of conditions and the following disclaimer.
20  *
21  * 2. Redistributions in binary form must reproduce the above copyright
22  * notice, this list of conditions and the following disclaimer in
23  * the documentation and/or other materials provided with the
24  * distribution.
25  *
26  * 3. The end-user documentation included with the redistribution, if
27  * any, must include the following acknowlegement:
28  * "This product includes software developed by the
29  * Apache Software Foundation (http://www.apache.org/)."
30  * Alternately, this acknowlegement may appear in the software itself,
31  * if and wherever such third-party acknowlegements normally appear.
32  *
33  * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
34  * Foundation" must not be used to endorse or promote products derived
35  * from this software without prior written permission. For written
36  * permission, please contact apache@apache.org.
37  *
38  * 5. Products derived from this software may not be called "Apache"
39  * nor may "Apache" appear in their names without prior written
40  * permission of the Apache Group.
41  *
42  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
43  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
44  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
45  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
46  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
49  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
50  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
51  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
52  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53  * SUCH DAMAGE.
54  * ====================================================================
55  *
56  * This software consists of voluntary contributions made by many
57  * individuals on behalf of the Apache Software Foundation. For more
58  * information on the Apache Software Foundation, please see
59  * <http://www.apache.org/>.
60  *
61  * [Additional notices, if required by prior licensing conditions]
62  *
63  */

64
65 package net.myvietnam.mvncore.servlet.filter;
66
67 import java.io.*;
68 import javax.servlet.ServletOutputStream JavaDoc;
69 import javax.servlet.http.HttpServletResponse JavaDoc;
70 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
71 import org.apache.commons.logging.*;
72
73 /**
74  * Implementation of <b>HttpServletResponseWrapper</b> that works with
75  * the CompressionServletResponseStream implementation..
76  *
77  * @author Amy Roh
78  * @author Dmitri Valdin
79  * @version $Revision: 1.6 $, $Date: 2004/12/10 04:29:05 $
80  */

81
82 public class CompressionServletResponseWrapper extends HttpServletResponseWrapper JavaDoc {
83
84     //minhnn : I remove the log because log4j does not support TRACE level
85
private static Log log = LogFactory.getLog(CompressionServletResponseWrapper.class);
86
87     // ----------------------------------------------------- Constructor
88

89     /**
90      * Calls the parent constructor which creates a ServletResponse adaptor
91      * wrapping the given response object.
92      */

93
94     public CompressionServletResponseWrapper(HttpServletResponse JavaDoc response) {
95         super(response);
96         origResponse = response;
97         //log.trace("CompressionServletResponseWrapper constructor gets called");
98
}
99
100
101     // ----------------------------------------------------- Instance Variables
102

103     /**
104      * Original response
105      */

106
107     protected HttpServletResponse JavaDoc origResponse = null;
108
109     /**
110      * Descriptive information about this Response implementation.
111      */

112
113     protected static final String JavaDoc info = "CompressionServletResponseWrapper";
114
115     /**
116      * The ServletOutputStream that has been returned by
117      * <code>getOutputStream()</code>, if any.
118      */

119
120     protected ServletOutputStream JavaDoc stream = null;
121
122
123     /**
124      * The PrintWriter that has been returned by
125      * <code>getWriter()</code>, if any.
126      */

127
128     protected PrintWriter writer = null;
129
130     /**
131      * The threshold number to compress
132      */

133     protected int threshold = 0;
134
135     /**
136      * Debug level
137      */

138 // private int debug = 0;
139

140     /**
141      * Content type
142      */

143     protected String JavaDoc contentType = null;
144
145     // --------------------------------------------------------- Public Methods
146

147
148     /**
149      * Set content type
150      */

151     public void setContentType(String JavaDoc contentType) {
152         //log.trace("setContentType to " + contentType);
153
this.contentType = contentType;
154         origResponse.setContentType(contentType);
155     }
156
157
158     /**
159      * Set threshold number
160      */

161     public void setCompressionThreshold(int threshold) {
162         //log.trace("setCompressionThreshold to " + threshold);
163
this.threshold = threshold;
164     }
165
166
167     /**
168      * Set debug level
169      */

170 // public void setDebugLevel(int debug) {
171
// this.debug = debug;
172
// }
173

174
175     /**
176      * Create and return a ServletOutputStream to write the content
177      * associated with this Response.
178      *
179      * @exception IOException if an input/output error occurs
180      */

181     public ServletOutputStream JavaDoc createOutputStream() throws IOException {
182         //log.trace("createOutputStream gets called");
183

184         CompressionResponseStream stream = new CompressionResponseStream(origResponse);
185         //stream.setDebugLevel(debug);
186
stream.setBuffer(threshold);
187
188         return stream;
189
190     }
191
192
193     /**
194      * Finish a response.
195      */

196     public void finishResponse() {
197         try {
198             if (writer != null) {
199                 writer.close();
200             } else {
201                 if (stream != null)
202                     stream.close();
203             }
204         } catch (IOException e) {
205         }
206     }
207
208
209     // ------------------------------------------------ ServletResponse Methods
210

211
212     /**
213      * Flush the buffer and commit this response.
214      *
215      * @exception IOException if an input/output error occurs
216      */

217     public void flushBuffer() throws IOException {
218         //log.trace("flush buffer @ CompressionServletResponseWrapper");
219

220         // minhnn fix NullPointerException
221
// this NPE occur when it filter a servlet mapping and then it later forward to not-exist jsp file
222
if (stream != null) {
223             //minhnn @todo : how to ensure the stream still open ???
224
((CompressionResponseStream)stream).flush();
225         }
226
227     }
228
229     /**
230      * Return the servlet output stream associated with this Response.
231      *
232      * @exception IllegalStateException if <code>getWriter</code> has
233      * already been called for this response
234      * @exception IOException if an input/output error occurs
235      */

236     public ServletOutputStream JavaDoc getOutputStream() throws IOException {
237
238         if (writer != null)
239             throw new IllegalStateException JavaDoc("getWriter() has already been called for this response");
240
241         if (stream == null) {
242             stream = createOutputStream();
243         }
244         //log.trace("stream is set to " + stream + " in getOutputStream");
245

246         return (stream);
247
248     }
249
250     /**
251      * Return the writer associated with this Response.
252      *
253      * @exception IllegalStateException if <code>getOutputStream</code> has
254      * already been called for this response
255      * @exception IOException if an input/output error occurs
256      */

257     public PrintWriter getWriter() throws IOException {
258
259         if (writer != null)
260             return (writer);
261
262         if (stream != null)
263             throw new IllegalStateException JavaDoc("getOutputStream() has already been called for this response");
264
265         stream = createOutputStream();
266         //log.trace("stream is set to " + stream + " in getWriter");
267
//String charset = getCharsetFromContentType(contentType);
268
String JavaDoc charEnc = origResponse.getCharacterEncoding();
269         //log.trace("character encoding is " + charEnc);
270
// HttpServletResponse.getCharacterEncoding() shouldn't return null
271
// according the spec, so feel free to remove that "if"
272
if (charEnc != null) {
273             writer = new PrintWriter(new OutputStreamWriter(stream, charEnc));
274         } else {
275             writer = new PrintWriter(stream);
276         }
277
278         return (writer);
279
280     }
281
282
283     public void setContentLength(int length) {
284     }
285
286
287     /**
288      * Returns character from content type. This method was taken from tomcat.
289      * @author rajo
290      */

291     private static String JavaDoc getCharsetFromContentType(String JavaDoc type) {
292
293         if (type == null) {
294             return null;
295         }
296         int semi = type.indexOf(";");
297         if (semi == -1) {
298             return null;
299         }
300         String JavaDoc afterSemi = type.substring(semi + 1);
301         int charsetLocation = afterSemi.indexOf("charset=");
302         if(charsetLocation == -1) {
303             return null;
304         } else {
305             String JavaDoc afterCharset = afterSemi.substring(charsetLocation + 8);
306             String JavaDoc encoding = afterCharset.trim();
307             return encoding;
308         }
309     }
310
311 }
312
Popular Tags