KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infohazard > maverick > util > FakeHttpServletResponse


1 /*
2  * $Id: FakeHttpServletResponse.java,v 1.8 2004/06/07 20:38:37 eelco12 Exp $
3  * $Source: /cvsroot/mav/maverick/src/java/org/infohazard/maverick/util/FakeHttpServletResponse.java,v $
4  */

5
6 package org.infohazard.maverick.util;
7
8 import java.io.IOException JavaDoc;
9 import java.io.PrintWriter JavaDoc;
10 import java.io.Reader JavaDoc;
11 import java.io.UnsupportedEncodingException JavaDoc;
12
13 import javax.servlet.ServletOutputStream JavaDoc;
14 import javax.servlet.ServletResponse JavaDoc;
15 import javax.servlet.http.Cookie JavaDoc;
16 import javax.servlet.http.HttpServletResponse JavaDoc;
17 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 /**
23  * Stores data written to the response and allows it to be obtained later.
24  */

25 public class FakeHttpServletResponse extends HttpServletResponseWrapper JavaDoc
26 {
27     /**
28      * Logger.
29      */

30     private static Log log = LogFactory.getLog(FakeHttpServletResponse.class);
31
32     /**
33      */

34     protected static final String JavaDoc NO_BUFFER_MSG = "The previous step never tried to write anything. Perhaps the document path is incorrect?";
35
36     /**
37      * We need to track the charset which is applied to the response
38      * using setContentType so that we can intelligently convert between
39      * streams and readers. If null, the default encoding is assumed.
40      */

41     protected String JavaDoc charset;
42
43     /**
44      * Actually holds all the output data.
45      */

46     protected Buffer buffer;
47     
48     /**
49      * Creates a response wrapper which buffers the output.
50      */

51     public FakeHttpServletResponse(HttpServletResponse JavaDoc wrapMe)
52     {
53         super(wrapMe);
54         
55         if (log.isDebugEnabled())
56             log.debug("Creating fake response, original buffer size is "
57                         + wrapMe.getBufferSize());
58     }
59
60     /**
61      * @return true if it is more efficient to call getOutputAsReader() than
62      * getOutputAsString(). If false, getOutputAsString() should be used.
63      */

64     public boolean prefersReader()
65     {
66         if (this.buffer == null)
67             throw new IllegalStateException JavaDoc(NO_BUFFER_MSG);
68         
69         return this.buffer.prefersReader();
70     }
71     
72     /**
73      * Provides the buffered output as a Reader. If output was written
74      * using a ServletOutputStream, the charset defined in the content-type
75      * is used to decode the data. If no charset is set, the default
76      * encoding is assumed.
77      */

78     public Reader JavaDoc getOutputAsReader() throws UnsupportedEncodingException JavaDoc
79     {
80         log.debug("Getting output as Reader");
81         
82         if (this.buffer == null)
83             throw new IllegalStateException JavaDoc(NO_BUFFER_MSG);
84         
85         return this.buffer.getAsReader();
86     }
87
88     /**
89      * Provides the buffered output as a String. If output was written
90      * using a ServletOutputStream, the charset defined in the content-type
91      * is used to decode the data. If no charset is set, the default
92      * encoding is assumed.
93      */

94     public String JavaDoc getOutputAsString() throws UnsupportedEncodingException JavaDoc
95     {
96         log.debug("Getting output as String");
97
98         if (this.buffer == null)
99             throw new IllegalStateException JavaDoc(NO_BUFFER_MSG);
100         
101         return this.buffer.getAsString();
102     }
103
104     /**
105      * @return the amount of data written to the output stream.
106      */

107     public int outputSize()
108     {
109         if (this.buffer == null)
110             return 0;
111         else
112             return this.buffer.size();
113     }
114
115     /**
116      * @see ServletResponse#getOutputStream
117      */

118     public ServletOutputStream JavaDoc getOutputStream() throws java.io.IOException JavaDoc
119     {
120         log.debug("View is using ServletOutputStream");
121
122         if (this.buffer == null)
123             this.buffer = new ServletOutputStreamBuffer(this.charset);
124         else
125         {
126             if (!(this.buffer instanceof ServletOutputStreamBuffer))
127                 throw new IllegalStateException JavaDoc("getWriter() already called");
128         }
129
130         return (ServletOutputStream JavaDoc)this.buffer;
131     }
132
133     /**
134      * @see ServletResponse#getWriter
135      */

136     public PrintWriter JavaDoc getWriter() throws java.io.IOException JavaDoc
137     {
138         log.debug("View is using PrintWriter");
139         
140         if (this.buffer == null)
141             this.buffer = new PrintWriterBuffer();
142         else
143         {
144             if (!(this.buffer instanceof PrintWriterBuffer))
145                 throw new IllegalStateException JavaDoc("getOutputStream() already called");
146         }
147
148         return (PrintWriter JavaDoc)this.buffer;
149     }
150
151     /**
152      * @see ServletResponse#setContentType
153      */

154     public void setContentType(String JavaDoc type)
155     {
156         if (log.isDebugEnabled())
157             log.debug("Setting contentType to " + type);
158         
159         // Don't pass through
160

161         // Extract out the charset.
162
// type might be something like this: text/html; charset=utf-8
163
int semicolonIndex = type.lastIndexOf(';');
164         if (semicolonIndex >= 0 && semicolonIndex != (type.length()-1))
165         {
166             String JavaDoc working = type.substring(semicolonIndex + 1);
167             working = working.trim();
168
169             if (working.toLowerCase().startsWith("charset"))
170             {
171                 working = working.substring(working.indexOf("=") + 1).trim();
172
173                 if (working.length() > 0)
174                     this.charset = working;
175             }
176         }
177     }
178
179     /**
180      * @see ServletResponse#setContentLength
181      */

182     public void setContentLength(int len)
183     {
184         if (log.isDebugEnabled())
185             log.debug("Someone wanted to set contentLength to " + len);
186             
187         // Don't pass through
188
}
189
190     /**
191      * @see ServletResponse#flushBuffer
192      */

193     public void flushBuffer() throws IOException JavaDoc
194     {
195         if (log.isDebugEnabled())
196             log.debug("Someone wanted to flush the buffer");
197             
198         // Don't pass through
199
}
200
201     /**
202      * @see ServletResponse#setLocale
203      */

204     public void setLocale(java.util.Locale JavaDoc loc)
205     {
206         if (log.isDebugEnabled())
207             log.debug("Set locale to " + loc);
208             
209         super.setLocale(loc);
210     }
211
212     /**
213      * @see HttpServletResponse#addCookie
214      */

215     public void addCookie(Cookie JavaDoc cookie)
216     {
217         if (log.isDebugEnabled())
218             log.debug("Added cookie " + cookie);
219             
220         super.addCookie(cookie);
221     }
222     
223     /**
224      * @see HttpServletResponse#setDateHeader
225      */

226     public void setDateHeader(java.lang.String JavaDoc name, long date)
227     {
228         if (log.isDebugEnabled())
229             log.debug("Set date header " + name + " to " + date);
230             
231         super.setDateHeader(name, date);
232     }
233
234     /**
235      * @see HttpServletResponse#addDateHeader
236      */

237     public void addDateHeader(java.lang.String JavaDoc name, long date)
238     {
239         if (log.isDebugEnabled())
240             log.debug("Add date header " + name + " to " + date);
241             
242         super.addDateHeader(name, date);
243     }
244
245     /**
246      * @see HttpServletResponse#setHeader
247      */

248     public void setHeader(java.lang.String JavaDoc name, java.lang.String JavaDoc value)
249     {
250         if (log.isDebugEnabled())
251             log.debug("Set header " + name + " to " + value);
252             
253         super.setHeader(name, value);
254     }
255
256     /**
257      * @see HttpServletResponse#addHeader
258      */

259     public void addHeader(java.lang.String JavaDoc name, java.lang.String JavaDoc value)
260     {
261         if (log.isDebugEnabled())
262             log.debug("Add header " + name + " to " + value);
263             
264         super.addHeader(name, value);
265     }
266
267     /**
268      * @see HttpServletResponse#setIntHeader
269      */

270     public void setIntHeader(java.lang.String JavaDoc name, int value)
271     {
272         if (log.isDebugEnabled())
273             log.debug("Set int header " + name + " to " + value);
274             
275         super.setIntHeader(name, value);
276     }
277
278     /**
279      * @see HttpServletResponse#addIntHeader
280      */

281     public void addIntHeader(java.lang.String JavaDoc name, int value)
282     {
283         if (log.isDebugEnabled())
284             log.debug("Add int header " + name + " to " + value);
285             
286         super.addIntHeader(name, value);
287     }
288
289     /**
290      * This actually sends to the real response and flags an error condition.
291      *
292      * @see HttpServletResponse#addIntHeader
293      */

294     public void sendError(int sc) throws IOException JavaDoc
295     {
296         log.warn("Sending error " + sc);
297
298         super.sendError(sc);
299     }
300     
301     /**
302      * This actually sends to the real response and flags an error condition.
303      *
304      * @see HttpServletResponse#addIntHeader
305      */

306     public void sendError(int sc, java.lang.String JavaDoc msg) throws IOException JavaDoc
307     {
308         log.warn("Sending error " + sc + ", " + msg);
309
310         super.sendError(sc, msg);
311     }
312     
313     /**
314      * @see HttpServletResponse#setStatus
315      */

316     public void setStatus(int sc)
317     {
318         if (log.isDebugEnabled())
319             log.debug("Setting status " + sc);
320             
321         super.setStatus(sc);
322     }
323
324     /**
325      * @see HttpServletResponse#setStatus
326      */

327     public void setStatus(int sc, java.lang.String JavaDoc sm)
328     {
329         if (log.isDebugEnabled())
330             log.debug("Setting status " + sc + ", " + sm);
331
332         super.setStatus(sc, sm);
333         // :FIXME: setStatus is deprecated As of version 2.1 [thusted 2003-10-27]
334
}
335
336     /**
337      */

338     protected HttpServletResponse JavaDoc getHttpResponse()
339     {
340         return (HttpServletResponse JavaDoc)this.getResponse();
341     }
342 }
343
Popular Tags