ws的interceptor和handler

1、interceptor
          ws的interceptor包括client端的in interceptor、out interceptor和server端的in interceptor、out interceptor。
     interceptor,攔截器主要在請求相應之前或者之後調用,很好的降低了代碼的耦合性,
          主要用途有身份認證等
  • cxf實現的interceptor
          LoggingInInterceptor:登入日誌打印攔截器
          LoggingOutInterceptor:登出日誌打印攔截器
          詳細地址:http://cxf.apache.org/javadoc/latest-2.7.x/org/apache/cxf/interceptor/Interceptor.html
          
          wss4jininterceptor:這個interceptor可以幫我們做一些權限校驗工作

          自定義interceptor:
          可以繼承AbstractPhaseInterceptor, 實現handleMessage方法,在無參構造方法中指定執行的階段Phase;
          
          Phase:(org.apache.cxf.Phase)感覺完全有必要整個枚舉,這還都是字符串。
               PRE_INVOKE、POST_INVOKE、INVOKE   ……           

2、handler
          ws的handler能夠在讀取soap消息前、後執行某些特定的代碼,有點AOP的意思。

          handler的作用:檢驗cookie,持久化消息,增加自定義表頭等
          
          handler的實現:javax.xml.ws.handler
  • LogicalHandler<LogicalMessageContext>
          只訪問soap消息的payload部分

          可以通過context.get(MessageContext.*)獲得true or false,進而對不同的邏輯執行不同代碼。
          For example:
               booleab outbound = (Boolean)context.get((MessageContext.MESSAGE_OUTBOUND_PROPERTY))
  • SOAPHandler<SOAPMessageContext>          
          可以訪問整個soap消息。

  • 配置多個handler
          如果要配置多個handler,需要寫一個配置文件:

          <?xml version="1.0" encoding="UTF-8"?>
<handler-chains xmlns="http://java.sun.com/xml/ns/javaee">
   <handler-chain>
    <handler>
        
 <handler-name>handler1</handler-name>
        <handler-class>com.baidu.iit.handler1</handler-class>
    </handler>
    <handler>
        <handler-name>handler2</handler-name>
        <handler-class>com.baidu.iit.handler2</handler-class>
    </handler>
   </handler-chain>
</handler-chains>
     
          按照配置順序,依次執行。

          @WebService
//這裏展示如何用@HandlerChain來聲明一組Handler,他們會對指定的web service使用的SOAP消息進行處理,
@HandlerChain(file="/handler_chains.xml")
public class CalcServiceImpl implements ICalcService {

3、interceptor和handler的區別
          interceptor要麼前要麼後處理,而handler是前後都處理
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章