Restlet ported to GWT !

Google Web Toolkit is a very successful technology that brings Java right inside Web browsers. It could be compared to traditional Applets but without requiring any extra plugin and with a more natural integration with the browser artifacts.

How is it possible? Well, they built a smart Java compiler that produces highly optimized JavaScript ‘bytecode’. What is great is that you can leverage your Java experience and the strong typing of the language. What is even better is that in GWT version 1.5, they added support for the generic types, the famous feature introduced in Java SE 5.

Now, how can you leverage REST in GWT? By default, they propose an opaque GWT-RPC mechanism that is closer to SOAP than to REST. It gives a feeling of transparent distribution but actually introduces tight coupling between client and server code and prevents you from leveraging existing RESTful back-ends. There is an HTTP request builder class that you can use, but it requires knowledge of lower HTTP levels like the exact headers syntax.

Wouldn’t it be nice to use the Restlet API instead, which provides a higher level mapping of HTTP semantics to a clean Java API ? Thanks to the support for generics added in GWT 1.5, we were able to achieve this port with reduced efforts and limited troubles. We had to get rid of the server-side features that don’t make sense in a browser, and of the APIs like NIO and logging that aren’t emulated by GWT. The major impact was the adaptation of the API to the asynchronous communication imposed by AJAX and GWT.

Restlet

The port, named “Restlet edition for GWT”, is available since Restlet 1.1 and its usage is documented in our Wiki. There is a discussion comparing the GWT-RPC and Restlet edition for GWT architectures and much more! The testing, most of the documentation as well as the back-end integration (see the server-side GWT extension) was contributed by Rob Heittman. I would like to thank him as well for the numerous design discussions while working on this port.

Rob Heittman is Chief Technology Officer at Solertium.  Using prototypes of the Restlet edition for GWT, Solertium has been building large enterprise applications with Restlet and GWT for the past year, for worldwide customers like IUCN and Conservation International.  “I love GWT, but I prefer RESTful web services to RPC,” says Rob.  “GWT backed by Restlet has been great for us. Now, GWT 1.5 and the new Restlet edition for GWT will make our client-side code as simple and elegant as our server code.”

Finally, if you want to follow the latest news about GWT, I recommend the onGWT blog. It is maintained by Didier Girard, a good friend who introduced me to the joys of GWT!

Updates:

