Sunday, September 1, 2013

JAX-WS Handler Processing

JAX-WS Handler mechanism gives us the plugable solution to handle message headers or other part of a message. JAX-WS handler allows us to trap inbound and outbound messages. It also allows us to modify inbound message's header or body, similarly it also allows use to modify outbound message header or body.

JAX-WS handler is not limited to modification in the message. It allow us to add new element in the inbound and outbound message.

The JAX-WS handler can be configured in Client and Server.
  Most of the time JAX-WS handler is used to add a tracking id in the inbound message header and returns same tracking id in the outbound response message header.

In JAX-WS, there are two kinds of Handler. It would be great if I can explain with an example. So for instance I am talking about SOAP message.

In real world, SOAP message is considered as an Envelop. A Envelop has three parts- Header, Body and Attachment.

So, the handler mechanism provides two kinds of Handlers.

  1. Protocol Handler
  2. Logical Handler
In our example, SOAP is a protocol. So, we will be taking about SOAPhandler. Also, we will seeing an example of LogicalHandler.

SOAPHandler - SOAPHandler is a type of Protocol handler. SOAPHandler deals with whose SOAP message, i.e complete Envelop.

LogicalHandler - Logical handler is a protocol independent handler. Logical handler deals with the part of message i.e. payload of the message.

One important thing with handler is we can use the same handler for incoming and outgoing messages.

You can found the Logical and SOAP handler in the following package respectively:
javax.xml.ws.handler.soap.SOAPHandler, javax.xml.ws.handler.LogicalHandler.

Both the Handler interfaces are extends from Handler interface.

In JAX-WS, there is a concept of Handler chain. Handler chain is a list of Handlers. The handler chain can contain mix of Protocol and Logical Handlers.

There is a specific Order of processing of those Handers mentioned in the Handler chain. All the Logical Handlers are preceded with Protocol Handlers. Suppose you have listed Handlers in the following order in the handler chain.

Original Handler List -  L1->P1->P2->L2->P3->L3

In the runtime environment, this order will not be preserved. The List of Handlers will be reorganized like this.

Runtime Handler List -  L1->L2->L3->P1->P2->P3

Now, As I have said in the beginning of this topic is Handler can be used in Inbound and Outbound web service call. So, there are following scenarios we have:

  1.  Outbount Handler processing - The processing of handler will starts from beginning of the Runtime Handler List. e.i. L1->L2->L3->P1->P2->P
  2. Inbound Handler processing - The processing of handler will starts from end of the Runtime Handler List. e.i. P3->P2->P1->L3->L2->L1

A sample Logical handler for Inbound and Outbound request.