Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

soap

log4j

axis

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?

like image 912
raspayu Avatar asked Sep 28 '12 10:09

raspayu


People also ask

How do you trace a SOAP request and response?

In SOAP web service, each HTTP request or response encapsulates a SOAP envelope, these messages are easy to trace by using Eclipse IDE, build-in “TCP/IP monitor” tool. The idea is host another server in between the client and server to perform port forward function to intercept the HTTP traffic.

Does Log4J use Axis?

The JCL SPI (and hence Axis) uses Log4J by default if it is available (in the CLASSPATH).


2 Answers

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.

like image 145
raspayu Avatar answered Oct 10 '22 02:10

raspayu


Save this file as "client-config.wsdd" in the working directory as you do for log4j.properties .

If you don't want to alter any code and debug your axis web service client, you can follow this method to log all incoming and outgoing soap messages.

<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

<handler name="log" type="java:org.apache.axis.handlers.LogHandler" >
    <parameter name="LogHandler.fileName" value="c:/logs/axis.log"/>
</handler>

<globalConfiguration>
    <requestFlow>
        <handler type="log" />
    </requestFlow>
    <responseFlow>
        <handler type="log" />
    </responseFlow>
</globalConfiguration>

<transport name="http"
    pivot="java:org.apache.axis.transport.http.HTTPSender" />

</deployment>
like image 31
Karan Avatar answered Oct 10 '22 02:10

Karan