WebService框架CXF实战一拦截器Interceptor(四)

拦截器(Interceptor)是CXF功能最主要的扩展点,可以在不对核心模块进行修改的情况下,动态添加很多功能。拦截器和JAX-WS Handler、Filter的功能类似,当服务被调用时,就会创建一个拦截器链(Interceptor Chain),拦截器链在服务输入(IN)或输出(OUT)阶段实现附加功能。

拦截器可以在客户端,也可以在服务端添加。当客户端发起一个WebService请求时,在客户端会创建输出拦截器链,服务端接收到客户端的后,会创建输入拦截器链。当服务端返回响应消息时,响应消息会经过服务端的输出拦截链,客户端接收到服务端的响应时,会创建输入拦截器链,响应消息先经过输入拦截器链处理。拦截器在服务端和客户端的作用如图所示。

拦截器链阶段

拦截器链有多个阶段,每个阶段都有多个拦截器。拦截器在拦截器链的哪个阶段起作用,可以在拦截器的构造函数中声明。

输入拦截器链有如下几个阶段,这些阶段按照在拦截器链中的先后顺序排列。

阶段名称 阶段功能描述
RECEIVE Transport level processing(接收阶段,传输层处理)
(PRE/USER/POST)_STREAM Stream level processing/transformations(流处理/转换阶段)
READ This is where header reading typically occurs(SOAPHeader读取)
(PRE/USER/POST)_PROTOCOL Protocol processing, such as JAX-WS SOAP handlers(协议处理阶段,例如JAX-WS的Handler处理)
UNMARSHAL Unmarshalling of the request(SOAP请求解码阶段)
(PRE/USER/POST)_LOGICAL Processing of the umarshalled request(SOAP请求解码处理阶段)
PRE_INVOKE Pre invocation actions(调用业务处理之前进入该阶段)
INVOKE Invocation of the service(调用业务阶段)
POST_INVOKE Invocation of the outgoing chain if there is one(提交业务处理结果,并触发输入连接器)

输出拦截器链有如下几个阶段,这些阶段按照在拦截器链中的先后顺序排列。

