mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-06-28 13:16:19 +08:00
🆕 #1942 【小程序】增加生成小程序码的okhttp实现
This commit is contained in:
parent
02d3c160b2
commit
604098b047
@ -7,7 +7,6 @@ import cn.binarywang.wx.miniapp.bean.WxMaQrcode;
|
||||
import cn.binarywang.wx.miniapp.bean.WxaCode;
|
||||
import cn.binarywang.wx.miniapp.bean.WxaCodeUnlimit;
|
||||
import cn.binarywang.wx.miniapp.executor.QrcodeBytesRequestExecutor;
|
||||
import cn.binarywang.wx.miniapp.executor.QrcodeFileRequestExecutor;
|
||||
import cn.binarywang.wx.miniapp.executor.QrcodeRequestExecutor;
|
||||
import lombok.AllArgsConstructor;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
@ -23,14 +22,12 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
|
||||
|
||||
@Override
|
||||
public byte[] createQrcodeBytes(String path, int width) throws WxErrorException {
|
||||
final QrcodeBytesRequestExecutor executor = new QrcodeBytesRequestExecutor(this.wxMaService.getRequestHttp());
|
||||
return this.wxMaService.execute(executor, CREATE_QRCODE_URL, new WxMaQrcode(path, width));
|
||||
return this.wxMaService.execute(QrcodeBytesRequestExecutor.create(this.wxMaService.getRequestHttp()), CREATE_QRCODE_URL, new WxMaQrcode(path, width));
|
||||
}
|
||||
|
||||
@Override
|
||||
public File createQrcode(String path, int width) throws WxErrorException {
|
||||
final QrcodeRequestExecutor executor = new QrcodeRequestExecutor(this.wxMaService.getRequestHttp());
|
||||
return this.wxMaService.execute(executor, CREATE_QRCODE_URL, new WxMaQrcode(path, width));
|
||||
return this.wxMaService.execute(QrcodeRequestExecutor.create(this.wxMaService.getRequestHttp()), CREATE_QRCODE_URL, new WxMaQrcode(path, width));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -41,8 +38,7 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
|
||||
@Override
|
||||
public byte[] createWxaCodeBytes(String path, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline)
|
||||
throws WxErrorException {
|
||||
final QrcodeBytesRequestExecutor executor = new QrcodeBytesRequestExecutor(this.wxMaService.getRequestHttp());
|
||||
return this.wxMaService.execute(executor, GET_WXACODE_URL, WxaCode.builder()
|
||||
return this.wxMaService.execute(QrcodeBytesRequestExecutor.create(this.wxMaService.getRequestHttp()), GET_WXACODE_URL, WxaCode.builder()
|
||||
.path(path)
|
||||
.width(width)
|
||||
.autoColor(autoColor)
|
||||
@ -54,8 +50,7 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
|
||||
@Override
|
||||
public File createWxaCode(String path, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline)
|
||||
throws WxErrorException {
|
||||
final QrcodeRequestExecutor executor = new QrcodeRequestExecutor(this.wxMaService.getRequestHttp());
|
||||
return this.wxMaService.execute(executor, GET_WXACODE_URL, WxaCode.builder()
|
||||
return this.wxMaService.execute(QrcodeRequestExecutor.create(this.wxMaService.getRequestHttp()), GET_WXACODE_URL, WxaCode.builder()
|
||||
.path(path)
|
||||
.width(width)
|
||||
.autoColor(autoColor)
|
||||
@ -77,7 +72,7 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
|
||||
@Override
|
||||
public byte[] createWxaCodeUnlimitBytes(String scene, String page, int width, boolean autoColor,
|
||||
WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException {
|
||||
return this.wxMaService.execute(new QrcodeBytesRequestExecutor(this.wxMaService.getRequestHttp()),
|
||||
return this.wxMaService.execute(QrcodeBytesRequestExecutor.create(this.wxMaService.getRequestHttp()),
|
||||
GET_WXACODE_UNLIMIT_URL,
|
||||
this.buildWxaCodeUnlimit(scene, page, width, autoColor, lineColor, isHyaline));
|
||||
}
|
||||
@ -85,7 +80,7 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
|
||||
@Override
|
||||
public File createWxaCodeUnlimit(String scene, String page, int width, boolean autoColor,
|
||||
WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException {
|
||||
return this.wxMaService.execute(new QrcodeRequestExecutor(this.wxMaService.getRequestHttp()),
|
||||
return this.wxMaService.execute(QrcodeRequestExecutor.create(this.wxMaService.getRequestHttp()),
|
||||
GET_WXACODE_UNLIMIT_URL,
|
||||
this.buildWxaCodeUnlimit(scene, page, width, autoColor, lineColor, isHyaline));
|
||||
}
|
||||
@ -110,8 +105,7 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
|
||||
|
||||
@Override
|
||||
public File createQrcode(String path, int width, String filePath) throws WxErrorException {
|
||||
final QrcodeFileRequestExecutor executor = new QrcodeFileRequestExecutor(this.wxMaService.getRequestHttp(), filePath);
|
||||
return this.wxMaService.execute(executor, CREATE_QRCODE_URL, new WxMaQrcode(path, width));
|
||||
return this.wxMaService.execute(QrcodeRequestExecutor.create(this.wxMaService.getRequestHttp(), filePath), CREATE_QRCODE_URL, new WxMaQrcode(path, width));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -122,8 +116,7 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
|
||||
@Override
|
||||
public File createWxaCode(String path, int width, String filePath, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline)
|
||||
throws WxErrorException {
|
||||
final QrcodeFileRequestExecutor executor = new QrcodeFileRequestExecutor(this.wxMaService.getRequestHttp(), filePath);
|
||||
return this.wxMaService.execute(executor, GET_WXACODE_URL, WxaCode.builder()
|
||||
return this.wxMaService.execute(QrcodeRequestExecutor.create(this.wxMaService.getRequestHttp(), filePath), GET_WXACODE_URL, WxaCode.builder()
|
||||
.path(path)
|
||||
.width(width)
|
||||
.autoColor(autoColor)
|
||||
@ -145,7 +138,7 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
|
||||
@Override
|
||||
public File createWxaCodeUnlimit(String scene, String page, String filePath, int width, boolean autoColor,
|
||||
WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException {
|
||||
return this.wxMaService.execute(new QrcodeFileRequestExecutor(this.wxMaService.getRequestHttp(), filePath),
|
||||
return this.wxMaService.execute(QrcodeRequestExecutor.create(this.wxMaService.getRequestHttp(), filePath),
|
||||
GET_WXACODE_UNLIMIT_URL,
|
||||
this.buildWxaCodeUnlimit(scene, page, width, autoColor, lineColor, isHyaline));
|
||||
}
|
||||
|
@ -0,0 +1,70 @@
|
||||
package cn.binarywang.wx.miniapp.executor;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
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 org.apache.commons.io.IOUtils;
|
||||
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.HttpPost;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* @author wenqiang
|
||||
* @since 2020/12/25
|
||||
*/
|
||||
public class ApacheQrcodeBytesRequestExecutor extends QrcodeBytesRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
|
||||
|
||||
public ApacheQrcodeBytesRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行http请求.
|
||||
*
|
||||
* @param uri uri
|
||||
* @param qrcodeWrapper 数据
|
||||
* @param wxType 微信模块类型
|
||||
* @return 响应结果
|
||||
* @throws WxErrorException 自定义异常
|
||||
* @throws IOException io异常
|
||||
*/
|
||||
@Override
|
||||
public byte[] execute(String uri, AbstractWxMaQrcodeWrapper qrcodeWrapper, WxType wxType) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
httpPost.setConfig(
|
||||
RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build()
|
||||
);
|
||||
}
|
||||
|
||||
httpPost.setEntity(new StringEntity(qrcodeWrapper.toJson()));
|
||||
|
||||
try (final CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost);
|
||||
final InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response)) {
|
||||
Header[] contentTypeHeader = response.getHeaders("Content-Type");
|
||||
if (contentTypeHeader != null && contentTypeHeader.length > 0
|
||||
&& ContentType.APPLICATION_JSON.getMimeType()
|
||||
.equals(ContentType.parse(contentTypeHeader[0].getValue()).getMimeType())) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
throw new WxErrorException(WxError.fromJson(responseContent, wxType));
|
||||
}
|
||||
|
||||
return IOUtils.toByteArray(inputStream);
|
||||
} finally {
|
||||
httpPost.releaseConnection();
|
||||
}
|
||||
}
|
||||
}
|
@ -10,11 +10,13 @@ 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;
|
||||
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.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@ -23,15 +25,14 @@ import java.nio.file.Paths;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/gentryhuang">gentryhuang</a>
|
||||
*/
|
||||
public class QrcodeFileRequestExecutor extends QrcodeRequestExecutor {
|
||||
/**
|
||||
* 二维码生成的文件路径,例如: /var/temp
|
||||
* @author wenqiang
|
||||
* @since 2020/12/25
|
||||
*/
|
||||
public class ApacheQrcodeFileRequestExecutor extends QrcodeRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
|
||||
private final String filePath;
|
||||
|
||||
public QrcodeFileRequestExecutor(RequestHttp requestHttp, String filePath) {
|
||||
public ApacheQrcodeFileRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp, String filePath) {
|
||||
super(requestHttp);
|
||||
this.filePath = filePath;
|
||||
}
|
||||
@ -69,7 +70,6 @@ public class QrcodeFileRequestExecutor extends QrcodeRequestExecutor {
|
||||
if (StringUtils.isBlank(filePath)) {
|
||||
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
|
||||
}
|
||||
|
||||
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg", Paths.get(filePath).toFile());
|
||||
} finally {
|
||||
httpPost.releaseConnection();
|
@ -0,0 +1,68 @@
|
||||
package cn.binarywang.wx.miniapp.executor;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper;
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.HttpRequest;
|
||||
import jodd.http.HttpResponse;
|
||||
import jodd.http.ProxyInfo;
|
||||
import jodd.net.MimeTypes;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.fs.FileUtils;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
||||
import okhttp3.*;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author wenqiang
|
||||
* @since 2020/12/25
|
||||
*/
|
||||
public class JoddHttpQrcodeFileRequestExecutor extends QrcodeRequestExecutor<HttpConnectionProvider, ProxyInfo> {
|
||||
|
||||
private final String filePath;
|
||||
|
||||
public JoddHttpQrcodeFileRequestExecutor(RequestHttp<HttpConnectionProvider, ProxyInfo> requestHttp, String filePath) {
|
||||
super(requestHttp);
|
||||
this.filePath = filePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行http请求.
|
||||
*
|
||||
* @param uri uri
|
||||
* @param qrcodeWrapper 数据
|
||||
* @param wxType 微信模块类型
|
||||
* @return 响应结果
|
||||
* @throws WxErrorException 自定义异常
|
||||
* @throws IOException io异常
|
||||
*/
|
||||
@Override
|
||||
public File execute(String uri, AbstractWxMaQrcodeWrapper qrcodeWrapper, WxType wxType) throws WxErrorException, IOException {
|
||||
HttpRequest request = HttpRequest.get(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
|
||||
}
|
||||
request.withConnectionProvider(requestHttp.getRequestHttpClient());
|
||||
|
||||
HttpResponse response = request.send();
|
||||
response.charset(StandardCharsets.UTF_8.name());
|
||||
String contentTypeHeader = response.header("Content-Type");
|
||||
if (MimeTypes.MIME_TEXT_PLAIN.equals(contentTypeHeader)) {
|
||||
String responseContent = response.bodyText();
|
||||
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
|
||||
}
|
||||
try (InputStream inputStream = new ByteArrayInputStream(response.bodyBytes())) {
|
||||
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package cn.binarywang.wx.miniapp.executor;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
||||
import okhttp3.*;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* @author wenqiang
|
||||
* @since 2020/12/25
|
||||
*/
|
||||
public class OkHttpQrcodeBytesRequestExecutor extends QrcodeBytesRequestExecutor<OkHttpClient, OkHttpProxyInfo> {
|
||||
|
||||
|
||||
public OkHttpQrcodeBytesRequestExecutor(RequestHttp<OkHttpClient, OkHttpProxyInfo> requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行http请求.
|
||||
*
|
||||
* @param uri uri
|
||||
* @param qrcodeWrapper 数据
|
||||
* @param wxType 微信模块类型
|
||||
* @return 响应结果
|
||||
* @throws WxErrorException 自定义异常
|
||||
* @throws IOException io异常
|
||||
*/
|
||||
@Override
|
||||
public byte[] execute(String uri, AbstractWxMaQrcodeWrapper qrcodeWrapper, WxType wxType) throws WxErrorException, IOException {
|
||||
RequestBody body = RequestBody.Companion.create(qrcodeWrapper.toJson(), MediaType.parse("text/plain; charset=utf-8"));
|
||||
Request request = new Request.Builder().url(uri).post(body).build();
|
||||
Response response = requestHttp.getRequestHttpClient().newCall(request).execute();
|
||||
String contentTypeHeader = response.header("Content-Type");
|
||||
if ("text/plain".equals(contentTypeHeader)) {
|
||||
String responseContent = response.body().string();
|
||||
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP));
|
||||
}
|
||||
|
||||
try (InputStream inputStream = response.body().byteStream()) {
|
||||
return IOUtils.toByteArray(inputStream);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
package cn.binarywang.wx.miniapp.executor;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.fs.FileUtils;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
||||
import okhttp3.*;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author wenqiang
|
||||
* @since 2020/12/25
|
||||
*/
|
||||
public class OkHttpQrcodeFileRequestExecutor extends QrcodeRequestExecutor<OkHttpClient, OkHttpProxyInfo> {
|
||||
|
||||
private final String filePath;
|
||||
|
||||
public OkHttpQrcodeFileRequestExecutor(RequestHttp<OkHttpClient, OkHttpProxyInfo> requestHttp, String filePath) {
|
||||
super(requestHttp);
|
||||
this.filePath = filePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行http请求.
|
||||
*
|
||||
* @param uri uri
|
||||
* @param qrcodeWrapper 数据
|
||||
* @param wxType 微信模块类型
|
||||
* @return 响应结果
|
||||
* @throws WxErrorException 自定义异常
|
||||
* @throws IOException io异常
|
||||
*/
|
||||
@Override
|
||||
public File execute(String uri, AbstractWxMaQrcodeWrapper qrcodeWrapper, WxType wxType) throws WxErrorException, IOException {
|
||||
RequestBody body = RequestBody.Companion.create(qrcodeWrapper.toJson(), MediaType.parse("text/plain; charset=utf-8"));
|
||||
Request request = new Request.Builder().url(uri).post(body).build();
|
||||
Response response = requestHttp.getRequestHttpClient().newCall(request).execute();
|
||||
String contentTypeHeader = response.header("Content-Type");
|
||||
if ("text/plain".equals(contentTypeHeader)) {
|
||||
String responseContent = response.body().string();
|
||||
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP));
|
||||
}
|
||||
|
||||
try (InputStream inputStream = response.body().byteStream()) {
|
||||
if (StringUtils.isBlank(filePath)) {
|
||||
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
|
||||
}
|
||||
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg", Paths.get(filePath).toFile());
|
||||
}
|
||||
}
|
||||
}
|
@ -2,31 +2,19 @@ package cn.binarywang.wx.miniapp.executor;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.ResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
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.HttpPost;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
*/
|
||||
public class QrcodeBytesRequestExecutor implements RequestExecutor<byte[], AbstractWxMaQrcodeWrapper> {
|
||||
protected RequestHttp<CloseableHttpClient, HttpHost> requestHttp;
|
||||
public abstract class QrcodeBytesRequestExecutor<H, P> implements RequestExecutor<byte[], AbstractWxMaQrcodeWrapper> {
|
||||
|
||||
protected RequestHttp<H, P> requestHttp;
|
||||
|
||||
public QrcodeBytesRequestExecutor(RequestHttp requestHttp) {
|
||||
this.requestHttp = requestHttp;
|
||||
@ -37,30 +25,16 @@ public class QrcodeBytesRequestExecutor implements RequestExecutor<byte[], Abstr
|
||||
handler.handle(this.execute(uri, data, wxType));
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] execute(String uri, AbstractWxMaQrcodeWrapper qrcodeWrapper, WxType wxType) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
httpPost.setConfig(
|
||||
RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build()
|
||||
);
|
||||
}
|
||||
|
||||
httpPost.setEntity(new StringEntity(qrcodeWrapper.toJson()));
|
||||
|
||||
try (final CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost);
|
||||
final InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response)) {
|
||||
Header[] contentTypeHeader = response.getHeaders("Content-Type");
|
||||
if (contentTypeHeader != null && contentTypeHeader.length > 0
|
||||
&& ContentType.APPLICATION_JSON.getMimeType()
|
||||
.equals(ContentType.parse(contentTypeHeader[0].getValue()).getMimeType())) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
throw new WxErrorException(WxError.fromJson(responseContent, wxType));
|
||||
}
|
||||
|
||||
return IOUtils.toByteArray(inputStream);
|
||||
} finally {
|
||||
httpPost.releaseConnection();
|
||||
public static RequestExecutor<byte[], AbstractWxMaQrcodeWrapper> create(RequestHttp requestHttp) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
return new ApacheQrcodeBytesRequestExecutor(requestHttp);
|
||||
case JODD_HTTP:
|
||||
return null;
|
||||
case OK_HTTP:
|
||||
return new OkHttpQrcodeBytesRequestExecutor(requestHttp);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,35 +2,21 @@ package cn.binarywang.wx.miniapp.executor;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.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.ResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
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.HttpPost;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
*/
|
||||
public class QrcodeRequestExecutor implements RequestExecutor<File, AbstractWxMaQrcodeWrapper> {
|
||||
protected RequestHttp<CloseableHttpClient, HttpHost> requestHttp;
|
||||
public abstract class QrcodeRequestExecutor<H, P> implements RequestExecutor<File, AbstractWxMaQrcodeWrapper> {
|
||||
protected RequestHttp<H, P> requestHttp;
|
||||
|
||||
public QrcodeRequestExecutor(RequestHttp requestHttp) {
|
||||
public QrcodeRequestExecutor(RequestHttp<H, P> requestHttp) {
|
||||
this.requestHttp = requestHttp;
|
||||
}
|
||||
|
||||
@ -39,30 +25,30 @@ public class QrcodeRequestExecutor implements RequestExecutor<File, AbstractWxMa
|
||||
handler.handle(this.execute(uri, data, wxType));
|
||||
}
|
||||
|
||||
@Override
|
||||
public File execute(String uri, AbstractWxMaQrcodeWrapper qrcodeWrapper, WxType wxType) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
httpPost.setConfig(
|
||||
RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build()
|
||||
);
|
||||
|
||||
public static RequestExecutor<File, AbstractWxMaQrcodeWrapper> create(RequestHttp requestHttp, String path) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
return new ApacheQrcodeFileRequestExecutor(requestHttp, path);
|
||||
case JODD_HTTP:
|
||||
return null;
|
||||
case OK_HTTP:
|
||||
return new OkHttpQrcodeFileRequestExecutor(requestHttp, path);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
httpPost.setEntity(new StringEntity(qrcodeWrapper.toJson(), ContentType.APPLICATION_JSON));
|
||||
|
||||
try (final CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost);
|
||||
final InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response)) {
|
||||
Header[] contentTypeHeader = response.getHeaders("Content-Type");
|
||||
if (contentTypeHeader != null && contentTypeHeader.length > 0
|
||||
&& ContentType.APPLICATION_JSON.getMimeType()
|
||||
.equals(ContentType.parse(contentTypeHeader[0].getValue()).getMimeType())) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
throw new WxErrorException(WxError.fromJson(responseContent, wxType));
|
||||
}
|
||||
|
||||
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
|
||||
} finally {
|
||||
httpPost.releaseConnection();
|
||||
public static RequestExecutor<File, AbstractWxMaQrcodeWrapper> create(RequestHttp requestHttp) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
return new ApacheQrcodeFileRequestExecutor(requestHttp, null);
|
||||
case JODD_HTTP:
|
||||
return null;
|
||||
case OK_HTTP:
|
||||
return new OkHttpQrcodeFileRequestExecutor(requestHttp, null);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user