KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2005 Joe Walker
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 package org.directwebremoting.util;
17
18 import java.io.IOException JavaDoc;
19 import java.io.PrintWriter JavaDoc;
20 import java.io.Writer JavaDoc;
21 import java.util.Locale JavaDoc;
22
23 import javax.servlet.ServletOutputStream JavaDoc;
24 import javax.servlet.http.Cookie JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
27
28 /**
29  * Used by ExecutionContext to forward results back via javascript.
30  * <p>We could like to implement {@link HttpServletResponse}, but there is a bug
31  * in WebLogic where it casts to a {@link HttpServletResponseWrapper} so we
32  * need to extend that.
33  * @author Joe Walker [joe at getahead dot ltd dot uk]
34  */

35 public final class SwallowingHttpServletResponse extends HttpServletResponseWrapper JavaDoc implements HttpServletResponse JavaDoc
36 {
37     /**
38      * Create a new HttpServletResponse that allows you to catch the body
39      * @param response The original HttpServletResponse
40      * @param sout The place we copy responses to
41      * @param characterEncoding The output encoding
42      */

43     public SwallowingHttpServletResponse(HttpServletResponse JavaDoc response, Writer JavaDoc sout, String JavaDoc characterEncoding)
44     {
45         super(response);
46
47         pout = new PrintWriter JavaDoc(sout);
48         outputStream = new WriterOutputStream(sout, characterEncoding);
49
50         this.characterEncoding = characterEncoding;
51     }
52
53     /* (non-Javadoc)
54      * @see javax.servlet.http.HttpServletResponseWrapper#addCookie(javax.servlet.http.Cookie)
55      */

56     public void addCookie(Cookie JavaDoc cookie)
57     {
58     }
59
60     /* (non-Javadoc)
61      * @see javax.servlet.http.HttpServletResponseWrapper#addDateHeader(java.lang.String, long)
62      */

63     public void addDateHeader(String JavaDoc name, long value)
64     {
65     }
66
67     /* (non-Javadoc)
68      * @see javax.servlet.http.HttpServletResponseWrapper#addHeader(java.lang.String, java.lang.String)
69      */

70     public void addHeader(String JavaDoc name, String JavaDoc value)
71     {
72     }
73
74     /* (non-Javadoc)
75      * @see javax.servlet.http.HttpServletResponseWrapper#addIntHeader(java.lang.String, int)
76      */

77     public void addIntHeader(String JavaDoc name, int value)
78     {
79     }
80
81     /* (non-Javadoc)
82      * @see javax.servlet.http.HttpServletResponseWrapper#containsHeader(java.lang.String)
83      */

84     public boolean containsHeader(String JavaDoc name)
85     {
86         return false;
87     }
88
89     /* (non-Javadoc)
90      * @see javax.servlet.http.HttpServletResponseWrapper#encodeRedirectUrl(java.lang.String)
91      */

92     public String JavaDoc encodeRedirectUrl(String JavaDoc url)
93     {
94         return url;
95     }
96
97     /* (non-Javadoc)
98      * @see javax.servlet.http.HttpServletResponseWrapper#encodeRedirectURL(java.lang.String)
99      */

100     public String JavaDoc encodeRedirectURL(String JavaDoc url)
101     {
102         return url;
103     }
104
105     /* (non-Javadoc)
106      * @see javax.servlet.http.HttpServletResponseWrapper#encodeUrl(java.lang.String)
107      */

108     public String JavaDoc encodeUrl(String JavaDoc url)
109     {
110         return url;
111     }
112
113     /* (non-Javadoc)
114      * @see javax.servlet.http.HttpServletResponseWrapper#encodeURL(java.lang.String)
115      */

116     public String JavaDoc encodeURL(String JavaDoc url)
117     {
118         return url;
119     }
120
121     /* (non-Javadoc)
122      * @see javax.servlet.ServletResponseWrapper#flushBuffer()
123      */

124     public void flushBuffer() throws IOException JavaDoc
125     {
126         pout.flush();
127     }
128
129     /* (non-Javadoc)
130      * @see javax.servlet.ServletResponseWrapper#getBufferSize()
131      */

132     public int getBufferSize()
133     {
134         return bufferSize;
135     }
136
137     /* (non-Javadoc)
138      * @see javax.servlet.ServletResponseWrapper#getCharacterEncoding()
139      */

140     public String JavaDoc getCharacterEncoding()
141     {
142         return characterEncoding;
143     }
144
145     /**
146      * @return The MIME type of the content
147      * @see javax.servlet.ServletResponse#setContentType(String)
148      */

149     public String JavaDoc getContentType()
150     {
151         return contentType;
152     }
153
154     /**
155      * Accessor for any error messages set using {@link #sendError(int)} or
156      * {@link #sendError(int, String)}
157      * @return The current error message
158      */

159     public String JavaDoc getErrorMessage()
160     {
161         return errorMessage;
162     }
163
164     /* (non-Javadoc)
165      * @see javax.servlet.ServletResponseWrapper#getLocale()
166      */

167     public Locale JavaDoc getLocale()
168     {
169         return locale;
170     }
171
172     /* (non-Javadoc)
173      * @see javax.servlet.ServletResponseWrapper#getOutputStream()
174      */

175     public ServletOutputStream JavaDoc getOutputStream()
176     {
177         return outputStream;
178     }
179
180     /**
181      * Accessor for the redirect URL set using {@link #sendRedirect(String)}
182      * @return The redirect URL
183      */

184     public String JavaDoc getRedirectedUrl()
185     {
186         return redirectedUrl;
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     /* (non-Javadoc)
199      * @see javax.servlet.ServletResponseWrapper#getWriter()
200      */

201     public PrintWriter JavaDoc getWriter()
202     {
203         return pout;
204     }
205
206     /* (non-Javadoc)
207      * @see javax.servlet.ServletResponseWrapper#isCommitted()
208      */

209     public boolean isCommitted()
210     {
211         return false;
212     }
213
214     /* (non-Javadoc)
215      * @see javax.servlet.ServletResponseWrapper#reset()
216      */

217     public void reset()
218     {
219     }
220
221     /* (non-Javadoc)
222      * @see javax.servlet.ServletResponseWrapper#resetBuffer()
223      */

224     public void resetBuffer()
225     {
226     }
227
228     /* (non-Javadoc)
229      * @see javax.servlet.http.HttpServletResponseWrapper#sendError(int)
230      */

231     public void sendError(int newStatus)
232     {
233         if (committed)
234         {
235             throw new IllegalStateException JavaDoc("Cannot set error status - response is already committed");
236         }
237
238         log.warn("Ignoring call to sendError(" + newStatus + ')');
239
240         status = newStatus;
241         committed = true;
242     }
243
244     /* (non-Javadoc)
245      * @see javax.servlet.http.HttpServletResponseWrapper#sendError(int, java.lang.String)
246      */

247     public void sendError(int newStatus, String JavaDoc newErrorMessage)
248     {
249         if (committed)
250         {
251             throw new IllegalStateException JavaDoc("Cannot set error status - response is already committed");
252         }
253
254         log.warn("Ignoring call to sendError(" + newStatus + ", " + newErrorMessage + ')');
255
256         status = newStatus;
257         errorMessage = newErrorMessage;
258         committed = true;
259     }
260
261     /* (non-Javadoc)
262      * @see javax.servlet.http.HttpServletResponseWrapper#sendRedirect(java.lang.String)
263      */

264     public void sendRedirect(String JavaDoc location)
265     {
266         if (committed)
267         {
268             throw new IllegalStateException JavaDoc("Cannot send redirect - response is already committed");
269         }
270
271         log.warn("Ignoring call to sendRedirect(" + location + ')');
272
273         redirectedUrl = location;
274         committed = true;
275     }
276
277     /* (non-Javadoc)
278      * @see javax.servlet.ServletResponseWrapper#setBufferSize(int)
279      */

280     public void setBufferSize(int bufferSize)
281     {
282         this.bufferSize = bufferSize;
283     }
284
285     /**
286      * @param characterEncoding The new encoding to use for response strings
287      * @see javax.servlet.ServletResponseWrapper#getCharacterEncoding()
288      */

289     public void setCharacterEncoding(String JavaDoc characterEncoding)
290     {
291         this.characterEncoding = characterEncoding;
292     }
293
294     /* (non-Javadoc)
295      * @see javax.servlet.ServletResponseWrapper#setContentLength(int)
296      */

297     public void setContentLength(int i)
298     {
299         // The content length of the original document is not likely to be the
300
// same as the content length of the new document.
301
}
302
303     /* (non-Javadoc)
304      * @see javax.servlet.ServletResponseWrapper#setContentType(java.lang.String)
305      */

306     public void setContentType(String JavaDoc contentType)
307     {
308         this.contentType = contentType;
309     }
310
311     /* (non-Javadoc)
312      * @see javax.servlet.http.HttpServletResponseWrapper#setDateHeader(java.lang.String, long)
313      */

314     public void setDateHeader(String JavaDoc name, long value)
315     {
316     }
317
318     /* (non-Javadoc)
319      * @see javax.servlet.http.HttpServletResponseWrapper#setHeader(java.lang.String, java.lang.String)
320      */

321     public void setHeader(String JavaDoc name, String JavaDoc value)
322     {
323     }
324
325     /* (non-Javadoc)
326      * @see javax.servlet.http.HttpServletResponseWrapper#setIntHeader(java.lang.String, int)
327      */

328     public void setIntHeader(String JavaDoc name, int value)
329     {
330     }
331
332     /* (non-Javadoc)
333      * @see javax.servlet.ServletResponseWrapper#setLocale(java.util.Locale)
334      */

335     public void setLocale(Locale JavaDoc locale)
336     {
337         this.locale = locale;
338     }
339
340     /* (non-Javadoc)
341      * @see javax.servlet.http.HttpServletResponseWrapper#setStatus(int)
342      */

343     public void setStatus(int status)
344     {
345         this.status = status;
346         log.warn("Ignoring call to setStatus(" + status + ')');
347     }
348
349     /**
350      * @see javax.servlet.http.HttpServletResponse#setStatus(int, java.lang.String)
351      * @deprecated
352      */

353     public void setStatus(int newStatus, String JavaDoc newErrorMessage)
354     {
355         status = newStatus;
356         errorMessage = newErrorMessage;
357         log.warn("Ignoring call to setStatus(" + newStatus + ", " + newErrorMessage + ')');
358     }
359
360     /**
361      * The ignored buffer size
362      */

363     private int bufferSize = 0;
364
365     /**
366      * The character encoding used
367      */

368     private String JavaDoc characterEncoding;
369
370     /**
371      * Has the response been comitted
372      */

373     private boolean committed = false;
374
375     /**
376      * The MIME type of the output body
377      */

378     private String JavaDoc contentType;
379
380     /**
381      * The error message sent with a status != HttpServletResponse.SC_OK
382      */

383     private String JavaDoc errorMessage;
384
385     /**
386      * Locale setting: defaults to platform default
387      */

388     private Locale JavaDoc locale = Locale.getDefault();
389
390     /**
391      * The forwarding output stream
392      */

393     private final ServletOutputStream JavaDoc outputStream;
394
395     /**
396      * The forwarding output stream
397      */

398     private final PrintWriter JavaDoc pout;
399
400     /**
401      * Where are we to redirect the user to?
402      */

403     private String JavaDoc redirectedUrl;
404
405     /**
406      * The HTTP status
407      */

408     private int status = HttpServletResponse.SC_OK;
409
410     /**
411      * The log stream
412      */

413     private static final Logger log = Logger.getLogger(SwallowingHttpServletResponse.class);
414 }
415
Popular Tags