Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@
</properties>

<dependencies>
<dependency>
<groupId>org.jodd</groupId>
<artifactId>jodd-http</artifactId>
<version>3.7</version>
<!-- 由于较新的3.8版本需要jdk8,故而此处采用较低版本 -->
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.7.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
4 changes: 3 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ rootProject.name = 'weixin-java-parent'
include ':weixin-java-common'
include ':weixin-java-cp'
include ':weixin-java-mp'
include ':weixin-java-pay'

project(':weixin-java-common').projectDir = "$rootDir/weixin-java-common" as File
project(':weixin-java-cp').projectDir = "$rootDir/weixin-java-cp" as File
project(':weixin-java-mp').projectDir = "$rootDir/weixin-java-mp" as File
project(':weixin-java-mp').projectDir = "$rootDir/weixin-java-mp" as File
project(':weixin-java-pay').projectDir = "$rootDir/weixin-java-pay" as File
5 changes: 5 additions & 0 deletions weixin-java-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
<artifactId>jetty-servlet</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jodd</groupId>
<artifactId>jodd-http</artifactId>
<version>3.7</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package me.chanjar.weixin.common.util.http;

import jodd.http.HttpConnectionProvider;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import jodd.http.ProxyInfo;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.fs.FileUtils;
import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpHost;
Expand All @@ -12,6 +18,7 @@
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;

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

private File tmpDirFile;

public MediaDownloadRequestExecutor() {
}

public MediaDownloadRequestExecutor(File tmpDirFile) {
this.tmpDirFile = tmpDirFile;
}

@Override
public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, IOException {
public File execute(RequestHttp requestHttp, String uri, String queryParam) throws WxErrorException, IOException {
if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) {
CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient();
HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy();
return executeApache(httpClient, httpProxy, uri, queryParam);
}
if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) {
HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient();
ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy();
return executeJodd(provider, proxyInfo, uri, queryParam);
} else {
//这里需要抛出异常,需要优化
return null;
}
}

private String getFileNameJodd(HttpResponse response) throws WxErrorException {
String content = response.header("Content-disposition");
if (content == null || content.length() == 0) {
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
}

Pattern p = Pattern.compile(".*filename=\"(.*)\"");
Matcher m = p.matcher(content);
if (m.matches()) {
return m.group(1);
}
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
}

private String getFileNameApache(CloseableHttpResponse response) throws WxErrorException {
Header[] contentDispositionHeader = response.getHeaders("Content-disposition");
if(contentDispositionHeader == null || contentDispositionHeader.length == 0){
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
}

Pattern p = Pattern.compile(".*filename=\"(.*)\"");
Matcher m = p.matcher(contentDispositionHeader[0].getValue());
if(m.matches()){
return m.group(1);
}
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
}


/**
* apache-http实现方式
* @param httpclient
* @param httpProxy
* @param uri
* @param queryParam
* @return
* @throws WxErrorException
* @throws IOException
*/
private File executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, IOException {
if (queryParam != null) {
if (uri.indexOf('?') == -1) {
uri += '?';
Expand All @@ -50,8 +110,8 @@ public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String u
}

try (CloseableHttpResponse response = httpclient.execute(httpGet);
InputStream inputStream = InputStreamResponseHandler.INSTANCE
.handleResponse(response)) {
InputStream inputStream = InputStreamResponseHandler.INSTANCE
.handleResponse(response)) {

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

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

}

private String getFileName(CloseableHttpResponse response) throws WxErrorException {
Header[] contentDispositionHeader = response.getHeaders("Content-disposition");
if(contentDispositionHeader == null || contentDispositionHeader.length == 0){
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());

/**
* jodd-http实现方式
* @param provider
* @param proxyInfo
* @param uri
* @param queryParam
* @return
* @throws WxErrorException
* @throws IOException
*/
private File executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, String queryParam) throws WxErrorException, IOException {
if (queryParam != null) {
if (uri.indexOf('?') == -1) {
uri += '?';
}
uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
}

Pattern p = Pattern.compile(".*filename=\"(.*)\"");
Matcher m = p.matcher(contentDispositionHeader[0].getValue());
if(m.matches()){
return m.group(1);
HttpRequest request = HttpRequest.get(uri);
if (proxyInfo != null) {
provider.useProxy(proxyInfo);
}
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
request.withConnectionProvider(provider);
HttpResponse response = request.send();
String contentType = response.header("Content-Type");
if (contentType != null && contentType.startsWith("application/json")) {
// application/json; encoding=utf-8 下载媒体文件出错
throw new WxErrorException(WxError.fromJson(response.bodyText()));
}

String fileName = getFileNameJodd(response);
if (StringUtils.isBlank(fileName)) {
return null;
}

InputStream inputStream = new ByteArrayInputStream(response.bodyBytes());
String[] nameAndExt = fileName.split("\\.");
return FileUtils.createTmpFile(inputStream, nameAndExt[0], nameAndExt[1], this.tmpDirFile);
}


}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package me.chanjar.weixin.common.util.http;

import jodd.http.HttpConnectionProvider;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import jodd.http.ProxyInfo;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
Expand All @@ -24,18 +29,46 @@
public class MediaUploadRequestExecutor implements RequestExecutor<WxMediaUploadResult, File> {

@Override
public WxMediaUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File file) throws WxErrorException, IOException {
public WxMediaUploadResult execute(RequestHttp requestHttp, String uri, File file) throws WxErrorException, IOException {
if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) {
CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient();
HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy();
return executeApache(httpClient, httpProxy, uri, file);
}
if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) {
HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient();
ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy();
return executeJodd(provider, proxyInfo, uri, file);
} else {
//这里需要抛出异常,需要优化
return null;
}


}

/**
* apache-http实现方式
* @param httpclient
* @param httpProxy
* @param uri
* @param file
* @return
* @throws WxErrorException
* @throws IOException
*/
private WxMediaUploadResult executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File file) throws WxErrorException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpPost.setConfig(config);
}
if (file != null) {
HttpEntity entity = MultipartEntityBuilder
.create()
.addBinaryBody("media", file)
.setMode(HttpMultipartMode.RFC6532)
.build();
.create()
.addBinaryBody("media", file)
.setMode(HttpMultipartMode.RFC6532)
.build();
httpPost.setEntity(entity);
httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
}
Expand All @@ -51,4 +84,32 @@ public WxMediaUploadResult execute(CloseableHttpClient httpclient, HttpHost http
}
}


/**
* jodd-http实现方式
* @param provider
* @param proxyInfo
* @param uri
* @param file
* @return
* @throws WxErrorException
* @throws IOException
*/
private WxMediaUploadResult executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, File file) throws WxErrorException, IOException {
HttpRequest request = HttpRequest.post(uri);
if (proxyInfo != null) {
provider.useProxy(proxyInfo);
}
request.withConnectionProvider(provider);
request.form("media", file);
HttpResponse response = request.send();
String responseContent = response.bodyText();
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}
return WxMediaUploadResult.fromJson(responseContent);
}


}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package me.chanjar.weixin.common.util.http;

import me.chanjar.weixin.common.exception.WxErrorException;
import org.apache.http.HttpHost;
import org.apache.http.impl.client.CloseableHttpClient;

import java.io.IOException;

Expand All @@ -15,13 +13,11 @@
public interface RequestExecutor<T, E> {

/**
* @param httpclient 传入的httpClient
* @param httpProxy http代理对象,如果没有配置代理则为空
* @param uri uri
* @param data 数据
* @param uri uri
* @param data 数据
* @throws WxErrorException
* @throws IOException
*/
T execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, E data) throws WxErrorException, IOException;
T execute(RequestHttp requestHttp, String uri, E data) throws WxErrorException, IOException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package me.chanjar.weixin.common.util.http;

/**
* Created by ecoolper on 2017/4/22.
*/
public interface RequestHttp {

/**
* 返回httpClient
* @return
*/
Object getRequestHttpClient();

/**
* 返回httpProxy
* @return
*/
Object getRequestHttpProxy();

}
Loading