装饰模式实现

This commit is contained in:
ecoolper
2017-04-22 15:06:12 +08:00
parent 76330ef3c6
commit 9ac1aad0e4
44 changed files with 860 additions and 1636 deletions

View File

@@ -0,0 +1,93 @@
package me.chanjar.weixin.mp.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.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class MaterialDeleteRequestExecutor implements RequestExecutor<Boolean, String> {
public MaterialDeleteRequestExecutor() {
super();
}
@Override
public Boolean execute(RequestHttp requestHttp, String uri, String materialId) throws WxErrorException, IOException {
if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) {
CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient();
HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy();
return executeApache(httpClient, httpProxy, uri, materialId);
}
if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) {
HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient();
ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy();
return executeJodd(provider, proxyInfo, uri, materialId);
} else {
//这里需要抛出异常,需要优化
return null;
}
}
private Boolean executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, String materialId) throws WxErrorException, IOException {
HttpRequest request = HttpRequest.post(uri);
if (proxyInfo != null) {
provider.useProxy(proxyInfo);
}
request.withConnectionProvider(provider);
request.query("media_id", materialId);
HttpResponse response = request.send();
String responseContent = response.bodyText();
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return true;
}
}
private Boolean executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
String materialId) throws WxErrorException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpPost.setConfig(config);
}
Map<String, String> params = new HashMap<>();
params.put("media_id", materialId);
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return true;
}
} finally {
httpPost.releaseConnection();
}
}
}

View File

@@ -0,0 +1,93 @@
package me.chanjar.weixin.mp.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.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class MaterialNewsInfoRequestExecutor implements RequestExecutor<WxMpMaterialNews, String> {
public MaterialNewsInfoRequestExecutor() {
super();
}
@Override
public WxMpMaterialNews execute(RequestHttp requestHttp, String uri,
String materialId) throws WxErrorException, IOException {
if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) {
CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient();
HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy();
return executeApache(httpClient, httpProxy, uri, materialId);
}
if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) {
HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient();
ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy();
return executeJodd(provider, proxyInfo, uri, materialId);
} else {
//这里需要抛出异常,需要优化
return null;
}
}
public WxMpMaterialNews executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpPost.setConfig(config);
}
Map<String, String> params = new HashMap<>();
params.put("media_id", materialId);
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class);
}
} finally {
httpPost.releaseConnection();
}
}
public WxMpMaterialNews executeJodd(HttpConnectionProvider httpclient, ProxyInfo httpProxy, String uri, String materialId) throws WxErrorException, IOException {
HttpRequest request = HttpRequest.post(uri);
if (httpProxy != null) {
httpclient.useProxy(httpProxy);
}
request.withConnectionProvider(httpclient);
request.query("media_id", materialId);
HttpResponse response = request.send();
String responseContent = request.bodyText();
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class);
}
}
}

View File

@@ -0,0 +1,121 @@
package me.chanjar.weixin.mp.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.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.mp.bean.material.WxMpMaterial;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialUploadResult;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;
public class MaterialUploadRequestExecutor implements RequestExecutor<WxMpMaterialUploadResult, WxMpMaterial> {
@Override
public WxMpMaterialUploadResult execute(RequestHttp requestHttp, String uri, WxMpMaterial material) throws WxErrorException, IOException {
if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) {
CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient();
HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy();
return executeApache(httpClient, httpProxy, uri, material);
}
if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) {
HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient();
ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy();
return executeJodd(provider, proxyInfo, uri, material);
} else {
//这里需要抛出异常,需要优化
return null;
}
}
private WxMpMaterialUploadResult executeJodd(HttpConnectionProvider provider, ProxyInfo httpProxy, String uri, WxMpMaterial material) throws WxErrorException, IOException {
HttpRequest request = HttpRequest.post(uri);
if (httpProxy != null) {
provider.useProxy(httpProxy);
}
request.withConnectionProvider(provider);
if (material == null) {
throw new WxErrorException(WxError.newBuilder().setErrorMsg("非法请求material参数为空").build());
}
File file = material.getFile();
if (file == null || !file.exists()) {
throw new FileNotFoundException();
}
request.form("media", file);
Map<String, String> form = material.getForm();
if (material.getForm() != null) {
request.form("description", WxGsonBuilder.create().toJson(form));
}
HttpResponse response = request.send();
String responseContent = response.bodyText();
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return WxMpMaterialUploadResult.fromJson(responseContent);
}
}
private WxMpMaterialUploadResult executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
WxMpMaterial material) throws WxErrorException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (httpProxy != null) {
RequestConfig response = RequestConfig.custom().setProxy(httpProxy).build();
httpPost.setConfig(response);
}
if (material == null) {
throw new WxErrorException(WxError.newBuilder().setErrorMsg("非法请求material参数为空").build());
}
File file = material.getFile();
if (file == null || !file.exists()) {
throw new FileNotFoundException();
}
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder
.addBinaryBody("media", file)
.setMode(HttpMultipartMode.RFC6532);
Map<String, String> form = material.getForm();
if (material.getForm() != null) {
multipartEntityBuilder.addTextBody("description", WxGsonBuilder.create().toJson(form));
}
httpPost.setEntity(multipartEntityBuilder.build());
httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return WxMpMaterialUploadResult.fromJson(responseContent);
}
} finally {
httpPost.releaseConnection();
}
}
}

View File

@@ -0,0 +1,92 @@
package me.chanjar.weixin.mp.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.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialVideoInfoResult;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class MaterialVideoInfoRequestExecutor implements RequestExecutor<WxMpMaterialVideoInfoResult, String> {
public MaterialVideoInfoRequestExecutor() {
super();
}
@Override
public WxMpMaterialVideoInfoResult execute(RequestHttp requestHttp, String uri, String materialId) throws WxErrorException, IOException {
if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) {
CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient();
HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy();
return executeApache(httpClient, httpProxy, uri, materialId);
}
if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) {
HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient();
ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy();
return executeJodd(provider, proxyInfo, uri, materialId);
} else {
//这里需要抛出异常,需要优化
return null;
}
}
private WxMpMaterialVideoInfoResult executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, String materialId) throws WxErrorException, IOException {
HttpRequest request = HttpRequest.post(uri);
if (proxyInfo != null) {
provider.useProxy(proxyInfo);
}
request.withConnectionProvider(provider);
request.query("media_id", materialId);
HttpResponse response = request.send();
String responseContent = response.bodyText();
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return WxMpMaterialVideoInfoResult.fromJson(responseContent);
}
}
private WxMpMaterialVideoInfoResult executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
String materialId) throws WxErrorException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpPost.setConfig(config);
}
Map<String, String> params = new HashMap<>();
params.put("media_id", materialId);
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return WxMpMaterialVideoInfoResult.fromJson(responseContent);
}
} finally {
httpPost.releaseConnection();
}
}
}

View File

