1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.shale.test.mock; |
19 | |
|
20 | |
import java.io.ByteArrayOutputStream; |
21 | |
import java.io.CharArrayWriter; |
22 | |
import java.io.IOException; |
23 | |
import java.io.PrintWriter; |
24 | |
import java.text.SimpleDateFormat; |
25 | |
import java.util.ArrayList; |
26 | |
import java.util.Date; |
27 | |
import java.util.Iterator; |
28 | |
import java.util.List; |
29 | |
import java.util.Locale; |
30 | |
import java.util.TimeZone; |
31 | |
|
32 | |
import javax.servlet.ServletOutputStream; |
33 | |
import javax.servlet.http.Cookie; |
34 | |
import javax.servlet.http.HttpServletResponse; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public class MockHttpServletResponse implements HttpServletResponse { |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | 36 | public MockHttpServletResponse() { } |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public String getHeader(String name) { |
64 | |
String match = name + ":"; |
65 | |
Iterator headers = this.headers.iterator(); |
66 | |
while (headers.hasNext()) { |
67 | |
String header = (String) headers.next(); |
68 | |
if (header.startsWith(match)) { |
69 | |
return header.substring(match.length() + 1).trim(); |
70 | |
} |
71 | |
} |
72 | |
return null; |
73 | |
} |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public String getMessage() { |
80 | |
return this.message; |
81 | |
} |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
public int getStatus() { |
88 | |
return this.status; |
89 | |
} |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
public void setOutputStream(ServletOutputStream stream) { |
102 | |
this.stream = stream; |
103 | |
} |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
public void setWriter(PrintWriter writer) { |
116 | |
this.writer = writer; |
117 | |
} |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | 18 | private String encoding = "ISO-8859-1"; |
124 | 18 | private String contentType = "text/html"; |
125 | 18 | private List headers = new ArrayList(); |
126 | 18 | private String message = null; |
127 | 18 | private int status = HttpServletResponse.SC_OK; |
128 | 18 | private ServletOutputStream stream = null; |
129 | 18 | private PrintWriter writer = null; |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
public void addCookie(Cookie cookie) { |
137 | |
|
138 | |
throw new UnsupportedOperationException(); |
139 | |
|
140 | |
} |
141 | |
|
142 | |
|
143 | |
|
144 | |
public void addDateHeader(String name, long value) { |
145 | |
|
146 | |
headers.add(name + ": " + formatDate(value)); |
147 | |
|
148 | |
} |
149 | |
|
150 | |
|
151 | |
|
152 | |
public void addHeader(String name, String value) { |
153 | |
|
154 | |
headers.add(name + ": " + value); |
155 | |
|
156 | |
} |
157 | |
|
158 | |
|
159 | |
|
160 | |
public void addIntHeader(String name, int value) { |
161 | |
|
162 | |
headers.add(name + ": " + value); |
163 | |
|
164 | |
} |
165 | |
|
166 | |
|
167 | |
|
168 | |
public boolean containsHeader(String name) { |
169 | |
|
170 | |
return getHeader(name) != null; |
171 | |
|
172 | |
} |
173 | |
|
174 | |
|
175 | |
|
176 | |
public String encodeRedirectUrl(String url) { |
177 | |
|
178 | |
return encodeRedirectURL(url); |
179 | |
|
180 | |
} |
181 | |
|
182 | |
|
183 | |
|
184 | |
public String encodeRedirectURL(String url) { |
185 | |
|
186 | |
return url; |
187 | |
|
188 | |
} |
189 | |
|
190 | |
|
191 | |
|
192 | |
public String encodeUrl(String url) { |
193 | |
|
194 | |
return encodeURL(url); |
195 | |
|
196 | |
} |
197 | |
|
198 | |
|
199 | |
|
200 | |
public String encodeURL(String url) { |
201 | |
|
202 | |
return url; |
203 | |
|
204 | |
} |
205 | |
|
206 | |
|
207 | |
|
208 | |
public void sendError(int status) { |
209 | |
|
210 | |
this.status = status; |
211 | |
|
212 | |
} |
213 | |
|
214 | |
|
215 | |
|
216 | |
public void sendError(int status, String message) { |
217 | |
|
218 | |
this.status = status; |
219 | |
this.message = message; |
220 | |
|
221 | |
} |
222 | |
|
223 | |
|
224 | |
|
225 | |
public void sendRedirect(String location) { |
226 | |
|
227 | |
this.status = HttpServletResponse.SC_MOVED_TEMPORARILY; |
228 | |
this.message = location; |
229 | |
|
230 | |
} |
231 | |
|
232 | |
|
233 | |
|
234 | |
public void setDateHeader(String name, long value) { |
235 | |
|
236 | |
removeHeader(name); |
237 | |
addDateHeader(name, value); |
238 | |
|
239 | |
} |
240 | |
|
241 | |
|
242 | |
|
243 | |
public void setHeader(String name, String value) { |
244 | |
|
245 | |
removeHeader(name); |
246 | |
addHeader(name, value); |
247 | |
|
248 | |
} |
249 | |
|
250 | |
|
251 | |
|
252 | |
public void setIntHeader(String name, int value) { |
253 | |
|
254 | |
removeHeader(name); |
255 | |
addIntHeader(name, value); |
256 | |
|
257 | |
} |
258 | |
|
259 | |
|
260 | |
|
261 | |
public void setStatus(int status) { |
262 | |
|
263 | |
throw new UnsupportedOperationException(); |
264 | |
|
265 | |
} |
266 | |
|
267 | |
|
268 | |
|
269 | |
public void setStatus(int status, String message) { |
270 | |
|
271 | |
throw new UnsupportedOperationException(); |
272 | |
|
273 | |
} |
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
public void flushBuffer() { |
281 | |
|
282 | |
throw new UnsupportedOperationException(); |
283 | |
|
284 | |
} |
285 | |
|
286 | |
|
287 | |
|
288 | |
public int getBufferSize() { |
289 | |
|
290 | |
throw new UnsupportedOperationException(); |
291 | |
|
292 | |
} |
293 | |
|
294 | |
|
295 | |
|
296 | |
public String getCharacterEncoding() { |
297 | |
|
298 | |
return this.encoding; |
299 | |
|
300 | |
} |
301 | |
|
302 | |
|
303 | |
|
304 | |
public String getContentType() { |
305 | |
|
306 | |
return this.contentType; |
307 | |
|
308 | |
} |
309 | |
|
310 | |
|
311 | |
|
312 | |
public Locale getLocale() { |
313 | |
|
314 | |
throw new UnsupportedOperationException(); |
315 | |
|
316 | |
} |
317 | |
|
318 | |
|
319 | |
|
320 | |
public ServletOutputStream getOutputStream() throws IOException { |
321 | |
|
322 | |
if (stream == null) { |
323 | |
if (writer != null) { |
324 | |
throw new IllegalStateException("Cannot call getOutputStream() after getWriter() has been called"); |
325 | |
} |
326 | |
stream = new MockServletOutputStream(new ByteArrayOutputStream()); |
327 | |
} |
328 | |
return stream; |
329 | |
|
330 | |
} |
331 | |
|
332 | |
|
333 | |
|
334 | |
public PrintWriter getWriter() throws IOException { |
335 | |
|
336 | |
if (writer == null) { |
337 | |
if (stream != null) { |
338 | |
throw new IllegalStateException("Cannot call getWriter() after getOutputStream() was called"); |
339 | |
} |
340 | |
writer = new MockPrintWriter(new CharArrayWriter()); |
341 | |
} |
342 | |
return writer; |
343 | |
|
344 | |
} |
345 | |
|
346 | |
|
347 | |
|
348 | |
public boolean isCommitted() { |
349 | |
|
350 | |
throw new UnsupportedOperationException(); |
351 | |
|
352 | |
} |
353 | |
|
354 | |
|
355 | |
|
356 | |
public void reset() { |
357 | |
|
358 | |
throw new UnsupportedOperationException(); |
359 | |
|
360 | |
} |
361 | |
|
362 | |
|
363 | |
|
364 | |
public void resetBuffer() { |
365 | |
|
366 | |
throw new UnsupportedOperationException(); |
367 | |
|
368 | |
} |
369 | |
|
370 | |
|
371 | |
|
372 | |
public void setBufferSize(int size) { |
373 | |
|
374 | |
throw new UnsupportedOperationException(); |
375 | |
|
376 | |
} |
377 | |
|
378 | |
|
379 | |
|
380 | |
public void setCharacterEncoding(String charset) { |
381 | |
|
382 | |
this.encoding = charset; |
383 | |
|
384 | |
} |
385 | |
|
386 | |
|
387 | |
|
388 | |
public void setContentLength(int length) { |
389 | |
|
390 | |
throw new UnsupportedOperationException(); |
391 | |
|
392 | |
} |
393 | |
|
394 | |
|
395 | |
|
396 | |
public void setContentType(String type) { |
397 | |
|
398 | |
contentType = type; |
399 | |
|
400 | |
} |
401 | |
|
402 | |
|
403 | |
|
404 | |
public void setLocale(Locale locale) { |
405 | |
|
406 | |
throw new UnsupportedOperationException(); |
407 | |
|
408 | |
} |
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | |
|
415 | |
|
416 | |
|
417 | |
|
418 | 1 | private static SimpleDateFormat format = |
419 | |
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz"); |
420 | |
static { |
421 | 1 | format.setTimeZone(TimeZone.getTimeZone("GMT")); |
422 | 1 | } |
423 | |
|
424 | |
|
425 | |
|
426 | |
|
427 | |
|
428 | |
|
429 | |
|
430 | |
|
431 | |
private String formatDate(long date) { |
432 | |
return format.format(new Date(date)); |
433 | |
} |
434 | |
|
435 | |
|
436 | |
|
437 | |
|
438 | |
|
439 | |
|
440 | |
|
441 | |
private void removeHeader(String name) { |
442 | |
String match = name + ":"; |
443 | |
Iterator headers = this.headers.iterator(); |
444 | |
while (headers.hasNext()) { |
445 | |
String header = (String) headers.next(); |
446 | |
if (header.startsWith(match)) { |
447 | |
headers.remove(); |
448 | |
return; |
449 | |
} |
450 | |
} |
451 | |
} |
452 | |
|
453 | |
|
454 | |
} |