KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > web > cache > filter > CachingResponseWrapper


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.appserv.web.cache.filter;
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 java.util.ArrayList JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Locale JavaDoc;
34
35 import javax.servlet.ServletOutputStream JavaDoc;
36 import javax.servlet.http.Cookie JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
39
40 import org.apache.catalina.util.RequestUtil;
41 // import org.apache.catalina.Context;
42

43 /**
44  * a wrapper to HttpServletResponse to cache the outbound headers and content
45  * @see javax.servlet.http.HttpServletResponseWrapper and
46  * @see javax.servlet.http.HttpServletResponse
47  * @see org.apache.catalina.connector.HttpResponseBase
48  */

49 public class CachingResponseWrapper extends HttpServletResponseWrapper JavaDoc {
50
51     int statusCode = HttpCacheEntry.VALUE_NOT_SET;
52
53     /**
54      * The HTTP headers explicitly added via addHeader(), but not including
55      * those to be added with setContentLength(), setContentType(), and so on.
56      * This collection is keyed by the header name, and the elements are
57      * ArrayLists containing the associated values that have been set.
58      *
59      */

60     HashMap JavaDoc headers = new HashMap JavaDoc();
61     /**
62      * cache all the set/addDateHeader calls
63      */

64     HashMap JavaDoc dateHeaders = new HashMap JavaDoc();
65     /**
66      * The set of Cookies associated with this Response.
67      */

68     ArrayList JavaDoc cookies = new ArrayList JavaDoc();
69
70     int contentLength = HttpCacheEntry.VALUE_NOT_SET;
71     String JavaDoc contentType;
72     Locale JavaDoc locale;
73
74     /**
75      * Error flag. True if the response runs into an error.
76      * treat the response to be in the error state if the servlet
77      * doesn't get the OutpuStream or the Writer.
78      */

79     boolean error = true;
80
81     /**
82      * OutputStream and PrintWriter objects for this response.
83      */

84     CachingOutputStreamWrapper cosw;
85     PrintWriter JavaDoc writer;
86
87     /**
88      * Constructs a response adaptor wrapping the given response.
89      * @throws java.lang.IllegalArgumentException if the response is null
90      */

91     public CachingResponseWrapper(HttpServletResponse JavaDoc response) {
92         super(response);
93     }
94     
95     /**
96      * Create and return a ServletOutputStream to write the content
97      * associated with this Response.
98      *
99      * @exception IOException if an input/output error occurs
100      */

101     private CachingOutputStreamWrapper createCachingOutputStreamWrapper()
102                                 throws IOException JavaDoc {
103         return new CachingOutputStreamWrapper();
104     }
105
106     /**
107      * Return the servlet output stream associated with this Response.
108      *
109      * @exception IllegalStateException if <code>getWriter</code> has
110      * already been called for this response
111      * @exception IOException if an input/output error occurs
112      */

113     public ServletOutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
114         if (writer != null)
115             throw new IllegalStateException JavaDoc ("getOutputStream<>getWriter");
116
117         if (cosw == null) {
118             cosw = createCachingOutputStreamWrapper();
119         }
120
121         error = false;
122         return (ServletOutputStream JavaDoc)cosw;
123     }
124       
125     /**
126      * Return the writer associated with this Response.
127      *
128      * @exception IllegalStateException if <code>getOutputStream</code> has
129      * already been called for this response
130      * @exception IOException if an input/output error occurs
131      */

132     public PrintWriter JavaDoc getWriter() throws IOException JavaDoc {
133
134         if (writer != null)
135             return (writer);
136
137         if (cosw != null)
138             throw new IllegalStateException JavaDoc ("getWriter<>getOutputStream");
139
140         cosw = createCachingOutputStreamWrapper();
141
142         OutputStreamWriter JavaDoc osw =
143             new OutputStreamWriter JavaDoc(cosw, getCharacterEncoding());
144
145         writer = new PrintWriter JavaDoc(osw);
146
147         error = false;
148         return (writer);
149     }
150     
151     /**
152      * Set the content length (in bytes) for this Response.
153      *
154      * @param len The new content length
155      */

156     public void setContentLength(int len) {
157         super.setContentLength(len);
158
159         this.contentLength = len;
160     }
161     
162     /**
163      * Set the content type for this Response.
164      *
165      * @param type The new content type
166      */

167     public void setContentType(String JavaDoc type) {
168         super.setContentType(type);
169
170         this.contentType = type;
171     }
172     
173     /**
174      * Set the Locale that is appropriate for this response, including
175      * setting the appropriate character encoding.
176      *
177      * @param locale The new locale
178      */

179     public void setLocale(Locale JavaDoc locale) {
180         super.setLocale(locale);
181
182         this.locale = locale;
183     }
184
185     /**
186      * The default behavior of this method is to call addCookie(Cookie cookie)
187      * on the wrapped response object.
188      */

