2012년 9월 24일 월요일

[TechNote] WebSphere Application Server does not support double reading of the input stream

WebSphere Application Server does not support double reading of the input stream

Problem(Abstract)

Calls to the getParameter family of methods fail after the input stream has been read from a request.

Cause

When a request's HTTP method is POST, the request parameters are sent with the post form data. If the input stream is read, then the request parameters are no longer available by way of the getParameter family of methods. The Web Container will log the following exception:

SRTServletReq E SRVE0133E: An error occurred while parsing parameters. java.io.IOException:SRVE0216E: post body contains less bytes than specified by content-length
at com.ibm.ws.webcontainer.servlet.RequestUtils.parsePostData(RequestUtils.java:538)

at com.ibm.ws.webcontainer.srt.SRTServletRequest.parseParameters(SRTServletRequest.java:1159)
at com.ibm.ws.webcontainer.srt.SRTServletRequest.getParameterMap(SRTServletRequest.java:1666)
at javax.servlet.ServletRequestWrapper.getParameterMap(ServletRequestWrapper.java:211)

Resolving the problem

It is working as designed. As stated in the Servlet Specification, the restriction on WebSphere® Application Server is that it cannot call the read method on the return object from HttpServletRequest#getInputStream(). The following scenarios show how to cause this problem.

The problem occurs because:
  1. When the request arrives in the server, the Web Container request wrapper object notes the existence of parameters in the request input stream.
  2. As part of filter processing the method, Class.writeToMemory() reads the parameters from the input stream.
  3. Subsequently the method, Class.setParameters(), calls super.getParameterMap() (in a debug statement). This results in the Web Container request wrapper getParameterMap() being called. This leads to the Web Container trying to read the parameters from the input stream (as it noted in step 1) only to find that they have already been read at step 1. This causes the observed error (post body contains less bytes than specified by content-length).

    Because the call that causes the error is in a debug statement, does the problem go away if the debug statement is removed? Also, is this the way the user intends the application to work? The user has written their own request wrapper object and as a result the Web Container request wrapper should not be involved in parsing parameters.

댓글 없음:

댓글 쓰기