I am in a situation where my application must check the contents / data / body / payload of a POST request without changing the results of subsequent calls to getParameter.
Reading the body from the input stream:
The body can be read using InputStream from request.getInputStream or BufferedReader from request.getReader .
Reading POST parameters:
POST requests typically include request parameters in the request body. They can be obtained using getParameter .
Problem:
The first call to getParameter internally parses the input stream and inserts all parameters into the HashMap parameter. This requires the inputStream to still contain parsing content. Thus, you cannot check the contents and have a getParameter working call.
Proposed (but not sufficient) solution
Create a request wrapper that caches the input stream and returns a cache for getInputStream.
I saw that this solution is offered all over the Internet, but it does not work, because getParameter does not actually call getInputStream , but refers to the original inputBuffer buried in the request object. I tried this, both from Servlet and using a filter
The only solution I can think of involves rewriting getParameter to actually analyze the cached input stream manually. But this seems like a bad idea.
Does anyone have an alternative that works? (This is Tomcat 5.5). It seems like this should be a common use case; I can’t believe how difficult it is.
rewolf
source share