java.lang.IllegalStateException: getReader() has already been called for this request

后端 未结 4 598
孤街浪徒
孤街浪徒 2020-12-29 04:12

I want to add logging to my Servlet, so I\'ve created Filter which should display request and go to the Servlet. But unfortunately I\'ve encoutered exception:



        
相关标签:
4条回答
  • 2020-12-29 04:28

    The main problem is that you can't read the input both as binary stream and character stream, not even if the one is called in a filter and the other in the servlet.

    0 讨论(0)
  • 2020-12-29 04:36

    As far as I can tell servlets are fundamentally broken in this regard. You can try and work around this problem as outlined here but that causes other mysterious problems when other things try and work with it.

    Effectively he suggests cloning the request, reading the body and then in the the cloned class overriding the getReader and getInputStream methods to return the stuff already retrieved.

    The code I ended up with was this:

    import javax.servlet.ServletInputStream;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletRequestWrapper;
    import java.io.*;
    
    //this class stops reading the request payload twice causing an exception
    public class WrappedRequest extends HttpServletRequestWrapper
    {
        private String _body;
        private HttpServletRequest _request;
    
        public WrappedRequest(HttpServletRequest request) throws IOException
        {
            super(request);
            _request = request;
    
            _body = "";
            try (BufferedReader bufferedReader = request.getReader())
            {
                String line;
                while ((line = bufferedReader.readLine()) != null)
                    _body += line;
            }
        }
    
        @Override
        public ServletInputStream getInputStream() throws IOException
        {
            final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(_body.getBytes());
            return new ServletInputStream()
            {
                public int read() throws IOException
                {
                    return byteArrayInputStream.read();
                }
            };
        }
    
        @Override
        public BufferedReader getReader() throws IOException
        {
            return new BufferedReader(new InputStreamReader(this.getInputStream()));
        }
    }
    

    Anyway this appeared to be working fine until we realised that uploading a file from the browser wasn't working. I bisected through the changes and discovered this was the culprit.

    Some people in the comments in that article say you need to override methods to do with parameters but don't explain how to do this.

    As a result I checked to see if there was any difference in the two requests. However after cloning the request it had identical sets of parameters (both original request + cloned had none) aswell as an identical set of headers.

    However in some manner the request was being effected and screwing up the understanding of the request further down the line - in my case causing a bizaare error in a library (extdirectspring) where something was trying to read the contents as Json. Taking out the code that read the body in the filter made it work again.

    My calling code looked like this:

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException
    {
        HttpServletRequest properRequest = ((HttpServletRequest)request);
    
        String pathInfo = properRequest.getPathInfo();
        String target = "";
        if(pathInfo == null)
            pathInfo = "";
    
        if(pathInfo.equals("/router"))
        {
            //note this is because servlet requests hate you!
            //if you read their contents more than once then they throw an exception so we need to do some madness
            //to make this not the case
            WrappedRequest wrappedRequest = new WrappedRequest(properRequest);
            target = ParseExtDirectTargetFrom(wrappedRequest);
            request = wrappedRequest;
        }
    
        boolean callingSpecialResetMethod = pathInfo.equals("/resetErrorState") || target.equals("resetErrorState");
        if(_errorHandler.IsRejectingRequests() && !callingSpecialResetMethod)
            return;
    
        try {
            filterChain.doFilter(request, response);
        }
        catch (Exception exception) {
            ((HttpServletResponse) response).sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "ERROR");
            _errorHandler.NotifyOf(exception);
        }
    }
    

    I've ommitted the contents of ParseExtDirectTargetFrom but it calls getReader().

    In my case the filter was working for all other requests but the strange behaviour in this case made me realise something wasn't quite right and what I was trying to do (implement sensible exception handling behaviour for tests) wasn't worth potentially breaking random future requests (as I couldn't figure out what had caused the request to become broken).

    Also it's worth noting that the broken code is unavoidable - I assumed it might be something from spring but ServletRequest goes all the way up - thats all you get even if you were making a servlet from scratch by subclassing HttpServlet

    My recommendation would be this - don't read the request body in a filter. You'll be opening up a can of worms that will cause strange problems later on.

    0 讨论(0)
  • 2020-12-29 04:40

    Looks like the restlet framework has called getRequestEntityStream() on the Request object which in turn calls getInputStream(), so calling getReader() on the request throws IllegalStateException. The Servlet API documentation for getReader() and getInputStream() says:

     public java.io.BufferedReader getReader()
        ...
        ...
    Throws:
        java.lang.IllegalStateException - if getInputStream() method has been called on this request
    
     public ServletInputStream getInputStream()
        ...
        ...
        Throws:
        java.lang.IllegalStateException - if the getReader() method has already been called for this request
    

    From the documentation it seems that we cannot call both getReader() and getInputStream() on the Request object. I suggest you use getInputStream() rather than getReader() in your wrapper.

    0 讨论(0)
  • 2020-12-29 04:50

    Use ContentCachingRequestWrapper class. Wrap HttpServletRequest in thi will resolve issue

    Sample : if you want to convert your "HttpServletRequest servletRequest" you can do some thing like

    import org.springframework.web.util.ContentCachingRequestWrapper;
    
    ContentCachingRequestWrapper request = new ContentCachingRequestWrapper(servletRequest);
    

    Hope it helps!!!

    0 讨论(0)
提交回复
热议问题