阶段名称 阶段功能描述
SETUP Any set up for the following phases(设置阶段)
(PRE/USER/POST)_LOGICAL Processing of objects about to marshalled
PREPARE_SEND Opening of the connection(消息发送准备阶段,在该阶段创建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 消息发送

在输出拦截器链的SEND阶段后,还会触发以_ENDING结尾阶段,这些ENDING阶段与以上阶段对应,主要用于清理或者关闭资源。ENDING阶段触发的顺序如下:

  1. SEND_ENDING
  2. POST_STREAM_ENDING
  3. USER_STREAM_ENDING
  4. POST_PROTOCOL_ENDING
  5. USER_PROTOCOL_ENDING
  6. MARSHAL_ENDING
  7. WRITE_ENDING
  8. PRE_PROTOCOL_ENDING
  9. PRE_STREAM_ENDING
  10. PREPARE_SEND_ENDING
  11. POST_LOGICAL_ENDING
  12. USER_LOGICAL_ENDING
  13. PRE_LOGICAL_ENDING
  14. SETUP_ENDING

CXF默认拦截器链

在CXF中,所有对消息的处理都是通过各种拦截器实现。CXF已经实现了多种拦截器,如操纵消息头、执行认证检查、验证消息数据、日志记录、消息压缩等,有些拦截器在发布服务、访问服务时已经默认添加到拦截器链。

CXF默认输入拦截器链,如果没有添加额外的拦截器,CXF输入会顺序经过以下拦截器:

拦截器名称 拦截器功能
AttachmentInInterceptor Parse the mime headers for mime boundaries, finds the “root” part and resets the input stream to it, and stores the other parts in a collection of Attachments
StaxInInterceptor Creates an XMLStreamReader from the transport InputStream on the Message
ReadHeadersInterceptor Parses the SOAP headers and stores them on the Message
SoapActionInInterceptor Parses “soapaction” header and looks up the operation if a unique operation can be found for that action.
MustUnderstandInterceptor Checks the MustUnderstand headers, its applicability and process it, if required
SOAPHandlerInterceptor SOAP Handler as per JAX-WS
LogicalHandlerInInterceptor Logical Handler as per JAX-WS
CheckFaultInterceptor Checks for fault, if present aborts interceptor chain and invokes fault handler chain
URIMappingInterceptor Can handle HTTP GET, extracts operation info and sets the same in the Message
DocLiteralnInterceptor Examines the first element in the SOAP body to determine the appropriate Operation (if soapAction did not find one) and calls the Databinding to read in the data.
SoapHeaderInterceptor Perform databinding of the SOAP headers for headers that are mapped to parameters
WrapperClassInInterceptor For wrapped doc/lit, the DocLiteralInInterceptor probably read in a single JAXB bean. This interceptor pulls the individual parts out of that bean to construct the Object[] needed to invoke the service.
SwAInInterceptor For Soap w/ Attachments, finds the appropriate attachments and assigns them to the correct spot in the parameter list.
HolderInInterceptor For OUT and IN/OUT parameters, JAX-WS needs to create Holder objects. This interceptor creates the Holders and puts them in the parameter list.
ServiceInvokerInInterceptor Actually invokes the service.

CXF默认输出拦截器链,如果没有添加额外的拦截器,CXF输入会顺序经过以下拦截器:

拦截器名称 拦截器功能
HolderOutInterceptor For OUT and IN/OUT params, pulls the values out of the JAX-WS Holder objects (created in HolderInInterceptor) and adds them to the param list for the out message.
SwAOutInterceptor For OUT parts that are Soap attachments, pulls them from the list and holds them for later.
WrapperClassOutInterceptor For doc/lit wrapped, takes the remaining parts and creates a wrapper JAXB bean to represent the whole message.
SoapHeaderOutFilterInterceptor Removes inbound marked headers
SoapActionOutInterceptor Sets the SOAP Action
MessageSenderInterceptor Calls back to the Destination object to have it setup the output streams, headers, etc… to prepare the outgoing transport.
SoapPreProtocolOutInterceptor This interceptor is responsible for setting up the SOAP version and header, so that this is available to any pre-protocol interceptors that require these to be available.
AttachmentOutInterceptor If this service uses attachments (either SwA or if MTOM is enabled), it sets up the Attachment marshallers and the mime stuff that is needed.
StaxOutInterceptor Creates an XMLStreamWriter from the OutputStream on the Message.
SoapHandlerInterceptor JAX-WS SOAPHandler
SoapOutInterceptor Writes start element for soap:envelope and complete elements for other header blocks in the message. Adds start element for soap:body too.
LogicalHandlerOutInterceptor JAX-WS Logical handler stuff
WrapperOutInterceptor If wrapped doc/lit and not using a wrapper bean or if RPC lit, outputs the wrapper element to the stream.
BareOutInterceptor Uses the databinding to write the params out.
SoapOutInterceptor$SoapOutEndingInterceptor Closes the soap:body and soap:envelope
StaxOutInterceptor$StaxOutEndingInterceptor Flushes the stax stream.
MessageSenderInt$MessageSenderEnding Closes the exchange, lets the transport know everything is done and should be flushed to the client.

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-29 08:31:38

WebService框架CXF实战一拦截器Interceptor(四)的相关文章

WebService框架CXF实战一自定义拦截器(五)

CXF已经内置了一些拦截器,这些拦截器大部分默认添加到拦截器链中,有些拦截器也可以手动添加,如手动添加CXF提供的日志拦截器.也可以自定义拦截器,CXF中实现自定义拦截器很简单,只要继承AbstractPhaseInterceptor或者AbstractPhaseInterceptor的子类(如AbstractSoapInterceptor)即可. 自定义权限认证拦截器 权限认证拦截器处理SOAPHeader中的认证信息,客户端在发起请求时在SOAPHeader中添加认证信息,服务端在接收到请求

WebService框架CXF实战一传输文件(六)

CXF的文件传输通过MTOM实现.MTOM(SOAP Message Transmission Optimization Mechanism)SOAP消息传输优化机制,可以在SOAP消息中发送二进制数据.MTOM允许将消息中包含的大型数据元素外部化,并将其作为无任何特殊编码的二进制数据随消息一起传送.相对于把二进制转为base64进行传输,MTOM具有更高的传输效率. 文件传输包装类 CXF文件传输DataHandler只有二进制数据,没有文件名.文件类型和文件大小等,需要额外的传输参数.通常自

WebService框架CXF实战一发布RESTFul服务(七)

JAX-RS概述 JAX-RS是Java提供用于开发RESTful Web服务基于注解(annotation)的API.JAX-RS旨在定义一个统一的规范,使得Java程序员可以使用一套固定的接口来开发REST应用,避免了依赖第三方框架.同时JAX-RS使用POJO编程模型和基于注解的配置并集成JAXB,可以有效缩短REST应用的开发周期.JAX-RS只定义RESTful API,具体实现由第三方提供,如Jersey.Apache CXF等. JAX-RS包含近五十多个接口.注解和抽象类: ja

WebService框架CXF实战(一)

Apache CXF提供了用于方便地构建和开发WebService的可靠基础架构.它允许创建高性能和可扩展的服务,可以部署在Tomcat和基于Spring的轻量级容器中,也可以部署在更高级的服务器上,例如Jboss.WebSphere或WebLogic. CXF提供了以下功能: WebService服务标准支持: Java API for XML Web Services (JAX-WS) SOAP WebService描述语言(Web Services Description Language

WebService框架CXF实战一在Tomcat中发布WebService(二)

服务接口及实现类请参考WebService框架CXF实战(一) 创建Maven Web项目,在pom.xml中添加CXF和Spring Web的引用,由于CXFServlet需要Spring Web的支持. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=&quo

详述 Spring MVC 框架中拦截器 Interceptor 的使用方法

1 前言 昨天新接了一个需要,"拦截 XXX,然后 OOO",好吧,说白了就是要用拦截器干点事(实现一个具体的功能).之前,也在网络上搜了很多关于Interceptor的文章,但感觉内容都大同小异,而且知识点零零散散,不太方便阅读.因此,正好借此机会,整理一篇关于拦截器的文章,在此分享给大家,以供大家参考阅读. 2 拦截器 2.1 概念 Java 里的拦截器是动态拦截 action 调用的对象.它提供了一种机制可以使开发者可以定义在一个 action 执行的前后执行的代码,也可以在一个

WebServices中使用cxf开发日志拦截器以及自定义拦截器

首先下载一个cxf实例,里面包含cxf的jar包.我下的是apache-cxf-2.5.9 1.为什么要设置拦截器? 为了在webservice请求过程中,能动态操作请求和响应数据, CXF设计了拦截器. 2.拦截器分类 1. 按所处的位置分:服务器端拦截器,客户端拦截器 2. 按消息的方向分:入拦截器,出拦截器 3. 按定义者分:系统拦截器,自定义拦截器 3.拦截器API Interceptor(拦截器接口) AbstractPhaseInterceptor(自定义拦截器从此继承) Loggi

五 、 Kafka producer 拦截器(interceptor) 和 六 、Kafka Streaming案例

五 Kafka producer 拦截器(interceptor) 5.1 拦截器原理 Producer 拦截器(interceptor)是在 Kafka 0.10 版本被引入的,主要用于实现 clients 端的定 制化控制逻辑. 对于 producer 而言,interceptor 使得用户在消息发送前以及 producer 回调逻辑前有机会 对消息做一些定制化需求,比如修改消息等.同时,producer 允许用户指定多个 interceptor 按序作用于同一条消息从而形成一个拦截链(in

Flume-NG源码阅读之SourceRunner,及选择器selector和拦截器interceptor的执行

在AbstractConfigurationProvider类中loadSources方法会将所有的source进行封装成SourceRunner放到了Map<String, SourceRunner> sourceRunnerMap之中.相关代码如下: 1 Map<String, String> selectorConfig = context.getSubProperties( 2 BasicConfigurationConstants.CONFIG_SOURCE_CHANNE