org.restlet.ext.servlet
Class ServletAdapter

java.lang.Object
  extended by org.restlet.engine.http.adapter.Adapter
      extended by org.restlet.engine.http.adapter.ServerAdapter
          extended by org.restlet.ext.servlet.ServletAdapter

public class ServletAdapter
extends ServerAdapter

HTTP adapter from Servlet calls to Restlet calls. This class can be used in any Servlet, just create a new instance and override the service() method in your Servlet to delegate all those calls to this class's service() method. Remember to set the next Restlet, for example using a Router instance. You can get the Restlet context directly on instances of this class, it will be based on the parent Servlet's context for logging purpose.

This class is especially useful when directly integrating Restlets with Spring managed Web applications. Here is a simple usage example:

 public class TestServlet extends HttpServlet {
     private ServletAdapter adapter;
 
     public void init() throws ServletException {
         super.init();
         this.adapter = new ServletAdapter(getServletContext());
 
         Restlet trace = new Restlet(this.adapter.getContext()) {
             public void handle(Request req, Response res) {
                 getLogger().info("Hello World");
                 res.setEntity("Hello World!", MediaType.TEXT_PLAIN);
             }
         };
 
         this.adapter.setNext(trace);
     }
 
     protected void service(HttpServletRequest req, HttpServletResponse res)
             throws ServletException, IOException {
         this.adapter.service(req, res);
     }
 }
 

Author:
Jerome Louvel

Constructor Summary
ServletAdapter(ServletContext context)
          Constructor.
ServletAdapter(ServletContext context, Restlet next)
          Constructor.
 
Method Summary
 Reference getBaseRef(HttpServletRequest request)
          Returns the base reference of new Restlet requests.
 Restlet getNext()
          Returns the next Restlet.
 Reference getRootRef(HttpServletRequest request)
          Returns the root reference of new Restlet requests.
 void service(HttpServletRequest request, HttpServletResponse response)
          Services a HTTP Servlet request as a Restlet request handled by the "target" Restlet.
 void setNext(Restlet next)
          Sets the next Restlet.
 HttpRequest toRequest(org.restlet.ext.servlet.internal.ServletCall servletCall)
          Converts a low-level Servlet call into a high-level Restlet request.
 
Methods inherited from class org.restlet.engine.http.adapter.ServerAdapter
addEntityHeaders, addResponseHeaders, commit, toRequest
 
Methods inherited from class org.restlet.engine.http.adapter.Adapter
getContext, getLogger
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ServletAdapter

public ServletAdapter(ServletContext context)
Constructor. Remember to manually set the "target" property before invoking the service() method.

Parameters:
context - The Servlet context.

ServletAdapter

public ServletAdapter(ServletContext context,
                      Restlet next)
Constructor.

Parameters:
context - The Servlet context.
next - The next Restlet.
Method Detail

getBaseRef

public Reference getBaseRef(HttpServletRequest request)
Returns the base reference of new Restlet requests.

Parameters:
request - The Servlet request.
Returns:
The base reference of new Restlet requests.

getNext

public Restlet getNext()
Returns the next Restlet.

Returns:
The next Restlet.

getRootRef

public Reference getRootRef(HttpServletRequest request)
Returns the root reference of new Restlet requests. By default it returns the result of getBaseRef().

Parameters:
request - The Servlet request.
Returns:
The root reference of new Restlet requests.

service

public void service(HttpServletRequest request,
                    HttpServletResponse response)
Services a HTTP Servlet request as a Restlet request handled by the "target" Restlet.

Parameters:
request - The HTTP Servlet request.
response - The HTTP Servlet response.

setNext

public void setNext(Restlet next)
Sets the next Restlet.

Parameters:
next - The next Restlet.

toRequest

public HttpRequest toRequest(org.restlet.ext.servlet.internal.ServletCall servletCall)
Converts a low-level Servlet call into a high-level Restlet request. In addition to the parent ServerAdapter, it also copies the Servlet's request attributes into the Restlet's request attributes map.

Parameters:
servletCall - The low-level Servlet call.
Returns:
A new high-level uniform request.


Copyright © 2005-2011 Noelios Technologies.