users@jersey.java.net

Adding listeners to Input Streams with Jersey

From: James Weir <James.Weir_at_Sun.COM>
Date: Thu, 27 Mar 2008 11:41:21 +0100

Hi all,

I need attach listeners for when I upload or download files (either
client or server)?. I filed an RFE against 0.5ea, was this RFE added
in ea0.6 ?

My problem is that I request the service to download an ISO image that
has been created by my Web Service. The download itself works well,
however I wanted to be able to update another field in the database
regarding the status of the download. It is important that the server
updates a field in the database when the download is completed. That
way if the user asks for the status (when re-logging in for example)
then the server can reply saying that this file has been downloaded.
The client today holds nothing on disk (only a cache in memory).

If this modif was not done, no worries, I can always get the client to
do a PUT to say when the download is finished on his side. Here is my
server side code today:

@Path("{itid}/image")
    @GET
    @ProduceMime("*/*")
    public Response downloadImage(@PathParam("name") String userName,
@PathParam("itid") String itid) {
 
          .....omitted code
       
        
//----------------------------------------------------------------------
        // Create the stream ready to send the contents
        
//----------------------------------------------------------------------
        InputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(f));
        } catch (FileNotFoundException e) {
            logger.error("Appliance image file not found:\n" + e);
            dbManager.close();
            throw new NotFoundException("Appliance image not found");
        }

        
/***********************************************************************
         * Returning the appliance information
         
**********************************************************************/
        r = Response.ok(in).type(mt).build();
        dbManager.close();
        return(r);
    }

It would be good if I could attach a listener to the buffered input
stream to get the bytes transferred and know when the download is
complete in order to do some extra work on the server (namely update the
database with some information)

Thanks
James