If you’ve evaluated RESTx, our brand new platform for building RESTful web services, you’ve certainly noticed its - hybrid nature. Indeed, besides its vocation of being the simplest way to create RESTful web services, has been designed with the idea of letting programmers use their favorite JVM language when creating resource components.

So far, we naturally had Python, with which the RESTx core has been written, and Java, the default language of the JVM. We’re happy to announce that, since version 0.9.4it is now possible to write RESTx components with !

By adding support for JavaScript in RESTx, our goal was to open the door of RESTful web services and application integration to web developers. To be sure, the need for integrating systems and applications is not the preserve of enterprise developers anymore, especially with the advent of SaaS and cloud computing. With RESTx, web developers can now accomplish such a complex task within the comfort of their favorite language.

RESTx.js

A bit of code being worth a thousand words, take a few seconds to look at RESTx demonstration Twitter component written in JavaScript.

Let’s now take a closer look at a few notable aspects.

Full language support

RESTx allows you to use JavaScript the way you’re used to do it. Check out how the Twitter component defines a global function used by its different service functions. Or how nested functions are used in each service function to encapsulate specific logic, as excerpted hereafter:

?View Code JAVASCRIPT
function status(method, input) {
  function getStatus() {
    result = RESTx.httpGet("http://api.twitter.com/1/users/show.json?screen_name=" + accountName)
    return response(function() { return RESTx.fromJsonStr(result.data).get("status").get("text") })
  }
...

It is also possible to return JavaScript data-structures, namely arrays and objects, to RESTx, as visible in the filterResults function.

Meta-information

Global and function-level variables are used to define meta-information for components and their services. Following the convention above configuration principle, these variables must used pre-defined names.

Here is an example of component meta-information:

?View Code JAVASCRIPT
this.name = "JsTwitterComponent"
this.description = "Provides access to a Twitter account."
this.documentation = "The Twitter component is designed to provide access to a Twitter account.\n"
                   + "It can be used to get as well as update status, or to view the timeline of a Twitter account.\n"
                   + "To create the resource, the Twitter account name and password need to be specified."
this.parameters = {
  accountName     : { type: TYPE.STRING, description: "Twitter account name", required: true },
  accountPassword : { type: TYPE.PASSWORD, description: "Password", required: true }
}

And here is an example of service level meta-information:

?View Code JAVASCRIPT
status.description = "You can GET the status or POST a new status to it."
status.inputType   = "text/plain"
status.outputType  = "text/plain"
function status(method, input) {
...

Lightweight scaffolding

True to RESTx no-framework nature, supporting functions and constants are provided to the JavaScript as pre-bound global variables to limit the invasiveness of the hosting infrastructure to a minimum. In the above code samples, RESTx and TYPE are two examples of such variables.

The following shows another example, where the HTTP method of the incoming requests is validated for a particular service:

?View Code JAVASCRIPT
switch(method) {
  case HTTP.GET : return getStatus()
  case HTTP.POST: return postStatus(input)
  default       : return RESULT.methodNotAllowed(method)
}

In that case, HTTP and RESULT are two globally bound variables that RESTx provides for the sake of clarity and brevity of the component code.

We hope that this brief introduction will decide you to further probe RESTx and explore its JavaScript support. If you think this support can be improved, please do tell us or, even better, contribute an improvement.

Calling all JVM polyglots!

As a parting note, we’d like to extend our call for contributors to the JVM polyglots out there. We believe that adding support for more JVM languages, like Groovy, JRuby, Scala or Clojure, will help lowering the bar to create RESTful web services in order to integrate systems. Again, our goal is to provide the most natural and least frameworkey support for each of these languages so their native speakers would find it palatable.

If you’re interested by this challenge, please contact us or send us your contribution.

No related posts.


One Response to “Speak JavaScript? RESTful web services are belong to you!”

This Week in #REST – Volume 24 (Aug 23 2010 – Sep 5 2010) « This week in REST September 6th, 2010, 12:27 am

[...] – The RESTx framework for building RESTful web sercies reaches the 0.9.4 milestone and now supports JavaScript on both the client and the server. See the release notes [...]

Leave a Comment