1

In our case, we want to get raw POST body and use request.getParameter() at the same time. But if we call request.getParameter(), we would not able to read raw body.

I found a solution for servlet 2.x Http Servlet request lose params from POST body after read it once.

But for servlet 3.x, ServletInputStream API is changed. The ServletInputStream now has abstract isReady(), isFinished() and setReadListener() to deal with non-blocking IO which must be implemented. Any idea to do this in servlet 3.x?

And for tomcat 7, it seems it doesn't call getInputStream when parsing parameter.

Community
  • 1
  • 1
bydsky
  • 1,604
  • 2
  • 14
  • 30
  • it is not clear from your link what your solution for servlets 2.x was and why it does no longer work in servlets 3.x – wero Jul 30 '15 at 13:15
  • I have updated the link to the answer of the question and add the detail change of ServletInputStream – bydsky Jul 30 '15 at 13:21

1 Answers1

2

The new Servlet 3.1 methods in ServletInputStream could be implemented as follows, extended the linked solution:

public class CachedServletInputStream extends ServletInputStream {
    private ByteArrayInputStream input;

    public CachedServletInputStream(byte[] cached) {
      /* create a new input stream from the cached request body */
      input = new ByteArrayInputStream(cached);
    }

    public int read() throws IOException {
        return input.read();
    }

    public boolean isFinished() {
        return input.available() == 0;
    }

    public boolean isReady() {
        return true;
    }

    public void setReadListener(ReadListener listener) {
        listener.onAllDataRead();
    }
}

Not sure if the ReadListener also needs a callback to onDataAvailable(). Alternatively you could also throw an UnsupportedOperationException from that method which maybe never is called in your application.

wero
  • 32,544
  • 3
  • 59
  • 84