Ha, right, forgot to detail it. It's a custom extension of
HttpServletResponseWrapper that overrides the 
getOutputStream and getWriter methods to force the JSP engine to write in a
dedicated buffer.

It's not published in any opensource project, but here is my code. It's
really not optimized, as it doesn't even stream the data, but instead puts
everything into memory. But you got the idea.


/**
 * This class implements a buffer that exposes a ServletResponse interface.
 * This is used to trick the JspEngine into dumping its results in
 * a buffer, rather than directly onto the wire.
 * <p/>
 * Only one of the two methods (getWriter or getOutputStream) can be called
 * during the lifecycle of a wrapper instance.
 *
 */
public class BufferedServletResponseWrapper
    extends HttpServletResponseWrapper
{

    private StringWriter writer;
    private PrintWriter printWriter;
    private ByteArrayOutputStream outBuffer;
    private ServletOutputStream out;

    public BufferedServletResponseWrapper(HttpServletResponse
httpServletResponse) {
        super(httpServletResponse);
    }

    public ServletOutputStream getOutputStream() throws IOException {
        if (printWriter != null) {
            throw new UnsupportedOperationException("Outputstream cannot be
obtained - getWriter already called");
        }

        if (out == null) {
            outBuffer = new ByteArrayOutputStream();
            out = new ServletOutputStream() {
                public void write(int b) throws IOException {
                    outBuffer.write(b);
                }
            };
        }
        return out;
    }

    public PrintWriter getWriter() throws IOException {

        if (out != null) {
            throw new UnsupportedOperationException("Writer cannot be
obtained - getOutputStream already called");
        }

        if (printWriter == null) {
            writer = new StringWriter();
            printWriter = new PrintWriter(writer);
        }
        return printWriter;
    }

    /**
     * This method is not supported, as it makes no sense to send a redirect
     * on a ServletResponse that is not connected.
     *
     * @param s
     * @throws UnsupportedOperationException always
     */
    public void sendRedirect(String s) throws IOException {
        throw new UnsupportedOperationException("sendRedirect no supported
in restlet proxy");
    }

    /**
     * Returns the content of the buffer as a string
     *
     * @return the content of the buffer
     */
    public InputStream toInputStream() {
        if (printWriter != null) {
            printWriter.flush();
            return new
ByteArrayInputStream(writer.getBuffer().toString().getBytes());
        }
        
        //TODO here we ignore the encoding of the page in the call to
toString()
        else if (out != null) {
            return new ByteArrayInputStream(outBuffer.toByteArray());
        } else {
            return new ByteArrayInputStream(new byte[]{});
        }
    }
}


regards,

--p.


Rob Heittman wrote:
> 
> Neat, Philippe ... is that BufferedServletResponseWrapper already
> open-sourced anywhere?
> 
> On Thu, Apr 23, 2009 at 10:02 AM, Philippe Duchesne
> <[email protected]>wrote:
> 
>> Hi all,
>>
>> i've faced a similar problem, so let me share how i solved it.
>>
> 
> ------------------------------------------------------
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=1880242
> 

-- 
View this message in context: 
http://n2.nabble.com/JSP-Servlet-Representation-tp2384688p2692567.html
Sent from the Restlet Discuss mailing list archive at Nabble.com.

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=1892614

Reply via email to