Re: Filter, HttpServletResponseWrapper, ServletOutputStream PrintWriter in Tomcat 5.x

2004-10-31 Thread Giampaolo Tomassoni
Never mind: was a buffering issue.

The JspWriter uses HttpServletResponseWrapper#flushBuffer() call, which I 
didn't override.

Thanks anyway,

Giampaolo

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Filter, HttpServletResponseWrapper, ServletOutputStream PrintWriter in Tomcat 5.x

2004-10-30 Thread Giampaolo Tomassoni
Dears,

I'm having troubles in doing a page hit counter Filter wrapping 
HttpServletResponse(s) with the purpose of obtaining the count of emitted 
bytes.

In my specialization of the HttpServletResponseWrapper I basicly catch 
getOutputStream() requests wrapping the ServletOutputStream returned by the 
Tomcat 5.x implementation to a specialized version of ServletOutputStream 
(see below). getWriter() request are catched as well returning a PrintWriter 
instance over my specialized ServletOutputStream (see below, too).

The problem is that, while this works for servlets invoking getOutputStream(), 
it doesn't for servlets (or, at least, jsp) which invoke getWriter(): no 
CounterOutputStream's byte write() methods are invoked during the jsp 
processing, the output stream received by the browser is empty with a 200 
status code.

I can't find why my code is acting this way. Do you have any clue to spare?

Finally, a cultural question. I see that the ServletOutputStream specification 
is somehow a mix between an OutputStream (infact, it extends it) and, 
basicly, a PrintWriter (it has a lot of print(), println() and string-base 
write() methods which, I guess, shouldn't be there). Does anybody know why 
ServletOutputStream is so messy? Hystorical reasons?

Thank you in advance for you attentions,

Giampaolo Tomassoni


-- code --

protected static class CounterResponseWrapper
extends HttpServletResponseWrapper {
protected interface Countable {
public int getCount();
};

protected static class CounterOutputStream
extends ServletOutputStream 
implements Countable {
public ServletOutputStream sos;

public void write(byte[] b, int off, int len)
throws IOException {
sos.write(b, off, len);
nWrittenBytes += len;
}

public void write(byte[] b)
throws IOException {
sos.write(b);
nWrittenBytes += b.length;
}

public void write(int b)
throws IOException {
sos.write(b);
nWrittenBytes++;
}

public void flush()
throws IOException
{ sos.flush(); }

public void close()
throws IOException
{ sos.close(); }


public int nWrittenBytes;

public int getCount()
{ return(nWrittenBytes); }


public CounterOutputStream(ServletOutputStream sos) {
super();
this.sos = sos;
nWrittenBytes = 0;
}
}

...

public Countablecountable = null;

public int  getCount()
{ return(countable == null ? 0 : countable.getCount()); }

public ServletOutputStream getOutputStream()
throws IOException {
if(countable != null)
// This is to accomplish servlet specifications
throw new IllegalStateException();

CounterOutputStream cos = new 
CounterOutputStream(super.getOutputStream());
countable = cos;
return(cos);
}

public PrintWriter getWriter()
throws IOException {
return(
new PrintWriter(
new OutputStreamWriter(
getOutputStream(),
getCharacterEncoding()
),
true
)
);
}

...
}


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Further details

2004-10-30 Thread Giampaolo Tomassoni
I got the feeling that has something to do with the JspWriter implementation 
of Tomcat 5.x, rather then with ServletOutputStream and PrintWriter: 
filtering a servlet invoking getWriter() to do its output works. It doesn't 
with jsp(s), which instead rely on JspWriter.

In my JspWriter API doc, I read the following: 

 The initial JspWriter object is associated with the PrintWriter object of
the ServletResponse... which will be created if necessary by invoking the
getWriter() method on the response object.

So it seems that a JspWriter implementation shall rely on the getWriter() call 
to obtain a PrintWriter (and, in fact, my overriding version of getWriter() 
gets called...).

Putting a [EMAIL PROTECTED] autoFlush=true/false% doesn't help either.

Any idea?

Regards everybody,

Giampaolo Tomassoni


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]