-
Notifications
You must be signed in to change notification settings - Fork 142
/
GenericResponseWrapper.java
303 lines (262 loc) · 8.7 KB
/
GenericResponseWrapper.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package org.tuckey.web.filters.urlrewrite.gzip;
/**
* Modified version of:
* http://svn.terracotta.org/svn/ehcache/trunk/web/web/src/main/java/net/sf/ehcache/constructs/web/GenericResponseWrapper.java
* <p>
*
* Copyright 2003-2009 Terracotta, Inc.
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.
*/
import jakarta.servlet.http.HttpServletResponseWrapper;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import org.tuckey.web.filters.urlrewrite.utils.Log;
/**
* Provides a wrapper for {@link jakarta.servlet.http.HttpServletResponseWrapper}.
* <p/>
* It is used to wrap the real Response so that we can modify it after
* that the target of the request has delivered its response.
* <p/>
* It uses the Wrapper pattern.
*
* @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a>
* @version $Id: GenericResponseWrapper.java 793 2008-10-07 07:28:03Z gregluck $
*/
public class GenericResponseWrapper extends HttpServletResponseWrapper implements Serializable {
private static final Log LOG = Log.getLog(GenericResponseWrapper.class);
private int statusCode = SC_OK;
private String contentType;
private final Map<String, List<Serializable>> headersMap = new TreeMap<String, List<Serializable>>(String.CASE_INSENSITIVE_ORDER);
private final List cookies = new ArrayList();
private final ServletOutputStream outstr;
private PrintWriter writer;
private boolean disableFlushBuffer = true;
/**
* Creates a GenericResponseWrapper
*/
public GenericResponseWrapper(final HttpServletResponse response, final OutputStream outstr) {
super(response);
this.outstr = new FilterServletOutputStream(outstr);
}
/**
* Gets the OutputStream.
*/
public ServletOutputStream getOutputStream() {
return outstr;
}
/**
* Sets the status code for this response.
*/
public void setStatus(final int code) {
statusCode = code;
super.setStatus(code);
}
/**
* Send the error. If the response is not ok, most of the logic is bypassed and the error is sent raw
* Also, the content is not cached.
*
* @param i the status code
* @param string the error message
* @throws IOException
*/
public void sendError(int i, String string) throws IOException {
statusCode = i;
super.sendError(i, string);
}
/**
* Send the error. If the response is not ok, most of the logic is bypassed and the error is sent raw
* Also, the content is not cached.
*
* @param i the status code
* @throws IOException
*/
public void sendError(int i) throws IOException {
statusCode = i;
super.sendError(i);
}
/**
* Send the redirect. If the response is not ok, most of the logic is bypassed and the error is sent raw.
* Also, the content is not cached.
*
* @param string the URL to redirect to
* @throws IOException
*/
public void sendRedirect(String string) throws IOException {
statusCode = HttpServletResponse.SC_MOVED_TEMPORARILY;
super.sendRedirect(string);
}
/**
* Sets the status code for this response.
*/
public void setStatus(final int code, final String msg) {
statusCode = code;
LOG.warn("Discarding message because this method is deprecated.");
super.setStatus(code);
}
/**
* Returns the status code for this response.
*/
public int getStatus() {
return statusCode;
}
/**
* Sets the content type.
*/
public void setContentType(final String type) {
this.contentType = type;
super.setContentType(type);
}
/**
* Gets the content type.
*/
public String getContentType() {
return contentType;
}
/**
* Gets the print writer.
*/
public PrintWriter getWriter() throws IOException {
if (writer == null) {
writer = new PrintWriter(new OutputStreamWriter(outstr, getCharacterEncoding()), true);
}
return writer;
}
/**
* @see jakarta.servlet.http.HttpServletResponseWrapper#addHeader(java.lang.String, java.lang.String)
*/
@Override
public void addHeader(String name, String value) {
List<Serializable> values = this.headersMap.get(name);
if (values == null) {
values = new LinkedList<Serializable>();
this.headersMap.put(name, values);
}
values.add(value);
super.addHeader(name, value);
}
/**
* @see jakarta.servlet.http.HttpServletResponseWrapper#setHeader(java.lang.String, java.lang.String)
*/
@Override
public void setHeader(String name, String value) {
final LinkedList<Serializable> values = new LinkedList<Serializable>();
values.add(value);
this.headersMap.put(name, values);
super.setHeader(name, value);
}
/**
* @see jakarta.servlet.http.HttpServletResponseWrapper#addDateHeader(java.lang.String, long)
*/
@Override
public void addDateHeader(String name, long date) {
List<Serializable> values = this.headersMap.get(name);
if (values == null) {
values = new LinkedList<Serializable>();
this.headersMap.put(name, values);
}
values.add(date);
super.addDateHeader(name, date);
}
/**
* @see jakarta.servlet.http.HttpServletResponseWrapper#setDateHeader(java.lang.String, long)
*/
@Override
public void setDateHeader(String name, long date) {
final LinkedList<Serializable> values = new LinkedList<Serializable>();
values.add(date);
this.headersMap.put(name, values);
super.setDateHeader(name, date);
}
/**
* @see jakarta.servlet.http.HttpServletResponseWrapper#addIntHeader(java.lang.String, int)
*/
@Override
public void addIntHeader(String name, int value) {
List<Serializable> values = this.headersMap.get(name);
if (values == null) {
values = new LinkedList<Serializable>();
this.headersMap.put(name, values);
}
values.add(value);
super.addIntHeader(name, value);
}
/**
* @see jakarta.servlet.http.HttpServletResponseWrapper#setIntHeader(java.lang.String, int)
*/
@Override
public void setIntHeader(String name, int value) {
final LinkedList<Serializable> values = new LinkedList<Serializable>();
values.add(value);
this.headersMap.put(name, values);
super.setIntHeader(name, value);
}
/**
* Adds a cookie.
*/
public void addCookie(final Cookie cookie) {
cookies.add(cookie);
super.addCookie(cookie);
}
/**
* Flushes buffer and commits response to client.
*/
public void flushBuffer() throws IOException {
flush();
// doing this might leads to response already committed exception
// when the PageInfo has not yet built but the buffer already flushed
// Happens in Weblogic when a servlet forward to a JSP page and the forward
// method trigger a flush before it forwarded to the JSP
// disableFlushBuffer for that purpose is 'true' by default
// EHC-447
if (!disableFlushBuffer) {
super.flushBuffer();
}
}
/**
* Resets the response.
*/
public void reset() {
super.reset();
cookies.clear();
headersMap.clear();
statusCode = SC_OK;
contentType = null;
}
/**
* Flushes all the streams for this response.
*/
public void flush() throws IOException {
if (writer != null) {
writer.flush();
}
outstr.flush();
}
/**
* Set if the wrapped reponse's buffer flushing should be disabled.
*/
public void setDisableFlushBuffer() {
this.disableFlushBuffer = true;
}
}