01: package org.gomba;
02:
03: import java.io.CharArrayWriter;
04: import java.io.IOException;
05: import java.io.PrintWriter;
06: import java.io.Writer;
07:
08: import javax.servlet.ServletOutputStream;
09: import javax.servlet.http.HttpServletResponse;
10: import javax.servlet.http.HttpServletResponseWrapper;
11:
12: /**
13: * This class wraps an existing HttpServletResponse and captures its body in a
14: * private Writer. The body can be accessed by calling getBody().
15: *
16: * @author Flavio Tordini
17: * @version $Id: CustomHttpServletResponseWrapper.java,v 1.2 2004/09/16 09:23:16 flaviotordini Exp $
18: */
19: class CustomHttpServletResponseWrapper extends
20: HttpServletResponseWrapper {
21:
22: private final Writer writer;
23:
24: /**
25: * Constructor.
26: */
27: public CustomHttpServletResponseWrapper(HttpServletResponse response) {
28: super (response);
29: // we could also use a StringWriter...
30: // what's the difference?
31: this .writer = new CharArrayWriter();
32: }
33:
34: /**
35: * @see javax.servlet.ServletResponse#getOutputStream()
36: */
37: public ServletOutputStream getOutputStream() throws IOException {
38: throw new IOException(
39: "This response output stream should not be accessed.");
40: }
41:
42: /**
43: * @see javax.servlet.ServletResponse#getWriter()
44: */
45: public PrintWriter getWriter() throws IOException {
46: return new PrintWriter(this .writer);
47: }
48:
49: /**
50: * @return The body of this response.
51: */
52: protected String getBody() {
53: return this.writer.toString();
54: }
55:
56: }
|