CXF之Interceptors

来源:互联网 发布:淘宝如何增加排名 编辑:程序博客网 时间:2024/05/20 20:44

 

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接口:

 

添加一个interceptor到一个interceptor链,你只需要把它添加到一个InterceptorProviders:

 

 

CXF包含的一些InterceptorProviders是:

. Client

. Endpoint

. Service

. Bus

. Binding

 

 

 

 

3. 编写并配置一个Interceptor

 

编写一个Interceptor

 

写一个拦截器是相对简单的。你的interceptor需要继承AbstractPhaseInterceptor或者它的许多子类,例如AbstractSoapInterceptor.

从AbstractPhaseInterceptor继承,允许你的interceptor访问消息接口的方法 。如例:

 

 

 

继承自AbstractPhaseInterceptor的子类,允许你的interceptor访问比在Message接口中更多的信息。

AbstractSoapInterceptor是AbstractPhaseInterceptor的子类。继承自这个类,允许你的interceptor

访问SoapMessage类的SOAP头和版本消息。如例:

 

注意,你需要指定这阶段要包含进来的interceptor. 这是在拦截器的构造器上进行

 

你还可以在拦截器运行前/后在同一阶段定义某些其它拦截器:

 

你可以添加interceptors到interceptor链中,通过编程方式 或 配置方式. ----略。

 

 

 

原创粉丝点击