I have written a servlet that processes and responds to xml requests from a Flash client. I now need this servlet to catch and respond to a new request/response format, but the work it does is exactly the same. So, I'm hoping to map the servlet to two different URLs and then configure a Filter for the new URL that will transform the new request/response formats into/from the original request/response formats so that I can re-use my original servlet to do the work.
In the original request/response format, the request body is an xml document. So, the servlet loads the request body into an xml document using the ServletRequest.getInputStream() method. It then extracts the parameters from the xml document, does its work and returns its results as an xml document.
In the new request format, the input parameters will be posted in the normal HTTP POST way (NOT as an xml document) and the response needs to be WordML.
How do I go about getting the new POST parameters into the original request format (as xml in the body of request) so that the servlet can call ServletRequest.getInputStream() and get what it expects - a ServletInputStream with the request parameters presented as xml?
My thoughts are to create a request wrapper class that extends HttpServletRequestWrapper and in that class overide the getInputStream() method and return a sub-class of the abstract ServletInputStream class. But, ServletInputStream indicates that all sub-classes have to impliment read(). I'm stumped about how I would impliment the abstract java.io.InputStream.read() method.
In the Filter, I would intercept the new format requests (based on the URL) and pass the request wrapper that overides getInputStream() in the Chain.doFilter() method. I would also pass a response wrapper so that I could alter the response created by the servelt after it has finished.
Can anyone see an easier/better way to accomplish what I'm trying to do? Any tips or suggestions would be greatly appreciated. I can post code stubs of my ideas if that would help.
Thanks much, Carl [ January 16, 2006: Message edited by: Carl Trapani ]
Carl, Another approach is to take the code out of the original servlet and put it in a helper class. Then you could have servlets that read in the values from Xml or form parameters and put them in an object. The helper class does the work based on the values in that object. Then the servlet takes an object returned from the helper class and converts it into XML or WordML. You could use XSLT for the transformation from XML to WordML if you want.
The helper class allows you to separate the logic you want to reuse from the presentation.