I am in the situation where my application needs to inspect the content/data/body/payload of a POST request without changing the results of subsequent getParameter calls.
Reading the body from the inputStream:
The body can be read using the InputStream from request.getInputStream
or BufferedReader from request.getReader
.
Reading POST parameters:
POST requests typically include request parameters in the body of the request. These can be retrieved using getParameter
.
The Problem:
the first getParameter
call internally parses the inputStream and inserts all parameters into a parameter HashMap. It requires the inputStream to still contain the contents for parsing. Thus one cannot inspect the content and still have a working getParameter call.
Proposed (but not sufficient) Solution
Create a request wrapper that caches the inputstream and returns the cache for getInputStream.
I've seen that solution suggested all over the web, but it doesn't work, because getParameter
doesn't actually call getInputStream
, but refers to the original inputBuffer buried in the request object. I've tried it, both from within the Servlet and by using a filter
The only solution I can think of involves rewriting getParameter to actually parse the cached inputstream manually. But this feels like a bad idea.
Does anybody have any alternative that works? (This is Tomcat 5.5) This feels like it should be a common use-case; I can't believe how difficult it is.