gdx 发表于 2018-11-22 13:36:13

【Apache CXF】Interceptor与Phase

  server端和client端都可以使用拦截器做一些记录、转换、验证、错误处理什么的。
比如处理传输的对象较大时使用GZIPIn/OutInterceptor。
  

  

  interceptor以phase方式组织起来,相似功能的interceptor属一个phase。
phase实现Comparator接口,以priority属性进行排序,phaseManager将其依次添加至SortedSet。
当需要doInterceptor()时则按顺序依次执行。
  

  

  CXF提供的Interceptor有in和out的区别。
比如当服务被调用时拦截器链会被创建并调用,此时对于client端是out interceptor,而对于server端则是in interceptor。
反之,当server端作响应时,对于server端是out interceptor,而对于client端则是in interceptor。
  

  
打开看org.apache.cxf.interceptor.Interceptor的类型层级关系发现数量好多。


  

  

  CXF中的很多组件都继承org.apache.cxf.interceptor.InterceptorProvider,以自由地添加/移除过滤器。

  
定义一个拦截器只需要继承org.apache.cxf.phase.AbstractPhaseInterceptor或其子类。
如果需要使用到Message接口中的一些方法的话还是直接继承AbstractPhaseInterceptor比较好;
如果想获取一些特定的信息,则需要继承那些特定的拦截器。

  

  

  原先我是想直接实现phaseInterceptor或者继承AbstractPhaseInterceptor,但我以annotation方式为服务设置interceptor时必须在constructor中指定phase,而且此时interceptor无法定义一个无参constructor。
于是我继承了SoapActionInInterceptor输出点信息,继承AbstractPhaseInterceptor的子类则是同一个phase:

package pac.king.webservice.interceptor;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.SoapActionInInterceptor;
import org.apache.cxf.interceptor.Fault;
public class MyInterceptor extends SoapActionInInterceptor {
    @Override
    public void handleMessage(SoapMessage m) throws Fault {
      System.out.println(m.get(m.PROTOCOL_HEADERS));
      System.out.println(m.get(m.REQUEST_URL));
    }
}  

  有三种方式设置interceptor。
  
·调用方法
比如像这样以编程方式启动服务时顺便添加interceptor:

JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setServiceClass(MyCxfServiceImpl.class);
factory.setAddress("http://localhost:8080/runtrain/services/MyCxfService");
factory.create().getEndpoint().getInInterceptors().add(new MyInterceptor(Phase.USER_PROTOCOL));  

  

  ·annotation
没什么特别的,interceptor注解支持两种参数:
/**
* Specifies a list of classes that are added to the inbound interceptor
* chain. This annotation effects SEI classes and service implementation
* classes.
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface InInterceptors {
    String[] interceptors() default { };
    Class
页: [1]
查看完整版本: 【Apache CXF】Interceptor与Phase