Similarities between Handlers and CXF Interceptors

JAX-WS handlers are internally implemented in CXF by use of interceptors, so by definition anything that can be done with the former can be done with the latter. Handlers and interceptors are quite similar, in fact both have a handleMessage()  which is defined by the top level interfaces. Both provide mechanisms for SOAP messages as well as more generic XML over HTTP routing. The routing direction is also similar in that handleFault(), when errors occur, causes the interceptors to run in reverse order.

Differences between Handlers and CXF Interceptors

The most important difference is that CXF Interceptors are split into two main categories: inbound and outgoing interceptors which are used to collect the server incoming message and the outgoing response. On the other hand  when using JAX-WS handlers, the same handlers are activated both on request and reply. When using Handlers you can discriminate if it’s an inbound/outbound message by checking the MessageContext.MESSAGE_OUTBOUND_PROPERTY.

public boolean handleMessage(SOAPMessageContext messageContext)
{
 Boolean outboundProperty = (Boolean)
 messageContext.get (MessageContext.MESSAGE_OUTBOUND_PROPERTY);

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

 System.out.println("** Response: "+messageContext.getMessage().toString());
 return true;
}


Another difference is that an additional close() method,used for object cleanup, is available with handlers but not interceptors.
public void close(MessageContext messageContext)
 {
 }

Another difference is how Fault exception are treated. When using Interceptors you can retrieve the Fault from the MessageContent:
public void handleMessage(SoapMessage message) throws Fault {
 Fault fault = (Fault) message.getContent(Exception.class);
 fault.setDetail(createDetailSomehow());
}

...provided that you registered your Fault Interceptor:


@org.apache.cxf.interceptor.OutFaultInterceptors (interceptors = {"com.sample.CustomFaultInterceptor" })
@WebService(
public class SayHiImpl  implements SayHi {
public long sayHi(long arg) {
return arg;
}

}

0
0
0
s2smodern

Related articles available on mastertheboss.com

JBoss web services

JAX-WS simplifies the development model for a web service endpoin

What is a Web Service One Way invocation?

JBoss recipe of the day

Asynchronous web services with JBoss WS

Developing rigorous and responsive web service client application

How to change the default Web Service deployment Port ?

JBoss recipe of the day

Using Axis Web Services with JBoss

Still not ready for JBoss WS ? if you don't have a JDK 1.5 compli

Invoking JBoss Web Services with Flex

Flex® is a free, open source framework for building highly in