mp usage jodd-http

This commit is contained in:
ecoolper
2017-04-21 21:55:02 +08:00
parent 897e139587
commit d476047c4c
37 changed files with 1772 additions and 118 deletions

View File

@@ -1,9 +1,9 @@
package me.chanjar.weixin.mp.util.http;
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.Utf8ResponseHandler;
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;
@@ -16,7 +16,7 @@ import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class MaterialDeleteRequestExecutor implements RequestExecutor<Boolean, String> {
public class MaterialDeleteRequestExecutor implements RequestExecutor<Boolean,CloseableHttpClient, HttpHost, String> {
public MaterialDeleteRequestExecutor() {

View File

@@ -1,9 +1,9 @@
package me.chanjar.weixin.mp.util.http;
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.Utf8ResponseHandler;
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;
@@ -18,7 +18,7 @@ import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class MaterialNewsInfoRequestExecutor implements RequestExecutor<WxMpMaterialNews, String> {
public class MaterialNewsInfoRequestExecutor implements RequestExecutor<WxMpMaterialNews,CloseableHttpClient, HttpHost, String> {
public MaterialNewsInfoRequestExecutor() {
super();

View File

@@ -1,9 +1,9 @@
package me.chanjar.weixin.mp.util.http;
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.Utf8ResponseHandler;
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;
@@ -16,10 +16,12 @@ import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;
public class MaterialUploadRequestExecutor implements RequestExecutor<WxMpMaterialUploadResult, WxMpMaterial> {
public class MaterialUploadRequestExecutor implements RequestExecutor<WxMpMaterialUploadResult,CloseableHttpClient, HttpHost, WxMpMaterial> {
@Override
public WxMpMaterialUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, WxMpMaterial material) throws WxErrorException, IOException {

View File

@@ -1,9 +1,9 @@
package me.chanjar.weixin.mp.util.http;
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.Utf8ResponseHandler;
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;
@@ -17,7 +17,7 @@ import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class MaterialVideoInfoRequestExecutor implements RequestExecutor<WxMpMaterialVideoInfoResult, String> {
public class MaterialVideoInfoRequestExecutor implements RequestExecutor<WxMpMaterialVideoInfoResult, CloseableHttpClient, HttpHost, String> {
public MaterialVideoInfoRequestExecutor() {
super();

View File

@@ -1,12 +1,10 @@
package me.chanjar.weixin.mp.util.http;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
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;
@@ -15,13 +13,14 @@ import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.InputStreamResponseHandler;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
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 class MaterialVoiceAndImageDownloadRequestExecutor implements RequestExecutor<InputStream,CloseableHttpClient, HttpHost, String> {
public MaterialVoiceAndImageDownloadRequestExecutor() {

View File

@@ -1,9 +1,9 @@
package me.chanjar.weixin.mp.util.http;
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.Utf8ResponseHandler;
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;
@@ -21,7 +21,7 @@ import java.io.IOException;
/**
* @author miller
*/
public class MediaImgUploadRequestExecutor implements RequestExecutor<WxMediaImgUploadResult, File> {
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) {

View File

@@ -1,11 +1,11 @@
package me.chanjar.weixin.mp.util.http;
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.InputStreamResponseHandler;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.Utf8ResponseHandler;
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;
@@ -26,20 +26,20 @@ import java.util.UUID;
* @author chanjarster
*
*/
public class QrCodeRequestExecutor implements RequestExecutor<File, WxMpQrCodeTicket> {
public class QrCodeRequestExecutor implements RequestExecutor<File, CloseableHttpClient, HttpHost, WxMpQrCodeTicket> {
@Override
public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
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")
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();

View File

@@ -0,0 +1,39 @@
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

@@ -0,0 +1,41 @@
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

@@ -0,0 +1,53 @@
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

@@ -0,0 +1,39 @@
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

@@ -0,0 +1,57 @@
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

@@ -0,0 +1,42 @@
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

@@ -0,0 +1,60 @@
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");
}
}
}