Just thought I would post this to the list because I hadn't seen it documented
anywhere else.
In Hosted Mode, the configuration of Google Web Toolkit is optimized to
facilitate use and testing of its own RPC mechanism. We have GWT applications,
but do not use GWT-RPC, since we have flexible server services using the REST
style and now Restlet. GWT clients are only one of many possible consumers of
the server-side REST services. Anyway, after starting to adopt Restlet, we
found the Restlet+GWT Hosted Mode combination tricky to get to work in any
straightforward way, largely due to path reporting problems in GWT's embedded
copy of Tomcat.
The briefest solution we found (if not the cleanest) was to subclass Restlet's
ServerServlet to cause it to manage and invoke the GWTShellServlet directly, as
attached, and then register this in place of GWTShellServlet in the Hosted Mode
working directory.
Happy to hear better solutions, and hope that anybody trying to do this
integration in the future finds this thread.
- Rob
package com.solertium.gwt.server;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gwt.dev.shell.GWTShellServlet;
import com.noelios.restlet.ext.servlet.ServerServlet;
/**
* This Servlet adapts GWT to Restlet for the purposes of running
* Restlet applications as a backend to the GWT app. Because neither
* the GWTShellServlet nor the Restlet ServerServlet is really adapted
* for this, this Servlet composites the resource space of both tools,
* invoking GWTShellServlet methods when the path calls for it, and
* has ServerServlet behavior otherwise.
*
* To enable it, you must modify the web.xml file in
* tomcat/webapps/ROOT/WEB-INF/. This is created in a new GWT project
* *after* the first run of Hosted Mode that invokes a servlet:
*
* <servlet>
* <servlet-name>ServerServlet</servlet-name>
*
<servlet-class>com.solertium.gwt.server.RestletServlet</servlet-class>
* <init-param>
* <param-name>module</param-name>
*
<param-value>com.yourcompany.yourmodule.gwt.YourModuleName</param-value>
* </init-param>
* </servlet>
*
* <servlet-mapping>
* <servlet-name>ServerServlet</servlet-name>
* <url-pattern>/*</url-pattern>
* </servlet-mapping>
*
* @author Rob Heittman <[EMAIL PROTECTED]>
*/
public class RestletServlet extends ServerServlet {
private String module = null;
private static final long serialVersionUID = 1L;
private GWTShellServlet gwtShellServlet = new GWTShellServlet();
@Override
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
module = servletConfig.getInitParameter("module");
gwtShellServlet.init(servletConfig);
}
@Override
public void destroy() {
super.destroy();
gwtShellServlet.destroy();
}
@Override
public void service(HttpServletRequest request, HttpServletResponse
response)
throws IOException, ServletException {
String path = request.getPathInfo();
if(path.startsWith("/"+module)){
gwtShellServlet.service(request,response);
return;
} else {
super.service(request,response);
return;
}
}
}