tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Delbecq <delbd+jaka...@oma.be>
Subject Re: how to manipulate static content
Date Thu, 05 Jul 2007 07:59:23 GMT
The easiest way is to subclass HttpServletResponseWrapper.
Override the methods 'getOutputStream' and 'getWriter()' (not sure
getWriter is really used by static content serving, so you may not have
to code something there). Basically, make a ByteArrayOutputStream as the
result of getOutputStream, and have the writer use that stream.

After calling chain, response is buffered in byteArrayOutputStream, you
can chunk/zip it as you want and write result to real outputStream.

/*add headers*/
myResponse = new MyHttpServletResponseWrapper(response);
chain.doFilter(myResponse,request);
sendAsChunkOrZipOrWhatever(response.getOutputStream(),myResponse.getByteArrayOutputStream().toByteArray());
return;

also, look here
http://tomcat.apache.org/tomcat-5.0-doc/config/http.html
for the 'compression' attributes of http connector.

En l'instant précis du 05/07/07 08:37, lior grinfeld s'exprimait en ces
termes:
> Thanks Pierre,
>
> for sure i will use that info, but now i am not writing a server, as i
> wrote
> earlier, i use tomcat to test a proxy.
> i am interested to see how my proxy behave when i send it the data
> chunked
> or zip the data. and i want this to be in reference to regular files.
>
> so basically all i want is to use the default servlet to return static
> content in one test and afterwards to return the same content but this
> time
> it will be zip or use chunk to transfer the content.
>
> i do not want to write servlets to each one of the behaviors, that i
> why i
> thought that the idea of writing filter (by David) was good.
> if i could do some kind of HttpServletResponseWrapper (as i saw in the
> j2ee
> tutorial) it will be excellent.
>
> Regards
> Lior
>
> On 7/4/07, Pierre Goupil <goupilpierre@gmail.com> wrote:
>>
>> Lior,
>>
>> Basically, I think that you need to handle the response by yourself.
>>
>> For instance, if you want to send an image :
>>
>> ***
>> RenderedImage rimg = ImageIO.read(new File(img));
>>             OutputStream os = response.getOutputStream(); // <=== get
>> the
>> response to be able to re-write it
>>             ImageIO.write(rimg, "jpg", os);
>>             os.close();
>> ***
>>
>>
>> In my example I assumed that the MIME content-type was set to the
>> relevant
>> value, since you said that your headers were OK. But anyway, here is my
>> stuff to do so :
>>
>>
>> ***
>> response.setContentType("image/jpg");
>> ***
>>
>>
>>
>> But if you want to be able to send a file for the browser to download
>> it,
>> or
>> to be able to send several different types of data all in one response,
>> you
>> need a multi-part response :
>>
>>
>> ***
>> response.setHeader("Content-Disposition",
>> "attachment;filename=\"test.doc\
>> "");
>> // <=== here
>> response.setContentType("text/doc");
>> out = response.getWriter();
>> out.print("what you wish");
>>
>> response.setContentType("image/png");
>> out = response.getWriter();
>> out.print("what you wish too");
>> out.close();
>> ***
>>
>>
>> Be aware that without a multi-part response, you can only set one
>> content-type. If you try and set it a second time, Tomcat will give
>> you a
>> (beautiful) exception saying something like (can't remember exactly)
>> "content-type has already been set".
>>
>> Here is a list of MIME-types, FYI :
>>
>> http://plugindoc.mozdev.org/winmime.html
>>
>> With this technique of multi-part response, you can for instance turn a
>> JSP
>> to a Word file : you set the response type to .doc and let Tomcat render
>> its
>> JSP compiled to HTML into it. Then the browser received a .doc file with
>> regular HTML into it, which word is able to read... et voilà !
>>
>>
>> Regards,
>>
>>
>> Pierre
>>
>>
>>
>> 2007/7/4, lior grinfeld <lior.grinfeld@gmail.com>:
>> >
>> > you were right, 10X.
>> >
>> > now i am facing yet one more challenge.
>> > i made filter  to overwrite the response, i added headers as i want
>> and
>> it
>> > works fine.
>> > now i want to manipulate the content , to do zip or to chunk the data.
>> >
>> > i tried to do StreamResponseWrapper that extends
>> > HttpServletResponseWrapper, without success.
>> > any ideas on how to implement it?
>> >
>> > 10X
>> > Lior
>> >
>> >
>> > On 7/4/07, David Delbecq <delbd+jakarta@oma.be> wrote:
>> > >
>> > > Could it be you forgot to call
>> filterChain.doFilter(request.response)
>> > > after adding your headers?
>> > >
>> > > En l'instant précis du 04/07/07 14:18, lior grinfeld s'exprimait en
>> ces
>> > > termes:
>> > > > Thanks for the tip, i used filters.
>> > > >
>> > > > now i facing other problem. if I use filter with request to static
>> > > > content (
>> > > > i did not write servlet for it , i want the defaultServlet to
>> handle
>> > it)
>> > > > for some reason i do not get the content, it looks like the
>> > > > defaultServlet
>> > > > is doing nothing now, without filter it return the file i ask,
>> with
>> > > > filter
>> > > > just the data the filter added.
>> > > > any ideas?
>> > > >
>> > > > 10X
>> > > > Lior
>> > > >
>> > > > On 7/4/07, David Delbecq <delbd+jakarta@oma.be> wrote:
>> > > >>
>> > > >> Either map to '/*' your custom servlet that returning the static
>> > > content
>> > > >> with your custom headers, either write a servletfilter that add
>> > > >> additional header before processing request.
>> > > >>
>> > > >> En l'instant précis du 04/07/07 09:52, lior grinfeld
>> s'exprimait en
>> > ces
>> > > >> termes:
>> > > >> > Hi,
>> > > >> >
>> > > >> > i am new at using tomcat.
>> > > >> > I want to use tomcat as a server to test our proxy. to do
>> that i
>> > > >> need to
>> > > >> > manipulate the response headers, i did that without problems
>> using
>> > > >> > servlets.
>> > > >> > now i want to manipulate the http response headers or
>> content of
>> > > >> requests
>> > > >> > for static content.
>> > > >> > Ex. if i will do "GET myServer/somefile.txt" the tomcat will
>> know
>> > to
>> > > >> > return
>> > > >> > the file and manipulate the response http headers as i want,
or
>> to
>> > do
>> > > >> > zip or
>> > > >> > to do chunks on the content.
>> > > >> > is it possible and how? where can i configure tomcat to
>> refer to
>> > the
>> > > >> > directory where all my files are stored?
>> > > >>
>> > > >>
>> > > >>
>> ---------------------------------------------------------------------
>> > > >> To start a new topic, e-mail: users@tomcat.apache.org
>> > > >> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> > > >> For additional commands, e-mail: users-help@tomcat.apache.org
>> > > >>
>> > > >>
>> > > >
>> > > >
>> > >
>> > >
>> > >
>> > >
>> ---------------------------------------------------------------------
>> > > To start a new topic, e-mail: users@tomcat.apache.org
>> > > To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> > > For additional commands, e-mail: users-help@tomcat.apache.org
>> > >
>> > >
>> >
>> >
>> > --
>> >
>> > Regards
>> > Lior Grinfeld
>> >
>>
>>
>>
>> -- 
>> "Mon horizon était désormais les pétales courbés,
>> Ma nouvelle demeure resplendissait de flèches d'herbes folles
>> Et d'oreillers de pollens odorants."
>>
>> (Naïmah, le philosophe Fémereif)
>>
>
>
>


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


Mime
View raw message