Saturday, January 11, 2014

Capturing SOAP packets in Apache Chainsaw

The logs from the handler mentioned in JAXWS Handler : Example for logging request / response SOAP packets can be forwarded over a socket to an external listener or a tool like Apache Chainsaw. In order to forward these logs over a socket, a SocketAppender needs to be used. The following modifications needs to be done to SOAPRequestResponseSpitter class. Add the imports
import org.apache.log4j.Logger;
Add the static block
private static Logger LOGGER = Logger.getLogger(SOAPRequestResponseSpitter.class);

 static {
  SocketAppender socketAppender = new SocketAppender("localhost", 4560);
  socketAppender.setReconnectionDelay(10000); // 10 seconds

In the log(..) method, write the soap packet to this logger.
 private void log(SOAPMessageContext smc) {
  Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

  if (outboundProperty.booleanValue()) {
   System.out.println("\nOutbound message:");
  } else {
   System.out.println("\nInbound message:");

  SOAPMessage message = smc.getMessage();
  ByteArrayOutputStream boas = new ByteArrayOutputStream();
  try {
  } catch (Exception e) {
   System.out.println("Exception in handler: " + e);
Note: Above mentioned may be the worst way to configure an appender. A configuration file like log4j.xml or should be used instead. I don't want to go into details of how to configure log4j hence using the simple approach above to avoid distracting from the main focus. Launch Apache Chainsaw and choose the second option, i.e.
    Let me use a simple Receive: SocketReceiver on port 4560 (Default SocketAppender port)
Once this setting is used, all the SOAP packets being intercepted by the handler will be displayed on a tab in the Chainsaw application. Apache Chainsaw & the SocketAppender is being used here to log SOAP packets only. This combination can be used to remotely view any log4j logs transmitted by the SocketAppender.