dev@jersey.java.net

Re: Direct OutputStream access

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Fri, 31 Aug 2007 20:52:21 -0400

On Aug 31, 2007, at 5:46 PM, Frank Martínez wrote:
>
> I think this is a good work based on a good specification.
> I was working on a propietary rest framework by the last 3 weeks, and
> then i found jsr311 and this project. I want to use this framework
> instead of develop the propietary ones, but there is one thing i wish
> in a rest framework, and is the ability to access the
> HttpServletResponse's OutputStream directly in some circumstances.
>
Thanks for the kind words. What you want to do is already possible
with a few caveats. In your resource class add a field as follows:

@UriTemeplate("someuri")
public MyResourceClass {

   @Resource
   HttpServletResponse servletResponse;

   @HttpMethod("GET")
   public void getSomething() {
     OutputStream o = servletResponse.getOutputStream();
     ...
   }

   ...
}

Jersey will inject a thread-local servletResponse when the resource
is created and you can use that to get an output stream as shown above.

Now for the caveats. Because of the way servlet works you'll have to
also write your own HTTP response headers before you start writing to
the output stream so any method that uses servletResponse in the way
you want should return void - you can't use the Response.Builder
mechanism. Also we haven't really tested this usage so you may well
run into some unforeseen problems. ResourceBean1 in the SimpleServlet
example uses the same mechanism to access the HttpServletRequest and
ServletConfig but not HttpServletRequest - please let us know how you
get on.

Another approach would be for you to wrap your query in an object
that implements InputStream and have your resource method return
that. There's already an EntityProvider for InputStream which will
pull the result data so you if you can invert the flow of control
that way then you'd be able to use the Response and Response.Builder
mechanism rather than having to do that yourself using
HttpServletResponse.

Regards,
Marc.

> For example:
> I have developed a tool that takes a hierarchical sql query (String)
> and an OutputStream and then executes the query and produces an xml
> output directly to the passed OutputStream. So I dont want to
> transform the SQL results in a graph of java objects and then
> serialize this again into xml using an EntityProvider .... and I dont
> want to get this result in a String and then return this string
> because it is not as good as the direct OutputStream writing (With
> large xml results)
>
> What do you think about that?
> Maybe if were possible to pass the output stream as an annotated
> method parameter like:
>
> @HttpMethod("GET")
> public void getParts(@Output OutputStream out, ...) {
> .....
> }
>
> Or something like that.
>
>
> --
> Frank D. Martínez M.
> Asimov Technologies Ltda.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: dev-help_at_jersey.dev.java.net
>

---
Marc Hadley <marc.hadley at sun.com>
CTO Office, Sun Microsystems.