KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > filters > gzip > GZIPResponseWrapper


1 /*
2  * $$Id: GZIPResponseWrapper.java,v 1.3 2005/06/07 12:31:56 bel70 Exp $$
3  *
4  * ***** BEGIN LICENSE BLOCK ***** The contents of this file are subject to the
5  * Mozilla Public License Version 1.1 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of the License
7  * at http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
11  * the specific language governing rights and limitations under the License.
12  *
13  * The Original Code is JGossip forum code.
14  *
15  * The Initial Developer of the Original Code is the JResearch, Org. Portions
16  * created by the Initial Developer are Copyright (C) 2004 the Initial
17  * Developer. All Rights Reserved.
18  *
19  * Contributor(s): Dmitry Belov <bel@jresearch.org>, Jayson Falkner
20  * <jayson@jspinsider.com>
21  *
22  * ***** END LICENSE BLOCK *****
23  */

24 package org.jresearch.gossip.filters.gzip;
25
26 import java.io.IOException JavaDoc;
27 import java.io.OutputStreamWriter JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29
30 import javax.servlet.ServletOutputStream JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
33
34 public class GZIPResponseWrapper extends HttpServletResponseWrapper JavaDoc {
35
36     protected HttpServletResponse JavaDoc origResponse = null;
37
38     protected ServletOutputStream JavaDoc stream = null;
39
40     protected PrintWriter JavaDoc writer = null;
41
42     public GZIPResponseWrapper(HttpServletResponse JavaDoc response) {
43         super(response);
44         origResponse = response;
45     }
46
47     public ServletOutputStream JavaDoc createOutputStream() throws IOException JavaDoc {
48         return (new GZIPResponseStream(origResponse));
49     }
50
51     public void finishResponse() {
52         try {
53             if (writer != null) {
54                 writer.close();
55             } else {
56                 if (stream != null) {
57                     stream.close();
58                 }
59             }
60         } catch (IOException JavaDoc e) {
61         }
62     }
63
64     public void flushBuffer() throws IOException JavaDoc {
65         if (stream != null) {
66             stream.flush();
67         }
68     }
69
70     public ServletOutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
71         if (writer != null) {
72             throw new IllegalStateException JavaDoc(
73                     "getWriter() has already been called!");
74         }
75
76         if (stream == null)
77             stream = createOutputStream();
78         return (stream);
79     }
80
81     public PrintWriter JavaDoc getWriter() throws IOException JavaDoc {
82         if (writer != null) {
83             return (writer);
84         }
85
86         if (stream != null) {
87             throw new IllegalStateException JavaDoc(
88                     "getOutputStream() has already been called!");
89         }
90
91         stream = createOutputStream();
92         // Reuse content's encoding
93
writer = new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(stream, origResponse
94                 .getCharacterEncoding()));
95         return (writer);
96     }
97
98     public void setContentLength(int length) {
99     }
100 }
Popular Tags