First stones to the Restlet edition for JavaScript

I have started working on the port of Restlet to the JavaScript language. It was quite a challenge since Java and JavaScript languages own some different and specific mechanims. Of course, Restlet can be used within GWT and so executed in a navigator but I would want something more generic and independant from the Java language.

For this port, the aim was to provide same entities that Restlet uses with some adaptations to leverage strengths of the JavaScript language like function callbacks and asynchronous handling. I also use the JavaScript OOP based on the JS.Class tool, a very powerful library that emulates OOP features in a friendly way. See the site of the tool for more details:

The code is available in the Restlet incubator at the following address: Some hints regarding work and progress on this subject can be reached on the page

Client-side foundations

Restlet central entities for the client side (Request, Response and Client) remain available to execute and handle REST requests / responses. Following snippet describes how to use them with the JavaScript edition. You can notice that handling is necessary asynchronous and the handle method of the Client class accepts a callback function to receive response. In this case, its parameter of the response object itself.

var request = new Request(
      Method.GET, "../resource/contact/1");
var client = new Client(new Context(), [Protocol.HTTP]);
client.handle(request, function(response) {
  var representation = response.getEntity();
  var content = representation.getText();
  console.log("content = "+content);
  //Prints something like that:

Using the ClientResource class

The higher-level class to handle HTTP methods, the ClientResource class, is also present in the edition. It provides common HTTP methods and supports content negocation based on the MediaType class. Following code describes how to execute a GET method and handle the response JSON content:

var clientResource
    = new ClientResource("../resource/contact/1");
clientResource.get(function(representation) {
  var content = representation.getText();

Following code describes how to use a PUT method and to manage request and response content with JSON:

var clientResource = new ClientResource("../resource/contact/1");
var contact = {
  id: "1",
  lastName: "lastName",
  firstName: "firstName"
var jsonRepresentation = new JsonRepresentation(contact);
clientResource.put(jsonRepresentation, function(representation) {
  var jsonRepresentation = new JsonRepresentation(representation);
  var obj = jsonRepresentation.getObject();

Following content simply executes a DELETE method:

var clientResource = new ClientResource("../resource/contact/1");
clientResource.delete(function(representation) {

Using representations

Representations within Restlet are responsible to handle request / response content. The JavaScript edition provides two ones:

  • JsonRepresentation – for the JSON format and based on the JSON parser provided on googlecode (
  • DomRepresentation – for the XML format from XML documents.
  • XmlRepresentation – for the XML format from JavaScript objects.

We described the use of the JsonRepresentation in the previous sections. The DomRepresentation class provides similar support but for XML, as described below:

var clientResource = new ClientResource("../resource/contact/1");
clientResource.get(function(representation) {
  var domRepresentation = new DomRepresentation(representation);
  var xml = domRepresentation.getXml();
  var contactElement = xml.childNodes[0];
  var idElement = contactElement.childNodes[0];
  var lastNameElement = contactElement.childNodes[1];
  var firstNameElement = contactElement.childNodes[2];

The XmlRepresentation class also provides similar support but for XML, as described below:

var clientResource = new ClientResource("../resource/contact/1");
clientResource.get(function(representation) {
  var xmlRepresentation = new XmlRepresentation(representation);
  var contact = xmlRepresentation.getObject();
  var id =;
  var lastName = contact.lastName;
  var firstName = contact.firstName;

Further directions

Whereas the current code allows sending requests, receiving and handling responses, I need now to add some built-in features of Restlet:

  • Services and filters (Not implemented)
  • Connector selection (At this moment, the chosen connector is hard-coded)
  • Converter service (Not implemented)
  • Full representation support (Need to be improved)
  • Full header and status support (Need to be improved and error handled)
This entry was posted in JavaScript, Restlet. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s