1 package servletunit;
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 import junit.framework.AssertionFailedError;
42
43 import javax.servlet.ServletOutputStream;
44 import javax.servlet.http.Cookie;
45 import javax.servlet.http.HttpServletResponse;
46 import java.io.*;
47 import java.util.HashMap;
48 import java.util.Locale;
49 import java.util.Date;
50 import java.text.SimpleDateFormat;
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public class HttpServletResponseSimulator implements HttpServletResponse
70 {
71 private OutputStream servOStream;
72
73 private boolean calledGetWriter, calledGetOutputStream;
74 private StringWriter stringWriter=null;
75 private PrintWriter printWriter=null;
76 private Locale locale = null;
77 private int contentLength;
78 private String contentType = null;
79 private int status = 200;
80 private String message = null;
81 private HashMap headers = new HashMap();
82 private HashMap cookies = new HashMap();
83
84 String charEncoding;
85
86 private boolean isCommitted = false;
87
88 public static final int SC_CONTINUE = 100;
89
90
91 public static final int SC_SWITCHING_PROTOCOLS = 101;
92
93 public static final int SC_OK = 200;
94
95 public static final int SC_CREATED = 201;
96
97 public static final int SC_ACCEPTED = 202;
98
99 public static final int SC_NON_AUTHORITATIVE_INFORMATION = 203;
100
101 public static final int SC_NO_CONTENT = 204;
102
103 public static final int SC_RESET_CONTENT = 205;
104
105 public static final int SC_PARTIAL_CONTENT = 206;
106
107 public static final int SC_MULTIPLE_CHOICES = 300;
108
109 public static final int SC_MOVED_PERMANENTLY = 301;
110
111 public static final int SC_MOVED_TEMPORARILY = 302;
112
113 public static final int SC_SEE_OTHER = 303;
114
115 public static final int SC_NOT_MODIFIED = 304;
116
117 public static final int SC_USE_PROXY = 305;
118
119 public static final int SC_BAD_REQUEST = 400;
120
121 public static final int SC_UNAUTHORIZED = 401;
122
123 public static final int SC_PAYMENT_REQUIRED = 402;
124
125 public static final int SC_FORBIDDEN = 403;
126
127 public static final int SC_NOT_FOUND = 404;
128
129 public static final int SC_METHOD_NOT_ALLOWED = 405;
130
131 public static final int SC_NOT_ACCEPTABLE = 406;
132
133 public static final int SC_PROXY_AUTHENTICATION_REQUIRED = 407;
134
135 public static final int SC_REQUEST_TIMEOUT = 408;
136
137 public static final int SC_CONFLICT = 409;
138
139 public static final int SC_GONE = 410;
140
141 public static final int SC_LENGTH_REQUIRED = 411;
142
143 public static final int SC_PRECONDITION_FAILED = 412;
144
145 public static final int SC_REQUEST_ENTITY_TOO_LARGE = 413;
146
147 public static final int SC_REQUEST_URI_TOO_LONG = 414;
148
149 public static final int SC_UNSUPPORTED_MEDIA_TYPE = 415;
150
151 public static final int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
152
153 public static final int SC_EXPECTATION_FAILED = 417;
154
155 public static final int SC_INTERNAL_SERVER_ERROR = 500;
156
157 public static final int SC_NOT_IMPLEMENTED = 501;
158
159 public static final int SC_BAD_GATEWAY = 502;
160
161 public static final int SC_SERVICE_UNAVAILABLE = 503;
162
163 public static final int SC_GATEWAY_TIMEOUT = 504;
164
165 public static final int SC_HTTP_VERSION_NOT_SUPPORTED = 505;
166
167
168
169
170 public void addCookie(Cookie cookie)
171 {
172 cookies.put( cookie.getName(), cookie );
173 }
174
175
176
177
178
179 public Cookie findCookie( String name ) {
180 return (Cookie) cookies.get( name );
181 }
182
183
184
185
186 public void addDateHeader(String name, long date)
187 {
188 this.headers.put(name,new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z").format(new Date(date)));
189 }
190
191
192
193
194 public void addHeader(String name, String value)
195 {
196 this.setHeader(name,value);
197 }
198
199
200
201
202
203 public String getHeader(String name) {
204 if (headers.containsKey(name))
205 return (String) headers.get(name);
206 else
207 return null;
208 }
209
210
211
212
213 public void addIntHeader(String name, int value)
214 {
215 this.setIntHeader(name,value);
216 }
217
218
219
220
221
222 public boolean containsHeader(String name)
223 {
224 return headers.containsKey(name);
225 }
226
227
228
229
230 public String encodeRedirectUrl(String url)
231 {
232 return url;
233 }
234
235
236
237
238
239 public String encodeRedirectURL(String url)
240 {
241 return url;
242 }
243
244
245
246
247 public String encodeUrl(String url)
248 {
249 return url;
250 }
251
252
253
254
255 public String encodeURL(String url)
256 {
257 return url;
258 }
259
260
261
262
263 public void flushBuffer() throws IOException
264 {
265 throw new UnsupportedOperationException("flushBuffer operation is not supported!");
266 }
267
268
269
270
271
272 public int getBufferSize()
273 {
274 throw new UnsupportedOperationException("getBufferSize operation is not supported!");
275 }
276
277
278
279
280 public String getCharacterEncoding()
281 {
282 return charEncoding;
283 }
284
285
286
287
288
289
290
291
292 public Locale getLocale()
293 {
294 if (locale == null)
295 return Locale.US;
296 else
297 return locale;
298 }
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322 public ServletOutputStream getOutputStream() throws IOException
323 {
324 if( this.calledGetWriter )
325 throw new IllegalStateException( "The getWriter method has already been called" );
326
327 ServletOutputStream oStream = null;
328 if( null == this.servOStream )
329 oStream = new ServletOutputStreamSimulator();
330 else
331 oStream = new ServletOutputStreamSimulator( this.servOStream );
332
333 this.servOStream = null;
334 this.calledGetOutputStream = true;
335 return oStream;
336 }
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373 public PrintWriter getWriter() throws IOException
374 {
375 if( this.calledGetOutputStream )
376 throw new IllegalStateException( "The getOutputStream method has already been called" );
377
378 if( stringWriter == null )
379 stringWriter = new StringWriter();
380 if( printWriter == null )
381 printWriter = new PrintWriter( stringWriter );
382
383 this.calledGetWriter = true;
384 return printWriter;
385 }
386
387
388
389
390
391
392
393
394 public StringBuffer getWriterBuffer()
395 {
396 if (stringWriter==null) return null;
397 return stringWriter.getBuffer();
398 }
399
400
401 public boolean isCommitted()
402 {
403 return isCommitted;
404 }
405
406 public void setIsCommitted(boolean isCommitted) {
407 this.isCommitted = isCommitted;
408 }
409
410
411
412
413
414
415
416
417 public void reset()
418 {
419 this.calledGetOutputStream = false;
420 this.calledGetWriter = false;
421 this.contentLength = 0;
422 this.contentType = null;
423 this.stringWriter = null;
424 this.printWriter = null;
425 headers = new HashMap();
426 }
427
428
429
430
431 public void resetBuffer()
432 {
433 throw new UnsupportedOperationException("resetBuffer operation is not supported.");
434 }
435
436
437
438
439
440
441
442
443
444 public void sendError(int sc) throws IOException
445 {
446 setStatus(sc);
447 throw new AssertionFailedError("received error: " + sc);
448 }
449
450
451
452
453
454
455
456
457
458
459
460 public void sendError(int sc, String msg) throws IOException
461 {
462 setStatus(sc,msg);
463 throw new AssertionFailedError("received error " + sc + " : " + msg);
464 }
465
466
467
468
469 public void sendRedirect(String location) throws IOException
470 {
471 reset();
472 setStatus(SC_MOVED_TEMPORARILY);
473 setHeader("Location", location);
474 }
475
476
477
478
479 public void setBufferSize(int size)
480 {
481 throw new UnsupportedOperationException("setBufferSize operation not supported.");
482 }
483
484
485
486
487
488
489
490
491
492
493
494
495 public void setContentLength(int len)
496 {
497 this.contentLength = len;
498 }
499
500
501
502
503
504 public int getContentLength(){
505 return this.contentLength;
506 }
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524 public void setContentType(String type)
525 {
526 this.contentType = type;
527 }
528
529
530
531
532
533 public String getContentType(){
534 return this.contentType;
535 }
536
537
538
539
540
541 public void setDateHeader(String name, long date)
542 {
543 this.addDateHeader(name,date);
544 }
545
546
547
548
549 public void setHeader(String name, String value)
550 {
551 if (name.equalsIgnoreCase("content-type")) {
552 setContentType(value);
553 return;
554 }
555 else if (name.equalsIgnoreCase("content-length")) {
556 this.setContentLength(Integer.parseInt(value));
557 return;
558 }
559
560 headers.put(name, value);
561 }
562
563
564
565
566 public void removeHeader(String name) {
567 if (headers.containsKey(name))
568 headers.remove(name);
569 }
570
571
572
573
574 public void setIntHeader(String name, int value)
575 {
576 setHeader(name, String.valueOf(value));
577 }
578
579
580
581
582
583
584
585
586
587
588
589
590
591 public void setLocale(Locale loc)
592 {
593 this.locale = loc;
594 }
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609 public void setOutputStream( OutputStream out )
610 {
611 this.servOStream = out;
612 }
613
614
615
616
617 public void setStatus(int sc)
618 {
619 setStatus(sc, null);
620 }
621
622
623
624
625 public void setStatus(int sc, String sm)
626 {
627 this.status = sc;
628 this.message = sm;
629 }
630
631
632
633
634
635 public int getStatusCode() {
636 return this.status;
637 }
638
639 public void setCharacterEncoding(String charEncoding) {
640 this.charEncoding = charEncoding;
641 }
642
643 public String getMessage() {
644 return message;
645 }
646
647
648 }
649