CXF-02 CXF对Interceptor拦截器的支持

今天我们来说一下CXFInterceptor,其主要功能就是在每个请求响应之前或响应之后,做一些事情。这里的Interceptor就和FilterStrutsInterceptor很类似,提供它的主要作用就是为了很好的降低代码的耦合性,提供代码的内聚性。下面我们就看看CXFInterceptor是怎么样工作的。

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

package com.iflytek.client;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.iflytek.service.IHelloWorldService;

/**
 * @author xdwang
 * 
 * @create Apr 25, 2013 10:35:50 PM
 * 
 * @email:[email protected]
 * 
 * @description CXF WebService客户端调用代码
 * 
 */
public class ServiceMessageInterceperClient {
	public static void main(String[] args) { // 调用WebService
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setServiceClass(IHelloWorldService.class);
		factory.setAddress("http://localhost:8000/helloWorld");
		
		factory.getInInterceptors().add(new LoggingInInterceptor());
		factory.getOutInterceptors().add(new LoggingOutInterceptor());
		
		IHelloWorldService service = (IHelloWorldService) factory.create();
		System.out.println("[result]" + service.sayHello("xdwang"));
	}
}

 

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

2013-7-25 19:22:42 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.iflytek.com/}IHelloWorldServiceService from class com.iflytek.service.IHelloWorldService
2013-7-25 19:22:43 org.apache.cxf.services.IHelloWorldServiceService.IHelloWorldServicePort.IHelloWorldService
信息: Outbound Message
---------------------------
ID: 1
Address: http://localhost:8000/helloWorld
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml
Headers: {Accept=[*/*], SOAPAction=[""]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHello xmlns:ns2="http://service.iflytek.com/"><name>xdwang</name></ns2:sayHello></soap:Body></soap:Envelope>
--------------------------------------
2013-7-25 19:22:43 org.apache.cxf.services.IHelloWorldServiceService.IHelloWorldServicePort.IHelloWorldService
信息: Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {Content-Length=[235], content-type=[text/xml;charset=UTF-8], Server=[Jetty(8.1.7.v20120910)]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:sayHelloResponse xmlns:ns1="http://service.iflytek.com/"><return>xdwang say: Hello World </return></ns1:sayHelloResponse></soap:Body></soap:Envelope>
--------------------------------------
[result]xdwang say: Hello World 

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

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

Interceptor代码如下:

package com.iflytek.interceptor;

import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;

/**
 * @author xdwang
 * 
 * @create 2013-7-25 下午7:24:12
 * 
 * @email:[email protected]
 * 
 * @description 自定义消息拦截器
 * 
 */
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.iflytek.service.deploy;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.apache.cxf.phase.Phase;

import com.iflytek.interceptor.MessageInterceptor;
import com.iflytek.service.HelloWorldService;

/**
 * @author xdwang
 * 
 * @create 2013-7-25 下午7:25:55
 * 
 * @email:[email protected]
 * 
 * @description 在服务器发布自定义的Interceptor
 * 
 */
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是用Endpointpush方法。这里用的是JaxWsServerFactoryBean和客户端调用的代码JaxWsProxyFactoryBean有点不同。

package com.iflytek.client;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.iflytek.service.IHelloWorldService;

/**
 * @author xdwang
 * 
 * @create 2013-7-25 下午7:28:18
 * 
 * @email:[email protected]
 * 
 * @description CXF WebService客户端调用代码
 * 
 */
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"));
	}
}

运行结果:

2013-7-25 19:28:52 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.iflytek.com/}HelloWorldServiceService from class com.iflytek.service.HelloWorldService
2013-7-25 19:28:52 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be http://localhost:9000/helloWorld
2013-7-25 19:28:52 org.eclipse.jetty.server.Server doStart
信息: jetty-8.1.7.v20120910
2013-7-25 19:28:53 org.eclipse.jetty.server.AbstractConnector doStart
信息: Started SelectChannelConnector@localhost:9000
2013-7-25 19:28:53 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL
信息: Creating Service {http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01}Discovery from WSDL: classpath:/org/apache/cxf/ws/discovery/wsdl/wsdd-discovery-1.1-wsdl-os.wsdl
2013-7-25 19:28:53 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be soap.udp://239.255.255.250:3702
2013-7-25 19:28:53 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01}DiscoveryProxy from class org.apache.cxf.jaxws.support.DummyImpl
Server start ......
============handleMessage==========
{org.apache.cxf.message.Message.PROTOCOL_HEADERS={Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], Content-Length=[196], content-type=[text/xml; charset=UTF-8], Host=[localhost:9000], Pragma=[no-cache], SOAPAction=[""], User-Agent=[Apache CXF 2.7.4]}, HTTP_CONTEXT_MATCH_STRATEGY=stem, org.apache.cxf.request.url=http://localhost:9000/helloWorld, org.apache.cxf.request.uri=/helloWorld, HTTP.REQUEST=(POST /helloWorld)@1344650582 org.eclipse.jetty.server.Request@5025bd56, HTTP.CONFIG=null, org.apache.cxf.transport.https.CertConstraints=null, Accept=*/*, org.apache.cxf.message.Message.PATH_INFO=/helloWorld, org.apache.cxf.message.Message.BASE_PATH=/helloWorld, org.apache.cxf.continuations.ContinuationProvider=org.apache.cxf.transport.http_jetty.continuations.JettyContinuationProvider@5db18235, org.apache.cxf.message.Message.IN_INTERCEPTORS=[org.apache.cxf.transport.https.CertConstraintsInterceptor@30db95a1], org.apache.cxf.binding.soap.SoapVersion=org.apache.cxf.binding.soap.Soap11@203c9616, org.apache.cxf.message.Message.ENCODING=UTF-8, org.apache.cxf.message.Message.QUERY_STRING=null, HTTP.RESPONSE=HTTP/1.1 200 

, org.apache.cxf.security.SecurityContext=org.apache.cxf.transport.http.AbstractHTTPDestination$2@69a18ee2, org.apache.cxf.request.method=POST, org.apache.cxf.async.post.response.dispatch=true, org.apache.cxf.configuration.security.AuthorizationPolicy=null, org.apache.cxf.message.MessageFIXED_PARAMETER_ORDER=false, org.apache.cxf.transport.Destination=org.apache.cxf.transport.http_jetty.JettyHTTPDestination@6dabcd9b, http.base.path=http://localhost:9000, Content-Type=text/xml; charset=UTF-8, [email protected]{,null}}
null=org.apache.cxf.transport.ChainInitiationObserver@49198ff2
{org.apache.cxf.message.Message.PROTOCOL_HEADERS={Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], Content-Length=[196], content-type=[text/xml; charset=UTF-8], Host=[localhost:9000], Pragma=[no-cache], SOAPAction=[""], User-Agent=[Apache CXF 2.7.4]}, HTTP_CONTEXT_MATCH_STRATEGY=stem, org.apache.cxf.request.url=http://localhost:9000/helloWorld, org.apache.cxf.request.uri=/helloWorld, HTTP.REQUEST=(POST /helloWorld)@1344650582 org.eclipse.jetty.server.Request@5025bd56, HTTP.CONFIG=null, org.apache.cxf.transport.https.CertConstraints=null, Accept=*/*, org.apache.cxf.message.Message.PATH_INFO=/helloWorld, org.apache.cxf.message.Message.BASE_PATH=/helloWorld, org.apache.cxf.continuations.ContinuationProvider=org.apache.cxf.transport.http_jetty.continuations.JettyContinuationProvider@5db18235, org.apache.cxf.message.Message.IN_INTERCEPTORS=[org.apache.cxf.transport.https.CertConstraintsInterceptor@30db95a1], org.apache.cxf.binding.soap.SoapVersion=org.apache.cxf.binding.soap.Soap11@203c9616, org.apache.cxf.message.Message.ENCODING=UTF-8, org.apache.cxf.message.Message.QUERY_STRING=null, HTTP.RESPONSE=HTTP/1.1 200 