@@ -0,0 +1,117 @@
package me.chanjar.weixin.mp.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.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
public class MaterialVoiceAndImageDownloadRequestExecutor implements RequestExecutor<InputStream, String> {
public MaterialVoiceAndImageDownloadRequestExecutor() {
super();
}
public MaterialVoiceAndImageDownloadRequestExecutor(File tmpDirFile) {
super();
}
@Override
public InputStream execute(RequestHttp requestHttp, String uri, String materialId) throws WxErrorException, IOException {
if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) {
CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient();
HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy();
return executeApache(httpClient, httpProxy, uri, materialId);
}
if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) {
HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient();
ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy();
return executeJodd(provider, proxyInfo, uri, materialId);
} else {
//这里需要抛出异常,需要优化
return null;
}
}
private InputStream executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, String materialId) throws WxErrorException, IOException {
HttpRequest request = HttpRequest.post(uri);
if (proxyInfo != null) {
provider.useProxy(proxyInfo);
}
request.withConnectionProvider(provider);
request.query("media_id", materialId);
HttpResponse response = request.send();
try (InputStream inputStream = new ByteArrayInputStream(response.bodyBytes())) {
// 下载媒体文件出错
byte[] responseContent = IOUtils.toByteArray(inputStream);
String responseContentString = new String(responseContent, "UTF-8");
if (responseContentString.length() < 100) {
try {
WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class);
if (wxError.getErrorCode() != 0) {
throw new WxErrorException(wxError);
}
} catch (com.google.gson.JsonSyntaxException ex) {
return new ByteArrayInputStream(responseContent);
}
}
return new ByteArrayInputStream(responseContent);
}
}
private InputStream executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
String materialId) throws WxErrorException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpPost.setConfig(config);
}
Map<String, String> params = new HashMap<>();
params.put("media_id", materialId);
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
try (CloseableHttpResponse response = httpclient.execute(httpPost);
InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);) {
// 下载媒体文件出错
byte[] responseContent = IOUtils.toByteArray(inputStream);
String responseContentString = new String(responseContent, "UTF-8");
if (responseContentString.length() < 100) {
try {
WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class);
if (wxError.getErrorCode() != 0) {
throw new WxErrorException(wxError);
}
} catch (com.google.gson.JsonSyntaxException ex) {
return new ByteArrayInputStream(responseContent);
}
}
return new ByteArrayInputStream(responseContent);
} finally {
httpPost.releaseConnection();
}
}
}

View File

@@ -0,0 +1,103 @@
package me.chanjar.weixin.mp.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.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.File;
import java.io.IOException;
/**
* @author miller
*/
public class MediaImgUploadRequestExecutor implements RequestExecutor<WxMediaImgUploadResult, File> {
@Override
public WxMediaImgUploadResult execute(RequestHttp requestHttp, String uri, File data) throws WxErrorException, IOException {
if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) {
CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient();
HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy();
return executeApache(httpClient, httpProxy, uri, data);
}
if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) {
HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient();
ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy();
return executeJodd(provider, proxyInfo, uri, data);
} else {
//这里需要抛出异常,需要优化
return null;
}
}
private WxMediaImgUploadResult executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, File data) throws WxErrorException, IOException {
if (data == null) {
throw new WxErrorException(WxError.newBuilder().setErrorMsg("文件对象为空").build());
}
HttpRequest request = HttpRequest.post(uri);
if (proxyInfo != null) {
provider.useProxy(proxyInfo);
}
request.withConnectionProvider(provider);
request.form("media", data);
HttpResponse response = request.send();
String responseContent = response.bodyText();
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}
return WxMediaImgUploadResult.fromJson(responseContent);
}
private WxMediaImgUploadResult executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
File data) throws WxErrorException, IOException {
if (data == null) {
throw new WxErrorException(WxError.newBuilder().setErrorMsg("文件对象为空").build());
}
HttpPost httpPost = new HttpPost(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpPost.setConfig(config);
}
HttpEntity entity = MultipartEntityBuilder
.create()
.addBinaryBody("media", data)
.setMode(HttpMultipartMode.RFC6532)
.build();
httpPost.setEntity(entity);
httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}
return WxMediaImgUploadResult.fromJson(responseContent);
}
}
}

View File

@@ -0,0 +1,116 @@
package me.chanjar.weixin.mp.util.http;
import jodd.http.HttpConnectionProvider;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import jodd.http.ProxyInfo;
import jodd.util.MimeTypes;
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.RequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
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;
import java.net.URLEncoder;
import java.util.UUID;
/**
* 获得QrCode图片 请求执行器
*
* @author chanjarster
*/
public class QrCodeRequestExecutor implements RequestExecutor<File, WxMpQrCodeTicket> {
@Override
public File execute(RequestHttp requestHttp, String uri,
WxMpQrCodeTicket ticket) throws WxErrorException, IOException {
if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) {
CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient();
HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy();
return executeApache(httpClient, httpProxy, uri, ticket);
}
if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) {
HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient();
ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy();
return executeJodd(provider, proxyInfo, uri, ticket);
} else {
//这里需要抛出异常,需要优化
return null;
}
}
private File executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, WxMpQrCodeTicket ticket) throws WxErrorException, IOException {
if (ticket != null) {
if (uri.indexOf('?') == -1) {
uri += '?';
}
uri += uri.endsWith("?")
? "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8")
: "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8");
}
HttpRequest request = HttpRequest.get(uri);
if (proxyInfo != null) {
provider.useProxy(proxyInfo);
}
request.withConnectionProvider(provider);
HttpResponse response = request.send();
String contentTypeHeader = response.header("Content-Type");
if (MimeTypes.MIME_TEXT_PLAIN.equals(contentTypeHeader)) {
String responseContent = response.bodyText();
throw new WxErrorException(WxError.fromJson(responseContent));
}
try (InputStream inputStream = new ByteArrayInputStream(response.bodyBytes())) {
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
}
}
private File executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
WxMpQrCodeTicket ticket) throws WxErrorException, IOException {
if (ticket != null) {
if (uri.indexOf('?') == -1) {
uri += '?';
}
uri += uri.endsWith("?")
? "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8")
: "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8");
}
HttpGet httpGet = new HttpGet(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpGet.setConfig(config);
}
try (CloseableHttpResponse response = httpclient.execute(httpGet);
InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);) {
Header[] contentTypeHeader = response.getHeaders("Content-Type");
if (contentTypeHeader != null && contentTypeHeader.length > 0) {
// 出错
if (ContentType.TEXT_PLAIN.getMimeType().equals(contentTypeHeader[0].getValue())) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
throw new WxErrorException(WxError.fromJson(responseContent));
}
}
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
} finally {
httpGet.releaseConnection();
}
}
}

View File

@@ -1,50 +0,0 @@
package me.chanjar.weixin.mp.util.http.apache;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class MaterialDeleteRequestExecutor implements RequestExecutor<Boolean,CloseableHttpClient, HttpHost, String> {
public MaterialDeleteRequestExecutor() {
super();
}
@Override
public Boolean execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpPost.setConfig(config);
}
Map<String, String> params = new HashMap<>();
params.put("media_id", materialId);
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
try(CloseableHttpResponse response = httpclient.execute(httpPost)){
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return true;
}
}finally {
httpPost.releaseConnection();
}
}
}

View File

@@ -1,52 +0,0 @@
package me.chanjar.weixin.mp.util.http.apache;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class MaterialNewsInfoRequestExecutor implements RequestExecutor<WxMpMaterialNews,CloseableHttpClient, HttpHost, String> {
public MaterialNewsInfoRequestExecutor() {
super();
}
@Override
public WxMpMaterialNews execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpPost.setConfig(config);
}
Map<String, String> params = new HashMap<>();
params.put("media_id", materialId);
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
try(CloseableHttpResponse response = httpclient.execute(httpPost)){
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class);
}
}finally {
httpPost.releaseConnection();
}
}
}

View File

@@ -1,68 +0,0 @@
package me.chanjar.weixin.mp.util.http.apache;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.mp.bean.material.WxMpMaterial;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialUploadResult;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;
public class MaterialUploadRequestExecutor implements RequestExecutor<WxMpMaterialUploadResult,CloseableHttpClient, HttpHost, WxMpMaterial> {
@Override
public WxMpMaterialUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, WxMpMaterial material) throws WxErrorException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (httpProxy != null) {
RequestConfig response = RequestConfig.custom().setProxy(httpProxy).build();
httpPost.setConfig(response);
}
if (material == null) {
throw new WxErrorException(WxError.newBuilder().setErrorMsg("非法请求material参数为空").build());
}
File file = material.getFile();
if (file == null || !file.exists()) {
throw new FileNotFoundException();
}
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder
.addBinaryBody("media", file)
.setMode(HttpMultipartMode.RFC6532);
Map<String, String> form = material.getForm();
if (material.getForm() != null) {
multipartEntityBuilder.addTextBody("description", WxGsonBuilder.create().toJson(form));
}
httpPost.setEntity(multipartEntityBuilder.build());
httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return WxMpMaterialUploadResult.fromJson(responseContent);
}
} finally {
httpPost.releaseConnection();
}
}
}

View File

@@ -1,50 +0,0 @@
package me.chanjar.weixin.mp.util.http.apache;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialVideoInfoResult;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class MaterialVideoInfoRequestExecutor implements RequestExecutor<WxMpMaterialVideoInfoResult, CloseableHttpClient, HttpHost, String> {
public MaterialVideoInfoRequestExecutor() {
super();
}
@Override
public WxMpMaterialVideoInfoResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpPost.setConfig(config);
}
Map<String, String> params = new HashMap<>();
params.put("media_id", materialId);
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
try(CloseableHttpResponse response = httpclient.execute(httpPost)){
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return WxMpMaterialVideoInfoResult.fromJson(responseContent);
}
}finally {
httpPost.releaseConnection();
}
}
}

View File

@@ -1,66 +0,0 @@
package me.chanjar.weixin.mp.util.http.apache;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
public class MaterialVoiceAndImageDownloadRequestExecutor implements RequestExecutor<InputStream,CloseableHttpClient, HttpHost, String> {
public MaterialVoiceAndImageDownloadRequestExecutor() {
super();
}
public MaterialVoiceAndImageDownloadRequestExecutor(File tmpDirFile) {
super();
}
@Override
public InputStream execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpPost.setConfig(config);
}
Map<String, String> params = new HashMap<>();
params.put("media_id", materialId);
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
try (CloseableHttpResponse response = httpclient.execute(httpPost);
InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);){
// 下载媒体文件出错
byte[] responseContent = IOUtils.toByteArray(inputStream);
String responseContentString = new String(responseContent, "UTF-8");
if (responseContentString.length() < 100) {
try {
WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class);
if (wxError.getErrorCode() != 0) {
throw new WxErrorException(wxError);
}
} catch (com.google.gson.JsonSyntaxException ex) {
return new ByteArrayInputStream(responseContent);
}
}
return new ByteArrayInputStream(responseContent);
}finally {
httpPost.releaseConnection();
}
}
}

View File

@@ -1,55 +0,0 @@
package me.chanjar.weixin.mp.util.http.apache;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.File;
import java.io.IOException;
/**
* @author miller
*/
public class MediaImgUploadRequestExecutor implements RequestExecutor<WxMediaImgUploadResult, CloseableHttpClient, HttpHost, File> {
@Override
public WxMediaImgUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File data) throws WxErrorException, IOException {
if (data == null) {
throw new WxErrorException(WxError.newBuilder().setErrorMsg("文件对象为空").build());
}
HttpPost httpPost = new HttpPost(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpPost.setConfig(config);
}
HttpEntity entity = MultipartEntityBuilder
.create()
.addBinaryBody("media", data)
.setMode(HttpMultipartMode.RFC6532)
.build();
httpPost.setEntity(entity);
httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}
return WxMediaImgUploadResult.fromJson(responseContent);
}
}
}

View File

@@ -1,66 +0,0 @@
package me.chanjar.weixin.mp.util.http.apache;
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.RequestExecutor;
import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.UUID;
/**
* 获得QrCode图片 请求执行器
* @author chanjarster
*
*/
public class QrCodeRequestExecutor implements RequestExecutor<File, CloseableHttpClient, HttpHost, WxMpQrCodeTicket> {
@Override
public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
WxMpQrCodeTicket ticket) throws WxErrorException, IOException {
if (ticket != null) {
if (uri.indexOf('?') == -1) {
uri += '?';
}
uri += uri.endsWith("?")
? "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8")
: "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8");
}
HttpGet httpGet = new HttpGet(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpGet.setConfig(config);
}
try (CloseableHttpResponse response = httpclient.execute(httpGet);
InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);) {
Header[] contentTypeHeader = response.getHeaders("Content-Type");
if (contentTypeHeader != null && contentTypeHeader.length > 0) {
// 出错
if (ContentType.TEXT_PLAIN.getMimeType().equals(contentTypeHeader[0].getValue())) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
throw new WxErrorException(WxError.fromJson(responseContent));
}
}
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
} finally {
httpGet.releaseConnection();
}
}
}

View File

@@ -1,39 +0,0 @@
package me.chanjar.weixin.mp.util.http.jodd;
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.http.RequestExecutor;
import java.io.IOException;
public class MaterialDeleteRequestExecutor implements RequestExecutor<Boolean, HttpConnectionProvider, ProxyInfo, String> {
public MaterialDeleteRequestExecutor() {
super();
}
@Override
public Boolean execute(HttpConnectionProvider httpclient, ProxyInfo httpProxy, String uri, String materialId) throws WxErrorException, IOException {
HttpRequest request = HttpRequest.post(uri);
if (httpProxy != null) {
httpclient.useProxy(httpProxy);
}
request.withConnectionProvider(httpclient);
request.query("media_id", materialId);
HttpResponse response = request.send();
String responseContent = response.bodyText();
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return true;
}
}
}

View File

@@ -1,41 +0,0 @@
package me.chanjar.weixin.mp.util.http.jodd;
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.http.RequestExecutor;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
import java.io.IOException;
public class MaterialNewsInfoRequestExecutor implements RequestExecutor<WxMpMaterialNews, HttpConnectionProvider, ProxyInfo, String> {
public MaterialNewsInfoRequestExecutor() {
super();
}
@Override
public WxMpMaterialNews execute(HttpConnectionProvider httpclient, ProxyInfo httpProxy, String uri, String materialId) throws WxErrorException, IOException {
HttpRequest request = HttpRequest.post(uri);
if (httpProxy != null) {
httpclient.useProxy(httpProxy);
}
request.withConnectionProvider(httpclient);
request.query("media_id", materialId);
HttpResponse response = request.send();
String responseContent = request.bodyText();
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class);
}
}
}

View File

@@ -1,53 +0,0 @@
package me.chanjar.weixin.mp.util.http.jodd;
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.http.RequestExecutor;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.mp.bean.material.WxMpMaterial;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialUploadResult;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;
public class MaterialUploadRequestExecutor implements RequestExecutor<WxMpMaterialUploadResult, HttpConnectionProvider, ProxyInfo, WxMpMaterial> {
@Override
public WxMpMaterialUploadResult execute(HttpConnectionProvider provider, ProxyInfo httpProxy, String uri, WxMpMaterial material) throws WxErrorException, IOException {
HttpRequest request = HttpRequest.post(uri);
if (httpProxy != null) {
provider.useProxy(httpProxy);
}
request.withConnectionProvider(provider);
if (material == null) {
throw new WxErrorException(WxError.newBuilder().setErrorMsg("非法请求material参数为空").build());
}
File file = material.getFile();
if (file == null || !file.exists()) {
throw new FileNotFoundException();
}
request.form("media", file);
Map<String, String> form = material.getForm();
if (material.getForm() != null) {
request.form("description", WxGsonBuilder.create().toJson(form));
}
HttpResponse response = request.send();
String responseContent = response.bodyText();
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return WxMpMaterialUploadResult.fromJson(responseContent);
}
}
}

View File

@@ -1,39 +0,0 @@
package me.chanjar.weixin.mp.util.http.jodd;
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.http.RequestExecutor;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialVideoInfoResult;
import java.io.IOException;
public class MaterialVideoInfoRequestExecutor implements RequestExecutor<WxMpMaterialVideoInfoResult, HttpConnectionProvider, ProxyInfo, String> {
public MaterialVideoInfoRequestExecutor() {
super();
}
@Override
public WxMpMaterialVideoInfoResult execute(HttpConnectionProvider provider, ProxyInfo httpProxy, String uri, String materialId) throws WxErrorException, IOException {
HttpRequest request = HttpRequest.post(uri);
if (httpProxy != null) {
provider.useProxy(httpProxy);
}
request.withConnectionProvider(provider);
request.query("media_id", materialId);
HttpResponse response =request.send();
String responseContent = response.bodyText();
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return WxMpMaterialVideoInfoResult.fromJson(responseContent);
}
}
}

