DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Debugging SOAP by Logging the Incoming HttpRequest

Debugging SOAP by Logging the Incoming HttpRequest

$$anonymous$$ user avatar by
$$anonymous$$
·
Jan. 29, 13 · Interview
Like (0)
Save
Tweet
Share
5.31K Views

Join the DZone community and get the full member experience.

Join For Free

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 ;-)

SOAP Web Protocols

Published at DZone with permission of $$anonymous$$. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Why It Is Important To Have an Ownership as a DevOps Engineer
  • Writing a Modern HTTP(S) Tunnel in Rust
  • SAST: How Code Analysis Tools Look for Security Flaws
  • Why Open Source Is Much More Than Just a Free Tier

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: