java CXF之Interceptors拦截器的编写
[来源] 达内 [编辑] 达内 [时间]2012-09-20
1. Interceptors and Phases(拦截器与阶段)
Interceptors是CXF内部的根本处理单元。当服务被调用时,一个InterceptorChain被创建并调用。每一个interceptor都有机会做他们想要处理的消息。包括:读取,转化,处理头部,验证消息 ,等。
Interceptors可以用于CXF的客户端和服务端。当一个CXF客户端调用一个CXF服务端的时候,客户端有一个传出的interceptor链,服务端有一个传入的interceptor链。当服务端发送响应给客户端时,服务端有一个传出的interceptor链,客户端有一个传入的interceptor链。此外,在SOAPFaults情况下,一个CXF web服务将创建单独的对外输出错误处理链,客户端将创建一个传入的错误处理链。
InterceptorChains被划分成阶段。每个阶段可以包含许多interceptors. 在传入链中,你将有以下几个阶段:
Phase ---------------------------------------------Functions
-----------------------------------------------------------------------------------------------------------------
RECEIVE ------------------------------------------Transport level processing
(PRE/USER/POST)_STREAM --------------------Stream level processing/transformations
READ ----------------------------------------------This is where header reading typically occurs.
(PRE/USER/POST)_PROTOCOL ----------------Protocol processing, such as JAX-WS SOAP handlers
UNMARSHAL --------------------------------------Unmarshalling of the request
(PRE/USER/POST)_LOGICAL -------------------Processing of the umarshalled request
PRE_INVOKE --------------------------------------Pre invocation actions
INVOKE --------------------------------------------Invocation of the service
POST_INVOKE ------------------------------------Invocation of the outgoing chain if there is one
传出链中,也有几个阶段:
Phase Functions
----------------------------------------------------------------------------------------------------------------
SETUP ----------------------------------------------Any set up for the following phases
(PRE/USER/POST)_LOGICAL -------------------Processing of objects about to marshalled
PREPARE_SEND ----------------------------------Opening of the connection
PRE_STREAM
PRE_PROTOCOL ----------------------------------Misc protocol actions.
WRITE ----------------------------------------------Writing of the protocol message, such as the SOAP Envelope.
MARSHAL -------------------------------------------Marshalling of the objects
(USER/POST)_PROTOCOL -----------------------Processing of the protocol message.
(USER/POST)_STREAM ---------------------------Processing of the byte level message
SEND ------------------------------------------------Final sending of message and closing of transport stream
2. InterceptorProviders
CXF包含的几个不同的组件可以提供将interceptors放到InterceptorChain中。 它们实现了InterceptorProvider接口:
public interface InterceptorProvider {
List getInInterceptors();
List getOutInterceptors();
List getOutFaultInterceptors();
List getInFaultInterceptors();
}
添加一个interceptor到一个interceptor链,你只需要把它添加到一个InterceptorProviders:
MyInterceptor interceptor = new MyInterceptor();
provider.getInInterceptors()。add(interceptor);
CXF包含的一些InterceptorProviders是:
. Client
. Endpoint
. Service
. Bus
. Binding