mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-02-14 12:06:24 +08:00
🎨 添加 Apache HttpComponents Client 5.x 为可选的 http client
Some checks failed
Publish to Maven Central / build-and-publish (push) Has been cancelled
Some checks failed
Publish to Maven Central / build-and-publish (push) Has been cancelled
This commit is contained in:
@@ -16,7 +16,7 @@ public class WxMinishopImageUploadCustomizeResult implements Serializable {
|
||||
private WxMinishopPicFileCustomizeResult imgInfo;
|
||||
|
||||
public static WxMinishopImageUploadCustomizeResult fromJson(String json) {
|
||||
JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
|
||||
JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject();
|
||||
WxMinishopImageUploadCustomizeResult result = new WxMinishopImageUploadCustomizeResult();
|
||||
result.setErrcode(jsonObject.get(WxConsts.ERR_CODE).getAsNumber().toString());
|
||||
if (result.getErrcode().equals("0")) {
|
||||
|
||||
@@ -21,7 +21,7 @@ public class WxMinishopImageUploadResult implements Serializable {
|
||||
|
||||
|
||||
public static WxMinishopImageUploadResult fromJson(String json) {
|
||||
JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
|
||||
JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject();
|
||||
WxMinishopImageUploadResult result = new WxMinishopImageUploadResult();
|
||||
result.setErrcode(jsonObject.get(WxConsts.ERR_CODE).getAsNumber().toString());
|
||||
if (result.getErrcode().equals("0")) {
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
package me.chanjar.weixin.common.executor;
|
||||
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.ProxyInfo;
|
||||
import me.chanjar.weixin.common.bean.CommonUploadParam;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.ResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
||||
import okhttp3.OkHttpClient;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -34,15 +38,19 @@ public abstract class CommonUploadRequestExecutor<H, P> implements RequestExecut
|
||||
* @param requestHttp 请求信息
|
||||
* @return 执行器
|
||||
*/
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
public static RequestExecutor<String, CommonUploadParam> create(RequestHttp requestHttp) {
|
||||
@SuppressWarnings("unchecked")
|
||||
public static RequestExecutor<String, CommonUploadParam> create(RequestHttp<?, ?> requestHttp) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
return new CommonUploadRequestExecutorApacheImpl(requestHttp);
|
||||
return new CommonUploadRequestExecutorApacheImpl(
|
||||
(RequestHttp<org.apache.http.impl.client.CloseableHttpClient, org.apache.http.HttpHost>) requestHttp);
|
||||
case JODD_HTTP:
|
||||
return new CommonUploadRequestExecutorJoddHttpImpl(requestHttp);
|
||||
return new CommonUploadRequestExecutorJoddHttpImpl((RequestHttp<HttpConnectionProvider, ProxyInfo>) requestHttp);
|
||||
case OK_HTTP:
|
||||
return new CommonUploadRequestExecutorOkHttpImpl(requestHttp);
|
||||
return new CommonUploadRequestExecutorOkHttpImpl((RequestHttp<OkHttpClient, OkHttpProxyInfo>) requestHttp);
|
||||
case HTTP_COMPONENTS:
|
||||
return new CommonUploadRequestExecutorHttpComponentsImpl(
|
||||
(RequestHttp<org.apache.hc.client5.http.impl.classic.CloseableHttpClient, org.apache.hc.core5.http.HttpHost>) requestHttp);
|
||||
default:
|
||||
throw new IllegalArgumentException("不支持的http执行器类型:" + requestHttp.getRequestType());
|
||||
}
|
||||
|
||||
@@ -28,8 +28,7 @@ import java.io.InputStream;
|
||||
* @author <a href="https://www.sacoc.cn">广州跨界</a>
|
||||
* created on 2024/01/11
|
||||
*/
|
||||
public class CommonUploadRequestExecutorApacheImpl
|
||||
extends CommonUploadRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
public class CommonUploadRequestExecutorApacheImpl extends CommonUploadRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
|
||||
public CommonUploadRequestExecutorApacheImpl(RequestHttp<CloseableHttpClient, HttpHost> requestHttp) {
|
||||
super(requestHttp);
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package me.chanjar.weixin.common.executor;
|
||||
|
||||
import lombok.Getter;
|
||||
import me.chanjar.weixin.common.bean.CommonUploadData;
|
||||
import me.chanjar.weixin.common.bean.CommonUploadParam;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.hc.Utf8ResponseHandler;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpPost;
|
||||
import org.apache.hc.client5.http.config.RequestConfig;
|
||||
import org.apache.hc.client5.http.entity.mime.HttpMultipartMode;
|
||||
import org.apache.hc.client5.http.entity.mime.InputStreamBody;
|
||||
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||
import org.apache.hc.core5.http.ContentType;
|
||||
import org.apache.hc.core5.http.HttpEntity;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* Apache HttpComponents 通用文件上传器
|
||||
*/
|
||||
public class CommonUploadRequestExecutorHttpComponentsImpl extends CommonUploadRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
|
||||
public CommonUploadRequestExecutorHttpComponentsImpl(RequestHttp<CloseableHttpClient, HttpHost> requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String execute(String uri, CommonUploadParam param, WxType wxType) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
|
||||
httpPost.setConfig(config);
|
||||
}
|
||||
if (param != null) {
|
||||
CommonUploadData data = param.getData();
|
||||
InnerStreamBody part = new InnerStreamBody(data.getInputStream(), ContentType.DEFAULT_BINARY, data.getFileName(), data.getLength());
|
||||
HttpEntity entity = MultipartEntityBuilder
|
||||
.create()
|
||||
.addPart(param.getName(), part)
|
||||
.setMode(HttpMultipartMode.EXTENDED)
|
||||
.build();
|
||||
httpPost.setEntity(entity);
|
||||
}
|
||||
String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
|
||||
if (StringUtils.isEmpty(responseContent)) {
|
||||
throw new WxErrorException(String.format("上传失败,服务器响应空 url:%s param:%s", uri, param));
|
||||
}
|
||||
WxError error = WxError.fromJson(responseContent, wxType);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return responseContent;
|
||||
}
|
||||
|
||||
/**
|
||||
* 内部流 请求体
|
||||
*/
|
||||
@Getter
|
||||
public static class InnerStreamBody extends InputStreamBody {
|
||||
|
||||
private final long contentLength;
|
||||
|
||||
public InnerStreamBody(final InputStream in, final ContentType contentType, final String filename, long contentLength) {
|
||||
super(in, contentType, filename);
|
||||
this.contentLength = contentLength;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package me.chanjar.weixin.common.requestexecuter.ocr;
|
||||
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.hc.Utf8ResponseHandler;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpPost;
|
||||
import org.apache.hc.client5.http.config.RequestConfig;
|
||||
import org.apache.hc.client5.http.entity.mime.HttpMultipartMode;
|
||||
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||
import org.apache.hc.core5.http.HttpEntity;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class OcrDiscernHttpComponentsRequestExecutor extends OcrDiscernRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
public OcrDiscernHttpComponentsRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String execute(String uri, File file, WxType wxType) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
|
||||
httpPost.setConfig(config);
|
||||
}
|
||||
if (file != null) {
|
||||
HttpEntity entity = MultipartEntityBuilder
|
||||
.create()
|
||||
.addBinaryBody("file", file)
|
||||
.setMode(HttpMultipartMode.EXTENDED)
|
||||
.build();
|
||||
httpPost.setEntity(entity);
|
||||
}
|
||||
String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
|
||||
WxError error = WxError.fromJson(responseContent, wxType);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return responseContent;
|
||||
}
|
||||
}
|
||||
@@ -33,9 +33,13 @@ public abstract class OcrDiscernRequestExecutor<H, P> implements RequestExecutor
|
||||
public static RequestExecutor<String, File> create(RequestHttp<?, ?> requestHttp) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
return new OcrDiscernApacheHttpRequestExecutor((RequestHttp<CloseableHttpClient, HttpHost>) requestHttp);
|
||||
return new OcrDiscernApacheHttpRequestExecutor(
|
||||
(RequestHttp<org.apache.http.impl.client.CloseableHttpClient, org.apache.http.HttpHost>) requestHttp);
|
||||
case HTTP_COMPONENTS:
|
||||
return new OcrDiscernHttpComponentsRequestExecutor(
|
||||
(RequestHttp<org.apache.hc.client5.http.impl.classic.CloseableHttpClient, org.apache.hc.core5.http.HttpHost>) requestHttp);
|
||||
default:
|
||||
return null;
|
||||
throw new IllegalArgumentException("不支持的http执行器类型:" + requestHttp.getRequestType());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package me.chanjar.weixin.common.util;
|
||||
|
||||
import org.apache.commons.lang3.RegExUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -17,7 +18,7 @@ public class DataUtils {
|
||||
public static <E> E handleDataWithSecret(E data) {
|
||||
E dataForLog = data;
|
||||
if(data instanceof String && StringUtils.contains((String)data, "&secret=")){
|
||||
dataForLog = (E) StringUtils.replaceAll((String)data,"&secret=\\w+&","&secret=******&");
|
||||
dataForLog = (E) RegExUtils.replaceAll((String)data,"&secret=\\w+&","&secret=******&");
|
||||
}
|
||||
return dataForLog;
|
||||
}
|
||||
|
||||
@@ -1,19 +1,18 @@
|
||||
package me.chanjar.weixin.common.util.http;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.ProxyInfo;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheMediaDownloadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.hc.HttpComponentsMediaDownloadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.jodd.JoddHttpMediaDownloadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpMediaDownloadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 下载媒体文件请求执行器.
|
||||
@@ -40,13 +39,17 @@ public abstract class BaseMediaDownloadRequestExecutor<H, P> implements RequestE
|
||||
public static RequestExecutor<File, String> create(RequestHttp<?, ?> requestHttp, File tmpDirFile) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
return new ApacheMediaDownloadRequestExecutor((RequestHttp<CloseableHttpClient, HttpHost>) requestHttp, tmpDirFile);
|
||||
return new ApacheMediaDownloadRequestExecutor(
|
||||
(RequestHttp<org.apache.http.impl.client.CloseableHttpClient, org.apache.http.HttpHost>) requestHttp, tmpDirFile);
|
||||
case JODD_HTTP:
|
||||
return new JoddHttpMediaDownloadRequestExecutor((RequestHttp<HttpConnectionProvider, ProxyInfo>) requestHttp, tmpDirFile);
|
||||
case OK_HTTP:
|
||||
return new OkHttpMediaDownloadRequestExecutor((RequestHttp<OkHttpClient, OkHttpProxyInfo>) requestHttp, tmpDirFile);
|
||||
case HTTP_COMPONENTS:
|
||||
return new HttpComponentsMediaDownloadRequestExecutor(
|
||||
(RequestHttp<org.apache.hc.client5.http.impl.classic.CloseableHttpClient, org.apache.hc.core5.http.HttpHost>) requestHttp, tmpDirFile);
|
||||
default:
|
||||
return null;
|
||||
throw new IllegalArgumentException("不支持的http执行器类型:" + requestHttp.getRequestType());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ public enum HttpClientType {
|
||||
*/
|
||||
JODD_HTTP,
|
||||
/**
|
||||
* apache httpclient.
|
||||
* apache httpclient 4.x.
|
||||
*/
|
||||
APACHE_HTTP,
|
||||
/**
|
||||
@@ -17,7 +17,7 @@ public enum HttpClientType {
|
||||
*/
|
||||
OK_HTTP,
|
||||
/**
|
||||
* apache httpclient5.
|
||||
* apache httpclient 5.x.
|
||||
*/
|
||||
APACHE_HTTP_5
|
||||
HTTP_COMPONENTS
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package me.chanjar.weixin.common.util.http;
|
||||
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheHttpResponseProxy;
|
||||
import me.chanjar.weixin.common.util.http.hc5.ApacheHttpClient5ResponseProxy;
|
||||
import me.chanjar.weixin.common.util.http.hc.HttpComponentsResponseProxy;
|
||||
import me.chanjar.weixin.common.util.http.jodd.JoddHttpResponseProxy;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpResponseProxy;
|
||||
|
||||
@@ -26,8 +26,8 @@ public interface HttpResponseProxy {
|
||||
return new ApacheHttpResponseProxy(response);
|
||||
}
|
||||
|
||||
static ApacheHttpClient5ResponseProxy from(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse response) {
|
||||
return new ApacheHttpClient5ResponseProxy(response);
|
||||
static HttpComponentsResponseProxy from(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse response) {
|
||||
return new HttpComponentsResponseProxy(response);
|
||||
}
|
||||
|
||||
static JoddHttpResponseProxy from(jodd.http.HttpResponse response) {
|
||||
@@ -40,7 +40,7 @@ public interface HttpResponseProxy {
|
||||
|
||||
String getFileName() throws WxErrorException;
|
||||
|
||||
default String extractFileNameFromContentString(String content) throws WxErrorException {
|
||||
static String extractFileNameFromContentString(String content) throws WxErrorException {
|
||||
if (content == null || content.isEmpty()) {
|
||||
throw new WxErrorException("无法获取到文件名,content为空");
|
||||
}
|
||||
|
||||
@@ -6,12 +6,11 @@ import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheMediaInputStreamUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.hc.HttpComponentsMediaInputStreamUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.jodd.JoddHttpMediaInputStreamUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpMediaInputStreamUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -33,16 +32,21 @@ public abstract class MediaInputStreamUploadRequestExecutor<H, P> implements Req
|
||||
handler.handle(this.execute(uri, data, wxType));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static RequestExecutor<WxMediaUploadResult, InputStreamData> create(RequestHttp<?, ?> requestHttp) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
return new ApacheMediaInputStreamUploadRequestExecutor((RequestHttp<CloseableHttpClient, HttpHost>) requestHttp);
|
||||
return new ApacheMediaInputStreamUploadRequestExecutor(
|
||||
(RequestHttp<org.apache.http.impl.client.CloseableHttpClient, org.apache.http.HttpHost>) requestHttp);
|
||||
case JODD_HTTP:
|
||||
return new JoddHttpMediaInputStreamUploadRequestExecutor((RequestHttp<HttpConnectionProvider, ProxyInfo>) requestHttp);
|
||||
case OK_HTTP:
|
||||
return new OkHttpMediaInputStreamUploadRequestExecutor((RequestHttp<OkHttpClient, OkHttpProxyInfo>) requestHttp);
|
||||
case HTTP_COMPONENTS:
|
||||
return new HttpComponentsMediaInputStreamUploadRequestExecutor(
|
||||
(RequestHttp<org.apache.hc.client5.http.impl.classic.CloseableHttpClient, org.apache.hc.core5.http.HttpHost>) requestHttp);
|
||||
default:
|
||||
return null;
|
||||
throw new IllegalArgumentException("不支持的http执行器类型:" + requestHttp.getRequestType());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,12 +8,11 @@ import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.service.WxService;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheMediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.hc.HttpComponentsMediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.jodd.JoddHttpMediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpMediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -40,16 +39,21 @@ public abstract class MediaUploadRequestExecutor<H, P> implements RequestExecuto
|
||||
handler.handle(this.execute(uri, data, wxType));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static RequestExecutor<WxMediaUploadResult, File> create(RequestHttp<?, ?> requestHttp) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
return new ApacheMediaUploadRequestExecutor((RequestHttp<CloseableHttpClient, HttpHost>) requestHttp);
|
||||
return new ApacheMediaUploadRequestExecutor(
|
||||
(RequestHttp<org.apache.http.impl.client.CloseableHttpClient, org.apache.http.HttpHost>) requestHttp);
|
||||
case JODD_HTTP:
|
||||
return new JoddHttpMediaUploadRequestExecutor((RequestHttp<HttpConnectionProvider, ProxyInfo>) requestHttp);
|
||||
case OK_HTTP:
|
||||
return new OkHttpMediaUploadRequestExecutor((RequestHttp<OkHttpClient, OkHttpProxyInfo>) requestHttp);
|
||||
case HTTP_COMPONENTS:
|
||||
return new HttpComponentsMediaUploadRequestExecutor(
|
||||
(RequestHttp<org.apache.hc.client5.http.impl.classic.CloseableHttpClient, org.apache.hc.core5.http.HttpHost>) requestHttp);
|
||||
default:
|
||||
return null;
|
||||
throw new IllegalArgumentException("不支持的http执行器类型:" + requestHttp.getRequestType());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,12 +6,11 @@ import me.chanjar.weixin.common.bean.result.WxMinishopImageUploadCustomizeResult
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheMinishopMediaUploadRequestCustomizeExecutor;
|
||||
import me.chanjar.weixin.common.util.http.hc.HttpComponentsMinishopMediaUploadRequestCustomizeExecutor;
|
||||
import me.chanjar.weixin.common.util.http.jodd.JoddHttpMinishopMediaUploadRequestCustomizeExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpMinishopMediaUploadRequestCustomizeExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -27,8 +26,7 @@ public abstract class MinishopUploadRequestCustomizeExecutor<H, P> implements Re
|
||||
this.respType = respType;
|
||||
if (imgUrl == null || imgUrl.isEmpty()) {
|
||||
this.uploadType = "0";
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
this.uploadType = "1";
|
||||
this.imgUrl = imgUrl;
|
||||
}
|
||||
@@ -43,13 +41,17 @@ public abstract class MinishopUploadRequestCustomizeExecutor<H, P> implements Re
|
||||
public static RequestExecutor<WxMinishopImageUploadCustomizeResult, File> create(RequestHttp<?, ?> requestHttp, String respType, String imgUrl) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
return new ApacheMinishopMediaUploadRequestCustomizeExecutor((RequestHttp<CloseableHttpClient, HttpHost>) requestHttp, respType, imgUrl);
|
||||
return new ApacheMinishopMediaUploadRequestCustomizeExecutor(
|
||||
(RequestHttp<org.apache.http.impl.client.CloseableHttpClient, org.apache.http.HttpHost>) requestHttp, respType, imgUrl);
|
||||
case JODD_HTTP:
|
||||
return new JoddHttpMinishopMediaUploadRequestCustomizeExecutor((RequestHttp<HttpConnectionProvider, ProxyInfo>) requestHttp, respType, imgUrl);
|
||||
case OK_HTTP:
|
||||
return new OkHttpMinishopMediaUploadRequestCustomizeExecutor((RequestHttp<OkHttpClient, OkHttpProxyInfo>) requestHttp, respType, imgUrl);
|
||||
case HTTP_COMPONENTS:
|
||||
return new HttpComponentsMinishopMediaUploadRequestCustomizeExecutor(
|
||||
(RequestHttp<org.apache.hc.client5.http.impl.classic.CloseableHttpClient, org.apache.hc.core5.http.HttpHost>) requestHttp, respType, imgUrl);
|
||||
default:
|
||||
return null;
|
||||
throw new IllegalArgumentException("不支持的http执行器类型:" + requestHttp.getRequestType());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,12 +6,11 @@ import me.chanjar.weixin.common.bean.result.WxMinishopImageUploadResult;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheMinishopMediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.hc.HttpComponentsMinishopMediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.jodd.JoddHttpMinishopMediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpMinishopMediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -32,13 +31,17 @@ public abstract class MinishopUploadRequestExecutor<H, P> implements RequestExec
|
||||
public static RequestExecutor<WxMinishopImageUploadResult, File> create(RequestHttp<?, ?> requestHttp) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
return new ApacheMinishopMediaUploadRequestExecutor((RequestHttp<CloseableHttpClient, HttpHost>) requestHttp);
|
||||
return new ApacheMinishopMediaUploadRequestExecutor(
|
||||
(RequestHttp<org.apache.http.impl.client.CloseableHttpClient, org.apache.http.HttpHost>) requestHttp);
|
||||
case JODD_HTTP:
|
||||
return new JoddHttpMinishopMediaUploadRequestExecutor((RequestHttp<HttpConnectionProvider, ProxyInfo>) requestHttp);
|
||||
case OK_HTTP:
|
||||
return new OkHttpMinishopMediaUploadRequestExecutor((RequestHttp<OkHttpClient, OkHttpProxyInfo>) requestHttp);
|
||||
case HTTP_COMPONENTS:
|
||||
return new HttpComponentsMinishopMediaUploadRequestExecutor(
|
||||
(RequestHttp<org.apache.hc.client5.http.impl.classic.CloseableHttpClient, org.apache.hc.core5.http.HttpHost>) requestHttp);
|
||||
default:
|
||||
return null;
|
||||
throw new IllegalArgumentException("不支持的http执行器类型:" + requestHttp.getRequestType());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,12 +6,11 @@ import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheSimpleGetRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.hc.HttpComponentsSimpleGetRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.jodd.JoddHttpSimpleGetRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpSimpleGetRequestExecutor;
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -37,13 +36,17 @@ public abstract class SimpleGetRequestExecutor<H, P> implements RequestExecutor<
|
||||
public static RequestExecutor<String, String> create(RequestHttp<?, ?> requestHttp) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
return new ApacheSimpleGetRequestExecutor((RequestHttp< CloseableHttpClient, HttpHost>) requestHttp);
|
||||
return new ApacheSimpleGetRequestExecutor(
|
||||
(RequestHttp<org.apache.http.impl.client.CloseableHttpClient, org.apache.http.HttpHost>) requestHttp);
|
||||
case JODD_HTTP:
|
||||
return new JoddHttpSimpleGetRequestExecutor((RequestHttp<HttpConnectionProvider, ProxyInfo>) requestHttp);
|
||||
case OK_HTTP:
|
||||
return new OkHttpSimpleGetRequestExecutor((RequestHttp<OkHttpClient, OkHttpProxyInfo>) requestHttp);
|
||||
case HTTP_COMPONENTS:
|
||||
return new HttpComponentsSimpleGetRequestExecutor(
|
||||
(RequestHttp<org.apache.hc.client5.http.impl.classic.CloseableHttpClient, org.apache.hc.core5.http.HttpHost>) requestHttp);
|
||||
default:
|
||||
throw new IllegalArgumentException("非法请求参数");
|
||||
throw new IllegalArgumentException("不支持的http执行器类型:" + requestHttp.getRequestType());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,12 +6,11 @@ import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheSimplePostRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.hc.HttpComponentsSimplePostRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.jodd.JoddHttpSimplePostRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpSimplePostRequestExecutor;
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -34,16 +33,21 @@ public abstract class SimplePostRequestExecutor<H, P> implements RequestExecutor
|
||||
handler.handle(this.execute(uri, data, wxType));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static RequestExecutor<String, String> create(RequestHttp<?, ?> requestHttp) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
return new ApacheSimplePostRequestExecutor((RequestHttp<CloseableHttpClient, HttpHost>) requestHttp);
|
||||
return new ApacheSimplePostRequestExecutor(
|
||||
(RequestHttp<org.apache.http.impl.client.CloseableHttpClient, org.apache.http.HttpHost>) requestHttp);
|
||||
case JODD_HTTP:
|
||||
return new JoddHttpSimplePostRequestExecutor((RequestHttp<HttpConnectionProvider, ProxyInfo>) requestHttp);
|
||||
case OK_HTTP:
|
||||
return new OkHttpSimplePostRequestExecutor((RequestHttp<OkHttpClient, OkHttpProxyInfo>) requestHttp);
|
||||
case HTTP_COMPONENTS:
|
||||
return new HttpComponentsSimplePostRequestExecutor(
|
||||
(RequestHttp<org.apache.hc.client5.http.impl.classic.CloseableHttpClient, org.apache.hc.core5.http.HttpHost>) requestHttp);
|
||||
default:
|
||||
throw new IllegalArgumentException("非法请求参数");
|
||||
throw new IllegalArgumentException("不支持的http执行器类型:" + requestHttp.getRequestType());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,6 @@ public class ApacheHttpResponseProxy implements HttpResponseProxy {
|
||||
throw new WxErrorException("无法获取到文件名,Content-disposition为空");
|
||||
}
|
||||
|
||||
return extractFileNameFromContentString(contentDispositionHeader[0].getValue());
|
||||
return HttpResponseProxy.extractFileNameFromContentString(contentDispositionHeader[0].getValue());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,14 +4,15 @@ import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
|
||||
import org.apache.http.Consts;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* .
|
||||
@@ -33,8 +34,7 @@ public class ApacheSimplePostRequestExecutor extends SimplePostRequestExecutor<C
|
||||
}
|
||||
|
||||
if (postEntity != null) {
|
||||
StringEntity entity = new StringEntity(postEntity, Consts.UTF_8);
|
||||
entity.setContentType("application/json; charset=utf-8");
|
||||
StringEntity entity = new StringEntity(postEntity, ContentType.APPLICATION_JSON.withCharset(StandardCharsets.UTF_8));
|
||||
httpPost.setEntity(entity);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package me.chanjar.weixin.common.util.http.hc;
|
||||
|
||||
import org.apache.hc.client5.http.impl.classic.BasicHttpClientResponseHandler;
|
||||
|
||||
/**
|
||||
* ApacheBasicResponseHandler
|
||||
*
|
||||
* @author altusea
|
||||
*/
|
||||
public class BasicResponseHandler extends BasicHttpClientResponseHandler {
|
||||
|
||||
public static final BasicHttpClientResponseHandler INSTANCE = new BasicHttpClientResponseHandler();
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package me.chanjar.weixin.common.util.http.hc5;
|
||||
package me.chanjar.weixin.common.util.http.hc;
|
||||
|
||||
import org.apache.hc.client5.http.impl.classic.AbstractHttpClientResponseHandler;
|
||||
import org.apache.hc.core5.http.HttpEntity;
|
||||
@@ -1,7 +1,9 @@
|
||||
package me.chanjar.weixin.common.util.http.hc5;
|
||||
package me.chanjar.weixin.common.util.http.hc;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
|
||||
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.hc.client5.http.ConnectionKeepAliveStrategy;
|
||||
@@ -43,7 +45,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
@Slf4j
|
||||
@Data
|
||||
@NotThreadSafe
|
||||
public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder {
|
||||
public class DefaultHttpComponentsClientBuilder implements HttpComponentsClientBuilder {
|
||||
|
||||
private final AtomicBoolean prepared = new AtomicBoolean(false);
|
||||
|
||||
@@ -121,45 +123,45 @@ public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder {
|
||||
*/
|
||||
private CloseableHttpClient closeableHttpClient;
|
||||
|
||||
private DefaultApacheHttpClientBuilder() {
|
||||
private DefaultHttpComponentsClientBuilder() {
|
||||
}
|
||||
|
||||
public static DefaultApacheHttpClientBuilder get() {
|
||||
public static DefaultHttpComponentsClientBuilder get() {
|
||||
return SingletonHolder.INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApacheHttpClientBuilder httpProxyHost(String httpProxyHost) {
|
||||
public HttpComponentsClientBuilder httpProxyHost(String httpProxyHost) {
|
||||
this.httpProxyHost = httpProxyHost;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApacheHttpClientBuilder httpProxyPort(int httpProxyPort) {
|
||||
public HttpComponentsClientBuilder httpProxyPort(int httpProxyPort) {
|
||||
this.httpProxyPort = httpProxyPort;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApacheHttpClientBuilder httpProxyUsername(String httpProxyUsername) {
|
||||
public HttpComponentsClientBuilder httpProxyUsername(String httpProxyUsername) {
|
||||
this.httpProxyUsername = httpProxyUsername;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApacheHttpClientBuilder httpProxyPassword(char[] httpProxyPassword) {
|
||||
public HttpComponentsClientBuilder httpProxyPassword(char[] httpProxyPassword) {
|
||||
this.httpProxyPassword = httpProxyPassword;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApacheHttpClientBuilder httpRequestRetryStrategy(HttpRequestRetryStrategy httpRequestRetryStrategy) {
|
||||
public HttpComponentsClientBuilder httpRequestRetryStrategy(HttpRequestRetryStrategy httpRequestRetryStrategy) {
|
||||
this.httpRequestRetryStrategy = httpRequestRetryStrategy;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApacheHttpClientBuilder keepAliveStrategy(ConnectionKeepAliveStrategy keepAliveStrategy) {
|
||||
public HttpComponentsClientBuilder keepAliveStrategy(ConnectionKeepAliveStrategy keepAliveStrategy) {
|
||||
this.connectionKeepAliveStrategy = keepAliveStrategy;
|
||||
return this;
|
||||
}
|
||||
@@ -242,6 +244,6 @@ public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder {
|
||||
* DefaultApacheHttpClientBuilder 改为单例模式,并持有唯一的CloseableHttpClient(仅首次调用创建)
|
||||
*/
|
||||
private static class SingletonHolder {
|
||||
private static final DefaultApacheHttpClientBuilder INSTANCE = new DefaultApacheHttpClientBuilder();
|
||||
private static final DefaultHttpComponentsClientBuilder INSTANCE = new DefaultHttpComponentsClientBuilder();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package me.chanjar.weixin.common.util.http.hc;
|
||||
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
|
||||
import org.apache.hc.client5.http.ConnectionKeepAliveStrategy;
|
||||
import org.apache.hc.client5.http.HttpRequestRetryStrategy;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||
|
||||
/**
|
||||
* httpclient build interface.
|
||||
*
|
||||
* @author altusea
|
||||
*/
|
||||
public interface HttpComponentsClientBuilder {
|
||||
|
||||
/**
|
||||
* 构建httpclient实例.
|
||||
*
|
||||
* @return new instance of CloseableHttpClient
|
||||
*/
|
||||
CloseableHttpClient build();
|
||||
|
||||
/**
|
||||
* 代理服务器地址.
|
||||
*/
|
||||
HttpComponentsClientBuilder httpProxyHost(String httpProxyHost);
|
||||
|
||||
/**
|
||||
* 代理服务器端口.
|
||||
*/
|
||||
HttpComponentsClientBuilder httpProxyPort(int httpProxyPort);
|
||||
|
||||
/**
|
||||
* 代理服务器用户名.
|
||||
*/
|
||||
HttpComponentsClientBuilder httpProxyUsername(String httpProxyUsername);
|
||||
|
||||
/**
|
||||
* 代理服务器密码.
|
||||
*/
|
||||
HttpComponentsClientBuilder httpProxyPassword(char[] httpProxyPassword);
|
||||
|
||||
/**
|
||||
* 重试策略.
|
||||
*/
|
||||
HttpComponentsClientBuilder httpRequestRetryStrategy(HttpRequestRetryStrategy httpRequestRetryStrategy);
|
||||
|
||||
/**
|
||||
* 超时时间.
|
||||
*/
|
||||
HttpComponentsClientBuilder keepAliveStrategy(ConnectionKeepAliveStrategy keepAliveStrategy);
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package me.chanjar.weixin.common.util.http.hc5;
|
||||
package me.chanjar.weixin.common.util.http.hc;
|
||||
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
@@ -28,9 +28,9 @@ import java.io.InputStream;
|
||||
*
|
||||
* @author altusea
|
||||
*/
|
||||
public class ApacheMediaDownloadRequestExecutor extends BaseMediaDownloadRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
public class HttpComponentsMediaDownloadRequestExecutor extends BaseMediaDownloadRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
|
||||
public ApacheMediaDownloadRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp, File tmpDirFile) {
|
||||
public HttpComponentsMediaDownloadRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp, File tmpDirFile) {
|
||||
super(requestHttp, tmpDirFile);
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ public class ApacheMediaDownloadRequestExecutor extends BaseMediaDownloadRequest
|
||||
}
|
||||
|
||||
return FileUtils.createTmpFile(inputStream, baseName, FilenameUtils.getExtension(fileName), super.tmpDirFile);
|
||||
} catch (final HttpException httpException) {
|
||||
} catch (HttpException httpException) {
|
||||
throw new ClientProtocolException(httpException.getMessage(), httpException);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package me.chanjar.weixin.common.util.http.hc5;
|
||||
package me.chanjar.weixin.common.util.http.hc;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
@@ -23,9 +23,9 @@ import java.io.IOException;
|
||||
*
|
||||
* @author altusea
|
||||
*/
|
||||
public class ApacheMediaInputStreamUploadRequestExecutor extends MediaInputStreamUploadRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
public class HttpComponentsMediaInputStreamUploadRequestExecutor extends MediaInputStreamUploadRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
|
||||
public ApacheMediaInputStreamUploadRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp) {
|
||||
public HttpComponentsMediaInputStreamUploadRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package me.chanjar.weixin.common.util.http.hc5;
|
||||
package me.chanjar.weixin.common.util.http.hc;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
@@ -22,9 +22,9 @@ import java.io.IOException;
|
||||
*
|
||||
* @author altusea
|
||||
*/
|
||||
public class ApacheMediaUploadRequestExecutor extends MediaUploadRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
public class HttpComponentsMediaUploadRequestExecutor extends MediaUploadRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
|
||||
public ApacheMediaUploadRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp) {
|
||||
public HttpComponentsMediaUploadRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package me.chanjar.weixin.common.util.http.hc5;
|
||||
package me.chanjar.weixin.common.util.http.hc;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.bean.result.WxMinishopImageUploadCustomizeResult;
|
||||
@@ -24,9 +24,9 @@ import java.io.IOException;
|
||||
* @author altusea
|
||||
*/
|
||||
@Slf4j
|
||||
public class ApacheMinishopMediaUploadRequestCustomizeExecutor extends MinishopUploadRequestCustomizeExecutor<CloseableHttpClient, HttpHost> {
|
||||
public class HttpComponentsMinishopMediaUploadRequestCustomizeExecutor extends MinishopUploadRequestCustomizeExecutor<CloseableHttpClient, HttpHost> {
|
||||
|
||||
public ApacheMinishopMediaUploadRequestCustomizeExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp, String respType, String imgUrl) {
|
||||
public HttpComponentsMinishopMediaUploadRequestCustomizeExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp, String respType, String imgUrl) {
|
||||
super(requestHttp, respType, imgUrl);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package me.chanjar.weixin.common.util.http.hc5;
|
||||
package me.chanjar.weixin.common.util.http.hc;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.bean.result.WxMinishopImageUploadResult;
|
||||
@@ -24,9 +24,9 @@ import java.io.IOException;
|
||||
* @author altusea
|
||||
*/
|
||||
@Slf4j
|
||||
public class ApacheMinishopMediaUploadRequestExecutor extends MinishopUploadRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
public class HttpComponentsMinishopMediaUploadRequestExecutor extends MinishopUploadRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
|
||||
public ApacheMinishopMediaUploadRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp) {
|
||||
public HttpComponentsMinishopMediaUploadRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
package me.chanjar.weixin.common.util.http.hc5;
|
||||
package me.chanjar.weixin.common.util.http.hc;
|
||||
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.HttpResponseProxy;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
|
||||
import org.apache.hc.core5.http.Header;
|
||||
|
||||
public class ApacheHttpClient5ResponseProxy implements HttpResponseProxy {
|
||||
public class HttpComponentsResponseProxy implements HttpResponseProxy {
|
||||
|
||||
private final CloseableHttpResponse response;
|
||||
|
||||
public ApacheHttpClient5ResponseProxy(CloseableHttpResponse closeableHttpResponse) {
|
||||
public HttpComponentsResponseProxy(CloseableHttpResponse closeableHttpResponse) {
|
||||
this.response = closeableHttpResponse;
|
||||
}
|
||||
|
||||
@@ -20,6 +20,6 @@ public class ApacheHttpClient5ResponseProxy implements HttpResponseProxy {
|
||||
throw new WxErrorException("无法获取到文件名,Content-disposition为空");
|
||||
}
|
||||
|
||||
return extractFileNameFromContentString(contentDispositionHeader[0].getValue());
|
||||
return HttpResponseProxy.extractFileNameFromContentString(contentDispositionHeader[0].getValue());
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package me.chanjar.weixin.common.util.http.hc5;
|
||||
package me.chanjar.weixin.common.util.http.hc;
|
||||
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
@@ -16,9 +16,9 @@ import java.io.IOException;
|
||||
*
|
||||
* @author altusea
|
||||
*/
|
||||
public class ApacheSimpleGetRequestExecutor extends SimpleGetRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
public class HttpComponentsSimpleGetRequestExecutor extends SimpleGetRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
|
||||
public ApacheSimpleGetRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp) {
|
||||
public HttpComponentsSimpleGetRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package me.chanjar.weixin.common.util.http.hc5;
|
||||
package me.chanjar.weixin.common.util.http.hc;
|
||||
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
@@ -19,9 +19,9 @@ import java.nio.charset.StandardCharsets;
|
||||
*
|
||||
* @author altusea
|
||||
*/
|
||||
public class ApacheSimplePostRequestExecutor extends SimplePostRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
public class HttpComponentsSimplePostRequestExecutor extends SimplePostRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
|
||||
public ApacheSimplePostRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp) {
|
||||
public HttpComponentsSimplePostRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package me.chanjar.weixin.common.util.http.hc5;
|
||||
package me.chanjar.weixin.common.util.http.hc;
|
||||
|
||||
import org.apache.hc.client5.http.impl.classic.AbstractHttpClientResponseHandler;
|
||||
import org.apache.hc.core5.http.HttpEntity;
|
||||
@@ -1,4 +1,4 @@
|
||||
package me.chanjar.weixin.common.util.http.hc5;
|
||||
package me.chanjar.weixin.common.util.http.hc;
|
||||
|
||||
import org.apache.hc.client5.http.HttpRequestRetryStrategy;
|
||||
import org.apache.hc.core5.http.HttpRequest;
|
||||
@@ -1,4 +1,4 @@
|
||||
package me.chanjar.weixin.common.util.http.hc5;
|
||||
package me.chanjar.weixin.common.util.http.hc;
|
||||
|
||||
import org.apache.hc.client5.http.ClientProtocolException;
|
||||
import org.apache.hc.client5.http.impl.classic.AbstractHttpClientResponseHandler;
|
||||
@@ -1,14 +0,0 @@
|
||||
package me.chanjar.weixin.common.util.http.hc5;
|
||||
|
||||
import org.apache.hc.client5.http.impl.classic.BasicHttpClientResponseHandler;
|
||||
|
||||
/**
|
||||
* ApacheBasicResponseHandler
|
||||
*
|
||||
* @author altusea
|
||||
*/
|
||||
public class ApacheBasicResponseHandler extends BasicHttpClientResponseHandler {
|
||||
|
||||
public static final ApacheBasicResponseHandler INSTANCE = new ApacheBasicResponseHandler();
|
||||
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package me.chanjar.weixin.common.util.http.hc5;
|
||||
|
||||
import org.apache.hc.client5.http.ConnectionKeepAliveStrategy;
|
||||
import org.apache.hc.client5.http.HttpRequestRetryStrategy;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||
|
||||
/**
|
||||
* httpclient build interface.
|
||||
*
|
||||
* @author altusea
|
||||
*/
|
||||
public interface ApacheHttpClientBuilder {
|
||||
|
||||
/**
|
||||
* 构建httpclient实例.
|
||||
*
|
||||
* @return new instance of CloseableHttpClient
|
||||
*/
|
||||
CloseableHttpClient build();
|
||||
|
||||
/**
|
||||
* 代理服务器地址.
|
||||
*/
|
||||
ApacheHttpClientBuilder httpProxyHost(String httpProxyHost);
|
||||
|
||||
/**
|
||||
* 代理服务器端口.
|
||||
*/
|
||||
ApacheHttpClientBuilder httpProxyPort(int httpProxyPort);
|
||||
|
||||
/**
|
||||
* 代理服务器用户名.
|
||||
*/
|
||||
ApacheHttpClientBuilder httpProxyUsername(String httpProxyUsername);
|
||||
|
||||
/**
|
||||
* 代理服务器密码.
|
||||
*/
|
||||
ApacheHttpClientBuilder httpProxyPassword(char[] httpProxyPassword);
|
||||
|
||||
/**
|
||||
* 重试策略.
|
||||
*/
|
||||
ApacheHttpClientBuilder httpRequestRetryStrategy(HttpRequestRetryStrategy httpRequestRetryStrategy);
|
||||
|
||||
/**
|
||||
* 超时时间.
|
||||
*/
|
||||
ApacheHttpClientBuilder keepAliveStrategy(ConnectionKeepAliveStrategy keepAliveStrategy);
|
||||
}
|
||||
@@ -15,6 +15,6 @@ public class JoddHttpResponseProxy implements HttpResponseProxy {
|
||||
@Override
|
||||
public String getFileName() throws WxErrorException {
|
||||
String content = response.header("Content-disposition");
|
||||
return extractFileNameFromContentString(content);
|
||||
return HttpResponseProxy.extractFileNameFromContentString(content);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,6 @@ public class OkHttpResponseProxy implements HttpResponseProxy {
|
||||
@Override
|
||||
public String getFileName() throws WxErrorException {
|
||||
String content = this.response.header("Content-disposition");
|
||||
return extractFileNameFromContentString(content);
|
||||
return HttpResponseProxy.extractFileNameFromContentString(content);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,17 +10,16 @@ import java.io.Reader;
|
||||
* @author niefy
|
||||
*/
|
||||
public class GsonParser {
|
||||
private static final JsonParser JSON_PARSER = new JsonParser();
|
||||
|
||||
public static JsonObject parse(String json) {
|
||||
return JSON_PARSER.parse(json).getAsJsonObject();
|
||||
return JsonParser.parseString(json).getAsJsonObject();
|
||||
}
|
||||
|
||||
public static JsonObject parse(Reader json) {
|
||||
return JSON_PARSER.parse(json).getAsJsonObject();
|
||||
return JsonParser.parseReader(json).getAsJsonObject();
|
||||
}
|
||||
|
||||
public static JsonObject parse(JsonReader json) {
|
||||
return JSON_PARSER.parse(json).getAsJsonObject();
|
||||
return JsonParser.parseReader(json).getAsJsonObject();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ public class StringManager {
|
||||
*
|
||||
* @param packageName The package name
|
||||
*/
|
||||
public static final synchronized StringManager getManager(
|
||||
public static synchronized StringManager getManager(
|
||||
String packageName) {
|
||||
return getManager(packageName, Locale.getDefault());
|
||||
}
|
||||
@@ -115,7 +115,7 @@ public class StringManager {
|
||||
* @param packageName The package name
|
||||
* @param locale The Locale
|
||||
*/
|
||||
public static final synchronized StringManager getManager(
|
||||
public static synchronized StringManager getManager(
|
||||
String packageName, Locale locale) {
|
||||
|
||||
Map<Locale, StringManager> map = MANAGERS.get(packageName);
|
||||
|
||||
@@ -8,12 +8,10 @@ import static org.testng.Assert.*;
|
||||
|
||||
public class HttpResponseProxyTest {
|
||||
|
||||
public final ApacheHttpResponseProxy httpResponseProxy = new ApacheHttpResponseProxy(null);
|
||||
|
||||
@Test
|
||||
public void testExtractFileNameFromContentString() throws WxErrorException {
|
||||
String content = "attachment; filename*=utf-8''%E6%B5%8B%E8%AF%95.xlsx; filename=\"æµ<EFBFBD>è¯<EFBFBD>.xlsx\"";
|
||||
String filename = httpResponseProxy.extractFileNameFromContentString(content);
|
||||
String filename = HttpResponseProxy.extractFileNameFromContentString(content);
|
||||
assertNotNull(filename);
|
||||
assertEquals(filename, "测试.xlsx");
|
||||
}
|
||||
@@ -22,7 +20,7 @@ public class HttpResponseProxyTest {
|
||||
public void testExtractFileNameFromContentString_another() throws WxErrorException {
|
||||
String content = "attachment; filename*=utf-8''%E8%90%A5%E4%B8%9A%E6%89%A7%E7%85%A7.jpg; filename=\"è<EFBFBD>¥ä¸<EFBFBD>æ<EFBFBD>§ç<EFBFBD>§.jpg\"";
|
||||
// String content = "attachment; filename=\"è<>¥ä¸<C3A4>æ<EFBFBD>§ç<C2A7>§.jpg\"";
|
||||
String filename = httpResponseProxy.extractFileNameFromContentString(content);
|
||||
String filename = HttpResponseProxy.extractFileNameFromContentString(content);
|
||||
assertNotNull(filename);
|
||||
assertEquals(filename, "营业执照.jpg");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user