Skip to content

Commit ccbfa98

Browse files
authored
🎨 添加 Apache HttpComponents Client 5.x 为可选的 http client
1 parent bf35797 commit ccbfa98

File tree

136 files changed

+2689
-492
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

136 files changed

+2689
-492
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@
154154
<dependency>
155155
<groupId>com.squareup.okhttp3</groupId>
156156
<artifactId>okhttp</artifactId>
157-
<version>4.5.0</version>
157+
<version>4.12.0</version>
158158
<scope>provided</scope>
159159
</dependency>
160160
<dependency>
@@ -212,7 +212,7 @@
212212
<dependency>
213213
<groupId>com.fasterxml.jackson</groupId>
214214
<artifactId>jackson-bom</artifactId>
215-
<version>2.18.1</version>
215+
<version>2.18.4</version>
216216
<type>pom</type>
217217
<scope>import</scope>
218218
</dependency>
@@ -333,7 +333,7 @@
333333
<dependency>
334334
<groupId>org.bouncycastle</groupId>
335335
<artifactId>bcpkix-jdk18on</artifactId>
336-
<version>1.78.1</version>
336+
<version>1.80</version>
337337
</dependency>
338338
</dependencies>
339339
</dependencyManagement>

solon-plugins/wx-java-channel-multi-solon-plugin/src/main/java/com/binarywang/solon/wxjava/channel/configuration/services/AbstractWxChannelConfiguration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import lombok.RequiredArgsConstructor;
99
import lombok.extern.slf4j.Slf4j;
1010
import me.chanjar.weixin.channel.api.WxChannelService;
11+
import me.chanjar.weixin.channel.api.impl.WxChannelServiceHttpComponentsImpl;
1112
import me.chanjar.weixin.channel.api.impl.WxChannelServiceHttpClientImpl;
1213
import me.chanjar.weixin.channel.api.impl.WxChannelServiceImpl;
1314
import me.chanjar.weixin.channel.config.WxChannelConfig;
@@ -84,6 +85,9 @@ public WxChannelService wxChannelService(WxChannelConfig wxChannelConfig, WxChan
8485
case HTTP_CLIENT:
8586
wxChannelService = new WxChannelServiceHttpClientImpl();
8687
break;
88+
case HTTP_COMPONENTS:
89+
wxChannelService = new WxChannelServiceHttpComponentsImpl();
90+
break;
8791
default:
8892
wxChannelService = new WxChannelServiceImpl();
8993
break;

solon-plugins/wx-java-channel-multi-solon-plugin/src/main/java/com/binarywang/solon/wxjava/channel/enums/HttpClientType.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ public enum HttpClientType {
1111
* HttpClient
1212
*/
1313
HTTP_CLIENT,
14+
/**
15+
* HttpComponents
16+
*/
17+
HTTP_COMPONENTS
1418
// WxChannelServiceOkHttpImpl 实现经测试无法正常完成业务固暂不支持OK_HTTP方式
1519
// /**
1620
// * OkHttp.

solon-plugins/wx-java-cp-multi-solon-plugin/src/main/java/com/binarywang/solon/wxjava/cp_multi/configuration/services/AbstractWxCpConfiguration.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
import lombok.RequiredArgsConstructor;
88
import lombok.extern.slf4j.Slf4j;
99
import me.chanjar.weixin.cp.api.WxCpService;
10-
import me.chanjar.weixin.cp.api.impl.WxCpServiceApacheHttpClientImpl;
11-
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
12-
import me.chanjar.weixin.cp.api.impl.WxCpServiceJoddHttpImpl;
13-
import me.chanjar.weixin.cp.api.impl.WxCpServiceOkHttpImpl;
10+
import me.chanjar.weixin.cp.api.impl.*;
1411
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
1512
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
1613
import org.apache.commons.lang3.StringUtils;
@@ -96,6 +93,9 @@ private WxCpService wxCpService(WxCpConfigStorage wxCpConfigStorage, WxCpMultiPr
9693
case HTTP_CLIENT:
9794
wxCpService = new WxCpServiceApacheHttpClientImpl();
9895
break;
96+
case HTTP_COMPONENTS:
97+
wxCpService = new WxCpServiceHttpComponentsImpl();
98+
break;
9999
default:
100100
wxCpService = new WxCpServiceImpl();
101101
break;

solon-plugins/wx-java-cp-multi-solon-plugin/src/main/java/com/binarywang/solon/wxjava/cp_multi/properties/WxCpMultiProperties.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ public enum HttpClientType {
117117
* HttpClient
118118
*/
119119
HTTP_CLIENT,
120+
/**
121+
* HttpComponents
122+
*/
123+
HTTP_COMPONENTS,
120124
/**
121125
* OkHttp
122126
*/

spring-boot-starters/wx-java-mp-multi-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/configuration/services/AbstractWxMpConfiguration.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
import lombok.RequiredArgsConstructor;
88
import lombok.extern.slf4j.Slf4j;
99
import me.chanjar.weixin.mp.api.WxMpService;
10-
import me.chanjar.weixin.mp.api.impl.WxMpServiceHttpClientImpl;
11-
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
12-
import me.chanjar.weixin.mp.api.impl.WxMpServiceJoddHttpImpl;
13-
import me.chanjar.weixin.mp.api.impl.WxMpServiceOkHttpImpl;
10+
import me.chanjar.weixin.mp.api.impl.*;
1411
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
1512
import me.chanjar.weixin.mp.config.WxMpHostConfig;
1613
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
@@ -91,6 +88,9 @@ public WxMpService wxMpService(WxMpConfigStorage configStorage, WxMpMultiPropert
9188
case HTTP_CLIENT:
9289
wxMpService = new WxMpServiceHttpClientImpl();
9390
break;
91+
case HTTP_COMPONENTS:
92+
wxMpService = new WxMpServiceHttpComponentsImpl();
93+
break;
9494
default:
9595
wxMpService = new WxMpServiceImpl();
9696
break;

spring-boot-starters/wx-java-mp-multi-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/properties/WxMpMultiProperties.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ public enum HttpClientType {
142142
* HttpClient
143143
*/
144144
HTTP_CLIENT,
145+
/**
146+
* HttpComponents
147+
*/
148+
HTTP_COMPONENTS,
145149
/**
146150
* OkHttp
147151
*/

weixin-java-channel/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<description>微信视频号/微信小店 Java SDK</description>
1515

1616
<properties>
17-
<jackson.version>2.18.1</jackson.version>
17+
<jackson.version>2.18.4</jackson.version>
1818
</properties>
1919

2020
<dependencies>
@@ -29,6 +29,11 @@
2929
<artifactId>jodd-http</artifactId>
3030
<scope>provided</scope>
3131
</dependency>
32+
<dependency>
33+
<groupId>org.apache.httpcomponents.client5</groupId>
34+
<artifactId>httpclient5</artifactId>
35+
<scope>provided</scope>
36+
</dependency>
3237

3338
<dependency>
3439
<groupId>com.fasterxml.jackson.core</groupId>

weixin-java-channel/src/main/java/me/chanjar/weixin/channel/api/impl/WxChannelBasicServiceImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,27 @@ public ChannelImageInfo uploadImg(int respType, String imgUrl) throws WxErrorExc
5656
public ChannelImageInfo uploadImg(int respType, File file, int height, int width) throws WxErrorException {
5757
String url = IMG_UPLOAD_URL + "?upload_type=0&resp_type=" + respType + "&height=" + height + "&width=" + width;
5858
RequestExecutor<String, File> executor = ChannelFileUploadRequestExecutor.create(shopService);
59-
String resJson = (String) shopService.execute(executor, url, file);
59+
String resJson = shopService.execute(executor, url, file);
6060
UploadImageResponse response = ResponseUtils.decode(resJson, UploadImageResponse.class);
6161
return response.getImgInfo();
6262
}
6363

6464
@Override
6565
public QualificationFileResponse uploadQualificationFile(File file) throws WxErrorException {
6666
RequestExecutor<String, File> executor = ChannelFileUploadRequestExecutor.create(shopService);
67-
String resJson = (String) shopService.execute(executor, UPLOAD_QUALIFICATION_FILE, file);
67+
String resJson = shopService.execute(executor, UPLOAD_QUALIFICATION_FILE, file);
6868
return ResponseUtils.decode(resJson, QualificationFileResponse.class);
6969
}
7070

7171
@Override
7272
public ChannelImageResponse getImg(String mediaId) throws WxErrorException {
7373
String appId = shopService.getConfig().getAppid();
74-
ChannelImageResponse rs = null;
74+
ChannelImageResponse rs;
7575
try {
7676
String url = GET_IMG_URL + "?media_id=" + mediaId;
7777
RequestExecutor<ChannelImageResponse, String> executor = ChannelMediaDownloadRequestExecutor.create(shopService,
7878
Files.createTempDirectory("wxjava-channel-" + appId).toFile());
79-
rs = (ChannelImageResponse) shopService.execute(executor, url, null);
79+
rs = shopService.execute(executor, url, null);
8080
} catch (IOException e) {
8181
throw new WxErrorException(WxError.builder().errorMsg(e.getMessage()).build(), e);
8282
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package me.chanjar.weixin.channel.api.impl;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import me.chanjar.weixin.channel.bean.token.StableTokenParam;
5+
import me.chanjar.weixin.channel.config.WxChannelConfig;
6+
import me.chanjar.weixin.channel.util.JsonUtils;
7+
import me.chanjar.weixin.common.util.http.HttpClientType;
8+
import me.chanjar.weixin.common.util.http.apache.ApacheBasicResponseHandler;
9+
import me.chanjar.weixin.common.util.http.hc.BasicResponseHandler;
10+
import me.chanjar.weixin.common.util.http.hc.DefaultHttpComponentsClientBuilder;
11+
import me.chanjar.weixin.common.util.http.hc.HttpComponentsClientBuilder;
12+
import org.apache.commons.lang3.StringUtils;
13+
import org.apache.hc.client5.http.classic.HttpClient;
14+
import org.apache.hc.client5.http.classic.methods.HttpGet;
15+
import org.apache.hc.client5.http.classic.methods.HttpPost;
16+
import org.apache.hc.client5.http.config.RequestConfig;
17+
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
18+
import org.apache.hc.core5.http.ContentType;
19+
import org.apache.hc.core5.http.HttpHost;
20+
import org.apache.hc.core5.http.io.entity.StringEntity;
21+
22+
import java.io.IOException;
23+
24+
import static me.chanjar.weixin.channel.constant.WxChannelApiUrlConstants.GET_ACCESS_TOKEN_URL;
25+
import static me.chanjar.weixin.channel.constant.WxChannelApiUrlConstants.GET_STABLE_ACCESS_TOKEN_URL;
26+
27+
/**
28+
* @author altusea
29+
*/
30+
@Slf4j
31+
public class WxChannelServiceHttpComponentsImpl extends BaseWxChannelServiceImpl<HttpClient, HttpHost> {
32+
33+
private CloseableHttpClient httpClient;
34+
private HttpHost httpProxy;
35+
36+
@Override
37+
public void initHttp() {
38+
WxChannelConfig config = this.getConfig();
39+
HttpComponentsClientBuilder apacheHttpClientBuilder = DefaultHttpComponentsClientBuilder.get();
40+
41+
apacheHttpClientBuilder.httpProxyHost(config.getHttpProxyHost())
42+
.httpProxyPort(config.getHttpProxyPort())
43+
.httpProxyUsername(config.getHttpProxyUsername())
44+
.httpProxyPassword(config.getHttpProxyPassword().toCharArray());
45+
46+
if (config.getHttpProxyHost() != null && config.getHttpProxyPort() > 0) {
47+
this.httpProxy = new HttpHost(config.getHttpProxyHost(), config.getHttpProxyPort());
48+
}
49+
50+
this.httpClient = apacheHttpClientBuilder.build();
51+
}
52+
53+
@Override
54+
public CloseableHttpClient getRequestHttpClient() {
55+
return httpClient;
56+
}
57+
58+
@Override
59+
public HttpHost getRequestHttpProxy() {
60+
return httpProxy;
61+
}
62+
63+
@Override
64+
public HttpClientType getRequestType() {
65+
return HttpClientType.HTTP_COMPONENTS;
66+
}
67+
68+
@Override
69+
protected String doGetAccessTokenRequest() throws IOException {
70+
WxChannelConfig config = this.getConfig();
71+
String url = StringUtils.isNotEmpty(config.getAccessTokenUrl()) ? config.getAccessTokenUrl() :
72+
StringUtils.isNotEmpty(config.getApiHostUrl()) ?
73+
GET_ACCESS_TOKEN_URL.replace("https://api.weixin.qq.com", config.getApiHostUrl()) : GET_ACCESS_TOKEN_URL;
74+
75+
url = String.format(url, config.getAppid(), config.getSecret());
76+
77+
HttpGet httpGet = new HttpGet(url);
78+
if (this.getRequestHttpProxy() != null) {
79+
RequestConfig requestConfig = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
80+
httpGet.setConfig(requestConfig);
81+
}
82+
return getRequestHttpClient().execute(httpGet, BasicResponseHandler.INSTANCE);
83+
}
84+
85+
/**
86+
* 获取稳定版接口调用凭据
87+
*
88+
* @param forceRefresh false 为普通模式, true为强制刷新模式
89+
* @return 返回json的字符串
90+
* @throws IOException the io exception
91+
*/
92+
@Override
93+
protected String doGetStableAccessTokenRequest(boolean forceRefresh) throws IOException {
94+
WxChannelConfig config = this.getConfig();
95+
String url = GET_STABLE_ACCESS_TOKEN_URL;
96+
97+
HttpPost httpPost = new HttpPost(url);
98+
if (this.getRequestHttpProxy() != null) {
99+
RequestConfig requestConfig = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
100+
httpPost.setConfig(requestConfig);
101+
}
102+
StableTokenParam requestParam = new StableTokenParam();
103+
requestParam.setAppId(config.getAppid());
104+
requestParam.setSecret(config.getSecret());
105+
requestParam.setGrantType("client_credential");
106+
requestParam.setForceRefresh(forceRefresh);
107+
String requestJson = JsonUtils.encode(requestParam);
108+
assert requestJson != null;
109+
110+
httpPost.setEntity(new StringEntity(requestJson, ContentType.APPLICATION_JSON));
111+
return getRequestHttpClient().execute(httpPost, BasicResponseHandler.INSTANCE);
112+
}
113+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package me.chanjar.weixin.channel.executor;
2+
3+
import me.chanjar.weixin.common.enums.WxType;
4+
import me.chanjar.weixin.common.error.WxErrorException;
5+
import me.chanjar.weixin.common.util.http.RequestHttp;
6+
import me.chanjar.weixin.common.util.http.ResponseHandler;
7+
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
8+
import org.apache.http.HttpEntity;
9+
import org.apache.http.HttpHost;
10+
import org.apache.http.client.config.RequestConfig;
11+
import org.apache.http.client.methods.HttpPost;
12+
import org.apache.http.entity.mime.HttpMultipartMode;
13+
import org.apache.http.entity.mime.MultipartEntityBuilder;
14+
import org.apache.http.impl.client.CloseableHttpClient;
15+
16+
import java.io.File;
17+
import java.io.IOException;
18+
19+
public class ApacheHttpChannelFileUploadRequestExecutor extends ChannelFileUploadRequestExecutor<CloseableHttpClient, HttpHost> {
20+
public ApacheHttpChannelFileUploadRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp) {
21+
super(requestHttp);
22+
}
23+
24+
@Override
25+
public String execute(String uri, File file, WxType wxType) throws WxErrorException, IOException {
26+
HttpPost httpPost = new HttpPost(uri);
27+
if (requestHttp.getRequestHttpProxy() != null) {
28+
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
29+
httpPost.setConfig(config);
30+
}
31+
if (file != null) {
32+
HttpEntity entity = MultipartEntityBuilder
33+
.create()
34+
.addBinaryBody("media", file)
35+
.setMode(HttpMultipartMode.RFC6532)
36+
.build();
37+
httpPost.setEntity(entity);
38+
}
39+
return requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
40+
}
41+
42+
@Override
43+
public void execute(String uri, File data, ResponseHandler<String> handler, WxType wxType)
44+
throws WxErrorException, IOException {
45+
handler.handle(this.execute(uri, data, wxType));
46+
}
47+
}

0 commit comments

Comments
 (0)