Java REST API for Dojo using Restlet

The Dijits of The Dojo Toolkit make it possible to handle the entire user experience with static HTML+JS+CSS. The hard part — at least for me — is how to dynamically generate and cache the JSON representations used to populate the Dijits with actual data.

Finding REST in all the wrong places

I looked into many solutions, the most naïve of which was my own J2EE Servlet that generated JSON through reflection. The problems with this are manifold, but among the biggest are lack of security and poor support for caching. Other REST solutions in other languages like Ruby and Python caught my interest, but quickly faded away. While Rails and Django stood out as very good for REST, they would have added unnecessary learning and complexity with the legacy Java system to which I wanted to add a RESTful face. A Java REST API is what I needed, but I could not find an appropriate framework or library for my needs.

Restlet: finally a REST Framework for me

I recently found Restlet, which has actually been around for a few years now and has matured substantially. Restlet is clearly the most comprehensive Object Oriented Framework for supporting RESTful interactions. Every aspect of REST that I can think of that I would want is there. Better than that though is how it is presented. Despite supporting so much of REST, Restlet is not hard to do small things with either. Even better again is how easy it is to progressively improve your Resources with things like Tags for caching. You do not need to design out the whole system with Restlet. You build as you go.

How does this help with Dojo Applications?

A Dojo Application as I envision it would have a set of static templates for representing various interactions and information for the user. The actual content of these templates would be filled in with AJAX from JSON data sourced from a REST API. In this model Restlet would be very useful because it has full support for JSON Representations of Resources already and is easy to build on.

How would this be deployed?

An HTTP server tuned for static content delivery would be used to serve the Dojo-powered HTML+JS user interface documents. This server would delegate data API requests to the Restlet Application using AJP. In this way it is easy to separate authentication and transport concerns from the REST data API concerns. A server such as Apache httpd could handle ActiveDirectory authentication, support for HTTPS and SSL client authentication. Application-specific authorizations such as which user is allowed to do what with what and the generation of representations would be handled by the Restlet Application. Many Restlet Application instances could be distributed in a cluster to be nearer to specific data to leverage CPU caching for reduced latency. When you go down this RESTful road it seems to open the way to supporting basic web functionality like bookmarking and caching which many web app frameworks like JSF seem to ignore completely.