Skip to content

Commit 885db18

Browse files
crskypbinarywang
authored andcommitted
使用装饰模式,支持apache-http和jodd-http (binarywang#194)
1 parent a8d443e commit 885db18

File tree

54 files changed

+2084
-171
lines changed

Some content is hidden

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

54 files changed

+2084
-171
lines changed

pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,17 @@
110110
</properties>
111111

112112
<dependencies>
113+
<dependency>
114+
<groupId>org.jodd</groupId>
115+
<artifactId>jodd-http</artifactId>
116+
<version>3.7</version>
117+
<!-- 由于较新的3.8版本需要jdk8,故而此处采用较低版本 -->
118+
</dependency>
119+
<dependency>
120+
<groupId>com.squareup.okhttp3</groupId>
121+
<artifactId>okhttp</artifactId>
122+
<version>3.7.0</version>
123+
</dependency>
113124
<dependency>
114125
<groupId>org.slf4j</groupId>
115126
<artifactId>slf4j-api</artifactId>

settings.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ rootProject.name = 'weixin-java-parent'
22
include ':weixin-java-common'
33
include ':weixin-java-cp'
44
include ':weixin-java-mp'
5+
include ':weixin-java-pay'
56

67
project(':weixin-java-common').projectDir = "$rootDir/weixin-java-common" as File
78
project(':weixin-java-cp').projectDir = "$rootDir/weixin-java-cp" as File
8-
project(':weixin-java-mp').projectDir = "$rootDir/weixin-java-mp" as File
9+
project(':weixin-java-mp').projectDir = "$rootDir/weixin-java-mp" as File
10+
project(':weixin-java-pay').projectDir = "$rootDir/weixin-java-pay" as File

weixin-java-common/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
<artifactId>jetty-servlet</artifactId>
4040
<scope>test</scope>
4141
</dependency>
42+
<dependency>
43+
<groupId>org.jodd</groupId>
44+
<artifactId>jodd-http</artifactId>
45+
<version>3.7</version>
46+
</dependency>
4247
</dependencies>
4348

4449
<build>

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaDownloadRequestExecutor.java

Lines changed: 104 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package me.chanjar.weixin.common.util.http;
22

3+
import jodd.http.HttpConnectionProvider;
4+
import jodd.http.HttpRequest;
5+
import jodd.http.HttpResponse;
6+
import jodd.http.ProxyInfo;
37
import me.chanjar.weixin.common.bean.result.WxError;
48
import me.chanjar.weixin.common.exception.WxErrorException;
59
import me.chanjar.weixin.common.util.fs.FileUtils;
10+
import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler;
11+
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
612
import org.apache.commons.lang3.StringUtils;
713
import org.apache.http.Header;
814
import org.apache.http.HttpHost;
@@ -12,6 +18,7 @@
1218
import org.apache.http.entity.ContentType;
1319
import org.apache.http.impl.client.CloseableHttpClient;
1420

21+
import java.io.ByteArrayInputStream;
1522
import java.io.File;
1623
import java.io.IOException;
1724
import java.io.InputStream;
@@ -21,21 +28,74 @@
2128
/**
2229
* 下载媒体文件请求执行器,请求的参数是String, 返回的结果是File
2330
* 视频文件不支持下载
31+
*
2432
* @author Daniel Qian
2533
*/
2634
public class MediaDownloadRequestExecutor implements RequestExecutor<File, String> {
2735

2836
private File tmpDirFile;
2937

30-
public MediaDownloadRequestExecutor() {
31-
}
32-
3338
public MediaDownloadRequestExecutor(File tmpDirFile) {
3439
this.tmpDirFile = tmpDirFile;
3540
}
3641

3742
@Override
38-
public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, IOException {
43+
public File execute(RequestHttp requestHttp, String uri, String queryParam) throws WxErrorException, IOException {
44+
if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) {
45+
CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient();
46+
HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy();
47+
return executeApache(httpClient, httpProxy, uri, queryParam);
48+
}
49+
if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) {
50+
HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient();
51+
ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy();
52+
return executeJodd(provider, proxyInfo, uri, queryParam);
53+
} else {
54+
//这里需要抛出异常,需要优化
55+
return null;
56+
}
57+
}
58+
59+
private String getFileNameJodd(HttpResponse response) throws WxErrorException {
60+
String content = response.header("Content-disposition");
61+
if (content == null || content.length() == 0) {
62+
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
63+
}
64+
65+
Pattern p = Pattern.compile(".*filename=\"(.*)\"");
66+
Matcher m = p.matcher(content);
67+
if (m.matches()) {
68+
return m.group(1);
69+
}
70+
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
71+
}
72+
73+
private String getFileNameApache(CloseableHttpResponse response) throws WxErrorException {
74+
Header[] contentDispositionHeader = response.getHeaders("Content-disposition");
75+
if(contentDispositionHeader == null || contentDispositionHeader.length == 0){
76+
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
77+
}
78+
79+
Pattern p = Pattern.compile(".*filename=\"(.*)\"");
80+
Matcher m = p.matcher(contentDispositionHeader[0].getValue());
81+
if(m.matches()){
82+
return m.group(1);
83+
}
84+
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
85+
}
86+
87+
88+
/**
89+
* apache-http实现方式
90+
* @param httpclient
91+
* @param httpProxy
92+
* @param uri
93+
* @param queryParam
94+
* @return
95+
* @throws WxErrorException
96+
* @throws IOException
97+
*/
98+
private File executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, IOException {
3999
if (queryParam != null) {
40100
if (uri.indexOf('?') == -1) {
41101
uri += '?';
@@ -50,8 +110,8 @@ public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String u
50110
}
51111

52112
try (CloseableHttpResponse response = httpclient.execute(httpGet);
53-
InputStream inputStream = InputStreamResponseHandler.INSTANCE
54-
.handleResponse(response)) {
113+
InputStream inputStream = InputStreamResponseHandler.INSTANCE
114+
.handleResponse(response)) {
55115

56116
Header[] contentTypeHeader = response.getHeaders("Content-Type");
57117
if (contentTypeHeader != null && contentTypeHeader.length > 0) {
@@ -62,7 +122,7 @@ public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String u
62122
}
63123
}
64124

65-
String fileName = getFileName(response);
125+
String fileName = getFileNameApache(response);
66126
if (StringUtils.isBlank(fileName)) {
67127
return null;
68128
}
@@ -76,18 +136,46 @@ public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String u
76136

77137
}
78138

79-
private String getFileName(CloseableHttpResponse response) throws WxErrorException {
80-
Header[] contentDispositionHeader = response.getHeaders("Content-disposition");
81-
if(contentDispositionHeader == null || contentDispositionHeader.length == 0){
82-
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
139+
140+
/**
141+
* jodd-http实现方式
142+
* @param provider
143+
* @param proxyInfo
144+
* @param uri
145+
* @param queryParam
146+
* @return
147+
* @throws WxErrorException
148+
* @throws IOException
149+
*/
150+
private File executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, String queryParam) throws WxErrorException, IOException {
151+
if (queryParam != null) {
152+
if (uri.indexOf('?') == -1) {
153+
uri += '?';
154+
}
155+
uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
83156
}
84157

85-
Pattern p = Pattern.compile(".*filename=\"(.*)\"");
86-
Matcher m = p.matcher(contentDispositionHeader[0].getValue());
87-
if(m.matches()){
88-
return m.group(1);
158+
HttpRequest request = HttpRequest.get(uri);
159+
if (proxyInfo != null) {
160+
provider.useProxy(proxyInfo);
89161
}
90-
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
162+
request.withConnectionProvider(provider);
163+
HttpResponse response = request.send();
164+
String contentType = response.header("Content-Type");
165+
if (contentType != null && contentType.startsWith("application/json")) {
166+
// application/json; encoding=utf-8 下载媒体文件出错
167+
throw new WxErrorException(WxError.fromJson(response.bodyText()));
168+
}
169+
170+
String fileName = getFileNameJodd(response);
171+
if (StringUtils.isBlank(fileName)) {
172+
return null;
173+
}
174+
175+
InputStream inputStream = new ByteArrayInputStream(response.bodyBytes());
176+
String[] nameAndExt = fileName.split("\\.");
177+
return FileUtils.createTmpFile(inputStream, nameAndExt[0], nameAndExt[1], this.tmpDirFile);
91178
}
92179

180+
93181
}

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaUploadRequestExecutor.java

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package me.chanjar.weixin.common.util.http;
22

3+
import jodd.http.HttpConnectionProvider;
4+
import jodd.http.HttpRequest;
5+
import jodd.http.HttpResponse;
6+
import jodd.http.ProxyInfo;
37
import me.chanjar.weixin.common.bean.result.WxError;
48
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
59
import me.chanjar.weixin.common.exception.WxErrorException;
10+
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
611
import org.apache.http.HttpEntity;
712
import org.apache.http.HttpHost;
813
import org.apache.http.client.config.RequestConfig;
@@ -24,18 +29,46 @@
2429
public class MediaUploadRequestExecutor implements RequestExecutor<WxMediaUploadResult, File> {
2530

2631
@Override
27-
public WxMediaUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File file) throws WxErrorException, IOException {
32+
public WxMediaUploadResult execute(RequestHttp requestHttp, String uri, File file) throws WxErrorException, IOException {
33+
if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) {
34+
CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient();
35+
HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy();
36+
return executeApache(httpClient, httpProxy, uri, file);
37+
}
38+
if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) {
39+
HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient();
40+
ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy();
41+
return executeJodd(provider, proxyInfo, uri, file);
42+
} else {
43+
//这里需要抛出异常,需要优化
44+
return null;
45+
}
46+
47+
48+
}
49+
50+
/**
51+
* apache-http实现方式
52+
* @param httpclient
53+
* @param httpProxy
54+
* @param uri
55+
* @param file
56+
* @return
57+
* @throws WxErrorException
58+
* @throws IOException
59+
*/
60+
private WxMediaUploadResult executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File file) throws WxErrorException, IOException {
2861
HttpPost httpPost = new HttpPost(uri);
2962
if (httpProxy != null) {
3063
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
3164
httpPost.setConfig(config);
3265
}
3366
if (file != null) {
3467
HttpEntity entity = MultipartEntityBuilder
35-
.create()
36-
.addBinaryBody("media", file)
37-
.setMode(HttpMultipartMode.RFC6532)
38-
.build();
68+
.create()
69+
.addBinaryBody("media", file)
70+
.setMode(HttpMultipartMode.RFC6532)
71+
.build();
3972
httpPost.setEntity(entity);
4073
httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
4174
}
@@ -51,4 +84,32 @@ public WxMediaUploadResult execute(CloseableHttpClient httpclient, HttpHost http
5184
}
5285
}
5386

87+
88+
/**
89+
* jodd-http实现方式
90+
* @param provider
91+
* @param proxyInfo
92+
* @param uri
93+
* @param file
94+
* @return
95+
* @throws WxErrorException
96+
* @throws IOException
97+
*/
98+
private WxMediaUploadResult executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, File file) throws WxErrorException, IOException {
99+
HttpRequest request = HttpRequest.post(uri);
100+
if (proxyInfo != null) {
101+
provider.useProxy(proxyInfo);
102+
}
103+
request.withConnectionProvider(provider);
104+
request.form("media", file);
105+
HttpResponse response = request.send();
106+
String responseContent = response.bodyText();
107+
WxError error = WxError.fromJson(responseContent);
108+
if (error.getErrorCode() != 0) {
109+
throw new WxErrorException(error);
110+
}
111+
return WxMediaUploadResult.fromJson(responseContent);
112+
}
113+
114+
54115
}
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package me.chanjar.weixin.common.util.http;
22

