package com.yh.webservice;
import org.apache.cxf.jaxws.handler.logical.LogicalMessageContextImpl;
import javax.xml.ws.handler.LogicalHandler;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.ws.handler.LogicalHandler;
import javax.xml.ws.handler.LogicalMessageContext;
import javax.xml.ws.handler.MessageContext;
import org.apache.cxf.jaxws.handler.logical.LogicalMessageContextImpl;
/**
-
@author wt
-
@version 1.0
-
@date 2020/5/9
*/
public class SessionLogicalHandler implements
LogicalHandler {public SessionLogicalHandler() {
super();
}// 静态 所有的 SessionLogicalHandler 共享 一个 cookie
public static List http_headers_cookie = null;@Override
public boolean handleMessage(LogicalMessageContext context) {
// TODO Auto-generated method stub
// javax.xml.ws.handler.MessageContext.Scope s=
// context.getScope(MessageContext.HTTP_REQUEST_HEADERS);
// Set-Cookie=[JSESSIONID=48B10A68BB05F69F8ED82A33F566C5D8; Path=/myapp;
// HttpOnly]LogicalMessageContextImpl c = (LogicalMessageContextImpl) context; // response 时记录服务端返回的session信息 if (c.get(MessageContext.HTTP_RESPONSE_HEADERS) != null) { Map<String, List> header = (Map<String, List>) c .get(MessageContext.HTTP_RESPONSE_HEADERS); List<String> ls = header.get("Set-Cookie");// 获取header 中cookie的信息 if (ls != null) { System.out.println(ls.get(0).split(";")[0]); http_headers_cookie = ls;// 保存cookie信息 } } else if (c.get(MessageContext.HTTP_REQUEST_HEADERS) != null && http_headers_cookie != null) {// request 请求的时候 把cookie信息设置进去 Map<String, List> header = (Map<String, List>) c .get(MessageContext.HTTP_REQUEST_HEADERS); header.put("cookie", http_headers_cookie); } else if (c.get(MessageContext.HTTP_REQUEST_HEADERS) == null && http_headers_cookie != null) {// request 请求的时候 把cookie信息设置进去 Map<String, List> header = new HashMap<String, List>(); header.put("cookie", http_headers_cookie); c.put(MessageContext.HTTP_REQUEST_HEADERS, header); } return true;
}
@Override
public boolean handleFault(LogicalMessageContext context) {
// TODO Auto-generated method stub
return false;
}@Override
public void close(MessageContext context) {
// TODO Auto-generated method stub}
}
package com.yh.webservice;
/**
* @author wt
* @version 1.0
* @date 2020/5/9
*/
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import com.hzjd.nbiot.entity.*;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
public class WsClassLoader {
// 本地
private static String WS_HOST = "http://localhost:8080/NBIoTService/services/NBIoTService?wsdl";
private JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
private WsClassLoader() {
}
private static WsClassLoader instance;
public static synchronized WsClassLoader getInstance() {
if (instance == null) {
instance = new WsClassLoader();
// 设置 handler
instance.factory.getHandlers().add(new SessionLogicalHandler());
}
return instance;
}
private Map<String, Object> wsCache = new HashMap<String, Object>();
@SuppressWarnings("unchecked")
public <T> T getWs(String address, Class<T> t) {
if (wsCache.get(t.getName()) == null) {
factory.setServiceClass(t);
String addressFull = WS_HOST + address;
factory.setAddress(addressFull);
Object client = factory.create();
// /开始超时设置
Client proxy = ClientProxy.getClient(client);
// proxy.get().add(new LogicalLoggingHandler());
HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
HTTPClientPolicy policy = new HTTPClientPolicy();
policy.setConnectionTimeout(60000);
policy.setReceiveTimeout(0);
conduit.setClient(policy);
// /结束超时设置
wsCache.put(t.getName(), client);
}
return (T) wsCache.get(t.getName());
}
public static void main(String[] args) {
NBIoTServicePortType client = (NBIoTServicePortType) WsClassLoader.getInstance()
.getWs("/NBIoTServicePortType", NBIoTServicePortType.class);
// login(client);
// EnableMeter(client);
}
}