40 thoughts on “Restlet ported to GWT !”

  1. Hi,
    I just tried the GWT-Restlet API with the GWT 1.5 RC1. It worked wonderfully in the hosted mode. I then attempted web mode. However, When I compiled the code using the default GWT project compiler, i got the errors. Error snippet:
    [ERROR] : Class.createForEnum(“org.restlet.gwt.data.”,”Dimension”>

    Am I missing on something. Please help me as I am in the middle of a migration project and need the REST client for GWT badly.

  2. Hi Nitin,

    We have also noticed this issue and fixed it in the latest Restlet 1.1 M5 release which is compatible with GWT 1.5 RC2 (it might work with 1.5 RC1 as well, but we didn’t test it).

    Best regards,
    Jerome

  3. Hi Jerome,

    Thanks for replying back. I tried with RC-2 the Restlet code which I downloaded from the link on your website. I am still getting error, though now it is different. Here is a brief snippet. I just want to check with you that I am not missing anything.
    [ERROR] at Client.java(45): final class Client extends Connector

    Can I ask you about the tentative release date of Restlet v 1.1?

    Best Regards,
    Nitin Gupta

  4. Hi Jerome,

    Here is the basic code which I wrote and called it on click of a button;

    CallbackExtension callback = new CallbackExtension();
    Client client = new Client(Protocol.HTTP);
    Reference ref = new Reference(Protocol.HTTP, “localhost”, 8080);
    ref.addSegment(“PATS/atom”);
    client.get(ref, callback);

    CallbackExtension is a simple extension to the CallBack class.

    It works fine in the hosted mode, however compiler complains when attempt is made for the hosted mode.

    Let me know if you would like me to post the error message as well.
    Best Regards,
    Nitin Gupta

  5. Hi Nitin,

    Thanks for the report. It was due to some volatile modifiers rejected by the GWT compiler. There were all removed from source code. This has been fixed in SVN trunk. You’ll be able to test again from the next 1.1 snapshot.

    Best,
    Jerome

  6. Hi
    I can also see this problem with GWT compile when I tried GWT and restlet. My other question is: in restlet examples (http://www.restlet.org/documentation/1.0/tutorial) HTTP authentication is handled with
    ChallengeScheme scheme = ChallengeScheme.HTTP_BASIC;
    ChallengeResponse authentication = new ChallengeResponse(scheme,
    “scott”, “tiger”);
    request.setChallengeResponse(authentication);

    Is it not available with restlet-gwt ??

    thanks
    Gabor

  7. Hi Jerome,

    Thanks for the update. I shall check an do some more testing.

    If there can be a quick work around for the Basic HTTP authentication, then please let me know.

    Warm Regards,
    Nitin

  8. Hi Nitin,

    I have just added support for preemptive authentication to our Restlet-GWT module. It will make it into the next Restlet 1.1 release.

    Here is how you should use it:

    final Client client = new Client(Protocol.HTTP);
    Request request = new Request(Method.GET,
    “http://localhost:8888/ping”);
    request.setChallengeResponse(new ChallengeResponse(
    ChallengeScheme.HTTP_BASIC, “scott”, “tiger”
    .toCharArray()));

    client.handle(request, new Callback() {
    @Override
    public void onEvent(Request request, Response response) {
    button.setText(response.getEntity().getText());
    }
    });

    Best regards,
    Jerome

  9. Hi Scott,

    We still have a stability issue with the wiki server. We are planning to upgrade it in the near future.

    Anyway, we have restarted it again. The link should work again.

    Best regards,
    Jerome

  10. You claim that in GWT “the back-end has to be built using a Servlet container in order to work properly and to invoke your custom classes and methods”… but in reality it would only be a matter of porting the GWT server side classes in order to make it work with another platform, such as C#/ASP.NET. Although the official GWT documentation only talks about a Java back-end, there really should be no major technical difficulties involved in supporting other platforms (I have achieved some tricky things with GWT RPC on the server side, such as getting GWT 1.3 and 1.5 to work at the same time, and having a unique central servlet for any number of RemoteService interfaces, so I should know if there were any – unless I am missing something).

  11. Hi Rogério,

    I agree that the server-side could be re-implemented in other languages. There is an example of that for PHP:
    http://code.google.com/p/gwtphp/

    It seems to me that reimplementing the GWT-RPC object encoding protocol isn’t trivial at all, especially if you consider having it work both ways. You have to take care of serialization policies for example which isn’t trivial.

    Anyway, REST provides a much reusable back-end, not tighted to GWT or any specific language.

    BTW, we are working in Restlet 1.2 on the support of the GWT-RPC encoding for our resource representations, as an alternative to more interoperable format like pure JSON or XML.

    The idea is to leverage HTTP content negotiation to provide the best of both worlds with the exact same server code, returning serializable Java beans as representations.

    Best regards,
    Jerome

  12. I am using gwt-restlet to access a JAX-RS hosted webservice on the localhost. In hosted mode for GWT, I get this on error console with eclipse:
    Starting the HTTP client
    Error while handling an HTTP client call

    I am using Eclipse 3.4 and GWT 1.6. Downloaded the restlet 2.0m3 zip to get access to gwt-restlet. I have an http monitor running on the glassfish server, I does not even get the request from the GWT application. Here is the client access code:

    […]

    If I use curl to access the webservice it works fine:
    curl -v -H “Accept: application/json” http://localhost/entApp-war/resources/Items/id/70
    I see request with the http event monitor in glassfish.

    Any help is appreciated..

    -Wish

  13. r.get() getting null how to solve this problem

    my code is
    ClientResource r = new ClientResource(“http://192.168.1.13:8888/auth/ramu/ram@123″);

    r.setOnResponse(new Uniform() {

    @Override

    public void handle(Request request, Response response) {

    // TODO Auto-generated method stub
    try {

    System.out.print(response.getEntity().getText());
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }
    });

    System.out.println(“testtt”+r.get());

Leave a Reply