001: /*
002: * Copyright (c) 1998-2003 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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: *
028: * $Id: ServletResponseWrapper.java,v 1.2 2004/09/29 00:12:46 cvs Exp $
029: */
030:
031: package javax.servlet;
032:
033: import java.io.IOException;
034: import java.io.PrintWriter;
035: import java.util.Locale;
036:
037: /**
038: * Wraps a servlet response in another response. Filters may
039: * use ServletResponseWrapper to grab results from the servlet.
040: *
041: * <p/>The default methods just call the wrapped response methods.
042: *
043: * @since servlet 2.3
044: */
045: public class ServletResponseWrapper implements ServletResponse {
046: // the wrapped response
047: private ServletResponse _response;
048:
049: /**
050: * Create a new ServletResponseWrapper, wrapping a specified response.
051: *
052: * @param response the response to wrap.
053: */
054: public ServletResponseWrapper(ServletResponse response) {
055: if (response == null)
056: throw new IllegalArgumentException();
057:
058: _response = response;
059: }
060:
061: /**
062: * Sets the response to be wrapped.
063: *
064: * @param response the response to wrap.
065: */
066: public void setResponse(ServletResponse response) {
067: if (response == null)
068: throw new IllegalArgumentException();
069:
070: _response = response;
071: }
072:
073: /**
074: * Gets the wrapped response
075: *
076: * @return the wrapped response
077: */
078: public ServletResponse getResponse() {
079: return _response;
080: }
081:
082: /**
083: * Sets the response content type. The content type includes
084: * the character encoding. The content type must be set before
085: * calling <code>getWriter()</code> so the writer can use the
086: * proper character encoding.
087: *
088: * <p>To set the output character encoding to ISO-8859-2, use the
089: * following:
090: *
091: * <code><pre>
092: * response.setContentType("text/html; charset=ISO-8859-2");
093: * </pre></code>
094: *
095: * @param type the mime type of the output
096: */
097: public void setContentType(String type) {
098: _response.setContentType(type);
099: }
100:
101: /**
102: * Returns the content type
103: *
104: * @since 2.4
105: */
106: public String getContentType() {
107: return _response.getContentType();
108: }
109:
110: /**
111: * Returns the character encoding the response is using for output.
112: */
113: public String getCharacterEncoding() {
114: return _response.getCharacterEncoding();
115: }
116:
117: /**
118: * Sets the character encoding the response is using for output.
119: *
120: * @since 2.4
121: */
122: public void setCharacterEncoding(String encoding) {
123: _response.setCharacterEncoding(encoding);
124: }
125:
126: /**
127: * Sets the output locale. The response will set the character encoding
128: * based on the locale. For example, setting the "kr" locale will set
129: * the character encoding to "EUC_KR".
130: */
131: public void setLocale(Locale locale) {
132: _response.setLocale(locale);
133: }
134:
135: /**
136: * Returns the output locale.
137: */
138: public Locale getLocale() {
139: return _response.getLocale();
140: }
141:
142: /**
143: * Returns an output stream for writing to the client. You can use
144: * the output stream to write binary data.
145: */
146: public ServletOutputStream getOutputStream() throws IOException {
147: return _response.getOutputStream();
148: }
149:
150: /**
151: * Returns a PrintWriter with the proper character encoding for writing
152: * text data to the client.
153: */
154: public PrintWriter getWriter() throws IOException {
155: return _response.getWriter();
156: }
157:
158: /**
159: * Sets the output buffer size to <code>size</code>. The servlet engine
160: * may round the size up.
161: *
162: * @param size the new output buffer size.
163: */
164: public void setBufferSize(int size) {
165: _response.setBufferSize(size);
166: }
167:
168: /**
169: * Returns the size of the output buffer.
170: */
171: public int getBufferSize() {
172: return _response.getBufferSize();
173: }
174:
175: /**
176: * Flushes the buffer to the client.
177: */
178: public void flushBuffer() throws IOException {
179: _response.flushBuffer();
180: }
181:
182: /**
183: * Returns true if some data has actually been send to the client. The
184: * data will be sent if the buffer overflows or if it's explicitly flushed.
185: */
186: public boolean isCommitted() {
187: return _response.isCommitted();
188: }
189:
190: /**
191: * Resets the output stream, clearing headers and the output buffer.
192: * Calling <code>reset()</code> after data has been committed is illegal.
193: *
194: * @throws IllegalStateException if <code>isCommitted()</code> is true.
195: */
196: public void reset() {
197: _response.reset();
198: }
199:
200: /**
201: * Resets the output stream without clearing headers and the output buffer.
202: * Calling <code>resetBuffer()</code> after data has been committed is
203: * illegal.
204: *
205: * @throws IllegalStateException if <code>isCommitted()</code> is true.
206: */
207: public void resetBuffer() {
208: _response.resetBuffer();
209: }
210:
211: /**
212: * Resin automatically handles output content length and chunking. This
213: * method is ignored.
214: *
215: * @deprecated
216: */
217: public void setContentLength(int len) {
218: _response.setContentLength(len);
219: }
220: }
|