users@jersey.java.net

Re: [Jersey] sending a byte stream to client

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Wed, 17 Jun 2009 08:32:48 -0400

You can also return an instance of javax.ws.rs.core.StreamingOutput if
you want to write the bytes to an output stream yourself:

https://jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/core/StreamingOutput.html

Marc.

On Jun 17, 2009, at 6:00 AM, Paul Sandoz wrote:

> Hi,
>
> Change your resource method to be as follows:
>
> ByteArrayOutputStream oStream = new ByteArrayOutputStream();
> long checkSum = FileCompressionUtils.compressFiles(fileSet,
> oStream, comment);
> if (checkSum == 0L) {
> resp = Response.noContent().build();
> } else {
> resp = Response.ok(
> oStream.toByteArray(),
> MediaType.APPLICATION_OCTET_STREAM_TYPE).build();
> }
>
>
> The writing of an OutputStream, nor a sub-class of, is not
> supported. Thus you are getting a server-side error that states that
> the type ByteArrayOutputStream is not supported.
>
> You can utilize an InputStream, or a sub-class of, because
> information can be obtained from it, namely the bytes, but that is
> not the case for OutputStream.
>
> I could add support specifically for ByteArrayOutputStream, but it
> is not possible to support OutputStream in general (for reasons
> described above).
>
> Paul.
>
>
> On Jun 17, 2009, at 2:11 AM, Saavedra, Gisella wrote:
>
>> I have the REST service method code below. When I check the status
>> just before the bytes are sent out, it is OK. But at the client
>> side I get an HTTP error of 500.
>> This is my client call:
>>
>> InputStream iStream = res.accept(
>> MediaType.APPLICATION_OCTET_STREAM_TYPE).post(
>> InputStream.class, formData);
>>
>>
>> Setting the response when I am ready to send the bytes out seems to
>> be the problem.
>> DO you have an example of client/server byte stream?
>>
>> Thanks.
>>
>>
>> REST service method:
>>
>>
>> @POST
>> @Produces("application/octet-stream")
>> @Consumes("application/x-www-form-urlencoded")
>> public Response getLogBundle(@FormParam("fromMillis") Long
>> inFromMillis,
>> @FormParam("toMillis") Long
>> inToMillis,
>> @FormParam("fileList") String
>> inFileList) {
>>
>> if ((inFromMillis == null) || (inToMillis == null) ||
>> StringUtils.isEmpty(inFileList)) {
>> throw new
>> RestApplicationException(Response.Status.BAD_REQUEST,
>> "Parameter fromMillis, toMillis, and/or fileList
>> is missing or is invalid.");
>> }
>> String[] files = inFileList.split(",");
>> Set<String> fileSet = new
>> HashSet<String>(Arrays.asList(files));
>> String comment = "Log files corresponding to date range " +
>> DEFAULT_SDF.format(inFromMillis) + " to " +
>> DEFAULT_SDF.format(inToMillis);
>> Response resp;
>>
>> ByteArrayOutputStream oStream = new ByteArrayOutputStream();
>> long checkSum = FileCompressionUtils.compressFiles(fileSet,
>> oStream, comment);
>> if (checkSum == 0L) {
>> resp = Response.noContent().build();
>> } else {
>> resp = Response.ok(oStream,
>> MediaType.APPLICATION_OCTET_STREAM_TYPE).build();
>> }
>>
>> return resp;
>> }
>>
>> -------------------------
>>
>> Gisella Saavedra
>> Sr. Software Engineer
>> gsaavedra_at_zebra.com
>> <image001.gif>
>> 1000 Broadway, Suite 150, Oakland, CA 94607 | T+1 510 267 5123
>> T Main+1 510 267 5000 F+1 510 267 5100 | http://www.zebra.com/zes
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>