KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > util > FakeHttpServletResponse


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.directwebremoting.util;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStreamWriter JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.io.UnsupportedEncodingException JavaDoc;
24 import java.io.Writer JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.LinkedList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Locale JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Set JavaDoc;
34
35 import javax.servlet.ServletOutputStream JavaDoc;
36 import javax.servlet.http.Cookie JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38
39 /**
40  * Mock implementation of the HttpServletResponse interface.
41  * @author Rod Johnson
42  * @author Juergen Hoeller
43  * @author Joe Walker [joe at getahead dot ltd dot uk]
44  */

45 public class FakeHttpServletResponse implements HttpServletResponse JavaDoc
46 {
47     /* (non-Javadoc)
48      * @see javax.servlet.ServletResponse#setCharacterEncoding(java.lang.String)
49      */

50     public void setCharacterEncoding(String JavaDoc characterEncoding)
51     {
52         this.characterEncoding = characterEncoding;
53     }
54
55     /* (non-Javadoc)
56      * @see javax.servlet.ServletResponse#getCharacterEncoding()
57      */

58     public String JavaDoc getCharacterEncoding()
59     {
60         return characterEncoding;
61     }
62
63     /* (non-Javadoc)
64      * @see javax.servlet.ServletResponse#getOutputStream()
65      */

66     public ServletOutputStream JavaDoc getOutputStream()
67     {
68         return outputStream;
69     }
70
71     /* (non-Javadoc)
72      * @see javax.servlet.ServletResponse#getWriter()
73      */

74     public PrintWriter JavaDoc getWriter() throws UnsupportedEncodingException JavaDoc
75     {
76         if (writer == null)
77         {
78             Writer JavaDoc targetWriter = (characterEncoding != null ? new OutputStreamWriter JavaDoc(content, characterEncoding) : new OutputStreamWriter JavaDoc(content));
79             writer = new PrintWriter JavaDoc(targetWriter);
80         }
81
82         return writer;
83     }
84
85     /* (non-Javadoc)
86      * @see javax.servlet.ServletResponse#flushBuffer()
87      */

88     public void flushBuffer()
89     {
90         if (writer != null)
91         {
92             writer.flush();
93         }
94
95         if (outputStream != null)
96         {
97             try
98             {
99                 outputStream.flush();
100             }
101             catch (IOException JavaDoc ex)
102             {
103                 throw new IllegalStateException JavaDoc("Could not flush OutputStream: " + ex.getMessage());
104             }
105         }
106
107         committed = true;
108     }
109
110     /* (non-Javadoc)
111      * @see javax.servlet.http.HttpServletResponse#sendError(int, java.lang.String)
112      */

113     public void sendError(int newStatus, String JavaDoc newErrorMessage) throws IOException JavaDoc
114     {
115         if (committed)
116         {
117             throw new IllegalStateException JavaDoc("Cannot set error status - response is already committed");
118         }
119
120         status = newStatus;
121         errorMessage = newErrorMessage;
122         committed = true;
123     }
124
125     /* (non-Javadoc)
126      * @see javax.servlet.http.HttpServletResponse#sendError(int)
127      */

128     public void sendError(int newStatus) throws IOException JavaDoc
129     {
130         if (committed)
131         {
132             throw new IllegalStateException JavaDoc("Cannot set error status - response is already committed");
133         }
134
135         status = newStatus;
136         committed = true;
137     }
138
139     /**
140      * Accessor for any error messages set using {@link #sendError(int)} or
141      * {@link #sendError(int, String)}
142      * @return The current error message
143      */

144     public String JavaDoc getErrorMessage()
145     {
146         return errorMessage;
147     }
148
149     /* (non-Javadoc)
150      * @see javax.servlet.http.HttpServletResponse#sendRedirect(java.lang.String)
151      */

152     public void sendRedirect(String JavaDoc url) throws IOException JavaDoc
153     {
154         if (committed)
155         {
156             throw new IllegalStateException JavaDoc("Cannot send redirect - response is already committed");
157         }
158
159         redirectedUrl = url;
160         committed = true;
161     }
162
163     /**
164      * Accessor for the redirect URL set using {@link #sendRedirect(String)}
165      * @return The redirect URL
166      */

167     public String JavaDoc getRedirectedUrl()
168     {
169         return redirectedUrl;
170     }
171
172     /* (non-Javadoc)
173      * @see javax.servlet.http.HttpServletResponse#setStatus(int)
174      */

175     public void setStatus(int status)
176     {
177         this.status = status;
178     }
179
180     /* (non-Javadoc)
181      * @see javax.servlet.http.HttpServletResponse#setStatus(int, java.lang.String)
182      */

183     public void setStatus(int status, String JavaDoc errorMessage)
184     {
185         this.status = status;
186         this.errorMessage = errorMessage;
187     }
188
189     /**
190      * What HTTP status code should be returned?
191      * @return The current http status code
192      */

193     public int getStatus()
194     {
195         return status;
196     }
197
198     /**
199      * Accessor for the content of output body
200      * @return A byte array of the output body
201      */

202     public byte[] getContentAsByteArray()
203     {
204         flushBuffer();
205         return content.toByteArray();
206     }
207
208     /**
209      * Accessor for the content of output body
210      * @return A string of the output body
211      * @throws UnsupportedEncodingException
212      */

213     public String JavaDoc getContentAsString() throws UnsupportedEncodingException JavaDoc
214     {
215         flushBuffer();
216         return (characterEncoding != null) ? content.toString(characterEncoding) : content.toString();
217     }
218
219     /* (non-Javadoc)
220      * @see javax.servlet.ServletResponse#setContentLength(int)
221      */

222     public void setContentLength(int contentLength)
223     {
224         this.contentLength = contentLength;
225     }
226
227     /**
228      * Accessor for the content length of the output
229      * @return The content length of the output
230      */

231     public int getContentLength()
232     {
233         return contentLength;
234     }
235
236     /* (non-Javadoc)
237      * @see javax.servlet.ServletResponse#setContentType(java.lang.String)
238      */

239     public void setContentType(String JavaDoc contentType)
240     {
241         this.contentType = contentType;
242         if (contentType != null)
243         {
244             int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
245
246             if (charsetIndex != -1)
247             {
248                 String JavaDoc encoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
249                 setCharacterEncoding(encoding);
250             }
251         }
252     }
253
254     /* (non-Javadoc)
255      * @see javax.servlet.ServletResponse#getContentType()
256      */

257     public String JavaDoc getContentType()
258     {
259         return contentType;
260     }
261
262     /* (non-Javadoc)
263      * @see javax.servlet.ServletResponse#setBufferSize(int)
264      */

265     public void setBufferSize(int bufferSize)
266     {
267         this.bufferSize = bufferSize;
268     }
269
270     /* (non-Javadoc)
271      * @see javax.servlet.ServletResponse#getBufferSize()
272      */

273     public int getBufferSize()
274     {
275         return bufferSize;
276     }
277
278     /**
279      * @param committed
280      */

281     public void setCommitted(boolean committed)
282     {
283         this.committed = committed;
284     }
285
286     /* (non-Javadoc)
287      * @see javax.servlet.ServletResponse#isCommitted()
288      */

289     public boolean isCommitted()
290     {
291         return committed;
292     }
293
294     /* (non-Javadoc)
295      * @see javax.servlet.ServletResponse#resetBuffer()
296      */

297     public void resetBuffer()
298     {
299         if (committed)
300         {
301             throw new IllegalStateException JavaDoc("Cannot reset buffer - response is already committed");
302         }
303
304         content.reset();
305     }
306
307     /* (non-Javadoc)
308      * @see javax.servlet.ServletResponse#reset()
309      */

310     public void reset()
311     {
312         resetBuffer();
313
314         characterEncoding = null;
315         contentLength = 0;
316         contentType = null;
317         locale = null;
318         cookies.clear();
319         headers.clear();
320         status = HttpServletResponse.SC_OK;
321         errorMessage = null;
322     }
323
324     /* (non-Javadoc)
325      * @see javax.servlet.ServletResponse#setLocale(java.util.Locale)
326      */

327     public void setLocale(Locale JavaDoc locale)
328     {
329         this.locale = locale;
330     }
331
332     /* (non-Javadoc)
333      * @see javax.servlet.ServletResponse#getLocale()
334      */

335     public Locale JavaDoc getLocale()
336     {
337         return locale;
338     }
339
340     /* (non-Javadoc)
341      * @see javax.servlet.http.HttpServletResponse#addCookie(javax.servlet.http.Cookie)
342      */

343     public void addCookie(Cookie JavaDoc cookie)
344     {
345         cookies.add(cookie);
346     }
347
348     /**
349      * Accessor for the array of current cookies
350      * @return The current set of output cookies
351      */

352     public Cookie JavaDoc[] getCookies()
353     {
354         return (Cookie JavaDoc[]) cookies.toArray(new Cookie JavaDoc[cookies.size()]);
355     }
356
357     /**
358      * Get a cookie by a given name
359      * @param name The name of the cookie to fetch
360      * @return A matching cookie or null if there was no match
361      */

362     public Cookie JavaDoc getCookie(String JavaDoc name)
363     {
364         for (Iterator JavaDoc it = cookies.iterator(); it.hasNext();)
365         {
366             Cookie JavaDoc cookie = (Cookie JavaDoc) it.next();
367             if (name.equals(cookie.getName()))
368             {
369                 return cookie;
370             }
371         }
372         return null;
373     }
374
375     /* (non-Javadoc)
376      * @see javax.servlet.http.HttpServletResponse#encodeUrl(java.lang.String)
377      */

378     public String JavaDoc encodeUrl(String JavaDoc url)
379     {
380         return url;
381     }
382
383     /* (non-Javadoc)
384      * @see javax.servlet.http.HttpServletResponse#encodeURL(java.lang.String)
385      */

386     public String JavaDoc encodeURL(String JavaDoc url)
387     {
388         return url;
389     }
390
391     /* (non-Javadoc)
392      * @see javax.servlet.http.HttpServletResponse#encodeRedirectUrl(java.lang.String)
393      */

394     public String JavaDoc encodeRedirectUrl(String JavaDoc url)
395     {
396         return url;
397     }
398
399     /* (non-Javadoc)
400      * @see javax.servlet.http.HttpServletResponse#encodeRedirectURL(java.lang.String)
401      */

402     public String JavaDoc encodeRedirectURL(String JavaDoc url)
403     {
404         return url;
405     }
406
407     /* (non-Javadoc)
408      * @see javax.servlet.http.HttpServletResponse#addHeader(java.lang.String, java.lang.String)
409      */

410     public void addHeader(String JavaDoc name, String JavaDoc value)
411     {
412         doAddHeader(name, value);
413     }
414
415     /* (non-Javadoc)
416      * @see javax.servlet.http.HttpServletResponse#setHeader(java.lang.String, java.lang.String)
417      */

418     public void setHeader(String JavaDoc name, String JavaDoc value)
419     {
420         headers.put(name, value);
421     }
422
423     /* (non-Javadoc)
424      * @see javax.servlet.http.HttpServletResponse#addDateHeader(java.lang.String, long)
425      */

426     public void addDateHeader(String JavaDoc name, long value)
427     {
428         doAddHeader(name, new Long JavaDoc(value));
429     }
430
431     /* (non-Javadoc)
432      * @see javax.servlet.http.HttpServletResponse#setDateHeader(java.lang.String, long)
433      */

434     public void setDateHeader(String JavaDoc name, long value)
435     {
436         headers.put(name, new Long JavaDoc(value));
437     }
438
439     /* (non-Javadoc)
440      * @see javax.servlet.http.HttpServletResponse#addIntHeader(java.lang.String, int)
441      */

442     public void addIntHeader(String JavaDoc name, int value)
443     {
444         doAddHeader(name, new Integer JavaDoc(value));
445     }
446
447     /* (non-Javadoc)
448      * @see javax.servlet.http.HttpServletResponse#setIntHeader(java.lang.String, int)
449      */

450     public void setIntHeader(String JavaDoc name, int value)
451     {
452         headers.put(name, new Integer JavaDoc(value));
453     }
454
455     /**
456      * @param name
457      * @param value
458      */

459     private void doAddHeader(String JavaDoc name, Object JavaDoc value)
460     {
461         Object JavaDoc oldValue = headers.get(name);
462         if (oldValue instanceof List JavaDoc)
463         {
464             List JavaDoc list = (List JavaDoc) oldValue;
465             list.add(value);
466         }
467         else if (oldValue != null)
468         {
469             List JavaDoc list = new LinkedList JavaDoc();
470             list.add(oldValue);
471             list.add(value);
472             headers.put(name, list);
473         }
474         else
475         {
476             headers.put(name, value);
477         }
478     }
479
480     /* (non-Javadoc)
481      * @see javax.servlet.http.HttpServletResponse#containsHeader(java.lang.String)
482      */

483     public boolean containsHeader(String JavaDoc name)
484     {
485         return headers.containsKey(name);
486     }
487
488     /**
489      * Accessor for the current set of headers
490      * @return The current set of headers
491      */

492     public Set JavaDoc getHeaderNames()
493     {
494         return headers.keySet();
495     }
496
497     /**
498      * Accessor for a header by a given name
499      * @param name The header name to lookup
500      * @return The data behind this header
501      */

502     public Object JavaDoc getHeader(String JavaDoc name)
503     {
504         return headers.get(name);
505     }
506
507     /**
508      * If there are multiple values for a given header, get them as a list
509      * @param name The header name to lookup
510      * @return The data behind this header
511      */

512     public List JavaDoc getHeaders(String JavaDoc name)
513     {
514         Object JavaDoc value = headers.get(name);
515         if (value instanceof List JavaDoc)
516         {
517             return (List JavaDoc) value;
518         }
519         else if (value != null)
520         {
521             return Collections.singletonList(value);
522         }
523         else
524         {
525             return Collections.EMPTY_LIST;
526         }
527     }
528
529     //---------------------------------------------------------------------
530
// Methods for FakeRequestDispatcher
531
//---------------------------------------------------------------------
532

533     /**
534      * What URL are we forwarding to?
535      * @param forwardedUrl What URL are we forwarding to?
536      */

537     public void setForwardedUrl(String JavaDoc forwardedUrl)
538     {
539         this.forwardedUrl = forwardedUrl;
540     }
541
542     /**
543      * What URL are we forwarding to?
544      * @return What URL are we forwarding to?
545      */

546     public String JavaDoc getForwardedUrl()
547     {
548         return forwardedUrl;
549     }
550
551     /**
552      * What URL are we including?
553      * @param includedUrl What URL are we including?
554      */

555     public void setIncludedUrl(String JavaDoc includedUrl)
556     {
557         this.includedUrl = includedUrl;
558     }
559
560     /**
561      * What URL are we including?
562      * @return What URL are we including?
563      */

564     public String JavaDoc getIncludedUrl()
565     {
566         return includedUrl;
567     }
568
569     private static final String JavaDoc CHARSET_PREFIX = "charset=";
570
571     private String JavaDoc characterEncoding = "ISO-8859-1";
572
573     private final ByteArrayOutputStream JavaDoc content = new ByteArrayOutputStream JavaDoc();
574
575     private final DelegatingServletOutputStream outputStream = new DelegatingServletOutputStream(this.content);
576
577     private PrintWriter JavaDoc writer;
578
579     private int contentLength = 0;
580
581     private String JavaDoc contentType;
582
583     private int bufferSize = 4096;
584
585     private boolean committed;
586
587     private Locale JavaDoc locale = Locale.getDefault();
588
589     private final List JavaDoc cookies = new ArrayList JavaDoc();
590
591     private final Map JavaDoc headers = new HashMap JavaDoc();
592
593     private int status = HttpServletResponse.SC_OK;
594
595     private String JavaDoc errorMessage;
596
597     private String JavaDoc redirectedUrl;
598
599     private String JavaDoc forwardedUrl;
600
601     private String JavaDoc includedUrl;
602 }
603
Popular Tags