RE: inconsistencies in Tomcat handling servlet filters...

2003-04-01 Thread Jacob Kjome
Many thanks to Jonathan Leech for pointing out how to fix my problem 
(below).  See the original thread here:
http://marc.theaimsgroup.com/?l=tomcat-user&m=104759543729238&w=2
and then the code I forgot to attach to the original message here:
http://marc.theaimsgroup.com/?l=tomcat-user&m=104759559429435&w=2

He asked me to forward it to the list since he isn't a subscribed 
member.  So, here it is (below).  Now the only issue left is why the third 
example I mentioned still doesn't work ...

Given this code

out.write("PRE");
CharResponseWrapper wrapper = new 
CharResponseWrapper((HttpServletResponse) response);
chain.doFilter(request, wrapper);
out.write(wrapper.toString());
out.write("POST");

It prints out everything for jsp pages but only the following for html page...

PRE

Lesson 3


This is a pure test page





The missing data is this...


POST
I guess this is the one last mystery to solve.   It must have something to 
do with not setting the content length, but why would it affect the 
filtering of html pages and not jsp pages?  H  Anyone have a guess 
as to why this is happening?

Jake


From: "Leech, Jonathan" <[EMAIL PROTECTED]>
To: "'[EMAIL PROTECTED]'" <[EMAIL PROTECTED]>
Subject: RE: inconsistencies in Tomcat handling servlet filters...
Date: Mon, 31 Mar 2003 11:19:57 -0700
Jake,

I think I can help you out  Do me a favor though and post/forward this
response to the tomcat mailing list because
I'm too lazy to figure out how to subscribe and reply properly.
The simple answer is you need to flush() your PrintWriter.  Do this:
GenericResponseWrapper.java:
PrintWriter writer = null;

public byte[] toByteArray() {
   if (writer != null) writer.flush();
   return output.toByteArray();
}
public String toString() {
   if (writer != null) writer.flush();
   return output.toString();
}
public PrintWriter getWriter() {
   writer = new PrintWriter(getOutputStream(), true);
   return writer;
}
> Questions to answer...
>
> 1.  Why don't all 3 examples produce exactly the same output?  It
> seems to me that they should.
The first example will work fine with the modifications I suggest above.
I don't know what's wrong in the third example, but perhaps it will work as
well.
> 2.  Why does using a response wrapper that uses a ByteArrayOutputStream
> (as in GenericResponseWrapper) not work with JSP's?  The original data
> is always unavailable.
flush()ing the PrintWriter cures this.  Incidentally, if your .jsp writes
enough data you will get some data but the rest truncated.
> 3.  Why do I have to use a PrintWriter when filtering JSP's?  Why
> can't I use an OutputStream?  Using an OutputStream gets me the same
results
> as #2.
The spec says in the Servlet you've got to use one or the other, but not
both.  You
should be able to relax this constraint in your filter if everything is
kosher.  Perhaps
flushing the PrintWriter properly will fix this as well.
> So, is there anything wrong with my code or is something just goofy in
> Tomcat?
Looks to me like your code would have worked just fine, as mine did, in
older versions
of Tomcat, but that they stopped flushing the PrintWriter for you after the
.jsp runs, at
some point between 4.0.3 and 4.1.18
Sincerely,
Jonathan Leech
Senior Software Engineer
Virtela Communications, Inc.


RE: inconsistencies in Tomcat handling servlet filters...

2003-03-14 Thread Jacob Kjome
Hi Yoav,

Actually, I already posted the code.  See..
http://marc.theaimsgroup.com/?l=tomcat-user&m=104759559429435&w=2
Also, in response to Kris Shneider, I have tried the compression filter 
that comes with the tomcat examples.  However, it is overly complex and 
shouldn't need to be.  The point of this current post was to simplify the 
discussion by providing a deadpan simple filter that should just 
work.  However, it only works under very specific condition of which I have 
detailed in the comments of my source.  There is no reason I shouldn't be 
able to use the servlet OutputStream in the filter or a 
ByteArrayOutputStream in the wrapper.  However, it seems that, for some 
reason, I must use a PrintWriter in the filter and a CharArrayWriter in the 
wrapper in order for things to work.  And then things still aren't 
consistently working between different file types such as static html and 
dynamic JSP/servlets.  That just doesn't make sense.

