streams problems in filter chains...

T

Thea

Hello.
I have some problem with application I am writing.
I have filter chain (classes implement interface Filter from
javax.servlet).
My problem is about handling streams from request and response.
In my chain I have two filters.
First filter in its doFilter() method retrieves file (image) from
database as an input stream and copies it contents into response's
output stream.
My second filter needs to act upon retrieved file...
And here the problem appears.
I tried googling, I tried reading manuals, but only got confused... And
now I'm not even trying to pretend I understand a thing...
I have structure like this:

currentFilter extends Filter{
doFilter(ServletRequest request, ServletResponse response, FilterChain
filterChain)
//do some request processing to get some params - only reading those
filterChain.doFilter();

}

As far as I understand if I call filterChain.doFilter() filter
underlying current will be executed.
Image should be in response's output stream, am I right?
If yes, how can I transform this output stream into input stream? I
need that because this filter will work on data and needs to read them
somehow....

I also tried something like this:
MyServletResponse myresp= new MyServletResponse(resp);
filterChain.doFilter(req,myresp);
where

public class MyServletResponse implements HttpServletResponse {
final HttpServletResponse wrapped;
InputStream input=null;

MyServletResponse(HttpServletResponse realResponse) {
this.wrapped = realResponse;
try {
log.debug(wrapped.getOutputStream());
ImageMagickProcessor im = new
ImageMagickProcessor(wrapped.getOutputStream(), input, command);
im.processImageMagick();
} catch (IOException e) {
log.error("Exception occured!",e);
}
}
public ServletOutputStream getOutputStream() throws IOException {

return wrapped.getOutputStream();
}
this is one of many trials...
ImageMagickProcessor is a class executing some console commands during
runtime. It processes given image and outputs results.
It accepts ouput stream and input stream and commnad.
I seem to be unable to supply it with proper input stream...

I'm sorry if my explanation is unclear, but I am really confused...
-_-'
I'll try to make it as simple as I can:
I have some filters in a chain.
I want to work in one filter on output of another filter.
In this particular filter I need to pipe data obtained from previous
filter(s) into ImageMagickProcessor.
Therefore I need proper streams. And I have no idea how to get them.
I'm not even sure which streams are proper...
Just ask me if you need some more info...

Help, please ^^
 
B

Babu Kalakrishnan

Thea said:
I have filter chain (classes implement interface Filter from
javax.servlet).
My problem is about handling streams from request and response.
In my chain I have two filters.
First filter in its doFilter() method retrieves file (image) from
database as an input stream and copies it contents into response's
output stream.

I think you haven't really understood the purpose of servlet filters.
The task that you describe above - (reading from the database and
writing out its contents) should ideally be performed in a servlet -
not a filter.
My second filter needs to act upon retrieved file...

OK - this is something that does fit into a "filter" pattern.

[snip]
As far as I understand if I call filterChain.doFilter() filter
underlying current will be executed.
Image should be in response's output stream, am I right?

The filterChain.doFilter method takes two arguments - A request and a
response. From the filter's point of view, this call invokes the actual
web application (the servlet), so any output that is written by the
servlet (or any filters that come later in the chain - you shouldn't be
really concerned about these) will be written to the outputstream of
the response object that you had passed as the parameter to this call.
If yes, how can I transform this output stream into input stream? I
need that because this filter will work on data and needs to read them
somehow....

I think you really need to read some of the tutorial available on the
web. For starters, try :

http://java.sun.com/products/servlet/Filters.html

Look at the examples there, and specifically the ones that use an
HttpResponseWrapper. The important points to note there are :

a) The argument that is passed in the call to doFilter() is not the
original response object that you received, but a wrapper around it.

b) generally the getInputStream() and/or getWriter() methods of this
wrapper class are overridden to return some custom streams / writers.
This provides a means for you to capture the output written by the
servlet, and do some processing.

For example, you could create a new instance of ByteArrayOutputStream
and return this from the overridden getOutputStream method - in which
case everything written by the servlet will be captured into a byte
array and can be accessed by you. When the doFilter call returns, you
could take the contents of this array, do some processing on it, and
write out this processed data into the actual outputstream of the
original response. (the one that you obtain by calling getOutputStream
on the original response object)

There are a lot of examples of servlet filters available on the web -
looking at the source code of these and trying to understand them is
probably the best way to learn about how to implement your own filters.

BK
 
T

Thea

Thankies ^^
That helped

Babu Kalakrishnan napisal(a):
Thea said:
I have filter chain (classes implement interface Filter from
javax.servlet).
My problem is about handling streams from request and response.
In my chain I have two filters.
First filter in its doFilter() method retrieves file (image) from
database as an input stream and copies it contents into response's
output stream.

I think you haven't really understood the purpose of servlet filters.
The task that you describe above - (reading from the database and
writing out its contents) should ideally be performed in a servlet -
not a filter.
My second filter needs to act upon retrieved file...

OK - this is something that does fit into a "filter" pattern.

[snip]
As far as I understand if I call filterChain.doFilter() filter
underlying current will be executed.
Image should be in response's output stream, am I right?

The filterChain.doFilter method takes two arguments - A request and a
response. From the filter's point of view, this call invokes the actual
web application (the servlet), so any output that is written by the
servlet (or any filters that come later in the chain - you shouldn't be
really concerned about these) will be written to the outputstream of
the response object that you had passed as the parameter to this call.
If yes, how can I transform this output stream into input stream? I
need that because this filter will work on data and needs to read them
somehow....

I think you really need to read some of the tutorial available on the
web. For starters, try :

http://java.sun.com/products/servlet/Filters.html

Look at the examples there, and specifically the ones that use an
HttpResponseWrapper. The important points to note there are :

a) The argument that is passed in the call to doFilter() is not the
original response object that you received, but a wrapper around it.

b) generally the getInputStream() and/or getWriter() methods of this
wrapper class are overridden to return some custom streams / writers.
This provides a means for you to capture the output written by the
servlet, and do some processing.

For example, you could create a new instance of ByteArrayOutputStream
and return this from the overridden getOutputStream method - in which
case everything written by the servlet will be captured into a byte
array and can be accessed by you. When the doFilter call returns, you
could take the contents of this array, do some processing on it, and
write out this processed data into the actual outputstream of the
original response. (the one that you obtain by calling getOutputStream
on the original response object)

There are a lot of examples of servlet filters available on the web -
looking at the source code of these and trying to understand them is
probably the best way to learn about how to implement your own filters.

BK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,732
Latest member
ArronPalin

Latest Threads

Top