1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.jcs.jcache.extras.web;
20
21 import java.io.IOException;
22 import java.io.OutputStream;
23 import java.io.OutputStreamWriter;
24 import java.io.PrintWriter;
25 import java.io.Serializable;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.LinkedList;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.TreeMap;
32 import java.util.concurrent.CopyOnWriteArraySet;
33 import javax.servlet.ServletOutputStream;
34 import javax.servlet.http.Cookie;
35 import javax.servlet.http.HttpServletResponse;
36 import javax.servlet.http.HttpServletResponseWrapper;
37
38 public class InMemoryResponse extends HttpServletResponseWrapper implements Serializable
39 {
40 private final OutputStream buffer;
41
42 private final Collection<Cookie> cookies = new CopyOnWriteArraySet<Cookie>();
43 private final Map<String, List<Serializable>> headers = new TreeMap<String, List<Serializable>>(String.CASE_INSENSITIVE_ORDER);
44 private int status = SC_OK;
45 private String contentType = null;
46 private PrintWriter writer;
47 private int contentLength;
48
49 public InMemoryResponse(final HttpServletResponse response, final OutputStream baos)
50 {
51 super(response);
52 this.buffer = baos;
53 }
54
55 private List<Serializable> ensureHeaderExists(final String s)
56 {
57 List<Serializable> values = headers.get(s);
58 if (values == null) {
59 values = new LinkedList<Serializable>();
60 headers.put(s, values);
61 }
62 return values;
63 }
64
65 @Override
66 public void addCookie(final Cookie cookie)
67 {
68 super.addCookie(cookie);
69 cookies.add(cookie);
70 }
71
72 @Override
73 public void addDateHeader(final String s, final long l)
74 {
75 super.addDateHeader(s, l);
76 ensureHeaderExists(s).add(l);
77 }
78
79 @Override
80 public void addHeader(final String s, final String s2)
81 {
82 super.addHeader(s, s2);
83 ensureHeaderExists(s).add(s2);
84 }
85
86 @Override
87 public void addIntHeader(final String s, final int i)
88 {
89 super.addIntHeader(s, i);
90 ensureHeaderExists(s).add(i);
91 }
92
93 @Override
94 public boolean containsHeader(final String s)
95 {
96 return headers.containsKey(s);
97 }
98
99 @Override
100 public String getHeader(final String s)
101 {
102 final List<Serializable> serializables = headers.get(s);
103 if (serializables.isEmpty())
104 {
105 return null;
106 }
107 return serializables.iterator().next().toString();
108 }
109
110 @Override
111 public Collection<String> getHeaderNames()
112 {
113 return headers.keySet();
114 }
115
116 @Override
117 public Collection<String> getHeaders(final String s)
118 {
119 final List<Serializable> serializables = headers.get(s);
120 final Collection<String> strings = new ArrayList<String>(serializables.size());
121 for (final Serializable ser : serializables)
122 {
123 strings.add(ser.toString());
124 }
125 return strings;
126 }
127
128 @Override
129 public int getStatus()
130 {
131 return status;
132 }
133
134 @Override
135 public void sendError(final int i) throws IOException
136 {
137 status = i;
138 super.sendError(i);
139 }
140
141 @Override
142 public void sendError(final int i, final String s) throws IOException
143 {
144 status = i;
145 super.sendError(i, s);
146 }
147
148 @Override
149 public void sendRedirect(final String s) throws IOException
150 {
151 status = SC_MOVED_TEMPORARILY;
152 super.sendRedirect(s);
153 }
154
155 @Override
156 public void setDateHeader(final String s, final long l)
157 {
158 super.setDateHeader(s, l);
159 final List<Serializable> serializables = ensureHeaderExists(s);
160 serializables.clear();
161 serializables.add(l);
162 }
163
164 @Override
165 public void setHeader(final String s, final String s2)
166 {
167 super.setHeader(s, s2);
168 final List<Serializable> serializables = ensureHeaderExists(s);
169 serializables.clear();
170 serializables.add(s2);
171 }
172
173 @Override
174 public void setIntHeader(final String s, final int i)
175 {
176 super.setIntHeader(s, i);
177 final List<Serializable> serializables = ensureHeaderExists(s);
178 serializables.clear();
179 serializables.add(i);
180 }
181
182 @Override
183 public void setStatus(int i)
184 {
185 status = i;
186 super.setStatus(i);
187 }
188
189 @Override
190 public void setStatus(final int i, final String s)
191 {
192 status = i;
193 super.setStatus(i, s);
194 }
195
196 @Override
197 public String getContentType()
198 {
199 return contentType;
200 }
201
202 @Override
203 public ServletOutputStream getOutputStream() throws IOException
204 {
205 return new ServletOutputStream()
206 {
207 @Override
208 public void write(final int b) throws IOException
209 {
210 buffer.write(b);
211 }
212 };
213 }
214
215 @Override
216 public PrintWriter getWriter() throws IOException
217 {
218 if (writer == null) {
219 writer = new PrintWriter(new OutputStreamWriter(buffer, getCharacterEncoding()), true);
220 }
221 return writer;
222 }
223
224 @Override
225 public void reset()
226 {
227 super.reset();
228 status = SC_OK;
229 headers.clear();
230 cookies.clear();
231 contentType = null;
232 contentLength = 0;
233 }
234
235 @Override
236 public void setContentLength(final int i)
237 {
238 super.setContentLength(i);
239 contentLength = i;
240 }
241
242 @Override
243 public void setContentType(final String s)
244 {
245 contentType = s;
246 super.setContentType(s);
247 }
248
249 @Override
250 public void flushBuffer() throws IOException
251 {
252 if (writer != null)
253 {
254 writer.flush();
255 }
256 else
257 {
258 buffer.flush();
259 }
260 }
261
262 public int getContentLength()
263 {
264 return contentLength;
265 }
266
267 public Collection<Cookie> getCookies()
268 {
269 return cookies;
270 }
271
272 public Map<String, List<Serializable>> getHeaders()
273 {
274 return headers;
275 }
276 }