2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 package javax.servlet;
19 import java.io.IOException;
20 import java.io.PrintWriter;
21 import java.util.Locale;
24 * Provides a convenient implementation of the ServletResponse interface that
25 * can be subclassed by developers wishing to adapt the response from a Servlet.
26 * This class implements the Wrapper or Decorator pattern. Methods default to
27 * calling through to the wrapped response object.
30 * @see javax.servlet.ServletResponse
32 public class ServletResponseWrapper implements ServletResponse {
33 private ServletResponse response;
36 * Creates a ServletResponse adaptor wrapping the given response object.
38 * @throws java.lang.IllegalArgumentException
39 * if the response is null.
41 public ServletResponseWrapper(ServletResponse response) {
42 if (response == null) {
43 throw new IllegalArgumentException("Response cannot be null");
45 this.response = response;
49 * Return the wrapped ServletResponse object.
51 public ServletResponse getResponse() {
56 * Sets the response being wrapped.
58 * @throws java.lang.IllegalArgumentException
59 * if the response is null.
61 public void setResponse(ServletResponse response) {
62 if (response == null) {
63 throw new IllegalArgumentException("Response cannot be null");
65 this.response = response;
69 * The default behavior of this method is to call
70 * setCharacterEncoding(String charset) on the wrapped response object.
75 public void setCharacterEncoding(String charset) {
76 this.response.setCharacterEncoding(charset);
80 * The default behavior of this method is to return getCharacterEncoding()
81 * on the wrapped response object.
84 public String getCharacterEncoding() {
85 return this.response.getCharacterEncoding();
89 * The default behavior of this method is to return getOutputStream() on the
90 * wrapped response object.
93 public ServletOutputStream getOutputStream() throws IOException {
94 return this.response.getOutputStream();
98 * The default behavior of this method is to return getWriter() on the
99 * wrapped response object.
102 public PrintWriter getWriter() throws IOException {
103 return this.response.getWriter();
107 * The default behavior of this method is to call setContentLength(int len)
108 * on the wrapped response object.
111 public void setContentLength(int len) {
112 this.response.setContentLength(len);
116 * The default behavior of this method is to call setContentLength(long len)
117 * on the wrapped response object.
120 public void setContentLengthLong(long length) {
121 this.response.setContentLengthLong(length);
125 * The default behavior of this method is to call setContentType(String
126 * type) on the wrapped response object.
129 public void setContentType(String type) {
130 this.response.setContentType(type);
134 * The default behavior of this method is to return getContentType() on the
135 * wrapped response object.
140 public String getContentType() {
141 return this.response.getContentType();
145 * The default behavior of this method is to call setBufferSize(int size) on
146 * the wrapped response object.
149 public void setBufferSize(int size) {
150 this.response.setBufferSize(size);
154 * The default behavior of this method is to return getBufferSize() on the
155 * wrapped response object.
158 public int getBufferSize() {
159 return this.response.getBufferSize();
163 * The default behavior of this method is to call flushBuffer() on the
164 * wrapped response object.
167 public void flushBuffer() throws IOException {
168 this.response.flushBuffer();
172 * The default behavior of this method is to return isCommitted() on the
173 * wrapped response object.
176 public boolean isCommitted() {
177 return this.response.isCommitted();
181 * The default behavior of this method is to call reset() on the wrapped
185 public void reset() {
186 this.response.reset();
190 * The default behavior of this method is to call resetBuffer() on the
191 * wrapped response object.
194 public void resetBuffer() {
195 this.response.resetBuffer();
199 * The default behavior of this method is to call setLocale(Locale loc) on
200 * the wrapped response object.
203 public void setLocale(Locale loc) {
204 this.response.setLocale(loc);
208 * The default behavior of this method is to return getLocale() on the
209 * wrapped response object.
212 public Locale getLocale() {
213 return this.response.getLocale();
218 * @since Servlet 3.0 TODO SERVLET3 - Add comments
220 public boolean isWrapperFor(ServletResponse wrapped) {
221 if (response == wrapped) {
224 if (response instanceof ServletResponseWrapper) {
225 return ((ServletResponseWrapper) response).isWrapperFor(wrapped);
232 * @since Servlet 3.0 TODO SERVLET3 - Add comments
234 public boolean isWrapperFor(Class<?> wrappedType) {
235 if (wrappedType.isAssignableFrom(response.getClass())) {
238 if (response instanceof ServletResponseWrapper) {
239 return ((ServletResponseWrapper) response).isWrapperFor(wrappedType);