CXF之五 拦截器Interceptor

拦截器(Interceptor)是CXF功能最主要的扩展点,可以在不对核心模块进行修改的情况下,动态添加很多功能。拦截器和JAX-WS Handler、Filter的功能类似,当服务被调用时,就会创建一个拦截器链(Interceptor Chain),拦截器链在服务输入(IN)或输出(OUT)阶段实现附加功能。 
拦截器可以在客户端,也可以在服务端添加。当客户端发起一个WebService请求时,在客户端会创建输出拦截器链,服务端接收到客户端的后,会创建输入拦截器链。当服务端返回响应消息时,响应消息会经过服务端的输出拦截链,客户端接收到服务端的响应时,会创建输入拦截器链,响应消息先经过输入拦截器链处理。拦截器在服务端和客户端的作用如图所示。 
CXF拦截器

拦截器链阶段

拦截器链有多个阶段,每个阶段都有多个拦截器。拦截器在拦截器链的哪个阶段起作用,可以在拦截器的构造函数中声明。 
输入拦截器链有如下几个阶段,这些阶段按照在拦截器链中的先后顺序排列。

阶段名称阶段功能描述
RECEIVETransport level processing(接收阶段,传输层处理)
(PRE/USER/POST)_STREAMStream level processing/transformations(流处理/转换阶段)
READThis is where header reading typically occurs(SOAPHeader读取)
(PRE/USER/POST)_PROTOCOLProtocol processing, such as JAX-WS SOAP handlers(协议处理阶段,例如JAX-WS的Handler处理)
UNMARSHALUnmarshalling of the request(SOAP请求解码阶段)
(PRE/USER/POST)_LOGICALProcessing of the umarshalled request(SOAP请求解码处理阶段)
PRE_INVOKEPre invocation actions(调用业务处理之前进入该阶段)
INVOKEInvocation of the service(调用业务阶段)
POST_INVOKEInvocation of the outgoing chain if there is one(提交业务处理结果,并触发输入连接器)


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

阶段名称阶段功能描述
SETUPAny set up for the following phases(设置阶段)
(PRE/USER/POST)_LOGICALProcessing of objects about to marshalled
PREPARE_SENDOpening of the connection(消息发送准备阶段,在该阶段创建Connection)
PRE_STREAM流准备阶段
PRE_PROTOCOLMisc protocol actions(协议准备阶段)
WRITEWriting of the protocol message, such as the SOAP Envelope.(写消息阶段)
MARSHALMarshalling of the objects
(USER/POST)_PROTOCOLProcessing of the protocol message
(USER/POST)_STREAMProcessing 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输入会顺序经过以下拦截器:

拦截器名称拦截器功能
AttachmentInInterceptorParse 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
StaxInInterceptorCreates an XMLStreamReader from the transport InputStream on the Message
ReadHeadersInterceptorParses the SOAP headers and stores them on the Message
SoapActionInInterceptorParses “soapaction” header and looks up the operation if a unique operation can be found for that action.
MustUnderstandInterceptorChecks the MustUnderstand headers, its applicability and process it, if required
SOAPHandlerInterceptorSOAP Handler as per JAX-WS
LogicalHandlerInInterceptorLogical Handler as per JAX-WS
CheckFaultInterceptorChecks for fault, if present aborts interceptor chain and invokes fault handler chain
URIMappingInterceptorCan handle HTTP GET, extracts operation info and sets the same in the Message
DocLiteralnInterceptorExamines 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.
SoapHeaderInterceptorPerform databinding of the SOAP headers for headers that are mapped to parameters
WrapperClassInInterceptorFor 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.
SwAInInterceptorFor Soap w/ Attachments, finds the appropriate attachments and assigns them to the correct spot in the parameter list.
HolderInInterceptorFor OUT and IN/OUT parameters, JAX-WS needs to create Holder objects. This interceptor creates the Holders and puts them in the parameter list.
ServiceInvokerInInterceptorActually invokes the service.


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

 

拦截器名称拦截器功能
HolderOutInterceptorFor 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.
SwAOutInterceptorFor OUT parts that are Soap attachments, pulls them from the list and holds them for later.
WrapperClassOutInterceptorFor doc/lit wrapped, takes the remaining parts and creates a wrapper JAXB bean to represent the whole message.
SoapHeaderOutFilterInterceptorRemoves inbound marked headers
SoapActionOutInterceptorSets the SOAP Action
MessageSenderInterceptorCalls back to the Destination object to have it setup the output streams, headers, etc… to prepare the outgoing transport.
SoapPreProtocolOutInterceptorThis 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.
AttachmentOutInterceptorIf this service uses attachments (either SwA or if MTOM is enabled), it sets up the Attachment marshallers and the mime stuff that is needed.
StaxOutInterceptorCreates an XMLStreamWriter from the OutputStream on the Message.
SoapHandlerInterceptorJAX-WS SOAPHandler
SoapOutInterceptorWrites start element for soap:envelope and complete elements for other header blocks in the message. Adds start element for soap:body too.
LogicalHandlerOutInterceptorJAX-WS Logical handler stuff
WrapperOutInterceptorIf wrapped doc/lit and not using a wrapper bean or if RPC lit, outputs the wrapper element to the stream.
BareOutInterceptorUses the databinding to write the params out.
SoapOutInterceptor$SoapOutEndingInterceptorCloses the soap:body and soap:envelope
StaxOutInterceptor$StaxOutEndingInterceptorFlushes the stax stream.
MessageSenderInt$MessageSenderEndingCloses the exchange, lets the transport know everything is done and should be flushed to the client.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值