1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.ehcache.constructs.web;
18
19 import net.sf.ehcache.constructs.web.filter.FilterServletOutputStream;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 import javax.servlet.ServletOutputStream;
24 import javax.servlet.http.Cookie;
25 import javax.servlet.http.HttpServletResponse;
26 import javax.servlet.http.HttpServletResponseWrapper;
27 import java.io.IOException;
28 import java.io.OutputStream;
29 import java.io.OutputStreamWriter;
30 import java.io.PrintWriter;
31 import java.io.Serializable;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.List;
35
36
37
38
39
40
41
42
43
44
45
46
47 public class GenericResponseWrapper extends HttpServletResponseWrapper implements Serializable {
48
49 private static final long serialVersionUID = -5976708169031065498L;
50
51 private static final Log LOG = LogFactory.getLog(GenericResponseWrapper.class);
52 private int statusCode = SC_OK;
53 private int contentLength;
54 private String contentType;
55 private final List headers = new ArrayList();
56 private final List cookies = new ArrayList();
57 private ServletOutputStream outstr;
58 private PrintWriter writer;
59
60
61
62
63 public GenericResponseWrapper(final HttpServletResponse response, final OutputStream outstr) {
64 super(response);
65 this.outstr = new FilterServletOutputStream(outstr);
66 }
67
68
69
70
71 public ServletOutputStream getOutputStream() {
72 return outstr;
73 }
74
75
76
77
78 public void setStatus(final int code) {
79 statusCode = code;
80 super.setStatus(code);
81 }
82
83
84
85
86
87
88
89
90
91 public void sendError(int i, String string) throws IOException {
92 statusCode = i;
93 super.sendError(i, string);
94 }
95
96
97
98
99
100
101
102
103 public void sendError(int i) throws IOException {
104 statusCode = i;
105 super.sendError(i);
106 }
107
108
109
110
111
112
113
114
115 public void sendRedirect(String string) throws IOException {
116 statusCode = HttpServletResponse.SC_MOVED_TEMPORARILY;
117 super.sendRedirect(string);
118 }
119
120
121
122
123 public void setStatus(final int code, final String msg) {
124 statusCode = code;
125 LOG.warn("Discarding message because this method is deprecated.");
126 super.setStatus(code);
127 }
128
129
130
131
132 public int getStatus() {
133 return statusCode;
134 }
135
136
137
138
139 public void setContentLength(final int length) {
140 this.contentLength = length;
141 super.setContentLength(length);
142 }
143
144
145
146
147 public int getContentLength() {
148 return contentLength;
149 }
150
151
152
153
154 public void setContentType(final String type) {
155 this.contentType = type;
156 super.setContentType(type);
157 }
158
159
160
161
162 public String getContentType() {
163 return contentType;
164 }
165
166
167
168
169
170 public PrintWriter getWriter() throws IOException {
171 if (writer == null) {
172 writer = new PrintWriter(new OutputStreamWriter(outstr, getCharacterEncoding()), true);
173 }
174 return writer;
175 }
176
177
178
179
180 public void addHeader(final String name, final String value) {
181 final String[] header = new String[]{name, value};
182 headers.add(header);
183 super.addHeader(name, value);
184 }
185
186
187
188
189 public void setHeader(final String name, final String value) {
190 addHeader(name, value);
191 }
192
193
194
195
196 public Collection getHeaders() {
197 return headers;
198 }
199
200
201
202
203 public void addCookie(final Cookie cookie) {
204 cookies.add(cookie);
205 super.addCookie(cookie);
206 }
207
208
209
210
211 public Collection getCookies() {
212 return cookies;
213 }
214
215
216
217
218 public void flushBuffer() throws IOException {
219 flush();
220 super.flushBuffer();
221 }
222
223
224
225
226 public void reset() {
227 super.reset();
228 cookies.clear();
229 headers.clear();
230 statusCode = SC_OK;
231 contentType = null;
232 contentLength = 0;
233 }
234
235
236
237
238 public void resetBuffer() {
239 super.resetBuffer();
240 }
241
242
243
244
245 public void flush() throws IOException {
246 if (writer != null) {
247 writer.flush();
248 }
249 outstr.flush();
250 }
251
252
253
254
255 public String encodeRedirectUrl(String s) {
256 return super.encodeRedirectURL(s);
257 }
258
259
260
261
262 public String encodeUrl(String s) {
263 return super.encodeURL(s);
264 }
265
266
267
268 }
269