• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How overide getInputStream()?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
Carl
[ January 16, 2006: Message edited by: Carl Trapani ]
 
author & internet detective
Posts: 40198
816
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 8943
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this article (customized request and response objects)
http://java.sun.com/products/servlet/Filters.html
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
Bill
 
Carl Trapani
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
Carl
 
Run away! Run away! Here, take this tiny ad with you:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic