• REST
    January 28, 2009

Jersey Based Java Client Talking to SmugMug

Jersey provides a great, easy-to-use REST client API. Recently someone wrote to our mailing list the opposite. We probably need to advertise better.

Paul initially introduced the API in this blog post almost a year ago. But it is, of course, not the only place to look at. Javadoc for Jersey Client API could be found here.

The Jersey Client API could be used pretty easily. However, instead of describing the API in detail, i would just like to present here a real life example. A Jersey based client talking to a 3rd party server application. The server application happens to be SmugMug. A photo sharing service application. I wrote the client application during the last Xmas break, because what was provided by SmugMug did not work well for me. I am happy with my Solaris, but sometimes i wish i have chosen another OS for my laptop ;-)

Putting together a functional prototype took me only a couple of hours, and this was mainly thanks to user friendliness of the Jersey Client API designed by Paul. Following is a screenshot of the main screen of the client.

I promised not to dig into details, but just for your curiosity. Only the very last statement of the java code below has anything to do with actual invocation of SmugMug API from java using Jersey Client API. All the rest is only about computing a digest for the file being sent, as SmugMug API requires it sent in a special HTTP request header.

MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
File picFile = new File(picDir, pic);
fis = new FileInputStream(picFile);
byte[] buf = new byte[8196];
int l;
while ((l = fis.read(buf)) > -1) {
digest.update(buf, 0, l);
String md5hash = asHex(digest.digest());
cr = uploadWebResource
.header("Content-MD5", md5hash)
.header("X-Smug-SessionID", sessionId)
.header("X-Smug-Version", "1.2.0")
.header("X-Smug-ResponseType", "JSON")
.header("X-Smug-AlbumID", car.album.id)
.put(ClientResponse.class, picFile);

Join the discussion

Comments ( 2 )
  • Paul Sandoz Wednesday, January 28, 2009


    Perhaps one way you could improve this is to write the support for the SmuhMug headers as one or more ClientFilter.

    For example, such a filter can calculate the MD5 hash. It should even be possible to avoid buffering the request entity by checking for entity type and handle instances of File as you currently do.

    That way if one needs to make repeat requests using a resource one does not have to add all the headers again.


  • Carmen Delessio Thursday, April 2, 2009

    Thanks for this helpful post. It gave me the direction I needed to create a Java Client to talk to Facebook's Rest Server.

    TinyFBClient is a simple client that uses Jersey. TinyFBClientExample is a JSP Facebook that uses the client.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.