Servlet Filter Came To The Rescue

December 29, 2007

In my previous post I outlined a couple of limitations that Flex HTTPService class has in dealing with Restful style web services. After giving it some thought, I came up with a solution that will allow me to continue to use Flex with Restful style web services without changes to the backend. The core of the solution is the idea of intercepting the incoming request on the server side using Servlet filter and use the Decorator pattern to adapt the needs of the service Restful web services. The solution consists of the following parts:

  1. Servlet filter to intercept incoming Restful style web service requests
  2. Develop a custom HttpServletRequest wrapper class that converts information like method name or header from the request parameters into right format that the server side web service handlers require.
  3. Develop a custom HttpServletResponse wrapper to convert all status code to 200 so flex will be able to get to response body

The above solution is very specific to my application and the willingness to bend some rules in making Restful web services calls from the Flex front end. Basically each web service request will append a request parameter with an agreed upon name between client and the server. Only when this special request parameter is present, the the Servlet filter will trigger the usage of request wrapper and response wrapper. The other issue with status code is since my Restful web service APIs return an error message in an XML structure in the response body, which allows the Flex client to find out what’s wrong.

With this solution, I am able to continue to Flex to develop my application UI.

Happy flexing everyone.