|
@@ -0,0 +1,196 @@
|
|
|
+package com.hrsoft.edi.stub.wt;
|
|
|
+
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.hrsoft.edi.stub.internal.XmlWriter;
|
|
|
+import com.qimen.api.QimenClient;
|
|
|
+import com.qimen.api.QimenRequest;
|
|
|
+import com.qimen.api.QimenResponse;
|
|
|
+import com.taobao.api.ApiException;
|
|
|
+import com.taobao.api.Constants;
|
|
|
+import com.taobao.api.TaobaoParser;
|
|
|
+import com.taobao.api.internal.cluster.ClusterManager;
|
|
|
+import com.taobao.api.internal.cluster.DnsConfig;
|
|
|
+import com.taobao.api.internal.parser.xml.QimenXmlParser;
|
|
|
+import com.taobao.api.internal.util.*;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.net.Proxy;
|
|
|
+import java.net.URL;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+public class WTWcsQimenClient implements QimenClient
|
|
|
+{
|
|
|
+ protected String serverUrl;
|
|
|
+
|
|
|
+ protected String appKey;
|
|
|
+
|
|
|
+ protected String appSecret;
|
|
|
+
|
|
|
+ protected String signMethod = "md5";
|
|
|
+
|
|
|
+ protected int connectTimeout = 15000;
|
|
|
+
|
|
|
+ protected int readTimeout = 30000;
|
|
|
+
|
|
|
+ protected boolean needEnableParser = true;
|
|
|
+
|
|
|
+ protected boolean useGzipEncoding = true;
|
|
|
+
|
|
|
+ private boolean isHttpDnsEnabled = false;
|
|
|
+
|
|
|
+ private String originalHttpHost = null;
|
|
|
+
|
|
|
+ public WTWcsQimenClient(String serverUrl, String appKey, String appSecret)
|
|
|
+ {
|
|
|
+ this.appKey = appKey;
|
|
|
+ this.appSecret = appSecret;
|
|
|
+ this.serverUrl = serverUrl;
|
|
|
+ }
|
|
|
+
|
|
|
+ public <T extends QimenResponse> T execute(QimenRequest<T> request)
|
|
|
+ throws ApiException
|
|
|
+ {
|
|
|
+ return this.execute(request, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public <T extends QimenResponse> T execute(QimenRequest<T> request, String session)
|
|
|
+ throws ApiException
|
|
|
+ {
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ RequestParametersHolder requestHolder = new RequestParametersHolder();
|
|
|
+ requestHolder.setApplicationParams(request.getQueryParams());
|
|
|
+ TaobaoHashMap protocalMustParams = new TaobaoHashMap();
|
|
|
+ protocalMustParams.put("method", request.getApiMethodName());
|
|
|
+ protocalMustParams.put("appKey", this.appKey);
|
|
|
+ Long timestamp = request.getTimestamp();
|
|
|
+ if (timestamp == null)
|
|
|
+ {
|
|
|
+ timestamp = System.currentTimeMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+ protocalMustParams.put("timestamp", new Date(timestamp));
|
|
|
+ requestHolder.setProtocalMustParams(protocalMustParams);
|
|
|
+ TaobaoHashMap protocalOptParams = new TaobaoHashMap();
|
|
|
+ protocalOptParams.put("session", session);
|
|
|
+ protocalOptParams.put("customerCode", request.getCustomerId());
|
|
|
+ requestHolder.setProtocalOptParams(protocalOptParams);
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ String apiBody = request.getBody();
|
|
|
+
|
|
|
+ protocalMustParams.put("sign", TaobaoUtils.signTopRequestWithBody(requestHolder, apiBody, this.appSecret, this.signMethod));
|
|
|
+ String sysMustQuery = WebUtils.buildQuery(requestHolder.getProtocalMustParams(), "UTF-8");
|
|
|
+ String sysOptQuery = WebUtils.buildQuery(requestHolder.getProtocalOptParams(), "UTF-8");
|
|
|
+ String realServerUrl = this.getServerUrl(this.serverUrl);
|
|
|
+ String fullUrl = WebUtils.buildRequestUrl(realServerUrl, new String[] {sysMustQuery, sysOptQuery});
|
|
|
+ TaobaoHashMap headerMap = new TaobaoHashMap();
|
|
|
+ if (this.useGzipEncoding)
|
|
|
+ {
|
|
|
+ headerMap.put("Accept-Encoding", "gzip");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (this.getTopHttpDnsHost() != null)
|
|
|
+ {
|
|
|
+ headerMap.put("TOP_HTTP_DNS_HOST", this.getTopHttpDnsHost());
|
|
|
+ }
|
|
|
+
|
|
|
+ HttpResponse response = HttpUtil.createPost(fullUrl)
|
|
|
+ .addHeaders(headerMap)
|
|
|
+ .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
|
|
|
+ .body(apiBody.getBytes(StandardCharsets.UTF_8))
|
|
|
+ .setConnectionTimeout(this.connectTimeout)
|
|
|
+ .setReadTimeout(this.readTimeout)
|
|
|
+ .execute();
|
|
|
+ requestHolder.setResponseBody(response.body());
|
|
|
+ requestHolder.setResponseHeaders(response.headers());
|
|
|
+ }
|
|
|
+ catch (IOException var17)
|
|
|
+ {
|
|
|
+ TaobaoLogger.logApiError(this.appKey,
|
|
|
+ request.getApiMethodName(),
|
|
|
+ this.serverUrl,
|
|
|
+ requestHolder.getAllParams(),
|
|
|
+ System.currentTimeMillis() - start,
|
|
|
+ var17.toString());
|
|
|
+ throw new ApiException(var17);
|
|
|
+ }
|
|
|
+
|
|
|
+ T tRsp = null;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ tRsp = request.getResponseClass().newInstance();
|
|
|
+ tRsp.setBody(requestHolder.getResponseBody());
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ throw new ApiException(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!tRsp.isSuccess())
|
|
|
+ {
|
|
|
+ TaobaoLogger.logApiError(appKey,
|
|
|
+ request.getApiMethodName(),
|
|
|
+ serverUrl,
|
|
|
+ requestHolder.getAllParams(),
|
|
|
+ System.currentTimeMillis() - start,
|
|
|
+ tRsp.getBody());
|
|
|
+ }
|
|
|
+ return tRsp;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getServerUrl(String serverUrl)
|
|
|
+ {
|
|
|
+ if (this.isHttpDnsEnabled)
|
|
|
+ {
|
|
|
+ DnsConfig dnsConfig = ClusterManager.GetDnsConfigFromCache();
|
|
|
+ return dnsConfig == null ? serverUrl : dnsConfig.getVipUrl(serverUrl);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return serverUrl;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getTopHttpDnsHost()
|
|
|
+ {
|
|
|
+ return this.isHttpDnsEnabled ? this.originalHttpHost : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void enableHttpDns()
|
|
|
+ {
|
|
|
+ WebUtils.setIgnoreHostCheck(true);
|
|
|
+ this.setHttpDnsHost(this.serverUrl);
|
|
|
+ ClusterManager.initRefreshThread(this.appKey, this.appSecret);
|
|
|
+ this.isHttpDnsEnabled = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void enableHttpDns(String onlineAppKey, String onlineAppSecret)
|
|
|
+ {
|
|
|
+ WebUtils.setIgnoreHostCheck(true);
|
|
|
+ this.setHttpDnsHost(this.serverUrl);
|
|
|
+ ClusterManager.initRefreshThread(onlineAppKey, onlineAppSecret);
|
|
|
+ this.isHttpDnsEnabled = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void setHttpDnsHost(String serverUrl)
|
|
|
+ {
|
|
|
+ if (serverUrl != null)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ URL url = new URL(serverUrl);
|
|
|
+ this.originalHttpHost = url.getHost();
|
|
|
+ }
|
|
|
+ catch (Exception var3)
|
|
|
+ {
|
|
|
+ throw new RuntimeException("error serverUrl:" + serverUrl, var3);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|