KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > pagecache > rollercache > CacheHttpServletResponseWrapper


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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