Advertise with us

Our eBooks

Apache CXF Interceptors

There are two ways to apply CXF Interceptors in your applications:

1) The recommended approach is to configure them in the jaxws-endpoint-config.xml descriptor of your application or, if you want to set them for all endpoints, in your webservices sibsystem

2) The legacy approach is to use CXF annotations in your code. This can be applied for older WildFly versions (versions 8-10)

Let's start from the first approach. You can include the jaxws-endpoint-config.xml descriptor in the WEB_INF folder of your application. Within this file, you will specify a list of interceptor class names for the "cxf.interceptors.in" and "cxf.interceptors.out". Example:

<?xml version="1.0" encoding="UTF-8"?>
<jaxws-config xmlns="urn:jboss:jbossws-jaxws-config:4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:javaee="http://java.sun.com/xml/ns/javaee"
  xsi:schemaLocation="urn:jboss:jbossws-jaxws-config:4.0 schema/jbossws-jaxws-config_4_0.xsd">
  <endpoint-config>
    <config-name>SampleWSConfig</config-name>
    <property>
      <property-name>cxf.interceptors.in</property-name>
      <property-value>com.sample.DemoRequestInterceptor</property-value>
    </property>
    <property>
      <property-name>cxf.interceptors.out</property-name>
      <property-value>com.sample.DemoResponseInterceptor</property-value>
    </property>
  </endpoint-config>
</jaxws-config>

As an alternative, you can set the same properties in the webservices subsystem of your WildFly configuration:

  <subsystem xmlns="urn:jboss:domain:webservices:2.0">
            <wsdl-host>${jboss.bind.address:127.0.0.1}</wsdl-host>
            <endpoint-config name="Standard-Endpoint-Config">
                <property name="cxf.interceptors.in" value="com.sample.DemoRequestInterceptor"/>
            </endpoint-config>

Using CXF Annotations in your Web services (deprecated)

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;
    }
 
}
© 2021 mastertheboss.com. All Rights Reserved.

Please publish modules in offcanvas position.