Restlet combat (thirteen) how to call Restlet in Servlet

When you see this topic, you may ask, aren't many of your previous articles related to servlets? Isn't this a repetition?

 

No, suppose you are using MVC framework, such as Struts, Spring MVC, etc. to build a Web system, but the current requirement requires your code to be able to interface with another system flexibly. Then you thought of using Restlet to build Restful web services. Therefore, you want to use restlet inside your servlet.

 

The scenario is like this, but it is actually very simple to understand, if and using restlet in servlet.

 

First, copy a copy of the code described in the API doc of ServletConverer in Restlet:

 

Java code   Favorite code
  1. public class TestServlet extends HttpServlet {  
  2.     private ServletConverter converter;  
  3.   
  4.     public void init() throws ServletException {  
  5.         super.init();  
  6.         this.converter = new ServletConverter(getServletContext());  
  7.   
  8.         Restlet trace = new Restlet(this.converter.getContext()) {  
  9.             public void handle(Request req, Response res) {  
  10.                 getLogger().info("Hello World");  
  11.                 res.setEntity("Hello World!", MediaType.TEXT_PLAIN);  
  12.             }  
  13.         };  
  14.   
  15.         this.converter.setTarget(trace);  
  16.     }  
  17.   
  18.     protected void service(HttpServletRequest req, HttpServletResponse res)  
  19.             throws ServletException, IOException {  
  20.         this.converter.service(req, res);  
  21.     }  
  22. }  

 

The above code is still relatively simple, but requires three steps:

1. Instantiate a ServletConverter

  

Java code   Favorite code
  1. this.converter = new ServletConverter(getServletContext());  

 

2. Set a restlet instance for the ServletConverter instance in the first step, and when this instance is called, execute the restlet's own code

  

Java code   Favorite code
  1. Restlet trace = new Restlet(this.converter.getContext()) {  
  2.     public void handle(Request req, Response res) {  
  3.         getLogger().info("Hello World");  
  4.         res.setEntity("Hello World!", MediaType.TEXT_PLAIN);  
  5.     }  
  6. };  
  7.   
  8. this.converter.setTarget(trace);  

 

3.调用执行的方法

   

Java代码   Favorite code
  1. this.converter.service(req, res);  

 

让我们打开源代码看一下上面的service方法:

 

Java代码   Favorite code
  1. public void service(HttpServletRequest request, HttpServletResponse response)  
  2.         throws ServletException, IOException {  
  3.     if (getTarget() != null) {  
  4.         Context.setCurrent(getContext());  
  5.   
  6.         // Convert the Servlet call to a Restlet call  
  7.         final ServletCall servletCall = new ServletCall(request  
  8.                 .getLocalAddr(), request.getLocalPort(), request, response);  
  9.         final HttpRequest httpRequest = toRequest(servletCall);  
  10.         final HttpResponse httpResponse = new HttpResponse(servletCall,  
  11.                 httpRequest);  
  12.   
  13.         // Adjust the relative reference  
  14.         httpRequest.getResourceRef().setBaseRef(getBaseRef(request));  
  15.   
  16.         // Adjust the root reference  
  17.         httpRequest.setRootRef(getRootRef(request));  
  18.   
  19.         // Handle the request and commit the response  
  20.         getTarget().handle(httpRequest, httpResponse);  
  21.         commit(httpResponse);  
  22.     } else {  
  23.         getLogger().warning("Unable to find the Restlet target");  
  24.     }  
  25. }  

 

The comments of the above code have clearly explained the entire calling process and will not be analyzed.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326486140&siteId=291194637