users@jersey.java.net

RE: [Jersey] sending a byte stream to client

From: Saavedra, Gisella <gsaavedra_at_navis.com>
Date: Wed, 17 Jun 2009 09:54:27 -0700

Thanks for the information. I actually changed it after I sent the
e-mail and at the client side I get a 200 OK HTTP status.

I could not use the InputStream at the client side. I got a message that
I needed a sort of MessageBodyReader.

I got it to work with:


                byte[] bytes = new byte[5000];
            bytes = res.accept(
                    MediaType.APPLICATION_OCTET_STREAM_TYPE).post(
                    bytes.getClass(), formData);



but this piece of code assumes a certain maximum number of bytes. Is
there a way that I can use a buffered InputStream at the client's end
without having to write a MessageBoydReader sort of handler?

Thanks,

Gisella



-----Original Message-----
From: Paul Sandoz [mailto:Paul.Sandoz_at_Sun.COM]
Sent: Wednesday, June 17, 2009 3:01 AM
To: users_at_jersey.dev.java.net
Subject: Re: [Jersey] sending a byte stream to client

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
>
>
>