开发者

Production-ready request wrapper for buffering request body on Tomcat

We need a HttpRequestWrapper, which is able to buffer POST/PUT request body, so that we can for example print it in log messages. The standard HttpServletRequest doesn't let us cache the body: if you read it once, it is gone, because it's a stream. We created simple wrapper class, which reads the body, stores it as String, and overrides getReader() / getInputStream() methods so that they read the body from that String. However, now we discovered that it doesn't work when you call getParameter*() methods (they always return null开发者_JAVA技巧/empty) - probably because those methods are delegated to the wrapped, original request, which is not aware of buffered body, and it tries to read the original stream which is already gone.

So, we probably need to override all parameter-related methods in POST requests (getParameter, getParameterMaps etc) - probably we'll have to manually parse the body and retrieve the params, which doesn't sound like easy task. We need to use it in production environment, so I would rather find some existing, bullet-proof code, instead of inventing our own.

But the strange thing is that I wasn't able to find library which would offer such class. Can anybody point me to such library? Have you already use such code in public, production environment?

I've also seen some internal Tomcat classes, whose names sound promising, like "BufferedInputFilter" (http://tomcat.apache.org/tomcat-6.0-doc/api/org/apache/coyote/http11/filters/BufferedInputFilter.html) - but this filter doesn't implement standard ServletFilter, so I don't whether I can use it and how.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