KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > core > util > CacheHttpServletResponseWrapper


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package org.apache.roller.ui.core.util;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9 import java.io.IOException JavaDoc;
10 import java.io.OutputStreamWriter JavaDoc;
11 import java.io.PrintWriter JavaDoc;
12 import java.util.Locale JavaDoc;
13 import javax.servlet.ServletOutputStream JavaDoc;
14 import javax.servlet.http.HttpServletResponse JavaDoc;
15 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
16
17 /**
18  * CacheServletResponse is a serialized representation of a response
19  *
20  * @author <a HREF="mailto:sergek@lokitech.com">Serge Knystautas</a>
21  * @version $Revision: 1.2 $
22  */

23 public class CacheHttpServletResponseWrapper extends HttpServletResponseWrapper JavaDoc {
24     private final Log log = LogFactory.getLog(this.getClass());
25
26     /**
27      * We cache the printWriter so we can maintain a single instance
28      * of it no matter how many times it is requested.
29      */

30     private PrintWriter JavaDoc cachedWriter;
31     private ResponseContent result = null;
32     private SplitServletOutputStream cacheOut = null;
33     private int status = SC_OK;
34
35     /**
36      * Constructor
37      *
38      * @param response The servlet response
39      */

40     public CacheHttpServletResponseWrapper(HttpServletResponse JavaDoc response) {
41         super(response);
42         result = new ResponseContent();
43     }
44
45     /**
46      * Get a response content
47      *
48      * @return The content
49      */

50     public ResponseContent getContent() {
51         //Create the byte array
52
result.commit();
53
54         //Return the result from this response
55
return result;
56     }
57
58     /**
59      * Set the content type
60      *
61      * @param value The content type
62      */

63     public void setContentType(String JavaDoc value) {
64         super.setContentType(value);
65         result.setContentType(value);
66     }
67
68     /**
69      * Set the date of a header
70      *
71      * @param name The header name
72      * @param value The date
73      */

74     public void setDateHeader(String JavaDoc name, long value) {
75         if (log.isDebugEnabled()) {
76             log.debug("dateheader: " + name + ": " + value);
77         }
78
79         if ("last-modified".equalsIgnoreCase(name)) {
80             result.setLastModified(value);
81         }
82
83         super.setDateHeader(name, value);
84     }
85
86     /**
87      * Set a header field
88      *
89      * @param name The header name
90      * @param value The header value
91      */

92     public void setHeader(String JavaDoc name, String JavaDoc value) {
93         if (log.isDebugEnabled()) {
94             log.debug("header: " + name + ": " + value);
95         }
96
97         super.setHeader(name, value);
98     }
99
100     /**
101      * Set the int value of the header
102      *
103      * @param name The header name
104      * @param value The int value
105      */

106     public void setIntHeader(String JavaDoc name, int value) {
107         if (log.isDebugEnabled()) {
108             log.debug("intheader: " + name + ": " + value);
109         }
110
111         super.setIntHeader(name, value);
112     }
113
114     /**
115      * We override this so we can catch the response status. Only
116      * responses with a status of 200 (<code>SC_OK</code>) will
117      * be cached.
118      */

119     public void setStatus(int status) {
120         super.setStatus(status);
121         this.status = status;
122     }
123
124     /**
125      * We override this so we can catch the response status. Only
126      * responses with a status of 200 (<code>SC_OK</code>) will
127      * be cached.
128      */

129     public void sendError(int status, String JavaDoc string) throws IOException JavaDoc {
130         super.sendError(status, string);
131         this.status = status;
132     }
133
134     /**
135      * We override this so we can catch the response status. Only
136      * responses with a status of 200 (<code>SC_OK</code>) will
137      * be cached.
138      */

139     public void sendError(int status) throws IOException JavaDoc {
140         super.sendError(status);
141         this.status = status;
142     }
143
144     /**
145      * We override this so we can catch the response status. Only
146      * responses with a status of 200 (<code>SC_OK</code>) will
147      * be cached.
148      */

149     public void setStatus(int status, String JavaDoc string) {
150         super.setStatus(status, string);
151         this.status = status;
152     }
153
154     public void sendRedirect(String JavaDoc location) throws IOException JavaDoc {
155         this.status = SC_MOVED_TEMPORARILY;
156         super.sendRedirect(location);
157     }
158
159     /**
160      * Retrieves the captured HttpResponse status.
161      */

162     public int getStatus() {
163         return status;
164     }
165
166     /**
167      * Set the locale
168      *
169      * @param value The locale
170      */

171     public void setLocale(Locale JavaDoc value) {
172         super.setLocale(value);
173         result.setLocale(value);
174     }
175
176     /**
177      * Get an output stream
178      *
179      * @throws IOException
180      */

181     public ServletOutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
182         // Pass this faked servlet output stream that captures what is sent
183
if (cacheOut == null) {
184             cacheOut = new SplitServletOutputStream(
185                 result.getOutputStream(), super.getOutputStream());
186         }
187
188         return cacheOut;
189     }
190
191     /**
192      * Get a print writer
193      *
194      * @throws IOException
195      */

196     public PrintWriter JavaDoc getWriter() throws IOException JavaDoc {
197        if (cachedWriter == null) {
198            cachedWriter = new SplitPrintWriter(
199               new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(result.getOutputStream(),"UTF-8")),
200               super.getWriter());
201        }
202        return cachedWriter;
203     }
204
205     public void flushBuffer() throws IOException JavaDoc {
206         super.flushBuffer();
207
208         if (cacheOut != null) {
209             cacheOut.flush();
210         }
211
212         if (cachedWriter != null) {
213             cachedWriter.flush();
214         }
215     }
216     
217     public String JavaDoc getCharacterEncoding()
218     {
219         return "UTF-8";
220     }
221 }
222
Popular Tags