Tuesday 17 July 2012

How to use Soap Handler Class to get Web Service Response?


import java.io.ByteArrayOutputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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;
import org.xml.sax.InputSource;

import pack.DefineStaticVariables;

import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
import org.w3c.dom.Document;

public class WebServiceResponseHandler implements SOAPHandler<SOAPMessageContext>
{
    public static String getSOAPResponse;
    public static String outputSoapMsg;
    DBCustom objDBCustom =null;
   
    public WebServiceResponseHandler()
    {
        objDBCustom = DefineStaticVariables.objDBCustom;
    }
   
    public void init(Map mapCollection)
    {
        objDBCustom.logDebugInfo("In init function..");
    }

    public boolean handleMessage(SOAPMessageContext messageContext)     {
        objDBCustom.logDebugInfo("In handleMessage() function..");
        getRequestResponse(messageContext);
        return true;
    }

    public boolean handleFault(SOAPMessageContext messageContext) {
        objDBCustom.logDebugInfo("In handleFault() function..");
        getRequestResponse(messageContext);
        return true;
    }

   
    public void close(MessageContext messageContext) {
        objDBCustom.logDebugInfo("In close() function..");
    }
   
    public void destroy() {
        objDBCustom.logDebugInfo("In destroy() function..");
    }
    public void getRequestResponse(SOAPMessageContext messageContext)
    {       
        objDBCustom.logDebugInfo("In getRequestResponse() function..");
        SOAPMessage objSOAPMessage = null;
        ByteArrayOutputStream objByteArrayOS =null;
        Boolean outboundProperty;
        Document objDocument = null;
        InputSource objInputSource =null;
        DocumentBuilderFactory documentBuilderFactory = null;
        DocumentBuilder documentBuilder =null;
        OutputFormat outFormat = null;
        Writer streamWriter = null;
        XMLSerializer serializeXML = null;
       
         try
        {
           
            objSOAPMessage = messageContext.getMessage();
           
            objByteArrayOS = new ByteArrayOutputStream();
            objSOAPMessage.writeTo(objByteArrayOS);
            outboundProperty = (Boolean) messageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
           
            if (!outboundProperty.booleanValue())
            {
                documentBuilderFactory = DocumentBuilderFactory.newInstance();
                documentBuilder = documentBuilderFactory.newDocumentBuilder();
                objInputSource = new InputSource(new StringReader(objByteArrayOS.toString()));
                objDocument = documentBuilder.parse(objInputSource);
                outFormat = new OutputFormat(objDocument);
                outFormat.setIndenting(true);
                outFormat.setIndent(2);
               
                streamWriter = new StringWriter();
                serializeXML = new XMLSerializer(streamWriter, outFormat);
                serializeXML.serialize(objDocument);
                getSOAPResponse = streamWriter.toString();
                objDBCustom.logDebugInfo("Web Service Response : " + getSOAPResponse);
               
                if(!getSOAPResponse.equals(""))
                {
                           myDBCustom.gResponseData.append(getSOAPResponse);
                }
                else
                     objDBCustom.logDebugInfo("Unable to get response Data");
            }
            else
            {
                 objDBCustom.logDebugInfo("In getRequestResponse() function: Web Service request sent");
            }
       }   
       catch (Exception errorException)
       {
            objDBCustom.logErrorInfo("Exception in logging handler: " + errorException.toString());           
       }
    }
}


Interface:  javax.xml.ws.handler.soap.SOAPMessageContext;
The interface SOAPMessageContext provides access to the SOAP message for either RPC request or response.

Implementation Class: com.sun.xml.internal.ws.handler.SOAPMessageContextImpl



In XML FILE:

<handler-chains xmlns="http://java.sun.com/xml/ns/javaee"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://java.sun.com/xml/ns/javaee">
    <handler-chain>
        <handler>
            <handler-name>WebServiceResponseHandler</handler-name>
            <handler-class>MasterProcess.WebServiceResponseHandler</handler-class>
        </handler>
    </handler-chain>
</handler-chains>

In Main Service File
@HandlerChain(file = "../Conf/ChainHandle.xml")
public class WSObjectContext extends Service
{
}

@Handler Chain, Associates the Web Service with an externally defined handler chain.

No comments:

Post a Comment