KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > web > util > compression > CompressionServletResponseWrapper


1 package com.genimen.djeneric.web.util.compression;
2
3 /*
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.IOException JavaDoc;
20 import java.io.OutputStreamWriter JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22
23 import javax.servlet.ServletOutputStream JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
26
27 /**
28  * Implementation of <b>HttpServletResponseWrapper</b> that works with
29  * the CompressionServletResponseStream implementation..
30  *
31  * @author Amy Roh
32  * @author Dmitri Valdin
33  * @version $Revision: 1.11 $, $Date: 2005/08/03 22:12:42 $
34  */

35
36 public class CompressionServletResponseWrapper extends HttpServletResponseWrapper JavaDoc
37 {
38
39   // ----------------------------------------------------- Constructor
40

41   /**
42    * Calls the parent constructor which creates a ServletResponse adaptor
43    * wrapping the given response object.
44    */

45
46   public CompressionServletResponseWrapper(HttpServletResponse JavaDoc response)
47   {
48     super(response);
49     origResponse = response;
50     if (debug > 1)
51     {
52       System.out.println("CompressionServletResponseWrapper constructor gets called");
53     }
54   }
55
56   // ----------------------------------------------------- Instance Variables
57

58   /**
59    * Original response
60    */

61
62   protected HttpServletResponse JavaDoc origResponse = null;
63
64   /**
65    * Descriptive information about this Response implementation.
66    */

67
68   protected static final String JavaDoc info = "CompressionServletResponseWrapper";
69
70   /**
71    * The ServletOutputStream that has been returned by
72    * <code>getOutputStream()</code>, if any.
73    */

74
75   protected ServletOutputStream JavaDoc stream = null;
76
77   /**
78    * The PrintWriter that has been returned by
79    * <code>getWriter()</code>, if any.
80    */

81
82   protected PrintWriter JavaDoc writer = null;
83
84   /**
85    * The threshold number to compress
86    */

87   protected int threshold = 0;
88
89   /**
90    * Debug level
91    */

92   private int debug = 0;
93
94   /**
95    * Content type
96    */

97   protected String JavaDoc contentType = null;
98
99   // --------------------------------------------------------- Public Methods
100

101   /**
102    * Set content type
103    */

104   public void setContentType(String JavaDoc contentType)
105   {
106     if (debug > 1)
107     {
108       System.out.println("setContentType to " + contentType);
109     }
110     this.contentType = contentType;
111     origResponse.setContentType(contentType);
112   }
113
114   /**
115    * Set threshold number
116    */

117   public void setCompressionThreshold(int threshold)
118   {
119     if (debug > 1)
120     {
121       System.out.println("setCompressionThreshold to " + threshold);
122     }
123     this.threshold = threshold;
124   }
125
126   /**
127    * Set debug level
128    */

129   public void setDebugLevel(int debug)
130   {
131     this.debug = debug;
132   }
133
134   /**
135    * Create and return a ServletOutputStream to write the content
136    * associated with this Response.
137    *
138    * @exception IOException if an input/output error occurs
139    */

140   public ServletOutputStream JavaDoc createOutputStream() throws IOException JavaDoc
141   {
142     if (debug > 1)
143     {
144       System.out.println("createOutputStream gets called");
145     }
146
147     CompressionResponseStream stream = new CompressionResponseStream(origResponse);
148     stream.setDebugLevel(debug);
149     stream.setBuffer(threshold);
150
151     return stream;
152
153   }
154
155   /**
156    * Finish a response.
157    */

158   public void finishResponse()
159   {
160     try
161     {
162       if (writer != null)
163       {
164         writer.close();
165       }
166       else
167       {
168         if (stream != null) stream.close();
169       }
170     }
171     catch (IOException JavaDoc e)
172     {
173     }
174   }
175
176   // ------------------------------------------------ ServletResponse Methods
177

178   /**
179    * Flush the buffer and commit this response.
180    *
181    * @exception IOException if an input/output error occurs
182    */

183   public void flushBuffer() throws IOException JavaDoc
184   {
185     if (debug > 1)
186     {
187       System.out.println("flush buffer @ CompressionServletResponseWrapper");
188     }
189     ((CompressionResponseStream) stream).flush();
190
191   }
192
193   /**
194    * Return the servlet output stream associated with this Response.
195    *
196    * @exception IllegalStateException if <code>getWriter</code> has
197    * already been called for this response
198    * @exception IOException if an input/output error occurs
199    */

200   public ServletOutputStream JavaDoc getOutputStream() throws IOException JavaDoc
201   {
202
203     if (writer != null) throw new IllegalStateException JavaDoc("getWriter() has already been called for this response");
204
205     if (stream == null) stream = createOutputStream();
206     if (debug > 1)
207     {
208       System.out.println("stream is set to " + stream + " in getOutputStream");
209     }
210
211     return (stream);
212
213   }
214
215   /**
216    * Return the writer associated with this Response.
217    *
218    * @exception IllegalStateException if <code>getOutputStream</code> has
219    * already been called for this response
220    * @exception IOException if an input/output error occurs
221    */

222   public PrintWriter JavaDoc getWriter() throws IOException JavaDoc
223   {
224
225     if (writer != null) return (writer);
226
227     if (stream != null) throw new IllegalStateException JavaDoc("getOutputStream() has already been called for this response");
228
229     stream = createOutputStream();
230     if (debug > 1)
231     {
232       System.out.println("stream is set to " + stream + " in getWriter");
233     }
234     //String charset = getCharsetFromContentType(contentType);
235
String JavaDoc charEnc = origResponse.getCharacterEncoding();
236     if (debug > 1)
237     {
238       System.out.println("character encoding is " + charEnc);
239     }
240     // HttpServletResponse.getCharacterEncoding() shouldn't return null
241
// according the spec, so feel free to remove that "if"
242
if (charEnc != null)
243     {
244       writer = new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(stream, charEnc));
245     }
246     else
247     {
248       writer = new PrintWriter JavaDoc(stream);
249     }
250     return (writer);
251
252   }
253
254   public void setContentLength(int length)
255   {
256   }
257
258   /**
259    * Returns character from content type. This method was taken from tomcat.
260    * @author rajo
261    */

262   private static String JavaDoc getCharsetFromContentType(String JavaDoc type)
263   {
264
265     if (type == null)
266     {
267       return null;
268     }
269     int semi = type.indexOf(";");
270     if (semi == -1)
271     {
272       return null;
273     }
274     String JavaDoc afterSemi = type.substring(semi + 1);
275     int charsetLocation = afterSemi.indexOf("charset=");
276     if (charsetLocation == -1)
277     {
278       return null;
279     }
280     else
281     {
282       String JavaDoc afterCharset = afterSemi.substring(charsetLocation + 8);
283       String JavaDoc encoding = afterCharset.trim();
284       return encoding;
285     }
286   }
287
288 }
289
290 /// the visibility of variables declared at class level and within methods.
291
class HW
292 {
293   int j; // j is visible to both method1 and method 2
294

295   void method1()
296   {
297     double x; // x is visible only to method1
298
}
299
300   void method2()
301   {
302     double y; // y is visible only to method2
303
}
304 }
Popular Tags