三、CXF对Interceptor拦截器的支持

简介:

前面在Axis中介绍过Axis的Handler,这里CXF的Interceptor就和Handler的功能类似。在每个请求响应之前或响应之后,做一些事情。这里的Interceptor就和Filter、Struts的Interceptor很类似,提供它的主要作用就是为了很好的降低代码的耦合性,提供代码的内聚性。下面我们就看看CXF的Interceptor是怎么样工作的。

1、 我们就用上面的HelloWorldService,客户端的调用代码重新写一份,代码如下:

package com.hoo.client;
 
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.phase.Phase;
import com.hoo.interceptor.MessageInterceptor;
import com.hoo.service.IHelloWorldService;
 
/**
 * <b>function:</b>CXF WebService客户端调用代码
 * @author hoojo
 * @createDate 2011-3-16 上午09:03:49
 * @file HelloWorldServiceClient.java
 * @package com.hoo.client
 * @project CXFWebService
 * @blog http://blog.csdn.net/IBM_hoojo
 * @email hoojo_@126.com
 * @version 1.0
 */
public class ServiceMessageInterceperClient {
    
    public static void main(String[] args) {
        //调用WebService
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(IHelloWorldService.class);
        factory.setAddress("http://localhost:9000/helloWorld");
        factory.getInInterceptors().add(new LoggingInInterceptor());
        factory.getOutInterceptors().add(new LoggingOutInterceptor());
        
        IHelloWorldService service = (IHelloWorldService) factory.create();
        System.out.println("[result]" + service.sayHello("hoojo"));
    }
}

上面的CXF的拦截器是添加在客户端,同样在服务器端也是可以添加拦截器Interceptor的。运行后结果如下:

2011-3-18 7:34:00 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.hoo.com/}IHelloWorldServiceService from class com.hoo.service.IHelloWorldService
2011-3-18 7:34:00 org.apache.cxf.interceptor.AbstractLoggingInterceptor log
信息: Outbound Message
---------------------------
ID: 1
Address: http://localhost:9000/helloWorld
Encoding: UTF-8
Content-Type: text/xml
Headers: {SOAPAction=[""], Accept=[*/*]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:sayHello xmlns:ns1="http://service.hoo.com/"><name>hoojo</name></ns1:sayHello></soap:Body></soap:Envelope>
--------------------------------------
2011-3-18 7:34:01 org.apache.cxf.interceptor.AbstractLoggingInterceptor log
信息: Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {content-type=[text/xml;charset=UTF-8], Content-Length=[230], Server=[Jetty(7.2.2.v20101205)]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:sayHelloResponse xmlns:ns1="http://service.hoo.com/"><return>hoojo say: Hello World </return></ns1:sayHelloResponse></soap:Body></soap:Envelope>
--------------------------------------
[result]hoojo say: Hello World 

上面的部分信息是LoggingInterceptor输出的日志信息,分别在请求和响应的时候输出日志信息,还有输出请求的时候参数的信息以及响应的时候返回值的信息。

2、 刚才是客户端添加Interceptor,现在我们自己编写一个Interceptor,这个Interceptor需要继承AbstractPhaseInterceptor,实现handleMessage和一个带参数的构造函数。然后在服务器端添加这个Interceptor。

Interceptor代码如下:

package com.hoo.interceptor;
 
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
 
/**
 * <b>function:</b> 自定义消息拦截器
 * @author hoojo
 * @createDate Mar 17, 2011 8:10:49 PM
 * @file MessageInterceptor.java
 * @package com.hoo.interceptor
 * @project CXFWebService
 * @blog http://blog.csdn.net/IBM_hoojo
 * @email hoojo_@126.com
 * @version 1.0
 */
public class MessageInterceptor extends AbstractPhaseInterceptor<Message> {
    
    //至少要一个带参的构造函数
    public MessageInterceptor(String phase) {
        super(phase);
    }
 
    public void handleMessage(Message message) throws Fault {
        System.out.println("############handleMessage##########");
        System.out.println(message);
        if (message.getDestination() != null) {
            System.out.println(message.getId() + "#" + message.getDestination().getMessageObserver());
        }
        if (message.getExchange() != null) {
            System.out.println(message.getExchange().getInMessage() + "#" + message.getExchange().getInFaultMessage());
            System.out.println(message.getExchange().getOutMessage() + "#" + message.getExchange().getOutFaultMessage());
        }
    }
}

下面看看发布服务和添加自定义拦截器的代码:

package com.hoo.service.deploy;
 
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.apache.cxf.phase.Phase;
import com.hoo.interceptor.MessageInterceptor;
import com.hoo.service.HelloWorldService;
 
/**
 * <b>function:</b>在服务器发布自定义的Interceptor
 * @author hoojo
 * @createDate 2011-3-18 上午07:38:28
 * @file DeployInterceptorService.java
 * @package com.hoo.service.deploy
 * @project CXFWebService
 * @blog http://blog.csdn.net/IBM_hoojo
 * @email hoojo_@126.com
 * @version 1.0
 */
public class DeployInterceptorService {
 
    public static void main(String[] args) throws InterruptedException {
        //发布WebService
        JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
        //设置Service Class
        factory.setServiceClass(HelloWorldService.class);
        factory.setAddress("http://localhost:9000/helloWorld");
        //设置ServiceBean对象
         factory.setServiceBean(new HelloWorldService());
        
        //添加请求和响应的拦截器,Phase.RECEIVE只对In有效,Phase.SEND只对Out有效
         factory.getInInterceptors().add(new MessageInterceptor(Phase.RECEIVE));
        factory.getOutInterceptors().add(new MessageInterceptor(Phase.SEND));
        
        factory.create();
        
        System.out.println("Server start ......");
        Thread.sleep(1000 * 60);
        System.exit(0);
        System.out.println("Server exit ");
    }
}

值得说的是,以前发布WebService是用Endpoint的push方法。这里用的是JaxWsServerFactoryBean和客户端调用的代码JaxWsProxyFactoryBean有点不同。

客户端调用代码:

package com.hoo.client;
 
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.hoo.service.IHelloWorldService;
 
/**
 * <b>function:</b>CXF WebService客户端调用代码
 * @author hoojo
 * @createDate 2011-3-16 上午09:03:49
 * @file HelloWorldServiceClient.java
 * @package com.hoo.client
 * @project CXFWebService
 * @blog http://blog.csdn.net/IBM_hoojo
 * @email hoojo_@126.com
 * @version 1.0
 */
public class HelloWorldServiceClient {
    
    public static void main(String[] args) {
        //调用WebService
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(IHelloWorldService.class);
        factory.setAddress("http://localhost:9000/helloWorld");
        
        IHelloWorldService service = (IHelloWorldService) factory.create();
        System.out.println("[result]" + service.sayHello("hoojo"));
    }
}





本文转自hoojo博客园博客,原文链接:http://www.cnblogs.com/hoojo/archive/2011/03/30/1999499.html,如需转载请自行联系原作者
目录
相关文章
|
7月前
|
前端开发 Java 数据库连接
【SpringMVC】JSR 303与interceptor拦截器快速入门
JSR 303是Java规范请求(Java Specification Request)的缩写,意思是Java 规范提案。是指向JCP(Java Community Process)提出新增一个标准化技术规范的正式请求。任何人都可以提交JSR,以向Java平台增添新的API和服务。JSR已成为Java界的一个重要标准。 JSR-303 是JAVA EE 6 中的一项子规范,叫做Bean Validation,Hibernate Validator 是 Bean Validation 的参考实现 . Hibe
QGS
|
11月前
|
存储 调度 数据安全/隐私保护
入门SpringMVC之Interceptor拦截器
SpringMVC中的Interceptor拦截器,它的主要作用是拦截指定的用户需求,并进行相应的预处理与后处理。
QGS
66 0
|
前端开发 Java Spring
SpringMVC拦截器interceptor
Spring MVC 的拦截器类似于 Servlet 开发中的过滤器 Filter,用于对处理器进行预处理和后处理。
95 0
SpringMVC拦截器interceptor
|
前端开发
jfinal拦截器Interceptor解析
jfinal拦截器Interceptor解析
271 0
jfinal拦截器Interceptor解析
|
前端开发 Java Spring
详解SpringMVC中使用Interceptor拦截器
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理。
1042 0
|
Web App开发 XML 前端开发
SSM-SpringMVC-33:SpringMVC中拦截器Interceptor讲解
   ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------       拦截器Interceptor: 对处理方法进行双向的拦截,可以对其做日志记录等   我选择的是实现HandlerInterceptor接口   我把执行流程放上来,单...
1848 0
|
Java Spring 数据安全/隐私保护