diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaQrcodeServiceImpl.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaQrcodeServiceImpl.java index 524be9fe9..9fb479394 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaQrcodeServiceImpl.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaQrcodeServiceImpl.java @@ -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)); } diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/ApacheQrcodeBytesRequestExecutor.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/ApacheQrcodeBytesRequestExecutor.java new file mode 100644 index 000000000..58c7beabe --- /dev/null +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/ApacheQrcodeBytesRequestExecutor.java @@ -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 { + + + public ApacheQrcodeBytesRequestExecutor(RequestHttp 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(); + } + } +} diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/QrcodeFileRequestExecutor.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/ApacheQrcodeFileRequestExecutor.java similarity index 88% rename from weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/QrcodeFileRequestExecutor.java rename to weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/ApacheQrcodeFileRequestExecutor.java index 6580678ef..c7f57b2f6 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/QrcodeFileRequestExecutor.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/ApacheQrcodeFileRequestExecutor.java @@ -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 gentryhuang + * @author wenqiang + * @since 2020/12/25 */ -public class QrcodeFileRequestExecutor extends QrcodeRequestExecutor { - /** - * 二维码生成的文件路径,例如: /var/temp - */ +public class ApacheQrcodeFileRequestExecutor extends QrcodeRequestExecutor { + private final String filePath; - public QrcodeFileRequestExecutor(RequestHttp requestHttp, String filePath) { + public ApacheQrcodeFileRequestExecutor(RequestHttp 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(); diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/JoddHttpQrcodeFileRequestExecutor.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/JoddHttpQrcodeFileRequestExecutor.java new file mode 100644 index 000000000..714a857ae --- /dev/null +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/JoddHttpQrcodeFileRequestExecutor.java @@ -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 { + + private final String filePath; + + public JoddHttpQrcodeFileRequestExecutor(RequestHttp 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"); + } + } +} diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/OkHttpQrcodeBytesRequestExecutor.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/OkHttpQrcodeBytesRequestExecutor.java new file mode 100644 index 000000000..9a1c677af --- /dev/null +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/OkHttpQrcodeBytesRequestExecutor.java @@ -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 { + + + public OkHttpQrcodeBytesRequestExecutor(RequestHttp 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); + } + } +} diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/OkHttpQrcodeFileRequestExecutor.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/OkHttpQrcodeFileRequestExecutor.java new file mode 100644 index 000000000..ff59c915a --- /dev/null +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/OkHttpQrcodeFileRequestExecutor.java @@ -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 { + + private final String filePath; + + public OkHttpQrcodeFileRequestExecutor(RequestHttp 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()); + } + } +} diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/QrcodeBytesRequestExecutor.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/QrcodeBytesRequestExecutor.java index ab2d262f2..a4a511256 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/QrcodeBytesRequestExecutor.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/QrcodeBytesRequestExecutor.java @@ -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 Binary Wang */ -public class QrcodeBytesRequestExecutor implements RequestExecutor { - protected RequestHttp requestHttp; +public abstract class QrcodeBytesRequestExecutor implements RequestExecutor { + + protected RequestHttp requestHttp; public QrcodeBytesRequestExecutor(RequestHttp requestHttp) { this.requestHttp = requestHttp; @@ -37,30 +25,16 @@ public class QrcodeBytesRequestExecutor implements RequestExecutor 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 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; } } } diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/QrcodeRequestExecutor.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/QrcodeRequestExecutor.java index 83e710dc1..cea0f9620 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/QrcodeRequestExecutor.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/executor/QrcodeRequestExecutor.java @@ -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 Binary Wang */ -public class QrcodeRequestExecutor implements RequestExecutor { - protected RequestHttp requestHttp; +public abstract class QrcodeRequestExecutor implements RequestExecutor { + protected RequestHttp requestHttp; - public QrcodeRequestExecutor(RequestHttp requestHttp) { + public QrcodeRequestExecutor(RequestHttp requestHttp) { this.requestHttp = requestHttp; } @@ -39,30 +25,30 @@ public class QrcodeRequestExecutor implements RequestExecutor 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 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; } } }