users@jersey.java.net

Downloading file using Jersey client API ?

From: James Weir <James.Weir_at_Sun.COM>
Date: Thu, 28 Feb 2008 16:44:19 +0100

Hi,

I wish a client to be able to download a file from a REST web service.
On the server side, I have written the following code to send the file
when the GET request comes in (I looked at the SimpleAtomServer example
for some inspiration).

@GET
@ProduceMime("*/*")
    public Response getLicense(@UriParam("gid") String
gid,_at_UriParam("aid") String aid) {
       
      ... (some code to check authentication etc)
      ...(some code to get some information from the database)
     ...(some code to check the last modified and etag information for
possible 304 response)
 
        // The file is present, create a File and check it really does exist
        String root = System.getProperty(Env.ROOTDIR);
        if (root == null) {
            logger.error("Getting root directory where to store binaries");
            throw new WebApplicationException(500);
        }
        File f = new File(root + l.getUri().toString() + "/" + l.getName());
        if ( ! f.exists()) {
            // File is not present
            logger.error("No license file present in the database");
            throw new NotFoundException("License meta-data not present
in the appliance");
        }
       
        
//----------------------------------------------------------------------
        // Get the file type and set the content type in the HTTP header
        
//----------------------------------------------------------------------
        MediaType mt = null;
        InputStream in = null;
        try {
            mt = MediaType.parse(mimeTypeMap.getContentType(f));
            in = new BufferedInputStream(new FileInputStream(f));
        } catch (FileNotFoundException e) {
            logger.error("License file not found:\n" + e);
            throw new NotFoundException("License meta-data not present
in the appliance");
        }
       
        
/***********************************************************************
         * Returning the appliance information
         
**********************************************************************/
        r =
Response.ok(in).lastModified(lastModified).tag(et).type(mt).build();
        return(r);
    }


My question is how do I use the Client API to retrieve the contents and
store them locally on disk ?
At the moment I am battling to get it to work. Any ideas ? or examples
you have so I can see how to do this ?
Also, am I setting the MediaType correctly here ?, I am currently using
MimeTypeMap to get the mime type of the file


Thanks for any help
James