|
Page Edited :
CXF20DOC :
Interceptors
Interceptors has been edited by Raphaƫl Corre (Nov 08, 2007). Content:Interceptors and PhasesInterceptors are the fundamental processing unit inside CXF. When a service is invoked, an InterceptorChain is created and invoked. Each interceptor gets a chance to do what they want with the message. This can include reading it, transforming it, processing headers, validating the message, etc. Some examples of interceptors inside CXF include:
InterceptorChains are divided up into Phases. Each phase contains many interceptors. On the incoming chains, you'll have the following phases:
On the outgoing chain there are the following phases:
InterceptorProvidersSeveral different components inside CXF may provide interceptors to an InterceptorChain. These implement the InterceptorProvider interface: public interface InterceptorProvider { List<Interceptor> getInInterceptors(); List<Interceptor> getOutInterceptors(); List<Interceptor> getOutFaultInterceptors(); List<Interceptor> getInFaultInterceptors(); }
Writing an InterceptorWriting an interceptor is relatively simple. The best way to do this is to extend from an abstract class. Practically speaking, there are two base classes you may be concerned in. The first is the AbstractPhaseInterceptor. The second is AbstractSoapInterceptor. The only difference between the first and second is that the later will give you a SoapMessage instead of just a Message class. This allows you to access the SOAP headers and version. Here is a small example of how to write a Soap interceptor: public class MySoapInterceptor extends AbstractSoapInterceptor { public MySoapInterceptor () { super(Phase.PRE_PROTOCOL); } public MySoapInterceptor (String phase) { super(phase); } public void handleMessage(SoapMessage msg) throws SoapFault { System.out.println("Hello World!"); } } To add this to your server, you'll want to get access to the Server object (see here for more info): MySoapInterceptor myInterceptor = new MySoapInterceptor();
Server server = serverFactoryBean.create();
server.getEndpoint().getInInterceptor().add(myInterceptor);
On the Client side the process is very similar: FooService client = ... ; // created from ClientProxyFactoryBean or generated JAX-WS client MySoapInterceptor myInterceptor = new MySoapInterceptor(); Client cxfClient = ClientProxy.getClient(client); cxfClient .getInInterceptor().add(myInterceptor); You may also want to specify what phase your interceptor is in. To do this, you can simply set the phase: public class MySoapInterceptor extends AbstractSoapInterceptor { public MySoapInterceptor() { super(Phase.USER_PROTOCOL); } ... } You can also express that you would like your interceptor to run before/after certain other interceptors in the same phase: public class MySoapInterceptor extends AbstractSoapInterceptor { public MySoapInterceptor() { super(Phase.USER_PROTOCOL); getAfter().add(SomeOtherInterceptor.class.getName()); } ... } |
Unsubscribe or edit your notifications preferences
