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