We're doing something similar in a filter, though for different reasons.
You have two ways to do it:
1. Quick and dirty. Set the reply buffer size to something large
and then it won't get flushed.

2. a. create a wrapper object that extends HttpServletResponseWrapper that
has its own "out" object.
   b. do chain.doFilter(request, wrapper)
         c. Now your data is writen to the wrapper. When the filter gets control
         back do something like this.
                 outWriter = response.getWriter();
                                         //print the string to the servlet's output
                                         CharArrayWriter caw = new CharArrayWriter();
                                         caw.write(wrapper.toString());

Jason Hunter has a good article about filters in javaworld. You might
want to look at it.

Regards,

Dror




On Thu, Feb 27, 2003 at 03:46:42AM -0800, David Orriss Jr wrote:
> Hello,
>
> I'm working on a project where we send XML data to servlets.  The data can
> be sent by a process that either opens a connection to the servlet and sends
> it directly (via an HTTP POST method), or as an uploaded file via a form on
> a web
> page.  Now, what we need to do is if the XML data is sent via direct
> connection and POST we send the data back as a "text/xml" content type.  If
> the data is 'uploaded' via a form we send the response back as a file and
> set the content-disposition like this:
>
> reply.getMimeHeaders().setHeader("content-disposition", "attachment;
> filename=download.xml");
>
> The problem is that we want to move this functionality into a filter.  That
> way we can have various servlets that can support the same capability
> without copying code.  The *problem* is that if the response data is over a
> certain size the content-dispostion is being ignored....  I inherited the
> filter code from another programmer, and what seems to be happening is that
> before we can change the content-disposition in the filter part of the data
> has already been flushed out.  My question is, how do I buffer or "hold
> back" the output stream that's now hitting the filter code on the way back
> to the client and modify the content-disposition accordingly.
>
> I've included code snippets of our approach so you can see what we're
> doing...  Any suggestions would be much appreciated.  I've thought about
> perhaps making a custom HttpServletResponseWrapper and possibly buffering
> the output there until the filter is ready to work with it
>
>     private String MULTIPART_CONTENT_TYPE = "multipart/form-data";
>     private String XML_CONTENT_TYPE = "text/xml";
>
> (in doFilter)
>
>     HttpServletRequest httpServletRequest = (HttpServletRequest) request;
>     String contentType = httpServletRequest.getContentType();
>     if (contentType == null) return;
>     boolean fileUploadDownload = contentType.indexOf(MULTIPART_CONTENT_TYPE)
> != -1;
>     //wrapperX extends HttpServletRequestWrapper and includes a few
> overridden bethods to
>     //support our efforts - among them are setContentType and setInputStream
>     WrapperX wrapperX = new WrapperX((HttpServletRequest)request);
>     if (fileUploadDownload) {
>         wrapperX.setContentType(XML_CONTENT_TYPE);
>         wrapperX.setInputStream(new
> WrapperS(getInputStream(httpServletRequest)));
>     }
>
>     filterChain.doFilter(wrapperX, response);
>
>     if (fileUploadDownload) {
>         HttpServletResponse httpServletResponse =
> (HttpServletResponse)response;
>         counter++;
>         httpServletResponse.setHeader("Content-Disposition:","attachment;
> filename=SubmitResponse_" + counter + ".xml" );
>
> System.out.println("getBufferSize()"+httpServletResponse.getBufferSize());
>     }
>
>
> --
> David Orriss Jr.
>
> Please email me if you want my ICQ/AIM/IM ID's.
>
> ___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
>

--
Dror Matalon
Zapatec Inc
1700 MLK Way
Berkeley, CA 94709
http://www.zapatec.com

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to