wood burning stoves*
The moose likes Servlets and the fly likes How overide getInputStream()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How overide getInputStream()?" Watch "How overide getInputStream()?" New topic

How overide getInputStream()?

Carl Trapani

Joined: Apr 21, 2005
Posts: 4
Hi all,

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,
[ January 16, 2006: Message edited by: Carl Trapani ]
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 30929

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.

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Check this article (customized request and response objects)

William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12823
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.

If you wrote your replacement request to a byte[] you could create a ByteArrayInputStream - which implements all of the InputStream methods.
Carl Trapani

Joined: Apr 21, 2005
Posts: 4
Thanks all for your great responses!

I think for now I'm going to go with Jeanne's idea of putting code into a helper class because I'm under the gun and can see exactly how the helper class will work.

I read (and re-read) Pradip's reference - it is a good one for anyone reading this post. Another good reference article about Code Re-use and Filters is Improving Code Reuse With Servlet Filters ...

Bill's answer was exactly what I was looking for because I'm really struggling to get abstract and interface[/] and [I]streams nailed down ... but I'm not there yet.

Thanks again,
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: How overide getInputStream()?