189     public void addCookie(Cookie JavaDoc cookie) {
190         super.addCookie(cookie);
191
192         synchronized (cookies) {
193             cookies.add(cookie);
194         }
195     }
196     
197     /**
198      * Set the specified header to the specified value.
199      *
200      * @param name Name of the header to set
201      * @param value Value to be set
202      */

203     public void setHeader(String JavaDoc name, String JavaDoc value) {
204         super.setHeader(name, value);
205
206         ArrayList JavaDoc values = new ArrayList JavaDoc();
207         values.add(value);
208
209         synchronized (headers) {
210             headers.put(name, values);
211         }
212     }
213
214     /**
215      * Set the specified integer header to the specified value.
216      *
217      * @param name Name of the header to set
218      * @param value Integer value to be set
219      */

220     public void setIntHeader(String JavaDoc name, int value) {
221         setHeader(name, "" + value);
222     }
223
224     /**
225      * Add the specified header to the specified value.
226      *
227      * @param name Name of the header to set
228      * @param value Value to be set
229      */

230     public void addHeader(String JavaDoc name, String JavaDoc value) {
231         super.addHeader(name, value);
232
233         ArrayList JavaDoc values = (ArrayList JavaDoc) headers.get(name);
234         if (values == null) {
235             values = new ArrayList JavaDoc();
236
237             synchronized (headers) {
238                 headers.put(name, values);
239             }
240         }
241
242         values.add(value);
243     }
244
245     /**
246      * Add the specified integer header to the specified value.
247      *
248      * @param name Name of the header to set
249      * @param value Integer value to be set
250      */

251     public void addIntHeader(String JavaDoc name, int value) {
252         addHeader(name, "" + value);
253     }
254
255     /**
256      * Set the specified date header to the specified value.
257      *
258      * @param name Name of the header to set
259      * @param value Date value to be set
260      */

261     public void setDateHeader(String JavaDoc name, long value) {
262         super.setDateHeader(name, value);
263
264         ArrayList JavaDoc values = new ArrayList JavaDoc();
265         values.add(new Long JavaDoc(value));
266
267         synchronized (dateHeaders) {
268             dateHeaders.put(name, values);
269         }
270     }
271
272     /**
273      * Add the specified date header to the specified value.
274      *
275      * @param name Name of the header to set
276      * @param value Date value to be set
277      */

278     public void addDateHeader(String JavaDoc name, long value) {
279         super.addDateHeader(name, value);
280
281         ArrayList JavaDoc values = (ArrayList JavaDoc) dateHeaders.get(name);
282         if (values == null) {
283             values = new ArrayList JavaDoc();
284
285             synchronized (dateHeaders) {
286                 dateHeaders.put(name, values);
287             }
288         }
289
290         values.add(new Long JavaDoc(value));
291     }
292
293     /**
294      * Set the HTTP status to be returned with this response.
295      *
296      * @param sc The new HTTP status
297      */

298     public void setStatus(int sc) {
299         super.setStatus(sc);
300
301         this.statusCode = sc;
302     }
303  
304     /**
305      * Send an error response with the specified status and a
306      * default message.
307      *
308      * @param status HTTP status code to send
309      *
310      * @exception IllegalStateException if this response has
311      * already been committed
312      * @exception IOException if an input/output error occurs
313      */

314     public void sendError(int status) throws IOException JavaDoc {
315         super.sendError(status);
316
317         error = true;
318     }
319
320     /**
321      * Send an error response with the specified status and message.
322      *
323      * @param status HTTP status code to send
324      * @param message Corresponding message to send
325      *
326      * @exception IllegalStateException if this response has
327      * already been committed
328      * @exception IOException if an input/output error occurs
329      */

330     public void sendError(int status, String JavaDoc message) throws IOException JavaDoc {
331         super.sendError(status, message);
332
333         error = true;
334     }
335
336     /**
337      * has the response been set to error
338      */

339     public boolean isError() {
340         return error;
341     }
342
343     /**
344      * return the Expires: date header value
345      */

346     public Long JavaDoc getExpiresDateHeader() {
347         return (Long JavaDoc)dateHeaders.get("Expires");
348     }
349
350     /**
351      * called by doFilter to cache the response that was just sent out
352      * @return the entry with cached response headers and body.
353      */

354     public HttpCacheEntry cacheResponse() throws IOException JavaDoc {
355         // create a new entry
356
HttpCacheEntry entry = new HttpCacheEntry();
357         entry.responseHeaders = headers;
358         entry.dateHeaders = dateHeaders;
359         entry.cookies = cookies;
360
361         entry.contentLength = contentLength;
362         entry.contentType = contentType;
363         entry.locale = locale;
364
365         entry.statusCode = statusCode;
366
367         // flush the writer??
368
if (writer != null) {
369             writer.flush();
370         }
371
372         entry.bytes = cosw.getBytes();
373         return entry;
374     }
375     
376     /**
377      * clear the contents of this wrapper
378      */

379     public void clear() {
380         cosw = null;
381         writer = null;
382     }
383 }
384
Popular Tags