新建CXF_HOME C:\ProgramFiles(my)\apache-cxf-2.7.2
在CLASSPATH %CXF_HOME%\lib
在PATH中添加 %CXF_HOME%\bin
cxf 拦截器
为了在webservice请求过程中动态操作请求和响应数据。
拦截器分类:
1.按位置:服务器端拦截器,客户端拦截器
2.按消息方向:入拦截器,出拦截器
3.按定义者分:系统拦截器,自定义拦截器
日志拦截器的实现
(1)服务端
Endpoint publish = Endpoint.publish(address, new HelloImpl());
//服务端日志入拦截器
EndpointImpl impl=(EndpointImpl) publish;
List<Interceptor<? extends Message>> inInterceptors = impl.getInInterceptors();
inInterceptors.add(new LoggingInInterceptor());
//服务端日志出拦截器
List<Interceptor<? extends Message>> outInterceptors = impl.getOutInterceptors();
outInterceptors.add(new LoggingOutInterceptor());
(2)客户端
HelloImplService factory=new HelloImplService();
Hello helloImplPort = factory.getHelloImplPort();
//发送请求的客户端对象
Client client = ClientProxy.getClient(helloImplPort);
//客户端出拦截器
List<Interceptor<? extends Message>> outInterceptors = client.getOutInterceptors();
outInterceptors.add(new LoggingOutInterceptor());
//客户端入拦截器
List<Interceptor<? extends Message>> inInterceptors = client.getInInterceptors();
inInterceptors.add(new LoggingInInterceptor());
/*int sum = helloImplPort.sum(10, 20);
System.out.println("sum:"+sum);*/
System.out.println(helloImplPort.sayHello("yanzhaojun"));
自定义拦截器
服务器端:
String address="http://172.31.255.104:8087/csf_client/Hello";
Endpoint publish = Endpoint.publish(address, new HelloImpl());
//服务端日志入拦截器
EndpointImpl impl=(EndpointImpl) publish;
List<Interceptor<? extends Message>> inInterceptors = impl.getInInterceptors();
inInterceptors.add(new LoggingInInterceptor());
inInterceptors.add(new CheckUserInterceptor());
//服务端日志出拦截器
/* List<Interceptor<? extends Message>> outInterceptors = impl.getOutInterceptors();
outInterceptors.add(new LoggingOutInterceptor());*/
System.out.println("发布webService成功!");
拦截器:
public class CheckUserInterceptor extends AbstractPhaseInterceptor<SoapMessage>{
/**
* @param phase
*/
public CheckUserInterceptor() {
super(Phase.PRE_PROTOCOL);//准备协议化的时候拦截
}
/* (non-Javadoc)
* @see org.apache.cxf.interceptor.Interceptor#handleMessage(org.apache.cxf.message.Message)
*/
@Override
public void handleMessage(SoapMessage msg) throws Fault {
// TODO Auto-generated method stub
Header header = msg.getHeader(new QName("hq"));
if(header!=null){
Element hq = (Element) header.getObject();
String name = hq.getElementsByTagName("name").item(0).getTextContent();
String password = hq.getElementsByTagName("password").item(0).getTextContent();
if("yanzhaojun".equals(name)&&"123456".equals(password)){
System.out.println("server: handleMessage()...通过拦截器");
return ;
}
}
//不能通过
System.out.println("server 没有通过拦截器");
throw new Fault(new RuntimeException("请求需要一个正确的你用户名和密码!"));
}
}
客户端:
HelloImplService factory=new HelloImplService();
Hello helloImplPort = factory.getHelloImplPort();
//发送请求的客户端对象
Client client = ClientProxy.getClient(helloImplPort);
//客户端出拦截器
List<Interceptor<? extends Message>> outInterceptors = client.getOutInterceptors();
outInterceptors.add(new LoggingOutInterceptor());
//自定义出拦截器
outInterceptors.add(new AddUserInterceptor("yanzhaojun", "123456"));
//客户端入拦截器
/*List<Interceptor<? extends Message>> inInterceptors = client.getInInterceptors();
inInterceptors.add(new LoggingInInterceptor());*/
/*int sum = helloImplPort.sum(10, 20);
System.out.println("sum:"+sum);*/
System.out.println(helloImplPort.sayHello("yanzhaojun"));
拦截器:
public class AddUserInterceptor extends AbstractPhaseInterceptor<SoapMessage>{
private String name ;
private String password;
/**
* @param phase
*/
public AddUserInterceptor(String name,String password) {
super(Phase.PRE_PROTOCOL);//准备协议化的时候拦截
// TODO Auto-generated constructor stub
this.name=name;
this.password=password;
}
/* (non-Javadoc)
* @see org.apache.cxf.interceptor.Interceptor#handleMessage(org.apache.cxf.message.Message)
*/
@Override
public void handleMessage(SoapMessage msg) throws Fault {
// TODO Auto-generated method stub
List<Header> headers = msg.getHeaders();
Document document = DOMHelper.createDocument();
Element root = document.createElement("hq");
Element name = document.createElement("name");
name.setTextContent(this.name);
root.appendChild(name);
Element pass = document.createElement("password");
pass.setTextContent(this.password);
root.appendChild(pass);
headers.add(new Header(new QName("hq"), root));
System.out.println("client :handlerMessage()");
}
}
使用jquery来发送请求(自己拼接soap请求消息,拿到soap响应消息以后需要自己去解析)
function clickaa(){
var name=document.getElementById("username").value;
var data='<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHello xmlns:ns2="http://test.hq.com/"><arg0>'+
name+'</arg0></ns2:sayHello></soap:Body></soap:Envelope>';
var xmlhttp;
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
var resA=xmlhttp.responseXML;
alert(resA);
var dd=resA.getElementsByTagName("return")[0];
alert(dd.firstChild.data);
}
};
//打开连接
xmlhttp.open("POST","http://172.31.255.104:8087/csf_client/Hello",true);
//设置请求头
xmlhttp.setRequestHeader("Content-type", "");
//发送请求
xmlhttp.send(data);
}