mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-10-30 01:58:23 +08:00
🆕 #1942 【小程序】增加生成小程序码的okhttp实现
This commit is contained in:
@ -7,7 +7,6 @@ import cn.binarywang.wx.miniapp.bean.WxMaQrcode;
|
|||||||
import cn.binarywang.wx.miniapp.bean.WxaCode;
|
import cn.binarywang.wx.miniapp.bean.WxaCode;
|
||||||
import cn.binarywang.wx.miniapp.bean.WxaCodeUnlimit;
|
import cn.binarywang.wx.miniapp.bean.WxaCodeUnlimit;
|
||||||
import cn.binarywang.wx.miniapp.executor.QrcodeBytesRequestExecutor;
|
import cn.binarywang.wx.miniapp.executor.QrcodeBytesRequestExecutor;
|
||||||
import cn.binarywang.wx.miniapp.executor.QrcodeFileRequestExecutor;
|
|
||||||
import cn.binarywang.wx.miniapp.executor.QrcodeRequestExecutor;
|
import cn.binarywang.wx.miniapp.executor.QrcodeRequestExecutor;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import me.chanjar.weixin.common.error.WxErrorException;
|
import me.chanjar.weixin.common.error.WxErrorException;
|
||||||
@ -23,14 +22,12 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] createQrcodeBytes(String path, int width) throws WxErrorException {
|
public byte[] createQrcodeBytes(String path, int width) throws WxErrorException {
|
||||||
final QrcodeBytesRequestExecutor executor = new QrcodeBytesRequestExecutor(this.wxMaService.getRequestHttp());
|
return this.wxMaService.execute(QrcodeBytesRequestExecutor.create(this.wxMaService.getRequestHttp()), CREATE_QRCODE_URL, new WxMaQrcode(path, width));
|
||||||
return this.wxMaService.execute(executor, CREATE_QRCODE_URL, new WxMaQrcode(path, width));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public File createQrcode(String path, int width) throws WxErrorException {
|
public File createQrcode(String path, int width) throws WxErrorException {
|
||||||
final QrcodeRequestExecutor executor = new QrcodeRequestExecutor(this.wxMaService.getRequestHttp());
|
return this.wxMaService.execute(QrcodeRequestExecutor.create(this.wxMaService.getRequestHttp()), CREATE_QRCODE_URL, new WxMaQrcode(path, width));
|
||||||
return this.wxMaService.execute(executor, CREATE_QRCODE_URL, new WxMaQrcode(path, width));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -41,8 +38,7 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
|
|||||||
@Override
|
@Override
|
||||||
public byte[] createWxaCodeBytes(String path, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline)
|
public byte[] createWxaCodeBytes(String path, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline)
|
||||||
throws WxErrorException {
|
throws WxErrorException {
|
||||||
final QrcodeBytesRequestExecutor executor = new QrcodeBytesRequestExecutor(this.wxMaService.getRequestHttp());
|
return this.wxMaService.execute(QrcodeBytesRequestExecutor.create(this.wxMaService.getRequestHttp()), GET_WXACODE_URL, WxaCode.builder()
|
||||||
return this.wxMaService.execute(executor, GET_WXACODE_URL, WxaCode.builder()
|
|
||||||
.path(path)
|
.path(path)
|
||||||
.width(width)
|
.width(width)
|
||||||
.autoColor(autoColor)
|
.autoColor(autoColor)
|
||||||
@ -54,8 +50,7 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
|
|||||||
@Override
|
@Override
|
||||||
public File createWxaCode(String path, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline)
|
public File createWxaCode(String path, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline)
|
||||||
throws WxErrorException {
|
throws WxErrorException {
|
||||||
final QrcodeRequestExecutor executor = new QrcodeRequestExecutor(this.wxMaService.getRequestHttp());
|
return this.wxMaService.execute(QrcodeRequestExecutor.create(this.wxMaService.getRequestHttp()), GET_WXACODE_URL, WxaCode.builder()
|
||||||
return this.wxMaService.execute(executor, GET_WXACODE_URL, WxaCode.builder()
|
|
||||||
.path(path)
|
.path(path)
|
||||||
.width(width)
|
.width(width)
|
||||||
.autoColor(autoColor)
|
.autoColor(autoColor)
|
||||||
@ -77,7 +72,7 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
|
|||||||
@Override
|
@Override
|
||||||
public byte[] createWxaCodeUnlimitBytes(String scene, String page, int width, boolean autoColor,
|
public byte[] createWxaCodeUnlimitBytes(String scene, String page, int width, boolean autoColor,
|
||||||
WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException {
|
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,
|
GET_WXACODE_UNLIMIT_URL,
|
||||||
this.buildWxaCodeUnlimit(scene, page, width, autoColor, lineColor, isHyaline));
|
this.buildWxaCodeUnlimit(scene, page, width, autoColor, lineColor, isHyaline));
|
||||||
}
|
}
|
||||||
@ -85,7 +80,7 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
|
|||||||
@Override
|
@Override
|
||||||
public File createWxaCodeUnlimit(String scene, String page, int width, boolean autoColor,
|
public File createWxaCodeUnlimit(String scene, String page, int width, boolean autoColor,
|
||||||
WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException {
|
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,
|
GET_WXACODE_UNLIMIT_URL,
|
||||||
this.buildWxaCodeUnlimit(scene, page, width, autoColor, lineColor, isHyaline));
|
this.buildWxaCodeUnlimit(scene, page, width, autoColor, lineColor, isHyaline));
|
||||||
}
|
}
|
||||||
@ -110,8 +105,7 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public File createQrcode(String path, int width, String filePath) throws WxErrorException {
|
public File createQrcode(String path, int width, String filePath) throws WxErrorException {
|
||||||
final QrcodeFileRequestExecutor executor = new QrcodeFileRequestExecutor(this.wxMaService.getRequestHttp(), filePath);
|
return this.wxMaService.execute(QrcodeRequestExecutor.create(this.wxMaService.getRequestHttp(), filePath), CREATE_QRCODE_URL, new WxMaQrcode(path, width));
|
||||||
return this.wxMaService.execute(executor, CREATE_QRCODE_URL, new WxMaQrcode(path, width));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -122,8 +116,7 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
|
|||||||
@Override
|
@Override
|
||||||
public File createWxaCode(String path, int width, String filePath, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline)
|
public File createWxaCode(String path, int width, String filePath, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline)
|
||||||
throws WxErrorException {
|
throws WxErrorException {
|
||||||
final QrcodeFileRequestExecutor executor = new QrcodeFileRequestExecutor(this.wxMaService.getRequestHttp(), filePath);
|
return this.wxMaService.execute(QrcodeRequestExecutor.create(this.wxMaService.getRequestHttp(), filePath), GET_WXACODE_URL, WxaCode.builder()
|
||||||
return this.wxMaService.execute(executor, GET_WXACODE_URL, WxaCode.builder()
|
|
||||||
.path(path)
|
.path(path)
|
||||||
.width(width)
|
.width(width)
|
||||||
.autoColor(autoColor)
|
.autoColor(autoColor)
|
||||||
@ -145,7 +138,7 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
|
|||||||
@Override
|
@Override
|
||||||
public File createWxaCodeUnlimit(String scene, String page, String filePath, int width, boolean autoColor,
|
public File createWxaCodeUnlimit(String scene, String page, String filePath, int width, boolean autoColor,
|
||||||
WxMaCodeLineColor lineColor, boolean isHyaline) throws WxErrorException {
|
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,
|
GET_WXACODE_UNLIMIT_URL,
|
||||||
this.buildWxaCodeUnlimit(scene, page, width, autoColor, lineColor, isHyaline));
|
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 me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.http.Header;
|
import org.apache.http.Header;
|
||||||
|
import org.apache.http.HttpHost;
|
||||||
import org.apache.http.client.config.RequestConfig;
|
import org.apache.http.client.config.RequestConfig;
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.client.methods.HttpPost;
|
||||||
import org.apache.http.entity.ContentType;
|
import org.apache.http.entity.ContentType;
|
||||||
import org.apache.http.entity.StringEntity;
|
import org.apache.http.entity.StringEntity;
|
||||||
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -23,15 +25,14 @@ import java.nio.file.Paths;
|
|||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author <a href="https://github.com/gentryhuang">gentryhuang</a>
|
* @author wenqiang
|
||||||
|
* @since 2020/12/25
|
||||||
*/
|
*/
|
||||||
public class QrcodeFileRequestExecutor extends QrcodeRequestExecutor {
|
public class ApacheQrcodeFileRequestExecutor extends QrcodeRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||||
/**
|
|
||||||
* 二维码生成的文件路径,例如: /var/temp
|
|
||||||
*/
|
|
||||||
private final String filePath;
|
private final String filePath;
|
||||||
|
|
||||||
public QrcodeFileRequestExecutor(RequestHttp requestHttp, String filePath) {
|
public ApacheQrcodeFileRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp, String filePath) {
|
||||||
super(requestHttp);
|
super(requestHttp);
|
||||||
this.filePath = filePath;
|
this.filePath = filePath;
|
||||||
}
|
}
|
||||||
@ -69,7 +70,6 @@ public class QrcodeFileRequestExecutor extends QrcodeRequestExecutor {
|
|||||||
if (StringUtils.isBlank(filePath)) {
|
if (StringUtils.isBlank(filePath)) {
|
||||||
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
|
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
|
||||||
}
|
}
|
||||||
|
|
||||||
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg", Paths.get(filePath).toFile());
|
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg", Paths.get(filePath).toFile());
|
||||||
} finally {
|
} finally {
|
||||||
httpPost.releaseConnection();
|
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 cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper;
|
||||||
import me.chanjar.weixin.common.enums.WxType;
|
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.error.WxErrorException;
|
||||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||||
import me.chanjar.weixin.common.util.http.ResponseHandler;
|
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.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||||
*/
|
*/
|
||||||
public class QrcodeBytesRequestExecutor implements RequestExecutor<byte[], AbstractWxMaQrcodeWrapper> {
|
public abstract class QrcodeBytesRequestExecutor<H, P> implements RequestExecutor<byte[], AbstractWxMaQrcodeWrapper> {
|
||||||
protected RequestHttp<CloseableHttpClient, HttpHost> requestHttp;
|
|
||||||
|
protected RequestHttp<H, P> requestHttp;
|
||||||
|
|
||||||
public QrcodeBytesRequestExecutor(RequestHttp requestHttp) {
|
public QrcodeBytesRequestExecutor(RequestHttp requestHttp) {
|
||||||
this.requestHttp = requestHttp;
|
this.requestHttp = requestHttp;
|
||||||
@ -37,30 +25,16 @@ public class QrcodeBytesRequestExecutor implements RequestExecutor<byte[], Abstr
|
|||||||
handler.handle(this.execute(uri, data, wxType));
|
handler.handle(this.execute(uri, data, wxType));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public static RequestExecutor<byte[], AbstractWxMaQrcodeWrapper> create(RequestHttp requestHttp) {
|
||||||
public byte[] execute(String uri, AbstractWxMaQrcodeWrapper qrcodeWrapper, WxType wxType) throws WxErrorException, IOException {
|
switch (requestHttp.getRequestType()) {
|
||||||
HttpPost httpPost = new HttpPost(uri);
|
case APACHE_HTTP:
|
||||||
if (requestHttp.getRequestHttpProxy() != null) {
|
return new ApacheQrcodeBytesRequestExecutor(requestHttp);
|
||||||
httpPost.setConfig(
|
case JODD_HTTP:
|
||||||
RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build()
|
return null;
|
||||||
);
|
case OK_HTTP:
|
||||||
}
|
return new OkHttpQrcodeBytesRequestExecutor(requestHttp);
|
||||||
|
default:
|
||||||
httpPost.setEntity(new StringEntity(qrcodeWrapper.toJson()));
|
return null;
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,35 +2,21 @@ package cn.binarywang.wx.miniapp.executor;
|
|||||||
|
|
||||||
import cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper;
|
import cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper;
|
||||||
import me.chanjar.weixin.common.enums.WxType;
|
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.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.RequestExecutor;
|
||||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||||
import me.chanjar.weixin.common.util.http.ResponseHandler;
|
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.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||||
*/
|
*/
|
||||||
public class QrcodeRequestExecutor implements RequestExecutor<File, AbstractWxMaQrcodeWrapper> {
|
public abstract class QrcodeRequestExecutor<H, P> implements RequestExecutor<File, AbstractWxMaQrcodeWrapper> {
|
||||||
protected RequestHttp<CloseableHttpClient, HttpHost> requestHttp;
|
protected RequestHttp<H, P> requestHttp;
|
||||||
|
|
||||||
public QrcodeRequestExecutor(RequestHttp requestHttp) {
|
public QrcodeRequestExecutor(RequestHttp<H, P> requestHttp) {
|
||||||
this.requestHttp = requestHttp;
|
this.requestHttp = requestHttp;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,30 +25,30 @@ public class QrcodeRequestExecutor implements RequestExecutor<File, AbstractWxMa
|
|||||||
handler.handle(this.execute(uri, data, wxType));
|
handler.handle(this.execute(uri, data, wxType));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public File execute(String uri, AbstractWxMaQrcodeWrapper qrcodeWrapper, WxType wxType) throws WxErrorException, IOException {
|
public static RequestExecutor<File, AbstractWxMaQrcodeWrapper> create(RequestHttp requestHttp, String path) {
|
||||||
HttpPost httpPost = new HttpPost(uri);
|
switch (requestHttp.getRequestType()) {
|
||||||
if (requestHttp.getRequestHttpProxy() != null) {
|
case APACHE_HTTP:
|
||||||
httpPost.setConfig(
|
return new ApacheQrcodeFileRequestExecutor(requestHttp, path);
|
||||||
RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build()
|
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));
|
public static RequestExecutor<File, AbstractWxMaQrcodeWrapper> create(RequestHttp requestHttp) {
|
||||||
|
switch (requestHttp.getRequestType()) {
|
||||||
try (final CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost);
|
case APACHE_HTTP:
|
||||||
final InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response)) {
|
return new ApacheQrcodeFileRequestExecutor(requestHttp, null);
|
||||||
Header[] contentTypeHeader = response.getHeaders("Content-Type");
|
case JODD_HTTP:
|
||||||
if (contentTypeHeader != null && contentTypeHeader.length > 0
|
return null;
|
||||||
&& ContentType.APPLICATION_JSON.getMimeType()
|
case OK_HTTP:
|
||||||
.equals(ContentType.parse(contentTypeHeader[0].getValue()).getMimeType())) {
|
return new OkHttpQrcodeFileRequestExecutor(requestHttp, null);
|
||||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
default:
|
||||||
throw new WxErrorException(WxError.fromJson(responseContent, wxType));
|
return null;
|
||||||
}
|
|
||||||
|
|
||||||
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
|
|
||||||
} finally {
|
|
||||||
httpPost.releaseConnection();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user