eXo REST framework use case

The new version of eXo comes with a powerful REST framework following the guidelines defined in the JSR 311 specification (JAX-RS)

The idea of the REST framework is to provide a simple way to create server side APIs that allows to expose to third party systems most of the platform functionalities.

Note that that trend is extremely popular in the consumer web where corp like Yahoo or Google expose many services using the REST paradigm. Even the server side part of the new OpenSocial specification is REST based… and could be implemented using eXo REST framework!

1) Some samples

We have exposed several services using the framework from the low level layers like WebDAV to the higher one like providing a Facebook application using the REST framework to browse eXo Document Management repository.

Another sample is a REST service that expose all the contact of an eXo Collaboration Suite user. We also have a Flex based client that read the exposed XML and displays the contacts. The flex can be embedded inside a portlet, a widget or any other web application:

Flex client that talks to the REST service

 

 

 

[Update]  I also paste a screenshot of the iGoogle gadget that allows to browse the document repository on eXo. It leverages the WebDAV protocol that itself is implemented over the REST framework

eXo JCR Google Gadget

2) Technical aspects of the framework

So any eXo Java service can be exposed via REST, it just has to use Java annotations inside services to dynamically map an URL and Java entities.

One of the most important type of annotation is @URITemplate, applicable for TYPE and METHOD scope which define relative URL to expose given service via HTTP.

In simple cases, a RESTable method is uniquely addressed as a concatenation of a base URL, a TYPE scoped URITemplate annotation (if any) and a METHOD scoped URITemplate annotation. Parameters of the service can also be mapped to some elements of the URL as shown in the next code fragment. There an HTTP call to the URL “server.com/level1/toto/titi/tata” would directly invoke the method3 method with “toto”, “titi” and “tata” as the value of the 3 method arguments.

All HTTP methods like standard PUT, POST, DELETE ones can be used and referenced using the HTTPMethod annotation. A Response object is returned to the container, in our sample the Response does not contain anything; the ok method returns a 200 HTTP status code.

More complex URL template are also supported especially the one of type ?param1=value1&param2=value2 as shown in the next code fragment where the parameters are directly mapped to the method signature arguments:

Another important feature of that REST framework is the use of Transformer objects that can be easily plugged at the Response generation time.

In the previous example, the transformer class is a simple StringOutputTransformer that will convert the incoming object into a String output of type “text/plain”.

The framework contains several transformers that are ready to use such as XSLT4DOMOutputTransformer which takes a DOM element and will transofrm it using an XSL template file as shown in the next code fragment:

3) The Contact REST Service

The ContactRESTService is a simple sample of an eXo service method that is exposed through a REST API.

Any URL of type “server.com/contact/getContactList/benjamin” will return an XML fragment within the HTTP response that will contain the list of all benjamin’s contacts that are located in the Contact Manager application of eXo CS.

As you can see in the next fragment, the code is simple, we simply get the ContactService and call the getAllContact method on the current user

Be part of the discussion, share your comments

comments

Keep in touch with the author