This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes Servlets and the fly likes Replace request data using Filter - HttpServletRequestWrapper Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Replace request data using Filter - HttpServletRequestWrapper" Watch "Replace request data using Filter - HttpServletRequestWrapper" New topic

Replace request data using Filter - HttpServletRequestWrapper

Kee Bler

Joined: Sep 27, 2004
Posts: 2
Here's my problem. The software I'm using has a servlet that listens for XML feeds (XML posted via HTTP). As you can image, it is expecting the XML to adhere to a certain schema. Now...I have a little integration project where the XML that will be sent has a different structure. I want to add a filter to intercept the request and transform the XML into the format that the software expects before passing it along to the servlet.

The XML is posted w/a utility class that creates an HttpURLConnection and writes the XML straight into the HttpURLConnection's OutputStream (that is to say, it is not parameterized). Please, don't ask...

Is there anyway to use HttpServletRequestWrapper to read the the data (using the request's BufferedReader), transform the XML and then put the new XML into wrapped request - essentially replacing the the original data (so that I don't have to muck with any of the servlet's code)??

I'm guessing I'm up the proverbial creek here, but thought someone on this forum with more brain cells and experience might have a slick trick up their sleeve.

Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
It sounds like you already know what you need to do .... extending the wrapper to implement whatever functionality you're trying to do is the right creek.

Kee Bler

Joined: Sep 27, 2004
Posts: 2
I guess what I am struggling with is how do I replace the body of the request? I can call the getReader() method but how do I change/replace the character stream from the old XML to the new XML - meaning, how do I swap in a new character stream into the request's BufferedReader?

I don't want to have to touch the servlet code at all but the servlet simply calls the request's getReader() method and expects the XML to be in a certain format. I might be on the right creek, but I sure feel like I don't have a paddle...


rocket mass heater kickstarter
subject: Replace request data using Filter - HttpServletRequestWrapper