I would very much appreciate it if either one or both of you could try out 
the code I posted at the link above and verfiy my claims.

Thanks much for looking in this :-)

Jake

At 10:04 AM 3/14/2003 -0500, Shapira, Yoav wrote:

Howdy,
I saw the original message but never got around to commenting on it, as:
- I wrote a compression filter from scratch just to try it out, right
when servler 2.3 came out, and tomcat 4.0.4 had no problems with that
filter, all worked fine.  I tried this with JSPs, servlets, and static
content.
- I've been using the tomcat supplied compression filter since 4.1.10
without problems.  I only use this one for servlets, as I don't have
much JSP and all my static content is very small (tiny images).
That said, if you really like your own filter, perhaps you can post the
source for the two different response wrappers, and we can start there
in looking for bugs?
Yoav Shapira
Millennium ChemInformatics
>-Original Message-
>From: Jacob Kjome [mailto:[EMAIL PROTECTED]
>Sent: Friday, March 14, 2003 9:27 AM
>To: Tomcat Users List
>Subject: Re: inconsistencies in Tomcat handling servlet filters...
>
>
>
>No one has any opinions or comments on this?  I can't believe no one
else
>has run into this problem.
>
>Jake
>
>At 04:46 PM 3/13/2003 -0600, you wrote:
>
>>I've mentioned this issue before regarding a GZIPFilter that wasn't
>>working under Tomcat except for static file content (not working for
JSP's
>>and servlets).  However, I didn't get much response to that and it was
>>probably due to the complexity of the problem.  Well, here is a
simplied
>>approach to the issue
>>
>>I am seeing vastly differing behavior for this filter depending on
whether
>>it is filtering static content or JSP's and servlets and it seems to
me
>>that this differing behavior must be a bug in Tomcat.  Can someone
>>validate whether or not this is a Tomcat bug?
>>
>>Here is the code in question.  Note that I have some comments in the
code
>>that explain what works, what doesn't so read that to get an idea of
what
>>I am talking about.  I have attached all the code in question, but
this is
>>the meat of it (based on the tutorial at
>>http://www.orionserver.com/tutorials/filters/3.html ).
>>
>>public class PrePostFilter extends GenericFilter {
>>
>> public void doFilter(final ServletRequest request,
>>  final ServletResponse response,
>>  FilterChain chain)
>> throws IOException, ServletException
{
>>
>> PrintWriter out = response.getWriter();
>>
>> //works as expected for static html, but not for JSP's where
>> original content is discarded
>> //GenericResponseWrapper wrapper = new
>> GenericResponseWrapper((HttpServletResponse) response);
>>
>> //works as expected for both static html and JSP's
>> CharResponseWrapper wrapper = new
>> CharResponseWrapper((HttpServletResponse) response);
>>
>> chain.doFilter(request, wrapper);
>> String responseString = wrapper.toString();
>> responseString = "PRE" + responseString +
"POST";
>> response.setContentLength(responseString.length());
>> out.write(responseString);
>>
>>/*
>> //works as expected for JSP's, but not static html where
>> everything but the "POST" data is written
>> out.write("PRE");
>> CharResponseWrapper wrapper = new
>> CharResponseWrapper((HttpServletResponse) response);
>> chain.doFilter(request, wrapper);
>> out.write(wrapper.toString());

RE: inconsistencies in Tomcat handling servlet filters...

2003-03-14 Thread Shapira, Yoav

Howdy,
I saw the original message but never got around to commenting on it, as:

- I wrote a compression filter from scratch just to try it out, right
when servler 2.3 came out, and tomcat 4.0.4 had no problems with that
filter, all worked fine.  I tried this with JSPs, servlets, and static
content.

- I've been using the tomcat supplied compression filter since 4.1.10
without problems.  I only use this one for servlets, as I don't have
much JSP and all my static content is very small (tiny images).

That said, if you really like your own filter, perhaps you can post the
source for the two different response wrappers, and we can start there
in looking for bugs?


Yoav Shapira
Millennium ChemInformatics


>-Original Message-
>From: Jacob Kjome [mailto:[EMAIL PROTECTED]
>Sent: Friday, March 14, 2003 9:27 AM
>To: Tomcat Users List
>Subject: Re: inconsistencies in Tomcat handling servlet filters...
>
>
>
>No one has any opinions or comments on this?  I can't believe no one
else
>has run into this problem.
>
>Jake
>
>At 04:46 PM 3/13/2003 -0600, you wrote:
>
>>I've mentioned this issue before regarding a GZIPFilter that wasn't
>>working under Tomcat except for static file content (not working for
JSP's
>>and servlets).  However, I didn't get much response to that and it was
>>probably due to the complexity of the problem.  Well, here is a
simplied
>>approach to the issue
>>
>>I am seeing vastly differing behavior for this filter depending on
whether
>>it is filtering static content or JSP's and servlets and it seems to
me
>>that this differing behavior must be a bug in Tomcat.  Can someone
>>validate whether or not this is a Tomcat bug?
>>
>>Here is the code in question.  Note that I have some comments in the
code
>>that explain what works, what doesn't so read that to get an idea of
what
>>I am talking about.  I have attached all the code in question, but
this is
>>the meat of it (based on the tutorial at
>>http://www.orionserver.com/tutorials/filters/3.html ).
>>
>>public class PrePostFilter extends GenericFilter {
>>
>> public void doFilter(final ServletRequest request,
>>  final ServletResponse response,
>>  FilterChain chain)
>> throws IOException, ServletException
{
>>
>> PrintWriter out = response.getWriter();
>>
>> //works as expected for static html, but not for JSP's where
>> original content is discarded
>> //GenericResponseWrapper wrapper = new
>> GenericResponseWrapper((HttpServletResponse) response);
>>
>> //works as expected for both static html and JSP's
>> CharResponseWrapper wrapper = new
>> CharResponseWrapper((HttpServletResponse) response);
>>
>> chain.doFilter(request, wrapper);
>> String responseString = wrapper.toString();
>> responseString = "PRE" + responseString +
"POST";
>> response.setContentLength(responseString.length());
>> out.write(responseString);
>>
>>/*
>> //works as expected for JSP's, but not static html where
>> everything but the "POST" data is written
>> out.write("PRE");
>> CharResponseWrapper wrapper = new
>> CharResponseWrapper((HttpServletResponse) response);
>> chain.doFilter(request, wrapper);
>> out.write(wrapper.toString());
>> out.write("POST");
>>*/
>>
>> out.flush();
>> out.close();
>> }
>>
>>}
>>
>>Questions to answer...
>>
>>1.  Why don't all 3 examples produce exactly the same output?  It
seems to
>>me that they should.
>>2.  Why does using a response wrapper that uses a
ByteArrayOutputStream
>>(as in GenericResponseWrapper) not work with JSP's?  The original data
is
>>always unavailable.
>>3.  Why do I have to use a PrintWriter when filtering JSP's?  Why
can't I
>>use an OutputStream?  Using an OutputStream gets me the same results
as
>#2.
>>
>>So, is there anything wrong with my code or is something just goofy in
>Tomcat?
>>
>>Jake
>>
>>
>>-
>>To unsubscribe, e-mail: [EMAIL PROTECTED]
>>For additional commands, e-mail: [EMAIL PROTECTED]



This e-mail, including any attachments, is a confidential business communication, and 
may contain information that is confidential, proprietary and/or privileged.  This 
e-mail is intended only for the individual(s) to whom it is addressed, and may not be 
saved, copied, printed, disclosed or used by anyone else.  If you are not the(an) 
intended recipient, please immediately delete this e-mail from your computer system 
and notify the sender.  Thank you.


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



Re: inconsistencies in Tomcat handling servlet filters...

2003-03-14 Thread Kris Schneider
Have you tried comparing the behavior of your filter to the one provided with TC?

$CATALINA_HOME/webapps/examples/WEB-INF/classes/compressionFilters

Quoting Jacob Kjome <[EMAIL PROTECTED]>:

> 
> 
> No one has any opinions or comments on this?  I can't believe no one else 
> has run into this problem.
> 
> Jake
> 
> At 04:46 PM 3/13/2003 -0600, you wrote:
> 
> >I've mentioned this issue before regarding a GZIPFilter that wasn't 
> >working under Tomcat except for static file content (not working for JSP's
> 
> >and servlets).  However, I didn't get much response to that and it was 
> >probably due to the complexity of the problem.  Well, here is a simplied 
> >approach to the issue
> >
> >I am seeing vastly differing behavior for this filter depending on whether
> 
> >it is filtering static content or JSP's and servlets and it seems to me 
> >that this differing behavior must be a bug in Tomcat.  Can someone 
> >validate whether or not this is a Tomcat bug?
> >
> >Here is the code in question.  Note that I have some comments in the code 
> >that explain what works, what doesn't so read that to get an idea of what 
> >I am talking about.  I have attached all the code in question, but this is
> 
> >the meat of it (based on the tutorial at 
> >http://www.orionserver.com/tutorials/filters/3.html ).
> >
> >public class PrePostFilter extends GenericFilter {
> >
> > public void doFilter(final ServletRequest request,
> >  final ServletResponse response,
> >  FilterChain chain)
> > throws IOException, ServletException {
> >
> > PrintWriter out = response.getWriter();
> >
> > //works as expected for static html, but not for JSP's where 
> > original content is discarded
> > //GenericResponseWrapper wrapper = new 
> > GenericResponseWrapper((HttpServletResponse) response);
> >
> > //works as expected for both static html and JSP's
> > CharResponseWrapper wrapper = new 
> > CharResponseWrapper((HttpServletResponse) response);
> >
> > chain.doFilter(request, wrapper);
> > String responseString = wrapper.toString();
> > responseString = "PRE" + responseString + "POST";
> > response.setContentLength(responseString.length());
> > out.write(responseString);
> >
> >/*
> > //works as expected for JSP's, but not static html where 
> > everything but the "POST" data is written
> > out.write("PRE");
> > CharResponseWrapper wrapper = new 
> > CharResponseWrapper((HttpServletResponse) response);
> > chain.doFilter(request, wrapper);
> > out.write(wrapper.toString());
> > out.write("POST");
> >*/
> >
> > out.flush();
> > out.close();
> > }
> >
> >}
> >
> >Questions to answer...
> >
> >1.  Why don't all 3 examples produce exactly the same output?  It seems to
> 
> >me that they should.
> >2.  Why does using a response wrapper that uses a ByteArrayOutputStream 
> >(as in GenericResponseWrapper) not work with JSP's?  The original data is 
> >always unavailable.
> >3.  Why do I have to use a PrintWriter when filtering JSP's?  Why can't I 
> >use an OutputStream?  Using an OutputStream gets me the same results as
> #2.
> >
> >So, is there anything wrong with my code or is something just goofy in
> Tomcat?
> >
> >Jake
> >
> >
> >-
> >To unsubscribe, e-mail: [EMAIL PROTECTED]
> >For additional commands, e-mail: [EMAIL PROTECTED]
> 


-- 
Kris Schneider 
D.O.Tech   

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



Re: inconsistencies in Tomcat handling servlet filters...

2003-03-14 Thread Jacob Kjome


No one has any opinions or comments on this?  I can't believe no one else 
has run into this problem.

Jake

At 04:46 PM 3/13/2003 -0600, you wrote:

I've mentioned this issue before regarding a GZIPFilter that wasn't 
working under Tomcat except for static file content (not working for JSP's 
and servlets).  However, I didn't get much response to that and it was 
probably due to the complexity of the problem.  Well, here is a simplied 
approach to the issue

I am seeing vastly differing behavior for this filter depending on whether 
it is filtering static content or JSP's and servlets and it seems to me 
that this differing behavior must be a bug in Tomcat.  Can someone 
validate whether or not this is a Tomcat bug?

Here is the code in question.  Note that I have some comments in the code 
that explain what works, what doesn't so read that to get an idea of what 
I am talking about.  I have attached all the code in question, but this is 
the meat of it (based on the tutorial at 
http://www.orionserver.com/tutorials/filters/3.html ).

public class PrePostFilter extends GenericFilter {

public void doFilter(final ServletRequest request,
 final ServletResponse response,
 FilterChain chain)
throws IOException, ServletException {
PrintWriter out = response.getWriter();

//works as expected for static html, but not for JSP's where 
original content is discarded
//GenericResponseWrapper wrapper = new 
GenericResponseWrapper((HttpServletResponse) response);

//works as expected for both static html and JSP's
CharResponseWrapper wrapper = new 
CharResponseWrapper((HttpServletResponse) response);

chain.doFilter(request, wrapper);
String responseString = wrapper.toString();
responseString = "PRE" + responseString + "POST";
response.setContentLength(responseString.length());
out.write(responseString);
/*
//works as expected for JSP's, but not static html where 
everything but the "POST" data is written
out.write("PRE");
CharResponseWrapper wrapper = new 
CharResponseWrapper((HttpServletResponse) response);
chain.doFilter(request, wrapper);
out.write(wrapper.toString());
out.write("POST");
*/

out.flush();
out.close();
}
}

Questions to answer...

1.  Why don't all 3 examples produce exactly the same output?  It seems to 
me that they should.
2.  Why does using a response wrapper that uses a ByteArrayOutputStream 
(as in GenericResponseWrapper) not work with JSP's?  The original data is 
always unavailable.
3.  Why do I have to use a PrintWriter when filtering JSP's?  Why can't I 
use an OutputStream?  Using an OutputStream gets me the same results as #2.

So, is there anything wrong with my code or is something just goofy in Tomcat?

Jake

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


Re: inconsistencies in Tomcat handling servlet filters...

2003-03-13 Thread Jacob Kjome


Sorry, forgot the promised attachment...
Jake
At 04:46 PM 3/13/2003 -0600, you wrote:
I've mentioned this issue before
regarding a GZIPFilter that wasn't working under Tomcat except for static
file content (not working for JSP's and servlets).  However, I
didn't get much response to that and it was probably due to the
complexity of the problem.  Well, here is a simplied approach to the
issue
I am seeing vastly differing behavior for this filter depending on
whether it is filtering static content or JSP's and servlets and it seems
to me that this differing behavior must be a bug in Tomcat.  Can
someone validate whether or not this is a Tomcat bug?
Here is the code in question.  Note that I have some comments in the
code that explain what works, what doesn't so read that to get an idea of
what I am talking about.  I have attached all the code in question,
but this is the meat of it (based on the tutorial at
http://www.orionserver.com/tutorials/filters/3.html ).
public class PrePostFilter extends GenericFilter {
    public void doFilter(final ServletRequest
request,

final ServletResponse response,

FilterChain chain)
   
throws IOException, ServletException {
    PrintWriter out =
response.getWriter();
    //works as expected for static
html, but not for JSP's where original content is discarded
    //GenericResponseWrapper
wrapper = new GenericResponseWrapper((HttpServletResponse)
response);
    //works as expected for both
static html and JSP's
    CharResponseWrapper wrapper =
new CharResponseWrapper((HttpServletResponse) response);
    chain.doFilter(request,
wrapper);
    String responseString =
wrapper.toString();
    responseString =
"PRE" + responseString +
"POST";
   
response.setContentLength(responseString.length());
   
out.write(responseString);
/*
    //works as expected for JSP's,
but not static html where everything but the "POST" data is
written
   
out.write("PRE");
    CharResponseWrapper wrapper =
new CharResponseWrapper((HttpServletResponse) response);
    chain.doFilter(request,
wrapper);
   
out.write(wrapper.toString());
   
out.write("POST");
*/
    out.flush();
    out.close();
    }
}
Questions to answer...
1.  Why don't all 3 examples produce exactly the same output? 
It seems to me that they should.
2.  Why does using a response wrapper that uses a
ByteArrayOutputStream (as in GenericResponseWrapper) not work with
JSP's?  The original data is always unavailable.
3.  Why do I have to use a PrintWriter when filtering JSP's? 
Why can't I use an OutputStream?  Using an OutputStream gets me the
same results as #2.
So, is there anything wrong with my code or is something just goofy in
Tomcat?
Jake

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




fitlerstuff.zip
Description: Zip archive
-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]