KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > filters > cache > CacheResponseWrapper


1 /*
2  * $$Id: CacheResponseWrapper.java,v 1.3 2005/06/07 12:32:31 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.cache;
25
26 import java.io.IOException JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.OutputStreamWriter JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30
31 import javax.servlet.ServletOutputStream JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
34
35 public class CacheResponseWrapper extends HttpServletResponseWrapper JavaDoc {
36
37     protected HttpServletResponse JavaDoc origResponse = null;
38
39     protected ServletOutputStream JavaDoc stream = null;
40
41     protected PrintWriter JavaDoc writer = null;
42
43     protected OutputStream JavaDoc cache = null;
44
45     public CacheResponseWrapper(HttpServletResponse JavaDoc response, OutputStream JavaDoc cache) {
46         super(response);
47         origResponse = response;
48         this.cache = cache;
49     }
50
51     public ServletOutputStream JavaDoc createOutputStream() throws IOException JavaDoc {
52         return (new CacheResponseStream(origResponse, cache));
53     }
54
55     public void flushBuffer() throws IOException JavaDoc {
56         stream.flush();
57     }
58
59     public ServletOutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
60         if (writer != null) {
61             throw new IllegalStateException JavaDoc(
62                     "getWriter() has already been called!");
63         }
64
65         if (stream == null)
66             stream = createOutputStream();
67         return (stream);
68     }
69
70     public PrintWriter JavaDoc getWriter() throws IOException JavaDoc {
71         if (writer != null) {
72             return (writer);
73         }
74
75         if (stream != null) {
76             throw new IllegalStateException JavaDoc(
77                     "getOutputStream() has already been called!");
78         }
79
80         stream = createOutputStream();
81         // Reuse content's encoding
82
writer = new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(stream, origResponse
83                 .getCharacterEncoding()));
84         return (writer);
85     }
86 }
Popular Tags