Thursday, August 7, 2014

Printing SOAP request/response messages while invoking web services in Java

In this post, we will see how to print SOAP messages sent from SOAP client and received from SOAP services in java. Also how to format the SOAP messages in Java so that they can be easily readable.

For how to create a webservice client you can refer to this post: JAX-WS Web Service Client for Java

Lets consider the service name as 'TestService' . So if you generated the webservice client as mentioned in above post using wsimport this should have generated below 2 java files out of others.

TestServiceService.java
TestService.java

To print SOAP request/response messages we have to write a SOAP handler and have to add that handler to the generated client's handler list.

Here is the code for adding the handler to SOAP client.

TestServiceService serviceImpl = new TestServiceService();
Testservice testservice = serviceImpl.getTestServicePort();

BindingProvider bindingProvider = (BindingProvider) testservice;
Binding binding = bindingProvider.getBinding();
List<Handler> handlerList = binding.getHandlerChain();

handlerList.add(new SOAPMessageWriterHandler());

This is the code for SOAP handler : SOAPMessageWriterHandler

import java.io.ByteArrayOutputStream;
import java.util.Set;

import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
  
public class SOAPMessageWriterHandler implements SOAPHandler<SOAPMessageContext> {  
  
    public boolean handleMessage(SOAPMessageContext smc) {  
  
        Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);  
  
        SOAPMessage message = smc.getMessage();  
  
        try
  {
   if (!outboundProperty.booleanValue()) {  
    System.out.println("SOAP Response : ");
    message.writeTo(System.out);   
   } else {  
    System.out.println("SOAP Request : ");
       message.writeTo(System.out);
   } 
  }
  catch (Exception e)
  {  
   e.printStackTrace();
  }    
        return outboundProperty;    
    }  
  
    public Set getHeaders() {  
        return null;  
    }  
  
    public boolean handleFault(SOAPMessageContext context) {  
        return true;  
    }  
  
    public void close(MessageContext context) {  
    }  
}  

Formatting SOAP messages

Above handler will print single line SOAP messages. If you want to format the SOAP messages you can use below code for that.

For this, you have to write a method that will convert SOAP message to string and then format the string using XML API of Java as below.

public static void printFormattedXML(SOAPMessage message) throws Exception
{
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
 message.writeTo(bout);
    String xml = bout.toString();

 Source xmlInput = new StreamSource(new StringReader(xml));
 StringWriter stringWriter = new StringWriter();
 StreamResult xmlOutput = new StreamResult(stringWriter);
 TransformerFactory transformerFactory = TransformerFactory.newInstance();
 transformerFactory.setAttribute("indent-number", 5);
 Transformer transformer = transformerFactory.newTransformer();
 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
 transformer.transform(xmlInput, xmlOutput);
 String xmlString = xmlOutput.getWriter().toString();

 System.out.println(xmlString);
}

You can use this method in handler to format and print the messages.
Just replace System.out.println with this method.

1 comment:

Creating and Deploying Java Web Application on AWS using Elastic Beanstalk

This tutorial is for creating simple java web application using eclipse and then deploying it on AWS cloud. Video tutorial for creating/de...