users@jersey.java.net

Re: [Jersey] StreamingOutput, multipart and boundary parameter

From: Jan Algermissen <algermissen1971_at_mac.com>
Date: Thu, 01 Oct 2009 19:03:18 +0200

Paul,

I have moved to a fressh Eclipse Project. Now it works fine.

Still odd, though :-)

Jan


On Oct 1, 2009, at 2:38 PM, Paul Sandoz wrote:

>
> On Oct 1, 2009, at 2:27 PM, Jan Algermissen wrote:
>
>> Paul,
>>
>> I created another method (note the media type!!)
>>
>> @GET @Produces("text/plainx")
>> public Response getResource() {
>> StreamingOutput o = new StreamingOutput() {
>> public void write(OutputStream outputStream) {
>> PrintWriter out = new PrintWriter(outputStream);
>> out.println("AAAAA");
>> out.close();
>>
>> } };
>> return Response.ok(o).build();
>> }
>>
>>
>> This works. If I use text/plain the resource class name is
>> returned as output.
>>
>
> That is really odd. There is no functionality in Jersey that does
> that.
>
>
>> Hmmm - that leaves me clueless I guess :-)
>>
>>
>> Any idea?
>>
>
> Can you send me a zip of the maven project and i can experiment?
>
> Paul.
>
>> Jan
>>
>> P.S. here is the complete class:
>>
>> package foo.resources;
>> import java.io.OutputStream;
>> import java.io.PrintWriter;
>> import javax.ws.rs.*;
>> import javax.ws.rs.core.*;
>>
>> @Path("/worksets/{worksetId}/{timestamp}/readwrite")
>> public class WorkSetRetrieve {
>>
>> @GET @Produces("text/plainx")
>> public Response getResource() {
>> StreamingOutput o = new StreamingOutput() {
>> public void write(OutputStream outputStream) {
>> PrintWriter out = new PrintWriter(outputStream);
>> out.println("AAAAA");
>> out.close();
>>
>> } };
>> return Response.ok(o).build();
>> }
>> }
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> On Oct 1, 2009, at 12:44 PM, Paul Sandoz wrote:
>>
>>>
>>> On Oct 1, 2009, at 12:35 PM, Jan Algermissen wrote:
>>>>
>>>>
>>>>>
>>>>> If you are not using a message body writer you can set the media
>>>>> type in a returned Response instance e.g. if you are using an
>>>>> instance of StreamingOutput for the entity.
>>>>>
>>>>> Paul.
>>>>>
>>>>> // Determine the boundary string to be used, creating one if
>>>>> needed
>>>>> MediaType entityMediaType = (MediaType)
>>>>> headers.getFirst("Content-Type");
>>>>> if (entityMediaType == null) {
>>>>> Map<String, String> parameters = new HashMap<String,
>>>>> String>();
>>>>> parameters.put("boundary", createBoundary());
>>>>> entityMediaType = new MediaType("multipart", "mixed",
>>>>> parameters);
>>>>> headers.putSingle("Content-Type", entityMediaType);
>>>>> }
>>>>> String boundaryString =
>>>>> entityMediaType.getParameters().get("boundary");
>>>>> if (boundaryString == null) {
>>>>> boundaryString = createBoundary();
>>>>> Map<String, String> parameters = new HashMap<String,
>>>>> String>();
>>>>> parameters.putAll(entityMediaType.getParameters());
>>>>> parameters.put("boundary", boundaryString);
>>>>> entityMediaType = new MediaType(entityMediaType.getType(),
>>>>> entityMediaType.getSubtype(),
>>>>> parameters);
>>>>> headers.putSingle("Content-Type", entityMediaType);
>>>>> }
>>>>>
>>>>
>>>> I think I will use a MessageBodyWriter but anyway, if I don't -
>>>> where would I put the code you sent? at the beginning of the
>>>> write method?
>>>>
>>>> public StreamingOutput getResource(
>>>> @PathParam("id") String id
>>>> ) {
>>>> return new StreamingOutput() {
>>>> public void write(OutputStream outputStream) {
>>>> // PUT CODE HERE???
>>>> PrintWriter out = new PrintWriter(outputStream);
>>>> out.println("Foo");
>>>> out.close();
>>>> } };
>>>> }
>>>>
>>>
>>> You could do:
>>>
>>> public Response getResource(...) {
>>> MediaType m = ... // create media type with boundary string
>>>
>>> StreamingOutput o = ...
>>>
>>> return Response.ok(o, m).build();
>>> }
>>>
>>>
>>> Or:
>>>
>>> public StreamingOutput getResource(
>>> final @Context HttpContext hc, // Jersey
>>> specific class
>>> @PathParam("id") String id
>>> ) {
>>> return new StreamingOutput() {
>>> public void write(OutputStream outputStream) {
>>> // This will be null if there is no @Produces
>>> MediaType ct =
>>> hc.getResponse().getMediaType();
>>> // Set media type with boundary
>>> MediaType ctWithBoundary = ...
>>>
>>> hc.getResponse().getHttpHeaders().put("Content-Type",
>>> ctWithBoundary);
>>>
>>> PrintWriter out = new PrintWriter(outputStream);
>>> out.println("Foo");
>>> out.close();
>>> } };
>>> }
>>>
>>> Paul.
>>>
>>
>> --------------------------------------
>> Jan Algermissen
>>
>> Mail: algermissen_at_acm.org
>> Blog: http://algermissen.blogspot.com/
>> Home: http://www.jalgermissen.com
>> --------------------------------------
>>
>>
>>
>

--------------------------------------
Jan Algermissen

Mail: algermissen_at_acm.org
Blog: http://algermissen.blogspot.com/
Home: http://www.jalgermissen.com
--------------------------------------