users@jersey.java.net

[Jersey] Error handling streaming binary data

From: Ralph Schwenn <rschwenn_at_web.de>
Date: Thu, 25 Sep 2014 09:46:01 +0200
We are building an application which streams binary data (which ist stored in a databse) to client.
The binary data ist delivered with StreamingOutput:
 
...
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getMediaFile(
...
final InputStream in = mf.getInputStream();
final StreamingOutput stream = new StreamingOutput() {
   public void write(OutputStream out) throws IOException, WebApplicationException {
      try {
         IOUtils.copyLarge(in,  out);
       } catch (Exception e) {
           ...
       } finally {
          IOUtils.closeQuietly(in);
       }
   }
};
...           
ResponseBuilder respBuilder = Response.status(Status.OK).entity(stream)
   .type(MediaType.APPLICATION_OCTET_STREAM)
   .header(HttpHeaders.CONTENT_LENGTH, mf.getLength());
   .header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=" + fname)
   .header("filename", fname);
return Response;
 
What would be the best practice for error handling when during streaming IOUtils.copyLarge() an error occurs?
 
At the moment the client (e. g. browser) waits until he gets a timeout and only a part of the binary data is received.
On client-side i cant see, that only part of the data was received.
How can i inform the client that an error occured?
 
Thanks,
Ralph