Adding Interceptors to your Web services

 

Interceptors can be added either via configuration, via annotations or even programmatically. The simplest of all is maybe using annoations. The following example uses two custom interceptors Test1Interceptor which is bound to the Server Incoming Chain and Test2Interceptor which is bound to the Outgoing Chain:

 

@org.apache.cxf.interceptor.InInterceptors (interceptors = {"com.example.Test1Interceptor" })
@org.apache.cxf.interceptor.OutInterceptors (interceptors = {"com.example.Test2Interceptor" })
})
@WebService(
public class SayHiImpl  implements SayHi {
   public long sayHi(long arg) {
     return arg;
   }
 
}

 

and here's how you can configure these interceptors using cxf.xml configuration file:


<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:cxf="http://cxf.apache.org/core"
 xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">

 <bean id="MyInterceptor1" class="com.example.Test1Interceptor"/>
 <bean id="MyInterceptor2" class="com.example.Test2Interceptor"/>
 

 <cxf:bus>
   <cxf:inInterceptors>
     <ref bean="MyInterceptor1"/>
   </cxf:inInterceptors>
   <cxf:outInterceptors>
     <ref bean="MyInterceptor2"/>
   </cxf:outInterceptors>
 </cxf:bus>
</beans>

Adding Fault Interceptors to your Web services

 

In the case of SOAPFaults, a CXF web service will create a separate outbound error handling chain and the client will create an inbound error handling chain. By default, the last interceptor in the outbound fault chain is Soap11FaultOutInterceptor which  automatically appends the details of the SOAPFault into the response. This might now be always desirable as we might want to display a custom error message.

In order to do that you need simply to add a custom OutFaultInterceptors which modifies the details of the SOAPFault. There are many places that we could do this, for example you could do that before the final interceptor (Soap11FaultOutInterceptor) is called.

public class CustomFaultInterceptor extends AbstractSoapInterceptor {

   public CustomFaultInterceptor() {
     super(Phase.MARSHAL);
   }

   public void handleMessage(SoapMessage message) throws Fault {
     Fault fault = (Fault) message.getContent(Exception.class);
     fault.setDetail(createDetailSomehow());
   }

 
}

And here's how you can bind this Interceptor to your Web service using annotations:
@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