A Solution to perform GWT RPC calls from within a Wave Gadget

The Problem

When you’re building a Google Wave Gadgets with GWT you might want to communicate with the server via RPC, because that’s any easy way to communicate. Until now it was not really possible to do. The reason, the standard RPC implementation works only when the client application is loaded from the same server to which the RPC calls are made. This is due to the cross-site restriction browsers enforce to avoid harmfull actions performed by malicious┬ásites. The problem is that Wave caches the gadget and runs it from it’s own server, instead of the originating server, and thus when the Gadget is loaded and runs, the original server is now on another domain, and therefor it’s not possible to perform the standard RPC calls.

The Solution

The solution lies in the iGoogle Gadget api. Wave Gadgets are build upon the iGoogle Gadget api and the Gadget api contains a proxy method to perform RPC calls to another server. Calls are routed through the iGoogle Gadget proxy server and for the client, the gadget code, it seems as if it performs a direct call to the external server. This is nothing new. However, there was not yet a GWT wrapper for access to these Google Gadgets api calls, and thus you would have to write that yourself.

But that’s about to change. When I was informed by the GWT team they where working on a new release of the GWT Gadget api I noticed the wrapper for the iGoogle proxy methods were added and wrote additional code to make it easy to do actual RPC calls from Wave. Now this code has been added to get GWT Gadget api library and will be present in the comming release. The trunk already contains these method, so if you want to start today, build your own version of the GWT Gadget api from trunk.

How To

Making RPC work within a Wave GWT Gadget is now easy. With these 2 steps any GWT Gadget can be made to work to use RPC.

1) For each service you want to use you need to redirect it to the Gadget api proxy to perform RPC call. This is done with the static method redirectThroughProxy. As shown in the following example:

  YourServiceAsync yourService = GWT.create(YourService.class);
  GadgetsGwtRpc.redirectThroughProxy((ServiceDefTarget) yourService);

2) On the server side you need to override the method readContent in your servlet implementing the service, the one extending RemoteServiceServlet:

  @Override
  protected String readContent(HttpServletRequest request)
      throws ServletException, IOException {
    return RPCServletUtils.readContentAsUtf8(request, false);
  }

This is needed because the current implementation of the iGoogle Gadget and thus Wave proxy rewrites the content type header and the GWT RPC implementation checks for this type to be text/x-gwt-rpc.

One Final Remark

Wave Gadgets can store state in the Wave itself. When storing state it’s also available in the Wave’s history. When you decide to store state on your own server, via RPC, you need to take care of showing historic versions yourself. This means depending on the case it’s better to store state in the Wave instead of obtaining the data from your own server.

Thanks to the GWT team for adding the code and make it easy to perform RPC calls from within Wave Gadgets.

To easily write Wave Gadgets with GWT use the cobogwave library that wraps all Wave specific methods.

Update June, 28: The new iGoogle GWT Gadget api is available for download. Version 1.2: http://code.google.com/p/gwt-google-apis/downloads/detail?name=gwt-gadgets-1.2.0.zip

This entry was posted in gadget, gwt, wave. Bookmark the permalink.

2 Responses to A Solution to perform GWT RPC calls from within a Wave Gadget

  1. Jetze van der Wal says:

    They’ve released a new version of the new gadget api and it includes the redirectThroughProxy method. At time of writing, the download links are not on the main donload pages yet.

    http://code.google.com/p/gwt-google-apis/downloads/detail?name=gwt-gadgets-1.2.0.zip&can=2&q=

    http://gwt-google-apis.googlecode.com/svn/javadoc/gadgets/1.2/index.html

  2. Hilbrand says:

    @Jetze,

    Ah, I hadn’t seen it yet. Thanks for pointing this out.