|
@@ -4,6 +4,7 @@ package com.yiidata.intergration.api.utils;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import com.datasophon.api.utils.HttpUtils;
|
|
|
import com.google.common.io.ByteStreams;
|
|
import com.google.common.io.ByteStreams;
|
|
|
import com.google.common.net.HttpHeaders;
|
|
import com.google.common.net.HttpHeaders;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -23,6 +24,7 @@ import org.apache.http.config.RegistryBuilder;
|
|
|
import org.apache.http.config.SocketConfig;
|
|
import org.apache.http.config.SocketConfig;
|
|
|
import org.apache.http.conn.socket.ConnectionSocketFactory;
|
|
import org.apache.http.conn.socket.ConnectionSocketFactory;
|
|
|
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
|
|
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
|
|
|
|
|
+import org.apache.http.conn.ssl.DefaultHostnameVerifier;
|
|
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
|
|
import org.apache.http.entity.StringEntity;
|
|
import org.apache.http.entity.StringEntity;
|
|
|
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
|
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
|
@@ -53,13 +55,14 @@ import java.security.NoSuchAlgorithmException;
|
|
|
import java.security.cert.CertificateException;
|
|
import java.security.cert.CertificateException;
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
+import java.util.Objects;
|
|
|
|
|
+import java.util.Optional;
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
*
|
|
*
|
|
|
* 封装 RestApi 调用
|
|
* 封装 RestApi 调用
|
|
|
*
|
|
*
|
|
|
- *
|
|
|
|
|
* <pre>
|
|
* <pre>
|
|
|
*
|
|
*
|
|
|
* Created by zhaopx.
|
|
* Created by zhaopx.
|
|
@@ -92,14 +95,17 @@ public class RestApiUtils {
|
|
|
//设置协议http和https对应的处理socket链接工厂的对象
|
|
//设置协议http和https对应的处理socket链接工厂的对象
|
|
|
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
|
|
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
|
|
|
.register("http", PlainConnectionSocketFactory.INSTANCE)
|
|
.register("http", PlainConnectionSocketFactory.INSTANCE)
|
|
|
- .register("https", new SSLConnectionSocketFactory(createIgnoreVerifySSL()))
|
|
|
|
|
|
|
+ .register("https", new SSLConnectionSocketFactory(createIgnoreVerifySSL(), null, null, new DefaultHostnameVerifier()))
|
|
|
.build();
|
|
.build();
|
|
|
|
|
|
|
|
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
|
|
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
|
|
|
httpClient = HttpClients.custom()
|
|
httpClient = HttpClients.custom()
|
|
|
.setConnectionManager(cm)
|
|
.setConnectionManager(cm)
|
|
|
.setDefaultCookieStore(cookieStore)
|
|
.setDefaultCookieStore(cookieStore)
|
|
|
- .setDefaultRequestConfig(RequestConfig.custom().setConnectTimeout(180000).build())
|
|
|
|
|
|
|
+ .setDefaultRequestConfig(RequestConfig.custom()
|
|
|
|
|
+ .setConnectTimeout(65000)
|
|
|
|
|
+ .setSocketTimeout(30000)
|
|
|
|
|
+ .setConnectionRequestTimeout(30000).build())
|
|
|
.setRetryHandler(new DefaultHttpRequestRetryHandler(3, false))
|
|
.setRetryHandler(new DefaultHttpRequestRetryHandler(3, false))
|
|
|
.build();
|
|
.build();
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@@ -108,7 +114,6 @@ public class RestApiUtils {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* 绕过验证
|
|
* 绕过验证
|
|
|
*
|
|
*
|
|
@@ -117,7 +122,7 @@ public class RestApiUtils {
|
|
|
* @throws KeyManagementException
|
|
* @throws KeyManagementException
|
|
|
*/
|
|
*/
|
|
|
public static SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException {
|
|
public static SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException {
|
|
|
- SSLContext sc = SSLContext.getInstance("SSLv3");
|
|
|
|
|
|
|
+ SSLContext sc = SSLContext.getInstance("TLSv1.2");
|
|
|
|
|
|
|
|
// 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
|
|
// 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
|
|
|
X509TrustManager trustManager = new X509TrustManager() {
|
|
X509TrustManager trustManager = new X509TrustManager() {
|
|
@@ -145,6 +150,7 @@ public class RestApiUtils {
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 调用一次远程 api
|
|
* 调用一次远程 api
|
|
|
|
|
+ *
|
|
|
* @param url
|
|
* @param url
|
|
|
* @return
|
|
* @return
|
|
|
* @throws IOException
|
|
* @throws IOException
|
|
@@ -156,6 +162,7 @@ public class RestApiUtils {
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 调用一次远程 api
|
|
* 调用一次远程 api
|
|
|
|
|
+ *
|
|
|
* @param url
|
|
* @param url
|
|
|
* @return
|
|
* @return
|
|
|
* @throws IOException
|
|
* @throws IOException
|
|
@@ -179,6 +186,7 @@ public class RestApiUtils {
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 通过 Post 请求调用Rest 接口
|
|
* 通过 Post 请求调用Rest 接口
|
|
|
|
|
+ *
|
|
|
* @param url
|
|
* @param url
|
|
|
* @param json
|
|
* @param json
|
|
|
* @param not200ThrowError 为 true 时,当返回不是 200,则抛出异常
|
|
* @param not200ThrowError 为 true 时,当返回不是 200,则抛出异常
|
|
@@ -192,6 +200,7 @@ public class RestApiUtils {
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* POST 请求,执行远程
|
|
* POST 请求,执行远程
|
|
|
|
|
+ *
|
|
|
* @param url
|
|
* @param url
|
|
|
* @param jsonStr
|
|
* @param jsonStr
|
|
|
* @param not200ThrowError
|
|
* @param not200ThrowError
|
|
@@ -205,6 +214,7 @@ public class RestApiUtils {
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* POST 请求执行远程链接
|
|
* POST 请求执行远程链接
|
|
|
|
|
+ *
|
|
|
* @param url
|
|
* @param url
|
|
|
* @param jsonStr 请求 Body 体
|
|
* @param jsonStr 请求 Body 体
|
|
|
* @param header 请求头
|
|
* @param header 请求头
|
|
@@ -252,6 +262,7 @@ public class RestApiUtils {
|
|
|
//---------
|
|
//---------
|
|
|
/**
|
|
/**
|
|
|
* 调用一次远程 api
|
|
* 调用一次远程 api
|
|
|
|
|
+ *
|
|
|
* @param url
|
|
* @param url
|
|
|
* @return
|
|
* @return
|
|
|
* @throws IOException
|
|
* @throws IOException
|
|
@@ -263,6 +274,7 @@ public class RestApiUtils {
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 调用一次远程 api
|
|
* 调用一次远程 api
|
|
|
|
|
+ *
|
|
|
* @param url
|
|
* @param url
|
|
|
* @return
|
|
* @return
|
|
|
* @throws IOException
|
|
* @throws IOException
|
|
@@ -286,6 +298,7 @@ public class RestApiUtils {
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 通过 Post 请求调用Rest 接口
|
|
* 通过 Post 请求调用Rest 接口
|
|
|
|
|
+ *
|
|
|
* @param url
|
|
* @param url
|
|
|
* @param json
|
|
* @param json
|
|
|
* @param not200ThrowError 为 true 时,当返回不是 200,则抛出异常
|
|
* @param not200ThrowError 为 true 时,当返回不是 200,则抛出异常
|
|
@@ -298,6 +311,7 @@ public class RestApiUtils {
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 通过 Post 请求调用Rest 接口
|
|
* 通过 Post 请求调用Rest 接口
|
|
|
|
|
+ *
|
|
|
* @param url
|
|
* @param url
|
|
|
* @param json
|
|
* @param json
|
|
|
* @param not200ThrowError 为 true 时,当返回不是 200,则抛出异常
|
|
* @param not200ThrowError 为 true 时,当返回不是 200,则抛出异常
|
|
@@ -322,19 +336,25 @@ public class RestApiUtils {
|
|
|
try {
|
|
try {
|
|
|
resp = httpClient.execute(get);
|
|
resp = httpClient.execute(get);
|
|
|
log.info("execute[get] url {} return code: {}", url, resp.getStatusLine().getStatusCode());
|
|
log.info("execute[get] url {} return code: {}", url, resp.getStatusLine().getStatusCode());
|
|
|
- HttpEntity entity = resp.getEntity();
|
|
|
|
|
- String result = EntityUtils.toString(entity);
|
|
|
|
|
|
|
+ final String contentType = Optional.ofNullable(resp.getFirstHeader("Content-Type")).map(Header::getValue).orElse("text/html");
|
|
|
|
|
+ final HttpEntity entity = resp.getEntity();
|
|
|
|
|
+ String result = EntityUtils.toString(entity, StandardCharsets.UTF_8);
|
|
|
EntityUtils.consume(entity);
|
|
EntityUtils.consume(entity);
|
|
|
if(not200ThrowError && resp.getStatusLine().getStatusCode() != 200) {
|
|
if(not200ThrowError && resp.getStatusLine().getStatusCode() != 200) {
|
|
|
throw new IOException(result);
|
|
throw new IOException(result);
|
|
|
}
|
|
}
|
|
|
- Object jsonResult = JSON.parse(result);
|
|
|
|
|
|
|
+ // 判断一下返回 类型
|
|
|
JSONObject jsonObject = new JSONObject(2);
|
|
JSONObject jsonObject = new JSONObject(2);
|
|
|
|
|
+ if(!contentType.contains("json")) {
|
|
|
|
|
+ jsonObject.put("result", result);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ Object jsonResult = JSON.parse(result);
|
|
|
if(jsonResult instanceof JSONArray) {
|
|
if(jsonResult instanceof JSONArray) {
|
|
|
jsonObject.put("result", jsonResult);
|
|
jsonObject.put("result", jsonResult);
|
|
|
} else {
|
|
} else {
|
|
|
jsonObject = (JSONObject) jsonResult;
|
|
jsonObject = (JSONObject) jsonResult;
|
|
|
}
|
|
}
|
|
|
|
|
+ }
|
|
|
jsonObject.put("status_code", resp.getStatusLine().getStatusCode());
|
|
jsonObject.put("status_code", resp.getStatusLine().getStatusCode());
|
|
|
return jsonObject;
|
|
return jsonObject;
|
|
|
} finally {
|
|
} finally {
|
|
@@ -353,7 +373,7 @@ public class RestApiUtils {
|
|
|
* @return 返回 下载的文件路径
|
|
* @return 返回 下载的文件路径
|
|
|
*/
|
|
*/
|
|
|
public static String download(String url, File downloadDir) throws IOException {
|
|
public static String download(String url, File downloadDir) throws IOException {
|
|
|
- return download(url, new HashMap<>(), downloadDir);
|
|
|
|
|
|
|
+ return download(url, new HashMap<>(), downloadDir, new NoProcessCall());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -364,6 +384,17 @@ public class RestApiUtils {
|
|
|
* @return 返回 下载的文件路径
|
|
* @return 返回 下载的文件路径
|
|
|
*/
|
|
*/
|
|
|
public static String download(String url, Map<String, String> headers, File downloadDir) throws IOException {
|
|
public static String download(String url, Map<String, String> headers, File downloadDir) throws IOException {
|
|
|
|
|
+ return download(url, headers, downloadDir, new NoProcessCall());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 根据url下载文件,保存到filepath中
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param url
|
|
|
|
|
+ * @param downloadDir
|
|
|
|
|
+ * @return 返回 下载的文件路径
|
|
|
|
|
+ */
|
|
|
|
|
+ public static String download(String url, Map<String, String> headers, File downloadDir, ProcessCall call) throws IOException {
|
|
|
if(!downloadDir.exists()) {
|
|
if(!downloadDir.exists()) {
|
|
|
if(!downloadDir.mkdirs()) {
|
|
if(!downloadDir.mkdirs()) {
|
|
|
throw new IOException(downloadDir.getAbsolutePath() + " not exists, do can not mkdir.");
|
|
throw new IOException(downloadDir.getAbsolutePath() + " not exists, do can not mkdir.");
|
|
@@ -407,10 +438,27 @@ public class RestApiUtils {
|
|
|
HttpEntity entity = response.getEntity();
|
|
HttpEntity entity = response.getEntity();
|
|
|
File filepath = new File(downloadDir, fileName);
|
|
File filepath = new File(downloadDir, fileName);
|
|
|
|
|
|
|
|
|
|
+ final long fileSize = entity.getContentLength();
|
|
|
|
|
+ call.fileSize(fileSize);
|
|
|
|
|
+ call.process(0.0f);
|
|
|
try(InputStream is = entity.getContent(); FileOutputStream fileout = new FileOutputStream(filepath);) {
|
|
try(InputStream is = entity.getContent(); FileOutputStream fileout = new FileOutputStream(filepath);) {
|
|
|
- ByteStreams.copy(is, fileout);
|
|
|
|
|
|
|
+ byte[] buf = new byte[ 4 * 1024]; //4KB 缓冲区
|
|
|
|
|
+ long total = 0;
|
|
|
|
|
+ while (true) {
|
|
|
|
|
+ int r = is.read(buf);
|
|
|
|
|
+ if (r == -1) {
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ fileout.write(buf, 0, r);
|
|
|
|
|
+ total += r;
|
|
|
|
|
+ if(total % 2048 == 1024) {
|
|
|
|
|
+ // 每 4M 推送一次进度
|
|
|
|
|
+ call.process(fileSize > 0 ? (float) total / fileSize : 0.0f);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
fileout.flush();
|
|
fileout.flush();
|
|
|
}
|
|
}
|
|
|
|
|
+ call.process(1.0f);
|
|
|
return filepath.getAbsolutePath();
|
|
return filepath.getAbsolutePath();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -560,9 +608,37 @@ public class RestApiUtils {
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 关闭 RPC 调用
|
|
* 关闭 RPC 调用
|
|
|
|
|
+ *
|
|
|
* @throws IOException
|
|
* @throws IOException
|
|
|
*/
|
|
*/
|
|
|
public static void shutdown() throws IOException {
|
|
public static void shutdown() throws IOException {
|
|
|
httpClient.close();
|
|
httpClient.close();
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 下载进度回调
|
|
|
|
|
+ */
|
|
|
|
|
+ public static interface ProcessCall {
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 下载的文件大小
|
|
|
|
|
+ * @param size
|
|
|
|
|
+ */
|
|
|
|
|
+ default void fileSize(long size) {};
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 进度推送
|
|
|
|
|
+ * @param process
|
|
|
|
|
+ */
|
|
|
|
|
+ public void process(float process);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ static class NoProcessCall implements ProcessCall {
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void process(float process) {
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
+
|