peiyuan1030 发表于 2016-12-30 09:34:03

Apache CXF Interceptors

  Apache CXF provides many built-in Interceptors that provide core services to the message that is being exchanged between consumer and service endpoint. These interceptors do the work of marshalling and unmarshalling, manipulating message headers, performing authorization checks, validating the message data, and so on. You can also create your own Interceptors which are able to process incoming and outgoing messages. However before that, you need to learn how Interceptors work: Interceptors are invoked in chain and organized in phases. When a CXF client invokes a CXF server, there is an outgoing interceptor chain for the client and an incoming chain for the server. When the server sends the response back to the client, there is an outgoing chain for the server and an incoming one for the client. Additionally, 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.
  

 
Writing an interceptor is relatively simple. Your interceptor needs to extend from either the AbstractPhaseInterceptor or one of its many subclasses such as AbstractSoapInterceptor.
1.public class MyPhaseInterceptor extends AbstractPhaseInterceptor {
2.public MyPhasedInterceptor() {
3.super(Phase.INVOKE); // Put this interceptor in this phase
4.}
5.public void handleMessage(Message msg) throws Fault {
6.// process the message
7.}
8.}



  

The most important thing you should notice is the constructor: it defines the phase name for your interceptor. When you specify the phase, your interceptor is ordered according to the phase in the chain.

Most of the time you will want to extend from sub-classes of AbstractPhaseInterceptor. For example, using the SoapHeaderInterceptoryou can access more specific information such as the SOAP Header.  
页: [1]
查看完整版本: Apache CXF Interceptors