Christopher Schultz schrieb am 18.11.2008 um 16:26:23 (-0500):

> > public class HttpResponseCatcher extends HttpServletResponseWrapper {
> > 
> >  private ByteArrayOutputStream buffer;
> > 
> >  public HttpResponseCatcher( HttpServletResponse res) {
> >   super( res);
> >   this.buffer = new ByteArrayOutputStream();
> >  }
> > 
> >  public ServletOutputStream getOutputStream() throws IOException {
> >   this.getResponse().getOutputStream();
> >   return new CapturedServletOutputStream( this.buffer);
> >  }

> >  public PrintWriter getWriter() throws IOException {
> >   this.getResponse().getWriter();
> >   return new PrintWriter(
> >     new OutputStreamWriter(
> >      new CapturedServletOutputStream( this.buffer)));
> >  }
> 
> You might actually want to use a single CapturedServletOutputStream or
> OutputStreamWriter for all calls, rather than constructing a new one
> each time. Otherwise, you might get strange behavior and weird output
> ordering.

This seems to have caused the effect observed, which was the absence of
the HTML file included via RequestDispatcher.include() in both the
buffer used to capture the response and the output itself.

Might this have to do with the fact that the PrintWriter is buffered?

I'm attaching the new version, which seems to work.

> >  public void flushBuffer() throws IOException {
> >   this.buffer.flush();
> >  }
> 
> Flushing a ByteArrayOutputStream doesn't do anything. What you really
> want to do is flush all the OutputStream and Writer objects you've
> created when calls to getOutputStream and getWriter come in.

I dropped this method.

> >  public String toString()     { return this.buffer.toString(); }
> 
> Although this will work, it might be surprising to users of your class.
> I would like a method such as "getCapturedOutput" instead.

I renamed this one.

Thanks for your help, Christopher and Chuck!

Michael Ludwig

package milu;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HttpResponseCatcher extends HttpServletResponseWrapper {

 private OutputStream buffer;
 private CapturedServletOutputStream stream;
 private PrintWriter writer;

 public HttpResponseCatcher( HttpServletResponse res) {
  super( res);
  this.buffer = new ByteArrayOutputStream();
  this.stream = new CapturedServletOutputStream( this.buffer);
  this.writer = new PrintWriter( new OutputStreamWriter( this.stream));
 }

 public ServletOutputStream getOutputStream() throws IOException {
  getResponse().getOutputStream();
  return stream;
 }

 public PrintWriter getWriter() throws IOException {
  getResponse().getWriter();
  /* Nicht jedesmal einen neuen PrintWriter erzeugen, sonst k÷nnen
   * Daten verloren gehen. */
  // return new PrintWriter( new OutputStreamWriter( stream));
  // return new PrintWriter( new OutputStreamWriter( stream), true);
  // The above two lines may lead to incomplete output as explained.
  return writer; // This seems to be correct.
 }

 public String getCapturedOutput() { return buffer.toString(); }
 public byte[] getByteArray() { return buffer.toString().getBytes(); }
 public char[] getCharArray() { return buffer.toString().toCharArray(); }
}

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to