Christopher Schultz schrieb am 16.11.2008 um 19:43:00 (-0500):
> Michael Ludwig wrote:
> > Christopher Schultz schrieb am 12.11.2008 um 14:46:08 (-0500):
> >> What you need to do is provide a unified buffer that /both/ calls
> >> can write to. If you use a ByteArrayOutputStream at the lowest
> >> level and then wrap that in an OutputStreamWriter for calls to
> >> getWriter(), you should be able to handle arbitrary use of your
> >> wrapper.
> > 
> > This sounds very reasonable. I followed this road. Unfortunately,
> > the included resource still is written elsewhere. Or maybe not
> > flushed?
> 
> What do you mean, it's still written elsewhere?

Christopher,

the included HTML snippet handled by the DefaultServlet does not appear
in the output when done via the PrintWriter. I don't know where it ends
up being written in that case. I assume it is being written elsewhere,
rather than not being written at all.

> If you can wrap the object that's being called (like the request, or
> the output stream/writer) then you can write your own logging
> statements at appropriate times.

It's not clear to me which methods I have to implement in order to see
the included static resource processed by Tomcat's DefaultServlet appear
in the output regardless of the output method (PW or SOS) being chosen.

I can see that in the case of the PrintWriter, the output lacks the
included static resource. I suspect the bug somewhere between my filter
and my servlet. Now in order to determine where exactly it is, I thought
it helpful to have a trace of the methods getting called, particularly
for the processing of the included resource by the DefaultServlet. It
may be that I'm not implementing a method I need to implement, and in
that case writing my own logging statements wouldn't tell the whole
story.

So if this is possible, I'd like to know how to enable this or, of
course, be told in what ways my thinking is mistaken, or simply besides
the point.

To give you more food for the latter option in case you're curious
enough, here's the code, six files altogether. At any rate, thanks
a lot for your continued interest!

Michael Ludwig

* ../include.html
* web.xml
* WEB-INF/src/milu/IncludeServlet.java
* WEB-INF/src/milu/GuFilter.java
* WEB-INF/src/milu/HttpResponseCatcher.java
* WEB-INF/src/milu/CapturedServletOutputStream.java

:: more /t1 ..\include.html
<p>Included text</p>

:: more /t1 web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee";
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd";
 version="2.5">

 <servlet>
  <servlet-name>include</servlet-name>
  <servlet-class>milu.IncludeServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>include</servlet-name>
  <url-pattern>/include</url-pattern>
 </servlet-mapping>

 <servlet>
  <servlet-name>include2</servlet-name>
  <servlet-class>milu.IncludeServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>include2</servlet-name>
  <url-pattern>/include2</url-pattern>
 </servlet-mapping>

 <filter>
  <filter-name>Gu</filter-name>
  <filter-class>milu.GuFilter</filter-class>
 </filter>

 <filter-mapping>
  <filter-name>Gu</filter-name>
  <servlet-name>include</servlet-name>
 </filter-mapping>

</web-app>

:: more /t1 src\milu\IncludeServlet.java
package milu;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class IncludeServlet extends HttpServlet {
 public void doGet (HttpServletRequest req, HttpServletResponse res)
  throws ServletException, IOException
 {
  res.setContentType("text/html; charset=UTF-8");
  PrintWriter out = res.getWriter(); // include doesn't appear in output
  // ServletOutputStream out = res.getOutputStream(); // works
  out.println( "<html><p>Before include</p>");
  RequestDispatcher rd =
   this.getServletContext().getRequestDispatcher( "/include.html");
  rd.include( req, res);
  out.println( "<p>After include</p></html>");
  out.close();
 }
}

:: more /t1 src\milu\GuFilter.java
package milu;

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

public final class GuFilter implements Filter {

 private FilterConfig config;
 public void init( FilterConfig config) { this.config = config; }
 public void destroy()                  { this.config = null;   }

 public void doFilter(
   ServletRequest req, ServletResponse res, FilterChain chain)
  throws IOException, ServletException
 {
  if ( res instanceof HttpServletResponse ) {
   ( (HttpServletResponse) res).setHeader( "Gurke", "eingelegt");

   HttpResponseCatcher wrapper =
    new HttpResponseCatcher( (HttpServletResponse) res);
   chain.doFilter( req, wrapper);

   String s = "<!-- Huhu -->" + wrapper.toString();
   ( (HttpServletResponse) res).setHeader(
     "Zeichen", Integer.toString( s.length()));

   try {
    ServletOutputStream out = res.getOutputStream();
    out.print( s);
    out.close();
   }
   catch ( IllegalStateException ise ) {
    PrintWriter out = res.getWriter();
    out.write( s);
    out.close();
   }
  }
  else {
   chain.doFilter( req, res);
  }
 }
}

:: more /t1 src\milu\HttpResponseCatcher.java
package milu;

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

public class HttpResponseCatcher extends HttpServletResponseWrapper {

 private ByteArrayOutputStream buffer;

 public HttpResponseCatcher( HttpServletResponse res) {
  super( res);
  this.buffer = new ByteArrayOutputStream();
 }

 public ServletOutputStream getOutputStream() throws IOException {
  new Throwable("######## STROM").printStackTrace();
  // siehe DefaultServlet.serveResource(), dort Aufruf von getOutputStream()
  this.getResponse().getOutputStream();
  return new CapturedServletOutputStream( this.buffer);
 }

 public PrintWriter getWriter() throws IOException {
  new Throwable("######## SCHREIBER").printStackTrace();
  this.getResponse().getWriter();
  return new PrintWriter(
    new OutputStreamWriter(
     new CapturedServletOutputStream( this.buffer)));
 }

 // http://marc.info/?l=tomcat-user&m=109913615025298
 public void flushBuffer() throws IOException {
  this.buffer.flush();
 }

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

:: more /t1 src\milu\CapturedServletOutputStream.java
package milu;

import java.io.IOException;
import java.io.ByteArrayOutputStream;
import javax.servlet.ServletOutputStream;

public class CapturedServletOutputStream extends ServletOutputStream {

 ByteArrayOutputStream buffer; // Puffern, was geschrieben wird.

 CapturedServletOutputStream( ByteArrayOutputStream buf) {
  this.buffer = buf;
 }

 public void write( int b) throws IOException {
  this.buffer.write( b);
 }
 public void write( byte[] b) throws IOException {
  new Throwable().printStackTrace();
  this.buffer.write( b);
 }
 public void write( byte[] b, int off, int len) throws IOException {
  new Throwable().printStackTrace();
  this.buffer.write( b, off, len);
 }
 public void flush() throws IOException {
  this.buffer.flush();
 }
 public void close() throws IOException {
  this.buffer.close();
 }
}

---------------------------------------------------------------------
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