001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.util;
017:
018: import java.io.IOException;
019: import java.io.PrintWriter;
020: import java.io.Writer;
021: import java.util.Locale;
022:
023: import javax.servlet.ServletOutputStream;
024: import javax.servlet.http.Cookie;
025: import javax.servlet.http.HttpServletResponse;
026: import javax.servlet.http.HttpServletResponseWrapper;
027:
028: import org.apache.commons.logging.LogFactory;
029: import org.apache.commons.logging.Log;
030:
031: /**
032: * Used by ExecutionContext to forward results back via javascript.
033: * <p>We could like to implement {@link HttpServletResponse}, but there is a bug
034: * in WebLogic where it casts to a {@link HttpServletResponseWrapper} so we
035: * need to extend that.
036: * @author Joe Walker [joe at getahead dot ltd dot uk]
037: */
038: public final class SwallowingHttpServletResponse extends
039: HttpServletResponseWrapper implements HttpServletResponse {
040: /**
041: * Create a new HttpServletResponse that allows you to catch the body
042: * @param response The original HttpServletResponse
043: * @param sout The place we copy responses to
044: * @param characterEncoding The output encoding
045: */
046: public SwallowingHttpServletResponse(HttpServletResponse response,
047: Writer sout, String characterEncoding) {
048: super (response);
049:
050: pout = new PrintWriter(sout);
051: outputStream = new WriterOutputStream(sout, characterEncoding);
052:
053: this .characterEncoding = characterEncoding;
054: }
055:
056: /* (non-Javadoc)
057: * @see javax.servlet.http.HttpServletResponseWrapper#addCookie(javax.servlet.http.Cookie)
058: */
059: @Override
060: public void addCookie(Cookie cookie) {
061: }
062:
063: /* (non-Javadoc)
064: * @see javax.servlet.http.HttpServletResponseWrapper#addDateHeader(java.lang.String, long)
065: */
066: @Override
067: public void addDateHeader(String name, long value) {
068: }
069:
070: /* (non-Javadoc)
071: * @see javax.servlet.http.HttpServletResponseWrapper#addHeader(java.lang.String, java.lang.String)
072: */
073: @Override
074: public void addHeader(String name, String value) {
075: }
076:
077: /* (non-Javadoc)
078: * @see javax.servlet.http.HttpServletResponseWrapper#addIntHeader(java.lang.String, int)
079: */
080: @Override
081: public void addIntHeader(String name, int value) {
082: }
083:
084: /* (non-Javadoc)
085: * @see javax.servlet.http.HttpServletResponseWrapper#containsHeader(java.lang.String)
086: */
087: @Override
088: public boolean containsHeader(String name) {
089: return false;
090: }
091:
092: /**
093: * @see javax.servlet.http.HttpServletResponseWrapper#encodeRedirectUrl(java.lang.String)
094: * @deprecated
095: */
096: @Override
097: @Deprecated
098: public String encodeRedirectUrl(String url) {
099: return url;
100: }
101:
102: /* (non-Javadoc)
103: * @see javax.servlet.http.HttpServletResponseWrapper#encodeRedirectURL(java.lang.String)
104: */
105: @Override
106: public String encodeRedirectURL(String url) {
107: return url;
108: }
109:
110: /**
111: * @see javax.servlet.http.HttpServletResponseWrapper#encodeUrl(java.lang.String)
112: * @deprecated
113: */
114: @Override
115: @Deprecated
116: public String encodeUrl(String url) {
117: return url;
118: }
119:
120: /* (non-Javadoc)
121: * @see javax.servlet.http.HttpServletResponseWrapper#encodeURL(java.lang.String)
122: */
123: @Override
124: public String encodeURL(String url) {
125: return url;
126: }
127:
128: /* (non-Javadoc)
129: * @see javax.servlet.ServletResponseWrapper#flushBuffer()
130: */
131: @Override
132: public void flushBuffer() throws IOException {
133: pout.flush();
134: }
135:
136: /* (non-Javadoc)
137: * @see javax.servlet.ServletResponseWrapper#getBufferSize()
138: */
139: @Override
140: public int getBufferSize() {
141: return bufferSize;
142: }
143:
144: /* (non-Javadoc)
145: * @see javax.servlet.ServletResponseWrapper#getCharacterEncoding()
146: */
147: @Override
148: public String getCharacterEncoding() {
149: return characterEncoding;
150: }
151:
152: /**
153: * @return The MIME type of the content
154: * @see javax.servlet.ServletResponse#setContentType(String)
155: */
156: @Override
157: public String getContentType() {
158: return contentType;
159: }
160:
161: /**
162: * Accessor for any error messages set using {@link #sendError(int)} or
163: * {@link #sendError(int, String)}
164: * @return The current error message
165: */
166: public String getErrorMessage() {
167: return errorMessage;
168: }
169:
170: /* (non-Javadoc)
171: * @see javax.servlet.ServletResponseWrapper#getLocale()
172: */
173: @Override
174: public Locale getLocale() {
175: return locale;
176: }
177:
178: /* (non-Javadoc)
179: * @see javax.servlet.ServletResponseWrapper#getOutputStream()
180: */
181: @Override
182: public ServletOutputStream getOutputStream() {
183: return outputStream;
184: }
185:
186: /**
187: * Accessor for the redirect URL set using {@link #sendRedirect(String)}
188: * @return The redirect URL
189: */
190: public String getRedirectedUrl() {
191: return redirectedUrl;
192: }
193:
194: /**
195: * What HTTP status code should be returned?
196: * @return The current http status code
197: */
198: public int getStatus() {
199: return status;
200: }
201:
202: /* (non-Javadoc)
203: * @see javax.servlet.ServletResponseWrapper#getWriter()
204: */
205: @Override
206: public PrintWriter getWriter() {
207: return pout;
208: }
209:
210: /* (non-Javadoc)
211: * @see javax.servlet.ServletResponseWrapper#isCommitted()
212: */
213: @Override
214: public boolean isCommitted() {
215: return false;
216: }
217:
218: /* (non-Javadoc)
219: * @see javax.servlet.ServletResponseWrapper#reset()
220: */
221: @Override
222: public void reset() {
223: }
224:
225: /* (non-Javadoc)
226: * @see javax.servlet.ServletResponseWrapper#resetBuffer()
227: */
228: @Override
229: public void resetBuffer() {
230: }
231:
232: /* (non-Javadoc)
233: * @see javax.servlet.http.HttpServletResponseWrapper#sendError(int)
234: */
235: @Override
236: public void sendError(int newStatus) {
237: if (committed) {
238: throw new IllegalStateException(
239: "Cannot set error status - response is already committed");
240: }
241:
242: log.warn("Ignoring call to sendError(" + newStatus + ')');
243:
244: status = newStatus;
245: committed = true;
246: }
247:
248: /* (non-Javadoc)
249: * @see javax.servlet.http.HttpServletResponseWrapper#sendError(int, java.lang.String)
250: */
251: @Override
252: public void sendError(int newStatus, String newErrorMessage) {
253: if (committed) {
254: throw new IllegalStateException(
255: "Cannot set error status - response is already committed");
256: }
257:
258: log.warn("Ignoring call to sendError(" + newStatus + ", "
259: + newErrorMessage + ')');
260:
261: status = newStatus;
262: errorMessage = newErrorMessage;
263: committed = true;
264: }
265:
266: /* (non-Javadoc)
267: * @see javax.servlet.http.HttpServletResponseWrapper#sendRedirect(java.lang.String)
268: */
269: @Override
270: public void sendRedirect(String location) {
271: if (committed) {
272: throw new IllegalStateException(
273: "Cannot send redirect - response is already committed");
274: }
275:
276: log.warn("Ignoring call to sendRedirect(" + location + ')');
277:
278: redirectedUrl = location;
279: committed = true;
280: }
281:
282: /* (non-Javadoc)
283: * @see javax.servlet.ServletResponseWrapper#setBufferSize(int)
284: */
285: @Override
286: public void setBufferSize(int bufferSize) {
287: this .bufferSize = bufferSize;
288: }
289:
290: /**
291: * @param characterEncoding The new encoding to use for response strings
292: * @see javax.servlet.ServletResponseWrapper#getCharacterEncoding()
293: */
294: @Override
295: public void setCharacterEncoding(String characterEncoding) {
296: this .characterEncoding = characterEncoding;
297: }
298:
299: /* (non-Javadoc)
300: * @see javax.servlet.ServletResponseWrapper#setContentLength(int)
301: */
302: @Override
303: public void setContentLength(int i) {
304: // The content length of the original document is not likely to be the
305: // same as the content length of the new document.
306: }
307:
308: /* (non-Javadoc)
309: * @see javax.servlet.ServletResponseWrapper#setContentType(java.lang.String)
310: */
311: @Override
312: public void setContentType(String contentType) {
313: this .contentType = contentType;
314: }
315:
316: /* (non-Javadoc)
317: * @see javax.servlet.http.HttpServletResponseWrapper#setDateHeader(java.lang.String, long)
318: */
319: @Override
320: public void setDateHeader(String name, long value) {
321: }
322:
323: /* (non-Javadoc)
324: * @see javax.servlet.http.HttpServletResponseWrapper#setHeader(java.lang.String, java.lang.String)
325: */
326: @Override
327: public void setHeader(String name, String value) {
328: }
329:
330: /* (non-Javadoc)
331: * @see javax.servlet.http.HttpServletResponseWrapper#setIntHeader(java.lang.String, int)
332: */
333: @Override
334: public void setIntHeader(String name, int value) {
335: }
336:
337: /* (non-Javadoc)
338: * @see javax.servlet.ServletResponseWrapper#setLocale(java.util.Locale)
339: */
340: @Override
341: public void setLocale(Locale locale) {
342: this .locale = locale;
343: }
344:
345: /* (non-Javadoc)
346: * @see javax.servlet.http.HttpServletResponseWrapper#setStatus(int)
347: */
348: @Override
349: public void setStatus(int status) {
350: this .status = status;
351: log.warn("Ignoring call to setStatus(" + status + ')');
352: }
353:
354: /**
355: * @see javax.servlet.http.HttpServletResponse#setStatus(int, java.lang.String)
356: * @deprecated
357: */
358: @Deprecated
359: @Override
360: public void setStatus(int newStatus, String newErrorMessage) {
361: status = newStatus;
362: errorMessage = newErrorMessage;
363: log.warn("Ignoring call to setStatus(" + newStatus + ", "
364: + newErrorMessage + ')');
365: }
366:
367: /**
368: * The ignored buffer size
369: */
370: private int bufferSize = 0;
371:
372: /**
373: * The character encoding used
374: */
375: private String characterEncoding;
376:
377: /**
378: * Has the response been comitted
379: */
380: private boolean committed = false;
381:
382: /**
383: * The MIME type of the output body
384: */
385: private String contentType = null;
386:
387: /**
388: * The error message sent with a status != HttpServletResponse.SC_OK
389: */
390: private String errorMessage = null;
391:
392: /**
393: * Locale setting: defaults to platform default
394: */
395: private Locale locale = Locale.getDefault();
396:
397: /**
398: * The forwarding output stream
399: */
400: private final ServletOutputStream outputStream;
401:
402: /**
403: * The forwarding output stream
404: */
405: private final PrintWriter pout;
406:
407: /**
408: * Where are we to redirect the user to?
409: */
410: private String redirectedUrl = null;
411:
412: /**
413: * The HTTP status
414: */
415: private int status = HttpServletResponse.SC_OK;
416:
417: /**
418: * The log stream
419: */
420: private static final Log log = LogFactory
421: .getLog(SwallowingHttpServletResponse.class);
422: }
|