April 24, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Processing Request/Response Messages of a Web Service Using Handler Chain

  • May 10, 2005
  • By Nandhini Arumugam & Jeyarani Venkatasamy
  • Send Email »
  • More Articles »

Web Services are all about processing messages. Handlers are classes that act as pre-processors and post-processors of these messages. A series of Handlers can be invoked in some order. This series of Handlers is called a Handler Chain. The order of invocation is determined by deployment configuration and also whether it's on the client or server side.

Why Use Handlers?

A Handler can be used to modify a SOAP request or response message on the client and the server side. A simple example of using handlers is to encrypt and decrypt secure data in the body of a SOAP message. A client application uses a handler to encrypt the data before it sends the SOAP message request to the Web service. The Web service receives the request and uses a handler to decrypt the data before it sends the data to the back-end component that implements the Web service. The same steps happen in reverse for the response SOAP message.

Another example can be accessing information in the header part of the SOAP message. A SOAP header can be used to store Web Service-specific information and then use handlers to manipulate it. A specific example could be sending the username and password in a SOAP header to an Authentication Handler.

SOAP message handlers also can be used to improve the performance of the Web service. This can be achieved by caching the SOAP response of the frequently used queries. Handlers also can be used to validate the SOAP request and also to log the interactions.

Handler Chain

A Handler is assembled into a chain of handlers known as a Handler Chain. An implementation of a Web Service might require several handler classes to be invoked, one after or before the request/response is served. The JAX-RPC runtime is capable of invoking several handler classes in a chain. This concept is called handler chain.

Handler Chain Model

A Handler class is tied to the service endpoint and can be configured to intercept the SOAP message and perform various operations on it at any of the following points in the client-service communication:

  1. At the client, just after the SOAP request is created and before it is sent to the Web Service.
  2. At the Web Service end, before the SOAP request is processed by the Web Service.
  3. At the Web Service end, before sending the SOAP response to the client.
  4. At the client, just before the JAX-RPC runtime processes the SOAP response received from the Web Service.

A Handler chain can be used with or without a back end component. The following section deals with the different cases of a Handler Chain.



Click here for a larger image.

Figure 1: Handler Chain Model with Back End Component

Figure 1 shows the execution flow of the Handler chain with a back end component. When the client application invokes the Web Service, the SOAP request reaches the handleRequest() of the handler in the handler chain. Once processed, the control is transferred to the Web Service and the Web Service is executed. The handleResponse() of the handler is invoked from the Web Service and the SOAP response is sent to the client.

Figure 2: Handler Chain Model with Back End Component

Example

The example assumes that the underlying server is Weblogic 8.x. This section explains about the Authentication Handler (example) with a back end component as a Balance Enquiry Service. The authentication details here are the account number and the pin, which are set in the SOAP header of the request message. The Authentication Handler retrieves the account number and pin and authenticate them. Once authenticated, the back end component BalanceEnquiry is invoked to get the balance of the account number provided.

Following are the steps for writing a Handler.

  1. The handler class should implement the interface javax.xml.rpc.handler.Handler or extend the GenericHandler class of Weblogic(weblogic.webservice.GenericHandler).
  2. The javax.xml.rpc.handler.Handler interface contains the following methods that are to be implemented:
    • init ()
    • destroy()
    • getHeaders()
    • handleRequest()
    • handleResponse()
    • handleFault()

If weblogic.webservice.GenericHandler is used, override only the necessary methods. In this example, the AuthenticationHandler overrides the handleRequest() and handleResponse() methods. If the handleRequest() method returns true, the next handler in the chain will be invoked. If the handler is the last component in the chain, the back end component (Web Service) will be invoked. In case it returns false, the handleResponse() of that corresponding handler will be invoked.

The AuthenticationHandler's handleRequest() retrieves the account number and pin from the SOAP header and authenticates it and then returns true or false based on the result.

AuthenticationHandler.java

import java.util.Map;import java.util.Iterator;import javax.xml.rpc.JAXRPCException;import javax.xml.rpc.handler.soap.SOAPMessageContext;import javax.xml.rpc.handler.HandlerInfo;import javax.xml.rpc.handler.MessageContext;import javax.xml.namespace.QName;import javax.xml.soap.*;import weblogic.webservice.GenericHandler;public class AuthenticationHandler extends GenericHandler{   private int me = System.identityHashCode(this);   private HandlerInfo handlerInfo = null;   String accountNo                = null;   String pin                      = null;   public void init(HandlerInfo handlerInfo)   {      this.handlerInfo = handlerInfo;   }   public boolean handleRequest(MessageContext messageContext)   {      System.err.println("** handleRequest called in: "+me);      try      {         SOAPMessageContext sctx = (SOAPMessageContext)messageContext;         SOAPMessage message     = sctx.getMessage();         SOAPPart sp             = message.getSOAPPart();         SOAPEnvelope senv       = sp.getEnvelope();         SOAPHeader sh           = senv.getHeader();         Iterator iter =  sh.getChildElements();         if(iter.hasNext())         {            iter.next();                 //skip text            Object obj = iter.next();    //account details            SOAPElement elt = (SOAPElement)obj;            iter = elt.getChildElements();            iter.next();                 //skip text node            obj = iter.next();            elt = (SOAPElement)obj;            accountNo = elt.getValue();            iter.next();                 //skip text node            obj = iter.next();            elt = (SOAPElement)obj;            pin = elt.getValue();         }      }      catch (Exception e)      {         e.printStackTrace();         throw new JAXRPCException(e);      }      if(accountNo.equals("12345") && pin.equals("6789"))         return true;      else         return false;   }   public boolean handleResponse(MessageContext messageContext)   {      System.out.println("Inside HandleResponse");      return true;   }   public QName[] getHeaders()   {      return handlerInfo.getHeaders();   }}




Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel