I am looking at the implementation of the HiddenMethodFilter in sitebricks here:
At line 65 there is the following code:
try {
String methodName = httpRequest.getParameter(this.hiddenFieldName);
if ("POST".equalsIgnoreCase(httpRequest.getMethod()) && !Strings.empty(methodName)) {
....
It checks if a specific parameter was set and uses that to wrap the request. However, in reading that parameter it will consume the stream and the eventual servlet will not be able read any data.
What would be the best way to avoid this? I implemented the HttpServletRequestWrapper here which reads the contents of the stream into a byte array. This however may use a lot of memory to store the requests.
private HttpServletRequestWrapper getWrappedRequest(HttpServletRequest httpRequest, final byte[] reqBytes)
throws IOException {
final ByteArrayInputStream byteInput = new ByteArrayInputStream(reqBytes);
return new HttpServletRequestWrapper(httpRequest) {
@Override
public ServletInputStream getInputStream() throws IOException {
ServletInputStream sis = new ServletInputStream() {
@Override
public int read() throws IOException {
return byteInput.read();
}
};
return sis;
}
};
}
Is there a better way? can we read the parameter without consuming the stream? (Some thing similar to peek) can we reset the stream?
If you are using POST
requests and read parameters from the httpRequest
this will affect the InputStream
and you will have problems in other parts needing to read it.
This is stated in ServletRequest#getParameterjavadoc:
If the parameter data was sent in the request body, such as occurs with an HTTP POST request, then reading the body directly via getInputStream() or getReader() can interfere with the execution of this method.
The ServletInputStream
is derived from InputStream
and inherits the markSupported
reset
etc which are actually no-ops and so you can not reset a ServletInputStream
.
This means that you will have to consume it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With