How can I log with Log4J SOAP request and response in AXIS 1.x?

raspayu picture raspayu · Sep 28, 2012 · Viewed 51.7k times · Source

I am having the next problem:

I want to log the SOAP requests/responses that land on my web service (server side). Trying to configure my web service in the wsdd file. I am always landing on pages like the next one:

How to use the org.apache.axis.handlers.LogHandler

Which recommends to configure the Apeche Axis LogHandler to log the request/response. That is not valid for me, since a)there is no way to link the log4j there, and b)I just am not able to make it work.

Does anyone know a way to make my log4j to log the request/responses?

Answer

raspayu picture raspayu · Sep 28, 2012

So after hours or Googling out there in the web, I decided to get adventurous and program my own handler. Is much easier than expected.

I made a class that extends the abstract class BasicHandler (org.apache.axis.handlers.BasicHandler), and implements the invoke method loging the request or the response. Here is my class, which I have baptized as SOAPLogHandler :

package com.mypackage.axishandlers;

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

public class SOAPLogHandler extends BasicHandler {

private static Logger LOG= Logger.getLogger(SOAPLogHandler.class);
private static final long serialVersionUID = 1L;

@Override
public void invoke(MessageContext msgContext) throws AxisFault {
    if(msgContext.getResponseMessage() != null && msgContext.getResponseMessage().getSOAPPart() != null) {
        LOG.info(" Response = " + msgContext.getResponseMessage().getSOAPPartAsString());
    } else {
        if(msgContext.getRequestMessage() != null && msgContext.getRequestMessage().getSOAPPartAsString() != null) {
            LOG.info(" Request = " + msgContext.getRequestMessage().getSOAPPartAsString());
        }    
    }
}  }

The idea is, to log first the request, and when processed, log the response. So, in the server-config.wsdd (or the wsdd file from your client if you are in the client side), we have to add a handler pointing to that class, and configure it to uses in the request/response chain:

1st add the handler

 <handler name="log" type="java:com.mypackage.axishandlers.SOAPLogHandler"/>

2nd add the use of that handler to the request/response from the http transport (focus on the log handler)

 <transport name="http">
  <requestFlow>
   <handler type="log"/>
   <handler type="URLMapper"/>
   <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
  </requestFlow>
  <responseFlow>
   <handler type="log"/>
  </responseFlow>
...
 </transport>

With that, the magic should be done, and you should receive a pretty log from the request/responses!

Disclaimer: I am not really sure from what will happend if you use some kind of SOAP multipart thing.