Read HTTPServletRequest's POST body AND then call getParameter in Tomcat

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 12:24:16

(That's a rather old tomcat, I'm assuming upgrading to a more modern one isn't an option.)

What you want to do will require intercepting the construction of the concrete HttpServletResponse object wrapping the underlying InputStream. Wrapping that InputStream in a push-back input stream (or equivalent) is necessary.

Tomcat 5.5 is so old I can't even think how that would be accomplished 'normally', but perhaps you could write a filter that uses reflection to reach in and swap the InputStream object inside the concrete request object.

As suggested by @caskey, a possible solution would be to use reflection to replace the inputBuffer with a replayable inputbuffer. But I did not use that approach as it felt naughty.

Instead I created a request wrapper in a filter which reads the inputstream into a byte array and returns a new InputStream that internally uses a ByteArrayInputStream around that array for all getInputStream calls.

After reading the inputstream to the byte array, I create a parameter map by parsing the payload. I merged the superclass' parameter map in to support GET cases with query parameters. I've overridden all the getParameter*() methods to use this parameter map.

I used apache.axis.utils.IOUtils.readFully to easily read the stream in to the byte array. And I'm currently using javax.servlet.http.HttpUtils.parsePostData to parse the data into parameter map. HttpUtils.parsePostData is actually deprecated, so I'll likely replace it with a better version when I find it.

But this works, yay!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!