KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > servlet > filter > GzipResponseWrapper


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.servlet.filter;
4
5 import java.io.IOException JavaDoc;
6 import java.io.OutputStreamWriter JavaDoc;
7 import java.io.PrintWriter JavaDoc;
8
9 import javax.servlet.ServletOutputStream JavaDoc;
10 import javax.servlet.http.HttpServletResponse JavaDoc;
11 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
12
13 /**
14  * Implementation of <b>HttpServletResponseWrapper</b> that works with
15  * the CompressionServletResponseStream implementation..
16  */

17
18 public class GzipResponseWrapper extends HttpServletResponseWrapper JavaDoc {
19
20     /**
21      * Calls the parent constructor which creates a ServletResponse adaptor
22      * wrapping the given response object.
23      *
24      * @param response
25      */

26     public GzipResponseWrapper(HttpServletResponse JavaDoc response) {
27         super(response);
28         origResponse = response;
29     }
30
31
32
33     /**
34      * Original response
35      */

36     protected HttpServletResponse JavaDoc origResponse = null;
37
38     /**
39      * The ServletOutputStream that has been returned by
40      * <code>getOutputStream()</code>, if any.
41      */

42     protected ServletOutputStream JavaDoc stream = null;
43
44     /**
45      * The PrintWriter that has been returned by <code>getWriter()</code>, if
46      * any.
47      */

48     protected PrintWriter JavaDoc writer = null;
49
50     /**
51      * The threshold number to compress
52      */

53     protected int threshold = 0;
54
55     /**
56      * Content type
57      */

58     protected String JavaDoc contentType = null;
59
60     // ---------------------------------------------------------------- public
61

62     
63     /**
64      * Set content type
65      *
66      * @param contentType
67      */

68     public void setContentType(String JavaDoc contentType) {
69         this.contentType = contentType;
70         origResponse.setContentType(contentType);
71     }
72
73     /**
74      * Set threshold number
75      *
76      * @param threshold
77      */

78     public void setCompressionThreshold(int threshold) {
79         this.threshold = threshold;
80     }
81
82     /**
83      * Creates and returns a ServletOutputStream to write the content associated
84      * with this Response.
85      *
86      * @return a ServletOutputStream to write the content associated with response
87      * @exception IOException
88      * if an input/output error occurs
89      */

90     public ServletOutputStream JavaDoc createOutputStream() throws IOException JavaDoc {
91         GzipResponseStream gzstream = new GzipResponseStream(origResponse);
92         gzstream.setBuffer(threshold);
93         return gzstream;
94     }
95
96
97     /**
98      * Finishes a response.
99      */

100     public void finishResponse() {
101         try {
102             if (writer != null) {
103                 writer.close();
104             } else {
105                 if (stream != null)
106                     stream.close();
107             }
108         } catch (IOException JavaDoc e) {
109             // ignore
110
}
111     }
112
113     // ---------------------------------------------------------------- ServletResponse
114

115
116     /**
117      * Flush the buffer and commit this response.
118      *
119      * @exception IOException
120      * if an input/output error occurs
121      */

122     public void flushBuffer() throws IOException JavaDoc {
123         if (stream != null) {
124             stream.flush();
125         }
126     }
127
128     /**
129      * Returns the servlet output stream associated with this Response.
130      *
131      * @return servlet output stream associated with this Response
132      * @exception IllegalStateException
133      * if <code>getWriter</code> has
134      * already been called for this response
135      * @exception IOException
136      * if an input/output error occurs
137      */

138     public ServletOutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
139
140         if (writer != null) {
141             throw new IllegalStateException JavaDoc("getWriter() has already been called for this response");
142         }
143         if (stream == null) {
144             stream = createOutputStream();
145         }
146         return(stream);
147
148     }
149
150     /**
151      * Returns the writer associated with this Response.
152      *
153      * @return the writer associated with this Response
154      * @exception IllegalStateException
155      * if <code>getOutputStream</code> has
156      * already been called for this response
157      * @exception IOException
158      * if an input/output error occurs
159      */

160     public PrintWriter JavaDoc getWriter() throws IOException JavaDoc {
161
162         if (writer != null) {
163             return writer;
164         }
165
166         if (stream != null) {
167             throw new IllegalStateException JavaDoc("getOutputStream() has already been called for this response");
168         }
169
170         stream = createOutputStream();
171         
172         String JavaDoc charEnc = origResponse.getCharacterEncoding();
173         if (charEnc != null) {
174             writer = new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(stream, charEnc));
175         } else {
176             writer = new PrintWriter JavaDoc(stream);
177         }
178         return(writer);
179     }
180
181
182     public void setContentLength(int length) {
183     }
184
185 }
186
Popular Tags