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.http;
20
21 import java.io.IOException;
22 import java.util.Collection;
23 import java.util.Map;
24 import java.util.function.Supplier;
25 import javax.servlet.ServletResponseWrapper;
26
27 /**
28 *
29 * Provides a convenient implementation of the HttpServletResponse interface that can be subclassed by developers
30 * wishing to adapt the response from a Servlet. This class implements the Wrapper or Decorator pattern. Methods default
31 * to calling through to the wrapped response object.
32 *
33 * @author Various
34 * @since Servlet 2.3
35 *
36 * @see javax.servlet.http.HttpServletResponse
37 */
38 public class HttpServletResponseWrapper extends ServletResponseWrapper implements HttpServletResponse {
39
40 /**
41 * Constructs a response adaptor wrapping the given response.
42 *
43 * @param response the {@link HttpServletResponse} to be wrapped.
44 *
45 * @throws java.lang.IllegalArgumentException if the response is null
46 */
47 public HttpServletResponseWrapper(HttpServletResponse response) {
48 super(response);
49 }
50
51 private HttpServletResponse _getHttpServletResponse() {
52 return (HttpServletResponse) super.getResponse();
53 }
54
55 /**
56 * The default behavior of this method is to call addCookie(Cookie cookie) on the wrapped response object.
57 */
58 @Override
59 public void addCookie(Cookie cookie) {
60 this._getHttpServletResponse().addCookie(cookie);
61 }
62
63 /**
64 * The default behavior of this method is to call containsHeader(String name) on the wrapped response object.
65 */
66 @Override
67 public boolean containsHeader(String name) {
68 return this._getHttpServletResponse().containsHeader(name);
69 }
70
71 /**
72 * The default behavior of this method is to call encodeURL(String url) on the wrapped response object.
73 */
74 @Override
75 public String encodeURL(String url) {
76 return this._getHttpServletResponse().encodeURL(url);
77 }
78
79 /**
80 * The default behavior of this method is to return encodeRedirectURL(String url) on the wrapped response object.
81 */
82 @Override
83 public String encodeRedirectURL(String url) {
84 return this._getHttpServletResponse().encodeRedirectURL(url);
85 }
86
87 /**
88 * The default behavior of this method is to call encodeUrl(String url) on the wrapped response object.
89 *
90 * @deprecated As of version 2.1, use {@link #encodeURL(String url)} instead
91 */
92 @Deprecated
93 @Override
94 public String encodeUrl(String url) {
95 return this._getHttpServletResponse().encodeUrl(url);
96 }
97
98 /**
99 * The default behavior of this method is to return encodeRedirectUrl(String url) on the wrapped response object.
100 *
101 * @deprecated As of version 2.1, use {@link #encodeRedirectURL(String url)} instead
102 */
103 @Deprecated
104 @Override
105 public String encodeRedirectUrl(String url) {
106 return this._getHttpServletResponse().encodeRedirectUrl(url);
107 }
108
109 /**
110 * The default behavior of this method is to call sendError(int sc, String msg) on the wrapped response object.
111 */
112 @Override
113 public void sendError(int sc, String msg) throws IOException {
114 this._getHttpServletResponse().sendError(sc, msg);
115 }
116
117 /**
118 * The default behavior of this method is to call sendError(int sc) on the wrapped response object.
119 */
120 @Override
121 public void sendError(int sc) throws IOException {
122 this._getHttpServletResponse().sendError(sc);
123 }
124
125 /**
126 * The default behavior of this method is to return sendRedirect(String location) on the wrapped response object.
127 */
128 @Override
129 public void sendRedirect(String location) throws IOException {
130 this._getHttpServletResponse().sendRedirect(location);
131 }
132
133 /**
134 * The default behavior of this method is to call setDateHeader(String name, long date) on the wrapped response
135 * object.
136 */
137 @Override
138 public void setDateHeader(String name, long date) {
139 this._getHttpServletResponse().setDateHeader(name, date);
140 }
141
142 /**
143 * The default behavior of this method is to call addDateHeader(String name, long date) on the wrapped response
144 * object.
145 */
146 @Override
147 public void addDateHeader(String name, long date) {
148 this._getHttpServletResponse().addDateHeader(name, date);
149 }
150
151 /**
152 * The default behavior of this method is to return setHeader(String name, String value) on the wrapped response
153 * object.
154 */
155 @Override
156 public void setHeader(String name, String value) {
157 this._getHttpServletResponse().setHeader(name, value);
158 }
159
160 /**
161 * The default behavior of this method is to return addHeader(String name, String value) on the wrapped response
162 * object.
163 */
164 @Override
165 public void addHeader(String name, String value) {
166 this._getHttpServletResponse().addHeader(name, value);
167 }
168
169 /**
170 * The default behavior of this method is to call setIntHeader(String name, int value) on the wrapped response
171 * object.
172 */
173 @Override
174 public void setIntHeader(String name, int value) {
175 this._getHttpServletResponse().setIntHeader(name, value);
176 }
177
178 /**
179 * The default behavior of this method is to call addIntHeader(String name, int value) on the wrapped response
180 * object.
181 */
182 @Override
183 public void addIntHeader(String name, int value) {
184 this._getHttpServletResponse().addIntHeader(name, value);
185 }
186
187 /**
188 * The default behavior of this method is to call setStatus(int sc) on the wrapped response object.
189 */
190 @Override
191 public void setStatus(int sc) {
192 this._getHttpServletResponse().setStatus(sc);
193 }
194
195 /**
196 * The default behavior of this method is to call setStatus(int sc, String sm) on the wrapped response object.
197 *
198 * @deprecated As of version 2.1, due to ambiguous meaning of the message parameter. To set a status code use
199 * {@link #setStatus(int)}, to send an error with a description use {@link #sendError(int, String)}
200 */
201 @Deprecated
202 @Override
203 public void setStatus(int sc, String sm) {
204 this._getHttpServletResponse().setStatus(sc, sm);
205 }
206
207 /**
208 * The default behaviour of this method is to call {@link HttpServletResponse#getStatus} on the wrapped response
209 * object.
210 *
211 * @return the current status code of the wrapped response
212 */
213 @Override
214 public int getStatus() {
215 return _getHttpServletResponse().getStatus();
216 }
217
218 /**
219 * The default behaviour of this method is to call {@link HttpServletResponse#getHeader} on the wrapped response
220 * object.
221 *
222 * @param name the name of the response header whose value to return
223 *
224 * @return the value of the response header with the given name, or <tt>null</tt> if no header with the given name
225 * has been set on the wrapped response
226 *
227 * @since Servlet 3.0
228 */
229 @Override
230 public String getHeader(String name) {
231 return _getHttpServletResponse().getHeader(name);
232 }
233
234 /**
235 * The default behaviour of this method is to call {@link HttpServletResponse#getHeaders} on the wrapped response
236 * object.
237 *
238 * <p>
239 * Any changes to the returned <code>Collection</code> must not affect this <code>HttpServletResponseWrapper</code>.
240 *
241 * @param name the name of the response header whose values to return
242 *
243 * @return a (possibly empty) <code>Collection</code> of the values of the response header with the given name
244 *
245 * @since Servlet 3.0
246 */
247 @Override
248 public Collection<String> getHeaders(String name) {
249 return _getHttpServletResponse().getHeaders(name);
250 }
251
252 /**
253 * The default behaviour of this method is to call {@link HttpServletResponse#getHeaderNames} on the wrapped
254 * response object.
255 *
256 * <p>
257 * Any changes to the returned <code>Collection</code> must not affect this <code>HttpServletResponseWrapper</code>.
258 *
259 * @return a (possibly empty) <code>Collection</code> of the names of the response headers
260 *
261 * @since Servlet 3.0
262 */
263 @Override
264 public Collection<String> getHeaderNames() {
265 return _getHttpServletResponse().getHeaderNames();
266 }
267
268 /**
269 * The default behaviour of this method is to call {@link HttpServletResponse#setTrailerFields} on the wrapped
270 * response object.
271 *
272 * @param supplier of trailer headers
273 *
274 * @since Servlet 4.0
275 */
276 @Override
277 public void setTrailerFields(Supplier<Map<String, String>> supplier) {
278 _getHttpServletResponse().setTrailerFields(supplier);
279 }
280
281 /**
282 * The default behaviour of this method is to call {@link HttpServletResponse#getTrailerFields} on the wrapped
283 * response object.
284 *
285 * @return supplier of trailer headers
286 *
287 * @since Servlet 4.0
288 */
289 @Override
290 public Supplier<Map<String, String>> getTrailerFields() {
291 return _getHttpServletResponse().getTrailerFields();
292 }
293 }
294