Saturday, August 08, 2009

The problem: I want to expose application back end functionality on Google Application Engine (GAE) to the GWT and Java SE front ends in the same way. I did not want to code 2 different technology things that do same business logic.
The Discussion: all 3 playing technologies use Java... GWT standard way to talk to server is GWT RPC - works great. But Zipped JSON underneath (seen somewhere analysis of the traffic done with a proxy.)
For Java Web Start App written with Java SE 6 any kind of serialization is an option but the rest of the protocol can be tricky to code test and maintain.
Easy to see that what I was looking was a way to create some kind of web services on GAE, but the classic Web Services with AXIS does not work (this is by design, documented and understandable).
So everyone I asked offered me to code a bunch of servlets to return XML serialization of the same POJOs used in GWT RPS realization. But I was looking for a way to have more than a single servlet for a single RPC method hand coded. So run in to the restlet.org And U know what?! It worked just out of the box.
The Solution:
The recipe is like that:
Get from restlet.org the restlet-gae-2.0m4.zip this is a GAE edition of the 2nd version that currently in development but as they work fast and release often - it will be production ready before I even start testing anything real.
Create a GAE web app in Eclipse 3.5 with Google Plugin.
Add 2 jars: org.restlet.ext.servlet.jar org.restlet.jar to the \war\WEB-INF\lib\ and to the build path of the project
create classes:
package biz.daich.google.engine.ex1.code.server.restlets;

import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;


public class HelloWorldResource extends ServerResource {

@Get
public String represent() {
return "hello, world (from the cloud!)";
}

}
-------------------------------------------
package biz.daich.google.engine.ex1.code.server.restlets;

import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;


public class RestletsApplication extends Application {

/**
* Creates a root Restlet that will receive all incoming calls.
*/
@Override
public synchronized Restlet createRoot() {
// Create a router Restlet that routes each call to a
// new instance of HelloWorldResource.
Router router = new Router(getContext());

// Defines only one route
router.attach("/", HelloWorldResource.class);
return router;
}
}
------------------------------------------------------
\war\WEB-INF\web.xml should look like this :

Pei_pivo.html

org.restlet.application
biz.daich.google.engine.ex1.code.server.restlets.RestletsApplication

RestletServlet
org.restlet.ext.servlet.ServerServlet

RestletServlet
/restlets/*
===============================================
now in the debug mode it works OK when you go to the http://localhost:8080/restlets/
it shows "hello, world (from the cloud!)" as expected.
see restlet.org wiki and documentation. It has GWT connector too. Will try it next.

No comments:

Followers