6

How do I view a message from a filter?

 3 years ago
source link: https://www.codesd.com/item/how-do-i-view-a-message-from-a-filter.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How do I view a message from a filter?

advertisements

I tried to use a javax.servlet.Filter to peek into a message.

public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
     BufferedReader reader = request.getReader();
     if ( reader.markSupported() ) {
         reader.mark( contentLen );
     }
     String content = reader.readLine();

     // search some pattern

     if ( reader.markSupported() ) {
         reader.reset();
     }
     chain.doFilter( request, response );
}

The servlet that finally receives the request throws this exeption:

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

Which is correct behaviour according to the javadoc.

My questions is how can I read the content of the input-stream anyway?

I also tried ServletInputStream is = request.getInputStream();


Not tested, but you could probably

  • read all the bytes from the request input stream and write them to a byte array,
  • construct a buffered reader over this byte array to read what you want in the filter,
  • construct a HttpServletRequestWrapper which overrides getInputStream() to return a stream over the byte array,
  • pass this wrapper to the filter chain.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK