关闭

webservice利用cxf框架编写日志拦截器和自定义拦截器

标签: webService利用cxf编写拦截器
710人阅读 评论(0) 收藏 举报
分类:

1、编写日志拦截器

1.,1、服务器端的关键代码:

//发布webService
public class ServerTest {
	public static void main(String[] args) {
		String address="http://localhost:8081/day02-ws/hellows";
		Endpoint endpoint = Endpoint.publish(address, new HelloWsImpl());
		System.out.println(endpoint);	
		EndpointImpl EndpointImpl = (EndpointImpl) endpoint;
	        org.apache.cxf.endpoint.Server server = EndpointImpl.getServer();
	        org.apache.cxf.endpoint.Endpoint cxfEndpoint = server.getEndpoint();
	        //服务端的日志入拦截器
	        cxfEndpoint.getInInterceptors().add(new LoggingInInterceptor());
	        //服务器端的日志出拦截器
	        cxfEndpoint.getInInterceptors().add(new LoggingOutInterceptor());
	        System.out.println("Web service started");
	}
}

1.2、客户端关键代码

//调用webService
public class ClientTest {
	public static void main(String[] args) {
		HelloWsImplService factory=new HelloWsImplService();
		HelloWs hwi=factory.getHelloWsImplPort();	
		//发送请求的客户端对象
		 org.apache.cxf.endpoint.Client client = org.apache.cxf.frontend.ClientProxy
	                .getClient(hwi);
	        org.apache.cxf.endpoint.Endpoint cxfEndpoint = client.getEndpoint();
	       
	      //客户端出拦截器
	        cxfEndpoint.getOutInterceptors().add(new LoggingOutInterceptor());
	      //客户端入拦截器  
	        cxfEndpoint.getOutInterceptors().add(new LoggingInInterceptor());
	        
	 
		
		String result= hwi.sayHello("xiaojie");
		System.out.println("client "+result);
	}
}

2、编写自定义拦截器(实现用户名和密码的校验)

2.1 、服务器端定义的入拦截器代码

//检查用户的拦截器
public class CheckUserInterceptor extends AbstractPhaseInterceptor<SoapMessage>{
	

	public CheckUserInterceptor() {
		// TODO Auto-generated constructor stub
		super(Phase.PRE_PROTOCOL);
	}

	@Override
	public void handleMessage(SoapMessage message) throws Fault {
		// TODO Auto-generated method stub
		Header header = message.getHeader(new QName("xiaojie"));
		if(header!=null){
			Element xiaojie = (Element) header.getObject();
			String name=xiaojie.getElementsByTagName("name").item(0).getTextContent();
			String password=xiaojie.getElementsByTagName("password").item(0).getTextContent();
			if("afan".equals(name)&&"123456".equals(password)){
				System.out.println("server拦截器通过");
				return;
			}
			//不能通过
			System.out.println("server拦截器没有通过");
			throw new Fault(new RuntimeException("请求需要一个正确的用户名和密码"));	
			
			
		}
		
	}

}

2.2、服务端使用自定义拦截器的代码:

//发布webService
public class ServerTest2 {
	public static void main(String[] args) {
		String address="http://localhost:8081/day02-ws/hellows";
		Endpoint endpoint = Endpoint.publish(address, new HelloWsImpl());
		System.out.println(endpoint);
		 EndpointImpl EndpointImpl = (EndpointImpl) endpoint;
	        org.apache.cxf.endpoint.Server server = EndpointImpl.getServer();
	        org.apache.cxf.endpoint.Endpoint cxfEndpoint = server.getEndpoint();
	        //服务端的入拦截器
	        cxfEndpoint.getInInterceptors().add(new CheckUserInterceptor());
	        System.out.println("Web service started");
	}
}

2.3、客户端自定义的出拦截器代码:

//自定义拦截器
public class AddUserInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
	private String name;
	private String password;
	
	
	//需要手动增加一个构造方法
	public AddUserInterceptor(String name, String password) {
		super(Phase.PRE_PROTOCOL);//确定拦截的时间,准备协议化时拦截
		this.name = name;
		this.password = password;
	}



	
	//以下的方法是:当拦截到某个消息的时候就会调用
	@SuppressWarnings("deprecation")
	/*
		请求体:
		 <Envelope>
		 	<head>
		 		<xiaojie>
		 			<name>afan</name>
		 			<password>123456</password>
		 			
		 		</xiaojie>
		 	</head>
		 	<Body>
		 		<sayHello>
		 			<arg0>tom</arg0>
		 		</sayHello>
		 	</Body>
		 </Envelope>
	 */
	@Override
	public void handleMessage(SoapMessage msg) throws Fault {
		// TODO Auto-generated method stub
		List<Header> headers = msg.getHeaders();//得到所有的请求头
		Document document = DOMHelper.createDocument();//创建文档
		Element rootEle = document.createElement("xiaojie");
		Element nameEle = document.createElement("name");
		nameEle.setTextContent(name);
		rootEle.appendChild(nameEle);
		
		Element passwordEle = document.createElement("password");
		passwordEle.setTextContent(password);
		rootEle.appendChild(passwordEle);
		
		
		
		headers.add(new Header(new QName("xiaojie"), rootEle));
		System.out.println("client handleMessage");
	}

}

2..4、客户端使用自定义的拦截器代码:

//调用webService
public class ClientTest2 {
	public static void main(String[] args) {
		HelloWsImplService factory=new HelloWsImplService();
		HelloWs hwi=factory.getHelloWsImplPort();
	
		//发送请求的客户端对象
		 org.apache.cxf.endpoint.Client client = org.apache.cxf.frontend.ClientProxy
	                .getClient(hwi);
	        org.apache.cxf.endpoint.Endpoint cxfEndpoint = client.getEndpoint();
	       
	      //客户端出拦截器
	        cxfEndpoint.getOutInterceptors().add(new AddUserInterceptor("afan", "123456"));
	 
		
		String result= hwi.sayHello("xiaojie");
		System.out.println("client "+result);
		
	}
}

如果遇到不懂的请联系本人qq:1913284695
或者微信:fyydbc

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:76688次
    • 积分:963
    • 等级:
    • 排名:千里之外
    • 原创:36篇
    • 转载:0篇
    • 译文:0篇
    • 评论:42条
    最新评论