Wednesday, October 31, 2007

Logging WebService Methods Called in Axis using a Handler

When you look in the access log of a server hosting web services, you are unable to see which web service methods, that are actually called. This is due to the fact that SOAP requests are HTTP POST requests, where the method called is a function of the SOAP message posted.

If you are using axis, there is a simple way to log which methods that are called. This can be done by implementing a handler and configure it into the chain of handlers, that axis executes on each request.

Here is the handler:
package com.blogspot.techpolesen.webservice.util;

import org.apache.axis.AxisFault;
import org.apache.axis.Handler;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.BasicHandler;
import org.apache.log4j.Logger;

public class LogMethodCalledAxisHandler extends BasicHandler {
private static final Logger LOG = Logger.getLogger("com.blogspot.techpolesen.webservice.util.LogMethodCalledAxisHandler");

public void invoke(MessageContext msgContext) throws AxisFault {
Handler serviceHandler = msgContext.getService();
String serviceName = serviceHandler == null ? null : serviceHandler.getName();
String methodName = msgContext.getOperation().getMethod().getName();
LOG.info("SOAP Method called: [" + methodName + "] in [" + serviceName + "]");
}
}
You then package this class in your war and put this into your axis deployment wsdd file:
<requestFlow>
<handler type="java:com.blogspot.techpolesen.webservice.util.LogMethodCalledAxisHandler"/>
</requestFlow>
This will make axis call this handler as part of the chain of handlers called for each SOAP request. In my implementation I simply log service name and method called using log4j.

BTW: This post was done with axis1 as the basis. In axis2, the handler concept has shifted a bit into modules and deployment is also done differently than the wsdd.

3 comments:

Anonymous said...

Thanks for the excellent article. Do you happen to know on how to log the Incoming SOAP Request and outgoing SOAP Response with the xml payload.

Thanks

Per Olesen said...

Yeah, kinda. If you have log4j configured in the application of yours that uses axis, you can set DEBUG level on this category:

org.apache.axis.transport.http.HTTPSender

This will make axis log everything that goes over the wire. Beware though, that this is A LOT of information. But hey, if you're debugging.

A little side note: Axis also logs uncaught exceptions from your implementations on the server-side as DEBUG level on this category:

org.apache.axis.EXCEPTIONS

Anonymous said...

i don't understand how can I log org.apache.axis.transport.http.HTTPSender. Coudl describe some example?

Thanks