KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > connection > ResponseWrapper


1 /*
2  * Copyright (c) 1998-2003 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.server.connection;
30
31 import javax.servlet.ServletOutputStream JavaDoc;
32 import javax.servlet.ServletResponse JavaDoc;
33 import javax.servlet.http.Cookie JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.PrintWriter JavaDoc;
37 import java.util.Locale JavaDoc;
38
39 /**
40  * Wraps a servlet response in another response. Filters may
41  * use ServletResponseWrapper to grab results from the servlet.
42  *
43  * <p/>The default methods just call the wrapped response methods.
44  *
45  * @since servlet 2.3
46  */

47 public class ResponseWrapper implements ServletResponse JavaDoc {
48   // the wrapped response
49
protected HttpServletResponse JavaDoc _response;
50   
51   /**
52    * Create a new ServletResponseWrapper, wrapping a specified response.
53    *
54    * @param response the response to wrap.
55    */

56   public ResponseWrapper()
57   {
58   }
59   
60   /**
61    * Create a new ServletResponseWrapper, wrapping a specified response.
62    *
63    * @param response the response to wrap.
64    */

65   public ResponseWrapper(HttpServletResponse JavaDoc response)
66   {
67     _response = response;
68   }
69   
70   /**
71    * Sets the response to be wrapped.
72    *
73    * @param response the response to wrap.
74    */

75   public void setResponse(HttpServletResponse JavaDoc response)
76   {
77     _response = response;
78   }
79   
80   /**
81    * Gets the wrapped response
82    *
83    * @return the wrapped response
84    */

85   public ServletResponse JavaDoc getResponse()
86   {
87     return _response;
88   }
89   
90   /**
91    * Sets the response content type. The content type includes
92    * the character encoding. The content type must be set before
93    * calling <code>getWriter()</code> so the writer can use the
94    * proper character encoding.
95    *
96    * <p>To set the output character encoding to ISO-8859-2, use the
97    * following:
98    *
99    * <code><pre>
100    * response.setContentType("text/html; charset=ISO-8859-2");
101    * </pre></code>
102    *
103    * @param type the mime type of the output
104    */

105   public void setContentType(String JavaDoc type)
106   {
107     _response.setContentType(type);
108   }
109   
110   /**
111    * Returns the content type
112    *
113    * @since 2.4
114    */

115   public String JavaDoc getContentType()
116   {
117     return _response.getContentType();
118   }
119   
120   /**
121    * Returns the character encoding the response is using for output.
122    */

123   public String JavaDoc getCharacterEncoding()
124   {
125     return _response.getCharacterEncoding();
126   }
127   
128   /**
129    * Sets the character encoding the response is using for output.
130    *
131    * @since 2.4
132    */

133   public void setCharacterEncoding(String JavaDoc encoding)
134   {
135     _response.setCharacterEncoding(encoding);
136   }
137   
138   /**
139    * Sets the output locale. The response will set the character encoding
140    * based on the locale. For example, setting the "kr" locale will set
141    * the character encoding to "EUC_KR".
142    */

143   public void setLocale(Locale JavaDoc locale)
144   {
145     _response.setLocale(locale);
146   }
147   /**
148    * Returns the output locale.
149    */

150   public Locale JavaDoc getLocale()
151   {
152     return _response.getLocale();
153   }
154   /**
155    * Returns an output stream for writing to the client. You can use
156    * the output stream to write binary data.
157    */

158   public ServletOutputStream JavaDoc getOutputStream()
159     throws IOException JavaDoc
160   {
161     return _response.getOutputStream();
162   }
163   /**
164    * Returns a PrintWriter with the proper character encoding for writing
165    * text data to the client.
166    */

167   public PrintWriter JavaDoc getWriter()
168     throws IOException JavaDoc
169   {
170     return _response.getWriter();
171   }
172   /**
173    * Sets the output buffer size to <code>size</code>. The servlet engine
174    * may round the size up.
175    *
176    * @param size the new output buffer size.
177    */

178   public void setBufferSize(int size)
179   {
180     _response.setBufferSize(size);
181   }
182   /**
183    * Returns the size of the output buffer.
184    */

185   public int getBufferSize()
186   {
187     return _response.getBufferSize();
188   }
189   /**
190    * Flushes the buffer to the client.
191    */

192   public void flushBuffer()
193     throws IOException JavaDoc
194   {
195     _response.flushBuffer();
196   }
197   /**
198    * Returns true if some data has actually been send to the client. The
199    * data will be sent if the buffer overflows or if it's explicitly flushed.
200    */

201   public boolean isCommitted()
202   {
203     return _response.isCommitted();
204   }
205   
206   /**
207    * Resets the output stream, clearing headers and the output buffer.
208    * Calling <code>reset()</code> after data has been committed is illegal.
209    *
210    * @throws IllegalStateException if <code>isCommitted()</code> is true.
211    */

212   public void reset()
213   {
214     _response.reset();
215   }
216   
217   /**
218    * Resets the output stream without clearing headers and the output buffer.
219    * Calling <code>resetBuffer()</code> after data has been committed is
220    * illegal.
221    *
222    * @throws IllegalStateException if <code>isCommitted()</code> is true.
223    */

224   public void resetBuffer()
225   {
226     _response.resetBuffer();
227   }
228   /**
229    * Resin automatically handles output content length and chunking. This
230    * method is ignored.
231    *
232    * @deprecated
233    */

234   public void setContentLength(int len)
235   {
236     _response.setContentLength(len);
237   }
238
239   /**
240    * Sets the HTTP status
241    *
242    * @param sc the HTTP status code
243    */

244   public void setStatus(int sc)
245   {
246     _response.setStatus(sc);
247   }
248   /**
249    * Sends an HTTP error page based on the status code
250    *
251    * @param sc the HTTP status code
252    */

253   public void sendError(int sc, String JavaDoc msg)
254     throws IOException JavaDoc
255   {
256     _response.sendError(sc, msg);
257   }
258   /**
259    * Sends an HTTP error page based on the status code
260    *
261    * @param sc the HTTP status code
262    */

263   public void sendError(int sc)
264     throws IOException JavaDoc
265   {
266     _response.sendError(sc);
267   }
268   /**
269    * Redirects the client to another page.
270    *
271    * @param location the location to redirect to.
272    */

273   public void sendRedirect(String JavaDoc location)
274     throws IOException JavaDoc
275   {
276     _response.sendRedirect(location);
277   }
278   /**
279    * Sets a header. This will override a previous header
280    * with the same name.
281    *
282    * @param name the header name
283    * @param value the header value
284    */

285   public void setHeader(String JavaDoc name, String JavaDoc value)
286   {
287     _response.setHeader(name, value);
288   }
289   /**
290    * Adds a header. If another header with the same name exists, both
291    * will be sent to the client.
292    *
293    * @param name the header name
294    * @param value the header value
295    */

296   public void addHeader(String JavaDoc name, String JavaDoc value)
297   {
298     _response.addHeader(name, value);
299   }
300   /**
301    * Returns true if the output headers include <code>name</code>
302    *
303    * @param name the header name to test
304    */

305   public boolean containsHeader(String JavaDoc name)
306   {
307     return _response.containsHeader(name);
308   }
309   /**
310    * Sets a header by converting a date to a string.
311    *
312    * <p>To set the page to expire in 15 seconds use the following:
313    * <pre><code>
314    * long now = System.currentTime();
315    * _response.setDateHeader("Expires", now + 15000);
316    * </code></pre>
317    *
318    * @param name name of the header
319    * @param date the date in milliseconds since the epoch.
320    */

321   public void setDateHeader(String JavaDoc name, long date)
322   {
323     _response.setDateHeader(name, date);
324   }
325   /**
326    * Adds a header by converting a date to a string.
327    *
328    * @param name name of the header
329    * @param date the date in milliseconds since the epoch.
330    */

331   public void addDateHeader(String JavaDoc name, long date)
332   {
333     _response.addDateHeader(name, date);
334   }
335   /**
336    * Sets a header by converting an integer value to a string.
337    *
338    * @param name name of the header
339    * @param value the value as an integer
340    */

341   public void setIntHeader(String JavaDoc name, int value)
342   {
343     _response.setIntHeader(name, value);
344   }
345   /**
346    * Adds a header by converting an integer value to a string.
347    *
348    * @param name name of the header
349    * @param value the value as an integer
350    */

351   public void addIntHeader(String JavaDoc name, int value)
352   {
353     _response.addIntHeader(name, value);
354   }
355   /**
356    * Sends a new cookie to the client.
357    */

358   public void addCookie(Cookie JavaDoc cookie)
359   {
360     _response.addCookie(cookie);
361   }
362   /**
363    * Encodes session information in a URL. Calling this will enable
364    * sessions for users who have disabled cookies.
365    *
366    * @param url the url to encode
367    * @return a url with session information encoded
368    */

369   public String JavaDoc encodeURL(String JavaDoc url)
370   {
371     return _response.encodeURL(url);
372   }
373   /**
374    * Encodes session information in a URL suitable for
375    * <code>sendRedirect()</code>
376    *
377    * @param url the url to encode
378    * @return a url with session information encoded
379    */

380   public String JavaDoc encodeRedirectURL(String JavaDoc name)
381   {
382     return _response.encodeRedirectURL(name);
383   }
384   
385   public void setStatus(int sc, String JavaDoc msg)
386   {
387     _response.setStatus(sc, msg);
388   }
389   /**
390    * @deprecated
391    */

392   public String JavaDoc encodeUrl(String JavaDoc url)
393   {
394     return encodeURL(url);
395   }
396   /**
397    * @deprecated
398    */

399   public String JavaDoc encodeRedirectUrl(String JavaDoc url)
400   {
401     return encodeRedirectURL(url);
402   }
403 }
404
Popular Tags