View File

@@ -1,57 +0,0 @@
package me.chanjar.weixin.mp.util.http.jodd;
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.http.RequestExecutor;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import org.apache.commons.io.IOUtils;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
public class MaterialVoiceAndImageDownloadRequestExecutor implements RequestExecutor<InputStream, HttpConnectionProvider, ProxyInfo, String> {
public MaterialVoiceAndImageDownloadRequestExecutor() {
super();
}
public MaterialVoiceAndImageDownloadRequestExecutor(File tmpDirFile) {
super();
}
@Override
public InputStream execute(HttpConnectionProvider provider, ProxyInfo httpProxy, String uri, String materialId) throws WxErrorException, IOException {
HttpRequest request = HttpRequest.post(uri);
if (httpProxy != null) {
provider.useProxy(httpProxy);
}
request.withConnectionProvider(provider);
request.query("media_id", materialId);
HttpResponse response = request.send();
InputStream inputStream = new ByteArrayInputStream(response.bodyBytes());
// 下载媒体文件出错
byte[] responseContent = IOUtils.toByteArray(inputStream);
String responseContentString = new String(responseContent, "UTF-8");
if (responseContentString.length() < 100) {
try {
WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class);
if (wxError.getErrorCode() != 0) {
throw new WxErrorException(wxError);
}
} catch (com.google.gson.JsonSyntaxException ex) {
return new ByteArrayInputStream(responseContent);
}
}
return new ByteArrayInputStream(responseContent);
}
}

View File

@@ -1,42 +0,0 @@
package me.chanjar.weixin.mp.util.http.jodd;
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.http.RequestExecutor;
import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult;
import java.io.File;
import java.io.IOException;
/**
* @author miller
*/
public class MediaImgUploadRequestExecutor implements RequestExecutor<WxMediaImgUploadResult, HttpConnectionProvider, ProxyInfo, File> {
@Override
public WxMediaImgUploadResult execute(HttpConnectionProvider provider, ProxyInfo httpProxy, String uri, File data) throws WxErrorException, IOException {
if (data == null) {
throw new WxErrorException(WxError.newBuilder().setErrorMsg("文件对象为空").build());
}
HttpRequest request = HttpRequest.post(uri);
if (httpProxy != null) {
provider.useProxy(httpProxy);
}
request.withConnectionProvider(provider);
request.form("media", data);
HttpResponse response =request.send();
String responseContent =response.bodyText();
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}
return WxMediaImgUploadResult.fromJson(responseContent);
}
}

View File

@@ -1,60 +0,0 @@
package me.chanjar.weixin.mp.util.http.jodd;
import jodd.http.HttpConnectionProvider;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import jodd.http.ProxyInfo;
import jodd.util.MimeTypes;
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.RequestExecutor;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.UUID;
/**
* 获得QrCode图片 请求执行器
*
* @author chanjarster
*/
public class QrCodeRequestExecutor implements RequestExecutor<File, HttpConnectionProvider, ProxyInfo, WxMpQrCodeTicket> {
@Override
public File execute(HttpConnectionProvider provider, ProxyInfo httpProxy, String uri,
WxMpQrCodeTicket ticket) throws WxErrorException, IOException {
if (ticket != null) {
if (uri.indexOf('?') == -1) {
uri += '?';
}
uri += uri.endsWith("?")
? "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8")
: "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8");
}
HttpRequest request = HttpRequest.get(uri);
if (httpProxy != null) {
provider.useProxy(httpProxy);
}
request.withConnectionProvider(provider);
HttpResponse response = request.send();
try (
InputStream inputStream = new ByteArrayInputStream(response.bodyBytes());) {
String contentTypeHeader = response.header("Content-Type");
// 出错
if (MimeTypes.MIME_TEXT_PLAIN.equals(contentTypeHeader)) {
String responseContent = response.bodyText();
throw new WxErrorException(WxError.fromJson(responseContent));
}
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
}
}
}