Platinum Partner
architects,high-perf,performance,tips and tricks

Debugging SOAP by Logging the Incoming HttpRequest

As I described here I had a situation in which we wanted to be able to store all incoming SOAP requests for debugging purposes. The thing that made it a little tricky was that the logging had to be done before the request reached the XFire servlet and after JBoss had decoded the request (it is sent using https) and made it readable for us, humans.
Now I am aware of the HTTPtunnelers and other tools out there that could be used for this, but to find one that works according to our demands and to find out how it works, we decided it would easier/quicker to create a Filter and use that for our needs.
I don’t know about you, but this is really a thing that just has to go wrong with the first attempt. The idea we had for the filter was to do something like this:

  • get the request
  • get the inputstream
  • log the inputstream to log file
  • continue the normal process

So we build a filter with the following code (only the main part is shown):

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
         throws IOException, ServletException {

      InputStream is = request.getInputStream();
      LOG.info( slurp(is));
      is.close();
      chain.doFilter(request, response);
   }

   private String slurp(InputStream is) throws IOException {
      StringBuffer out = new StringBuffer();
      byte[] b = new byte[4096];
      for (int n; (n = is.read(b)) != -1;) {
          out.append(new String(b, 0, n));
      }
      return out.toString();
  }
When you try this implementation of the filter you will definitely receive errors in your web service! This is caused by the fact that the inputstream is already processed in the filter so you can not process it again in your web service/servlet: it just doesn’t receive a body of the request anymore.
After we realized this, I remembered an example of the book ‘Head First Servlets and JSP’. In that book they have a similar problem and to solve it they make their own implementation of the HttpServletRepsonse by constructing a wrapper around the original one. We quickly realized we had to do the same thing here but then for the request. So what we will do in our filter implementation is that we will pass an instance of our LoggingRequest to the chain. In our loggingRequest everything that is read from the inputstream is copied to a stringbuffer and when we get back in our filter (when the reponse is sent back to the client) we get the content of that stringbuffer and we have our desired output. Sounds nice, huh? Well, here is the code of the whole filter:
package net.pascalalma.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LogRequestFilter implements Filter {

   private static final Log LOG = LogFactory.getLog(LogRequestFilter.class);

   /**
    * DoFilter method.
    */
   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
         throws IOException, ServletException {
      LoggingRequest req = new LoggingRequest((HttpServletRequest)request);
      chain.doFilter(req, response);
      LOG.info(req.getPayload());
   }

   /**
    * Init method.
    */
   public void init(FilterConfig config) throws ServletException {
   }

   /**
    * Destroy method.
    */
   public void destroy() {
   }

   /* Private class definitions */
   private class LoggingRequest extends HttpServletRequestWrapper {

      private LoggingInputStream is;

      public LoggingRequest(HttpServletRequest request) throws IOException {
         super(request);
         this.is = new LoggingInputStream(request.getInputStream());
      }

      @Override
      public ServletInputStream getInputStream() throws IOException {
         return is;
      }

      public String getPayload() {
         return is.getPayload();
      }
   }

   private class LoggingInputStream extends ServletInputStream {

      private StringBuffer bfr = new StringBuffer();
      private ServletInputStream is;

      public LoggingInputStream(ServletInputStream is) {
         super();
         this.is = is;
      }

      // Since we are not sure which method is used just overwrite all 4 of them:
      @Override
      public int read() throws IOException {
         int ch = is.read();
         if (ch != -1) {
            bfr.append((char)ch);
         }
         return ch;
      }

      @Override
      public int read(byte[] b) throws IOException {
         int ch = is.read(b);
         if (ch != -1) {
            for (byte byteSingle: b )
              bfr.append( (char)byteSingle);
         }
         return ch;
      }

      @Override
      public int read(byte[] b, int o, int l) throws IOException {
         int ch = is.read(b,o,l);
         if (ch != -1) {
            for (byte byteSingle: b )
               bfr.append( (char)byteSingle);

         }
         return ch;
      }
      @Override
      public int readLine(byte[] b, int o, int l) throws IOException {
         int ch = is.readLine(b,o,l);
         if (ch != -1) {
            bfr.append(b);
         }
         return ch;
      }

      public String getPayload() {
         return bfr.toString();
      }
   }
}
So although it took a little longer then we had planned, it sure was fun to do ;-)

Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}