add SoapClinet.reset

This commit is contained in:
Looly 2019-09-21 07:52:47 +08:00
parent 8d51e2343d
commit c648c8c945
2 changed files with 142 additions and 85 deletions

View File

@ -14,6 +14,7 @@
* 【core】 FileUtil.normalize在win下支持samba路径issue#549@Github * 【core】 FileUtil.normalize在win下支持samba路径issue#549@Github
* 【core】 修复Validator注释错误pr#70@Gitee * 【core】 修复Validator注释错误pr#70@Gitee
* 【cron】 添加获取任务表的方法issue#I12E5H@Gitee * 【cron】 添加获取任务表的方法issue#I12E5H@Gitee
* 【http】 SoapClient增加reset方法用于此对象的复用issue#I12CCC@Gitee
### Bug修复 ### Bug修复
* 【core】 修复DateUtil.offset导致的时区错误问题issue#I1294O@Gitee * 【core】 修复DateUtil.offset导致的时区错误问题issue#I1294O@Gitee

View File

@ -33,31 +33,66 @@ import cn.hutool.http.HttpResponse;
/** /**
* SOAP客户端 * SOAP客户端
* *
* <p>
* 此对象用于构建一个SOAP消息并通过HTTP接口发出消息内容
* SOAP消息本质上是一个XML文本可以通过调用{@link #getMsgStr(boolean)} 方法获取消息体
* <p>
* 使用方法
*
* <pre>
* SoapClient client = SoapClient.create(url)
* .setMethod(methodName, namespaceURI)
* .setCharset(CharsetUtil.CHARSET_GBK)
* .setParam(param1, "XXX");
*
* String response = client.send(true);
*
* </pre>
*
* @author looly * @author looly
* @since 4.5.4 * @since 4.5.4
*/ */
public class SoapClient { public class SoapClient {
/** XML消息体的Content-Type */ /**
* XML消息体的Content-Type
*/
private static final String TEXT_XML_CONTENT_TYPE = "text/xml;charset="; private static final String TEXT_XML_CONTENT_TYPE = "text/xml;charset=";
/** 请求的URL地址 */ /**
* 请求的URL地址
*/
private String url; private String url;
/** 编码 */ /**
* 编码
*/
private Charset charset = CharsetUtil.CHARSET_UTF_8; private Charset charset = CharsetUtil.CHARSET_UTF_8;
/** SOAP消息 */ /**
private SOAPMessage message; * 默认连接超时
/** 消息方法节点 */ */
private SOAPBodyElement methodEle;
/** 应用于方法上的命名空间URI */
private String namespaceURI;
/** 消息工厂,用于创建消息 */
private MessageFactory factory;
/** 默认连接超时 */
private int connectionTimeout = HttpGlobalConfig.getTimeout(); private int connectionTimeout = HttpGlobalConfig.getTimeout();
/** 默认读取超时 */ /**
* 默认读取超时
*/
private int readTimeout = HttpGlobalConfig.getTimeout(); private int readTimeout = HttpGlobalConfig.getTimeout();
/**
* 消息工厂用于创建消息
*/
private MessageFactory factory;
/**
* SOAP消息
*/
private SOAPMessage message;
/**
* 消息方法节点
*/
private SOAPBodyElement methodEle;
/**
* 应用于方法上的命名空间URI
*/
private String namespaceURI;
/** /**
* 创建SOAP客户端默认使用soap1.1版本协议 * 创建SOAP客户端默认使用soap1.1版本协议
* *
@ -71,7 +106,7 @@ public class SoapClient {
/** /**
* 创建SOAP客户端 * 创建SOAP客户端
* *
* @param url WS的URL地址 * @param url WS的URL地址
* @param protocol 协议{@link SoapProtocol} * @param protocol 协议{@link SoapProtocol}
* @return {@link SoapClient} * @return {@link SoapClient}
*/ */
@ -82,8 +117,8 @@ public class SoapClient {
/** /**
* 创建SOAP客户端 * 创建SOAP客户端
* *
* @param url WS的URL地址 * @param url WS的URL地址
* @param protocol 协议{@link SoapProtocol} * @param protocol 协议{@link SoapProtocol}
* @param namespaceURI 方法上的命名空间URI * @param namespaceURI 方法上的命名空间URI
* @return {@link SoapClient} * @return {@link SoapClient}
* @since 4.5.6 * @since 4.5.6
@ -104,7 +139,7 @@ public class SoapClient {
/** /**
* 构造 * 构造
* *
* @param url WS的URL地址 * @param url WS的URL地址
* @param protocol 协议版本{@link SoapProtocol} * @param protocol 协议版本{@link SoapProtocol}
*/ */
public SoapClient(String url, SoapProtocol protocol) { public SoapClient(String url, SoapProtocol protocol) {
@ -114,8 +149,8 @@ public class SoapClient {
/** /**
* 构造 * 构造
* *
* @param url WS的URL地址 * @param url WS的URL地址
* @param protocol 协议版本{@link SoapProtocol} * @param protocol 协议版本{@link SoapProtocol}
* @param namespaceURI 方法上的命名空间URI * @param namespaceURI 方法上的命名空间URI
* @since 4.5.6 * @since 4.5.6
*/ */
@ -144,6 +179,26 @@ public class SoapClient {
return this; return this;
} }
/**
* 重置SOAP客户端用于客户端复用
*
* <p>
* 重置后需调用serMethod方法重新指定请求方法并调用setParam方法重新定义参数
*
* @return this
* @since 4.6.7
*/
public SoapClient reset() {
try {
this.message = factory.createMessage();
} catch (SOAPException e) {
throw new SoapRuntimeException(e);
}
this.methodEle = null;
return this;
}
/** /**
* 设置编码 * 设置编码
* *
@ -186,11 +241,11 @@ public class SoapClient {
/** /**
* 设置头信息 * 设置头信息
* *
* @param name 头信息标签名 * @param name 头信息标签名
* @param actorURI 中间的消息接收者 * @param actorURI 中间的消息接收者
* @param roleUri Role的URI * @param roleUri Role的URI
* @param mustUnderstand 标题项对于要对其进行处理的接收者来说是强制的还是可选的 * @param mustUnderstand 标题项对于要对其进行处理的接收者来说是强制的还是可选的
* @param relay relay属性 * @param relay relay属性
* @return this * @return this
*/ */
public SoapClient setHeader(QName name, String actorURI, String roleUri, Boolean mustUnderstand, Boolean relay) { public SoapClient setHeader(QName name, String actorURI, String roleUri, Boolean mustUnderstand, Boolean relay) {
@ -222,8 +277,8 @@ public class SoapClient {
/** /**
* 设置请求方法 * 设置请求方法
* *
* @param name 方法名及其命名空间 * @param name 方法名及其命名空间
* @param params 参数 * @param params 参数
* @param useMethodPrefix 是否使用方法的命名空间前缀 * @param useMethodPrefix 是否使用方法的命名空间前缀
* @return this * @return this
*/ */
@ -234,8 +289,8 @@ public class SoapClient {
/** /**
* 设置请求方法 * 设置请求方法
* *
* @param name 方法名及其命名空间 * @param name 方法名及其命名空间
* @param params 参数 * @param params 参数
* @param useMethodPrefix 是否使用方法的命名空间前缀 * @param useMethodPrefix 是否使用方法的命名空间前缀
* @return this * @return this
*/ */
@ -267,7 +322,7 @@ public class SoapClient {
* 方法名自动识别前缀前缀和方法名使用:分隔<br> * 方法名自动识别前缀前缀和方法名使用:分隔<br>
* 当识别到前缀后自动添加xmlns属性关联到传入的namespaceURI * 当识别到前缀后自动添加xmlns属性关联到传入的namespaceURI
* *
* @param methodName 方法名可有前缀也可无 * @param methodName 方法名可有前缀也可无
* @param namespaceURI 命名空间URI * @param namespaceURI 命名空间URI
* @return this * @return this
*/ */
@ -301,7 +356,7 @@ public class SoapClient {
/** /**
* 设置方法参数使用方法的前缀 * 设置方法参数使用方法的前缀
* *
* @param name 参数名 * @param name 参数名
* @param value 参数值可以是字符串或Map或{@link SOAPElement} * @param value 参数值可以是字符串或Map或{@link SOAPElement}
* @return this * @return this
*/ */
@ -312,8 +367,8 @@ public class SoapClient {
/** /**
* 设置方法参数 * 设置方法参数
* *
* @param name 参数名 * @param name 参数名
* @param value 参数值可以是字符串或Map或{@link SOAPElement} * @param value 参数值可以是字符串或Map或{@link SOAPElement}
* @param useMethodPrefix 是否使用方法的命名空间前缀 * @param useMethodPrefix 是否使用方法的命名空间前缀
* @return this * @return this
*/ */
@ -336,7 +391,7 @@ public class SoapClient {
/** /**
* 批量设置参数 * 批量设置参数
* *
* @param params 参数列表 * @param params 参数列表
* @param useMethodPrefix 是否使用方法的命名空间前缀 * @param useMethodPrefix 是否使用方法的命名空间前缀
* @return this * @return this
* @since 4.5.6 * @since 4.5.6
@ -448,7 +503,7 @@ public class SoapClient {
final HttpResponse res = sendForResponse(); final HttpResponse res = sendForResponse();
final MimeHeaders headers = new MimeHeaders(); final MimeHeaders headers = new MimeHeaders();
for (Entry<String, List<String>> entry : res.headers().entrySet()) { for (Entry<String, List<String>> entry : res.headers().entrySet()) {
if(StrUtil.isNotEmpty(entry.getKey())) { if (StrUtil.isNotEmpty(entry.getKey())) {
headers.setHeader(entry.getKey(), CollUtil.get(entry.getValue(), 0)); headers.setHeader(entry.getKey(), CollUtil.get(entry.getValue(), 0));
} }
} }
@ -482,6 +537,7 @@ public class SoapClient {
} }
// -------------------------------------------------------------------------------------------------------- Private method start // -------------------------------------------------------------------------------------------------------- Private method start
/** /**
* 发送请求获取异步响应 * 发送请求获取异步响应
* *
@ -489,12 +545,12 @@ public class SoapClient {
*/ */
private HttpResponse sendForResponse() { private HttpResponse sendForResponse() {
return HttpRequest.post(this.url)// return HttpRequest.post(this.url)//
.setFollowRedirects(true)// .setFollowRedirects(true)//
.setConnectionTimeout(this.connectionTimeout) .setConnectionTimeout(this.connectionTimeout)
.setReadTimeout(this.readTimeout) .setReadTimeout(this.readTimeout)
.contentType(getXmlContentType())// .contentType(getXmlContentType())//
.body(getMsgStr(false))// .body(getMsgStr(false))//
.executeAsync(); .executeAsync();
} }
/** /**
@ -509,9 +565,9 @@ public class SoapClient {
/** /**
* 设置方法参数 * 设置方法参数
* *
* @param ele 方法节点 * @param ele 方法节点
* @param name 参数名 * @param name 参数名
* @param value 参数值 * @param value 参数值
* @param prefix 命名空间前缀 * @param prefix 命名空间前缀
* @return {@link SOAPElement}子节点 * @return {@link SOAPElement}子节点
*/ */
@ -528,7 +584,7 @@ public class SoapClient {
throw new SoapRuntimeException(e); throw new SoapRuntimeException(e);
} }
if(null != value) { if (null != value) {
if (value instanceof SOAPElement) { if (value instanceof SOAPElement) {
// 单个子节点 // 单个子节点
try { try {