001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.server.connection;
031:
032: import javax.servlet.ServletOutputStream;
033: import javax.servlet.ServletResponse;
034: import javax.servlet.http.Cookie;
035: import javax.servlet.http.HttpServletResponse;
036: import java.io.IOException;
037: import java.io.PrintWriter;
038: import java.util.Locale;
039:
040: /**
041: * Wraps a servlet response in another response. Filters may
042: * use ServletResponseWrapper to grab results from the servlet.
043: *
044: * <p/>The default methods just call the wrapped response methods.
045: *
046: * @since servlet 2.3
047: */
048: public class ResponseWrapper implements ServletResponse {
049: // the wrapped response
050: protected HttpServletResponse _response;
051:
052: /**
053: * Create a new ServletResponseWrapper, wrapping a specified response.
054: *
055: * @param response the response to wrap.
056: */
057: public ResponseWrapper() {
058: }
059:
060: /**
061: * Create a new ServletResponseWrapper, wrapping a specified response.
062: *
063: * @param response the response to wrap.
064: */
065: public ResponseWrapper(HttpServletResponse response) {
066: _response = response;
067: }
068:
069: /**
070: * Sets the response to be wrapped.
071: *
072: * @param response the response to wrap.
073: */
074: public void setResponse(HttpServletResponse response) {
075: _response = response;
076: }
077:
078: /**
079: * Gets the wrapped response
080: *
081: * @return the wrapped response
082: */
083: public ServletResponse getResponse() {
084: return _response;
085: }
086:
087: /**
088: * Sets the response content type. The content type includes
089: * the character encoding. The content type must be set before
090: * calling <code>getWriter()</code> so the writer can use the
091: * proper character encoding.
092: *
093: * <p>To set the output character encoding to ISO-8859-2, use the
094: * following:
095: *
096: * <code><pre>
097: * response.setContentType("text/html; charset=ISO-8859-2");
098: * </pre></code>
099: *
100: * @param type the mime type of the output
101: */
102: public void setContentType(String type) {
103: _response.setContentType(type);
104: }
105:
106: /**
107: * Returns the content type
108: *
109: * @since 2.4
110: */
111: public String getContentType() {
112: return _response.getContentType();
113: }
114:
115: /**
116: * Returns the character encoding the response is using for output.
117: */
118: public String getCharacterEncoding() {
119: return _response.getCharacterEncoding();
120: }
121:
122: /**
123: * Sets the character encoding the response is using for output.
124: *
125: * @since 2.4
126: */
127: public void setCharacterEncoding(String encoding) {
128: _response.setCharacterEncoding(encoding);
129: }
130:
131: /**
132: * Sets the output locale. The response will set the character encoding
133: * based on the locale. For example, setting the "kr" locale will set
134: * the character encoding to "EUC_KR".
135: */
136: public void setLocale(Locale locale) {
137: _response.setLocale(locale);
138: }
139:
140: /**
141: * Returns the output locale.
142: */
143: public Locale getLocale() {
144: return _response.getLocale();
145: }
146:
147: /**
148: * Returns an output stream for writing to the client. You can use
149: * the output stream to write binary data.
150: */
151: public ServletOutputStream getOutputStream() throws IOException {
152: return _response.getOutputStream();
153: }
154:
155: /**
156: * Returns a PrintWriter with the proper character encoding for writing
157: * text data to the client.
158: */
159: public PrintWriter getWriter() throws IOException {
160: return _response.getWriter();
161: }
162:
163: /**
164: * Sets the output buffer size to <code>size</code>. The servlet engine
165: * may round the size up.
166: *
167: * @param size the new output buffer size.
168: */
169: public void setBufferSize(int size) {
170: _response.setBufferSize(size);
171: }
172:
173: /**
174: * Returns the size of the output buffer.
175: */
176: public int getBufferSize() {
177: return _response.getBufferSize();
178: }
179:
180: /**
181: * Flushes the buffer to the client.
182: */
183: public void flushBuffer() throws IOException {
184: _response.flushBuffer();
185: }
186:
187: /**
188: * Returns true if some data has actually been send to the client. The
189: * data will be sent if the buffer overflows or if it's explicitly flushed.
190: */
191: public boolean isCommitted() {
192: return _response.isCommitted();
193: }
194:
195: /**
196: * Resets the output stream, clearing headers and the output buffer.
197: * Calling <code>reset()</code> after data has been committed is illegal.
198: *
199: * @throws IllegalStateException if <code>isCommitted()</code> is true.
200: */
201: public void reset() {
202: _response.reset();
203: }
204:
205: /**
206: * Resets the output stream without clearing headers and the output buffer.
207: * Calling <code>resetBuffer()</code> after data has been committed is
208: * illegal.
209: *
210: * @throws IllegalStateException if <code>isCommitted()</code> is true.
211: */
212: public void resetBuffer() {
213: _response.resetBuffer();
214: }
215:
216: /**
217: * Resin automatically handles output content length and chunking. This
218: * method is ignored.
219: *
220: * @deprecated
221: */
222: public void setContentLength(int len) {
223: _response.setContentLength(len);
224: }
225:
226: /**
227: * Sets the HTTP status
228: *
229: * @param sc the HTTP status code
230: */
231: public void setStatus(int sc) {
232: _response.setStatus(sc);
233: }
234:
235: /**
236: * Sends an HTTP error page based on the status code
237: *
238: * @param sc the HTTP status code
239: */
240: public void sendError(int sc, String msg) throws IOException {
241: _response.sendError(sc, msg);
242: }
243:
244: /**
245: * Sends an HTTP error page based on the status code
246: *
247: * @param sc the HTTP status code
248: */
249: public void sendError(int sc) throws IOException {
250: _response.sendError(sc);
251: }
252:
253: /**
254: * Redirects the client to another page.
255: *
256: * @param location the location to redirect to.
257: */
258: public void sendRedirect(String location) throws IOException {
259: _response.sendRedirect(location);
260: }
261:
262: /**
263: * Sets a header. This will override a previous header
264: * with the same name.
265: *
266: * @param name the header name
267: * @param value the header value
268: */
269: public void setHeader(String name, String value) {
270: _response.setHeader(name, value);
271: }
272:
273: /**
274: * Adds a header. If another header with the same name exists, both
275: * will be sent to the client.
276: *
277: * @param name the header name
278: * @param value the header value
279: */
280: public void addHeader(String name, String value) {
281: _response.addHeader(name, value);
282: }
283:
284: /**
285: * Returns true if the output headers include <code>name</code>
286: *
287: * @param name the header name to test
288: */
289: public boolean containsHeader(String name) {
290: return _response.containsHeader(name);
291: }
292:
293: /**
294: * Sets a header by converting a date to a string.
295: *
296: * <p>To set the page to expire in 15 seconds use the following:
297: * <pre><code>
298: * long now = System.currentTime();
299: * _response.setDateHeader("Expires", now + 15000);
300: * </code></pre>
301: *
302: * @param name name of the header
303: * @param date the date in milliseconds since the epoch.
304: */
305: public void setDateHeader(String name, long date) {
306: _response.setDateHeader(name, date);
307: }
308:
309: /**
310: * Adds a header by converting a date to a string.
311: *
312: * @param name name of the header
313: * @param date the date in milliseconds since the epoch.
314: */
315: public void addDateHeader(String name, long date) {
316: _response.addDateHeader(name, date);
317: }
318:
319: /**
320: * Sets a header by converting an integer value to a string.
321: *
322: * @param name name of the header
323: * @param value the value as an integer
324: */
325: public void setIntHeader(String name, int value) {
326: _response.setIntHeader(name, value);
327: }
328:
329: /**
330: * Adds a header by converting an integer value to a string.
331: *
332: * @param name name of the header
333: * @param value the value as an integer
334: */
335: public void addIntHeader(String name, int value) {
336: _response.addIntHeader(name, value);
337: }
338:
339: /**
340: * Sends a new cookie to the client.
341: */
342: public void addCookie(Cookie cookie) {
343: _response.addCookie(cookie);
344: }
345:
346: /**
347: * Encodes session information in a URL. Calling this will enable
348: * sessions for users who have disabled cookies.
349: *
350: * @param url the url to encode
351: * @return a url with session information encoded
352: */
353: public String encodeURL(String url) {
354: return _response.encodeURL(url);
355: }
356:
357: /**
358: * Encodes session information in a URL suitable for
359: * <code>sendRedirect()</code>
360: *
361: * @param url the url to encode
362: * @return a url with session information encoded
363: */
364: public String encodeRedirectURL(String name) {
365: return _response.encodeRedirectURL(name);
366: }
367:
368: public void setStatus(int sc, String msg) {
369: _response.setStatus(sc, msg);
370: }
371:
372: /**
373: * @deprecated
374: */
375: public String encodeUrl(String url) {
376: return encodeURL(url);
377: }
378:
379: /**
380: * @deprecated
381: */
382: public String encodeRedirectUrl(String url) {
383: return encodeRedirectURL(url);
384: }
385:
386: //
387: // caucho response
388: //
389:
390: public void setFooter(String key, String value) {
391: if (_response instanceof CauchoResponse)
392: ((CauchoResponse) _response).setFooter(key, value);
393: }
394:
395: public void addFooter(String key, String value) {
396: if (_response instanceof CauchoResponse)
397: ((CauchoResponse) _response).addFooter(key, value);
398: }
399: }
|