KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

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

46     protected PrintWriter writer = null;
47
48     /**
49      * The threshold number to compress
50      */

51     protected int threshold = 0;
52
53     /**
54      * Content type
55      */

56     protected String contentType = null;
57
58     // ---------------------------------------------------------------- public
59

60     
61     /**
62      * Set content type
63      *
64      * @param contentType
65      */

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

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

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

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

112
113     /**
114      * Flush the buffer and commit this response.
115      *
116      * @exception IOException
117      * if an input/output error occurs
118      */

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

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

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