users@jersey.java.net

Re: [Jersey] Accessing the response object (in standard JSR311)

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Tue, 06 Oct 2009 09:01:47 -0400

On Oct 5, 2009, at 1:23 PM, Jan Algermissen wrote:
>
> Given that HttpContext is a Jersey 'extension' - how would one
> access the response object if not using the ResponseBuilder pattern?
>
> E.g. if I had
>
> public StreamingOutput getResource(
> final @Context HttpContext hc, // Jersey
> specific class
> @PathParam("id") String id
> ) {
> return new StreamingOutput() {
> public void write(OutputStream outputStream) {
> Response resp = hc.getResponse();
> // do something with response, e.g. set headers
>
> PrintWriter out = new PrintWriter(outputStream);
> out.println("Foo");
> out.close();
> } };
> }
>
>
> How could I avoid the Jersey specific class?
>
If you just need to set some headers and can do so outside the context
of the StreamingOutput#write method then this should work:

public StreamingOutput getResource(
         @PathParam("id") String id
     ) {

     StreamingOutput strOut = new StreamingOutput() {
         public void write(OutputStream outputStream) {
             PrintWriter out = new PrintWriter(outputStream);
             out.println("Foo");
             out.close();
         } };
     return Response.ok(strOut).header(a,b).header(c,d).build();
}

Marc.