package org.gomba;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
/**
* This class wraps an existing HttpServletResponse and captures its body in a
* private Writer. The body can be accessed by calling getBody().
*
* @author Flavio Tordini
* @version $Id: CustomHttpServletResponseWrapper.java,v 1.2 2004/09/16 09:23:16 flaviotordini Exp $
*/
class CustomHttpServletResponseWrapper extends HttpServletResponseWrapper {
private final Writer writer;
/**
* Constructor.
*/
public CustomHttpServletResponseWrapper(HttpServletResponse response) {
super(response);
// we could also use a StringWriter...
// what's the difference?
this.writer = new CharArrayWriter();
}
/**
* @see javax.servlet.ServletResponse#getOutputStream()
*/
public ServletOutputStream getOutputStream() throws IOException {
throw new IOException(
"This response output stream should not be accessed.");
}
/**
* @see javax.servlet.ServletResponse#getWriter()
*/
public PrintWriter getWriter() throws IOException {
return new PrintWriter(this.writer);
}
/**
* @return The body of this response.
*/
protected String getBody() {
return this.writer.toString();
}
}