, org.apache.cxf.security.SecurityContext=org.apache.cxf.transport.http.AbstractHTTPDestination$2@69a18ee2, org.apache.cxf.request.method=POST, org.apache.cxf.async.post.response.dispatch=true, org.apache.cxf.configuration.security.AuthorizationPolicy=null, org.apache.cxf.message.MessageFIXED_PARAMETER_ORDER=false, org.apache.cxf.transport.Destination=org.apache.cxf.transport.http_jetty.JettyHTTPDestination@6dabcd9b, http.base.path=http://localhost:9000, Content-Type=text/xml; charset=UTF-8, [email protected]{,null}}=null
null=null
============handleMessage==========
{javax.xml.ws.wsdl.port={http://service.iflytek.com/}HelloWorldServicePort, org.apache.cxf.ws.policy.EffectivePolicy=org.apache.cxf.ws.policy.EffectivePolicyImpl@4cd297c0, org.apache.cxf.service.model.MessageInfo=[MessageInfo OUTPUT: {http://service.iflytek.com/}sayHelloResponse], http.headers.copied=true, wrote.envelope.start=true, org.apache.cxf.message.Message.PROTOCOL_HEADERS={}, javax.xml.ws.wsdl.service={http://service.iflytek.com/}HelloWorldServiceService, org.apache.cxf.message.Message.ENCODING=UTF-8, org.apache.cxf.binding.soap.SoapVersion=org.apache.cxf.binding.soap.Soap11@203c9616, HTTP.RESPONSE=HTTP/1.1 200 
Content-Type: text/xml;charset=UTF-8

, javax.xml.ws.wsdl.interface={http://service.iflytek.com/}HelloWorldService, org.apache.cxf.mime.headers={}, javax.xml.ws.wsdl.operation={http://service.iflytek.com/}sayHello, javax.xml.ws.wsdl.description=http://localhost:9000/helloWorld?wsdl, org.apache.cxf.service.model.BindingMessageInfo=org.apache.cxf.service.model.BindingMessageInfo@15e8e5a7, Content-Type=text/xml, org.apache.cxf.headers.Header.list=[]}
{javax.xml.ws.wsdl.port={http://service.iflytek.com/}HelloWorldServicePort, org.apache.cxf.service.model.MessageInfo=[MessageInfo INPUT: {http://service.iflytek.com/}sayHello], org.apache.cxf.message.Message.PROTOCOL_HEADERS={Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], Content-Length=[196], content-type=[text/xml; charset=UTF-8], Host=[localhost:9000], Pragma=[no-cache], SOAPAction=[""], User-Agent=[Apache CXF 2.7.4]}, HTTP_CONTEXT_MATCH_STRATEGY=stem, org.apache.cxf.request.url=http://localhost:9000/helloWorld, javax.xml.ws.wsdl.interface={http://service.iflytek.com/}HelloWorldService, org.apache.cxf.request.uri=/helloWorld, HTTP.REQUEST=(POST /helloWorld)@1344650582 org.eclipse.jetty.server.Request@5025bd56, HTTP.CONFIG=null, org.apache.cxf.transport.https.CertConstraints=null, Accept=*/*, org.apache.cxf.headers.Header.list=[], org.apache.cxf.message.Message.PATH_INFO=/helloWorld, org.apache.cxf.message.Message.BASE_PATH=/helloWorld, org.apache.cxf.continuations.ContinuationProvider=org.apache.cxf.transport.http_jetty.continuations.JettyContinuationProvider@5db18235, javax.xml.ws.wsdl.service={http://service.iflytek.com/}HelloWorldServiceService, org.apache.cxf.message.Message.IN_INTERCEPTORS=[org.apache.cxf.transport.https.CertConstraintsInterceptor@30db95a1], org.apache.cxf.binding.soap.SoapVersion=org.apache.cxf.binding.soap.Soap11@203c9616, org.apache.cxf.message.Message.ENCODING=UTF-8, org.apache.cxf.message.Message.QUERY_STRING=null, HTTP.RESPONSE=HTTP/1.1 200 
Content-Type: text/xml;charset=UTF-8

, org.apache.cxf.security.SecurityContext=org.apache.cxf.transport.http.AbstractHTTPDestination$2@69a18ee2, org.apache.cxf.request.method=POST, org.apache.cxf.async.post.response.dispatch=true, org.apache.cxf.configuration.security.AuthorizationPolicy=null, javax.xml.ws.wsdl.operation={http://service.iflytek.com/}sayHello, org.apache.cxf.message.MessageFIXED_PARAMETER_ORDER=false, org.apache.cxf.transport.Destination=org.apache.cxf.transport.http_jetty.JettyHTTPDestination@6dabcd9b, org.apache.cxf.jaxws.context.WrappedMessageContext.SCOPES={}, javax.xml.ws.wsdl.description=http://localhost:9000/helloWorld?wsdl, http.base.path=http://localhost:9000, Content-Type=text/xml; charset=UTF-8, [email protected]{,null}}=null
{javax.xml.ws.wsdl.port={http://service.iflytek.com/}HelloWorldServicePort, org.apache.cxf.ws.policy.EffectivePolicy=org.apache.cxf.ws.policy.EffectivePolicyImpl@4cd297c0, org.apache.cxf.service.model.MessageInfo=[MessageInfo OUTPUT: {http://service.iflytek.com/}sayHelloResponse], http.headers.copied=true, wrote.envelope.start=true, org.apache.cxf.message.Message.PROTOCOL_HEADERS={}, javax.xml.ws.wsdl.service={http://service.iflytek.com/}HelloWorldServiceService, org.apache.cxf.message.Message.ENCODING=UTF-8, org.apache.cxf.binding.soap.SoapVersion=org.apache.cxf.binding.soap.Soap11@203c9616, HTTP.RESPONSE=HTTP/1.1 200 
Content-Type: text/xml;charset=UTF-8

, javax.xml.ws.wsdl.interface={http://service.iflytek.com/}HelloWorldService, org.apache.cxf.mime.headers={}, javax.xml.ws.wsdl.operation={http://service.iflytek.com/}sayHello, javax.xml.ws.wsdl.description=http://localhost:9000/helloWorld?wsdl, org.apache.cxf.service.model.BindingMessageInfo=org.apache.cxf.service.model.BindingMessageInfo@15e8e5a7, Content-Type=text/xml, org.apache.cxf.headers.Header.list=[]}=null

猜你喜欢

转载自xdwangiflytek.iteye.com/blog/1913660