1 /*
2 * Copyright (c) 1997-2018 Oracle and/or its affiliates and others.
3 * All rights reserved.
4 * Copyright 2004 The Apache Software Foundation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package javax.servlet;
20
21 import java.io.IOException;
22 import java.io.PrintWriter;
23 import java.util.Locale;
24
25 /**
26 *
27 * Provides a convenient implementation of the ServletResponse interface that can be subclassed by developers wishing to
28 * adapt the response from a Servlet. This class implements the Wrapper or Decorator pattern. Methods default to calling
29 * through to the wrapped response object.
30 *
31 * @author Various
32 * @since Servlet 2.3
33 *
34 * @see javax.servlet.ServletResponse
35 */
36 public class ServletResponseWrapper implements ServletResponse {
37 private ServletResponse response;
38
39 /**
40 * Creates a ServletResponse adaptor wrapping the given response object.
41 *
42 * @param response the {@link ServletResponse} to be wrapped
43 *
44 * @throws java.lang.IllegalArgumentException if the response is null.
45 */
46 public ServletResponseWrapper(ServletResponse response) {
47 if (response == null) {
48 throw new IllegalArgumentException("Response cannot be null");
49 }
50 this.response = response;
51 }
52
53 /**
54 * Return the wrapped ServletResponse object.
55 *
56 * @return the wrapped {@link ServletResponse}
57 */
58 public ServletResponse getResponse() {
59 return this.response;
60 }
61
62 /**
63 * Sets the response being wrapped.
64 *
65 * @param response the {@link ServletResponse} to be installed
66 *
67 * @throws java.lang.IllegalArgumentException if the response is null.
68 */
69 public void setResponse(ServletResponse response) {
70 if (response == null) {
71 throw new IllegalArgumentException("Response cannot be null");
72 }
73 this.response = response;
74 }
75
76 /**
77 * The default behavior of this method is to call setCharacterEncoding(String charset) on the wrapped response
78 * object.
79 *
80 * @since Servlet 2.4
81 */
82 @Override
83 public void setCharacterEncoding(String charset) {
84 this.response.setCharacterEncoding(charset);
85 }
86
87 /**
88 * The default behavior of this method is to return getCharacterEncoding() on the wrapped response object.
89 */
90 @Override
91 public String getCharacterEncoding() {
92 return this.response.getCharacterEncoding();
93 }
94
95 /**
96 * The default behavior of this method is to return getOutputStream() on the wrapped response object.
97 */
98 @Override
99 public ServletOutputStream getOutputStream() throws IOException {
100 return this.response.getOutputStream();
101 }
102
103 /**
104 * The default behavior of this method is to return getWriter() on the wrapped response object.
105 */
106 @Override
107 public PrintWriter getWriter() throws IOException {
108 return this.response.getWriter();
109 }
110
111 /**
112 * The default behavior of this method is to call setContentLength(int len) on the wrapped response object.
113 */
114 @Override
115 public void setContentLength(int len) {
116 this.response.setContentLength(len);
117 }
118
119 /**
120 * The default behavior of this method is to call setContentLengthLong(long len) on the wrapped response object.
121 */
122 @Override
123 public void setContentLengthLong(long len) {
124 this.response.setContentLengthLong(len);
125 }
126
127 /**
128 * The default behavior of this method is to call setContentType(String type) on the wrapped response object.
129 */
130 @Override
131 public void setContentType(String type) {
132 this.response.setContentType(type);
133 }
134
135 /**
136 * The default behavior of this method is to return getContentType() on the wrapped response object.
137 *
138 * @since Servlet 2.4
139 */
140 @Override
141 public String getContentType() {
142 return this.response.getContentType();
143 }
144
145 /**
146 * The default behavior of this method is to call setBufferSize(int size) on the wrapped response object.
147 */
148 @Override
149 public void setBufferSize(int size) {
150 this.response.setBufferSize(size);
151 }
152
153 /**
154 * The default behavior of this method is to return getBufferSize() on the wrapped response object.
155 */
156 @Override
157 public int getBufferSize() {
158 return this.response.getBufferSize();
159 }
160
161 /**
162 * The default behavior of this method is to call flushBuffer() on the wrapped response object.
163 */
164 @Override
165 public void flushBuffer() throws IOException {
166 this.response.flushBuffer();
167 }
168
169 /**
170 * The default behavior of this method is to return isCommitted() on the wrapped response object.
171 */
172 @Override
173 public boolean isCommitted() {
174 return this.response.isCommitted();
175 }
176
177 /**
178 * The default behavior of this method is to call reset() on the wrapped response object.
179 */
180 @Override
181 public void reset() {
182 this.response.reset();
183 }
184
185 /**
186 * The default behavior of this method is to call resetBuffer() on the wrapped response object.
187 */
188 @Override
189 public void resetBuffer() {
190 this.response.resetBuffer();
191 }
192
193 /**
194 * The default behavior of this method is to call setLocale(Locale loc) on the wrapped response object.
195 */
196 @Override
197 public void setLocale(Locale loc) {
198 this.response.setLocale(loc);
199 }
200
201 /**
202 * The default behavior of this method is to return getLocale() on the wrapped response object.
203 */
204 @Override
205 public Locale getLocale() {
206 return this.response.getLocale();
207 }
208
209 /**
210 * Checks (recursively) if this ServletResponseWrapper wraps the given {@link ServletResponse} instance.
211 *
212 * @param wrapped the ServletResponse instance to search for
213 *
214 * @return true if this ServletResponseWrapper wraps the given ServletResponse instance, false otherwise
215 *
216 * @since Servlet 3.0
217 */
218 public boolean isWrapperFor(ServletResponse wrapped) {
219 if (response == wrapped) {
220 return true;
221 } else if (response instanceof ServletResponseWrapper) {
222 return ((ServletResponseWrapper) response).isWrapperFor(wrapped);
223 } else {
224 return false;
225 }
226 }
227
228 /**
229 * Checks (recursively) if this ServletResponseWrapper wraps a {@link ServletResponse} of the given class type.
230 *
231 * @param wrappedType the ServletResponse class type to search for
232 *
233 * @return true if this ServletResponseWrapper wraps a ServletResponse of the given class type, false otherwise
234 *
235 * @throws IllegalArgumentException if the given class does not implement {@link ServletResponse}
236 *
237 * @since Servlet 3.0
238 */
239 public boolean isWrapperFor(Class<?> wrappedType) {
240 if (!ServletResponse.class.isAssignableFrom(wrappedType)) {
241 throw new IllegalArgumentException("Given class " + wrappedType.getName() + " not a subinterface of "
242 + ServletResponse.class.getName());
243 }
244 if (wrappedType.isAssignableFrom(response.getClass())) {
245 return true;
246 } else if (response instanceof ServletResponseWrapper) {
247 return ((ServletResponseWrapper) response).isWrapperFor(wrappedType);
248 } else {
249 return false;
250 }
251 }
252
253 }
254