33
import me.chanjar.weixin.common.exception.WxErrorException;
4-
import org.apache.http.HttpHost;
5-
import org.apache.http.impl.client.CloseableHttpClient;
64

75
import java.io.IOException;
86

@@ -15,13 +13,11 @@
1513
public interface RequestExecutor<T, E> {
1614

1715
/**
18-
* @param httpclient 传入的httpClient
19-
* @param httpProxy http代理对象,如果没有配置代理则为空
20-
* @param uri uri
21-
* @param data 数据
16+
* @param uri uri
17+
* @param data 数据
2218
* @throws WxErrorException
2319
* @throws IOException
2420
*/
25-
T execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, E data) throws WxErrorException, IOException;
21+
T execute(RequestHttp requestHttp, String uri, E data) throws WxErrorException, IOException;
2622

2723
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package me.chanjar.weixin.common.util.http;
2+
3+
/**
4+
* Created by ecoolper on 2017/4/22.
5+
*/
6+
public interface RequestHttp {
7+
8+
/**
9+
* 返回httpClient
10+
* @return
11+
*/
12+
Object getRequestHttpClient();
13+
14+
/**
15+
* 返回httpProxy
16+
* @return
17+
*/
18+
Object getRequestHttpProxy();
19+
20+
}

0 commit comments

Comments
 (0)