Debugging SOAP by Logging the Incoming HttpRequest
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

Published at DZone with permission of $$anonymous$$. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Writing a Vector Database in a Week in Rust
-
Send Email Using Spring Boot (SMTP Integration)
-
Database Integration Tests With Spring Boot and Testcontainers
-
How Web3 Is Driving Social and Financial Empowerment
Comments