mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-11-02 12:38:22 +08:00
🎨 #1189 优化错误异常输出,移除冗余代码
This commit is contained in:
@ -7,6 +7,7 @@ import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.WxType;
|
||||
import me.chanjar.weixin.common.bean.WxJsapiSignature;
|
||||
import me.chanjar.weixin.common.bean.WxNetCheckResult;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
@ -180,7 +181,7 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
|
||||
private WxMpOAuth2AccessToken getOAuth2AccessToken(String url) throws WxErrorException {
|
||||
try {
|
||||
RequestExecutor<String, String> executor = SimpleGetRequestExecutor.create(this);
|
||||
String responseText = executor.execute(url, null);
|
||||
String responseText = executor.execute(url, null, WxType.MP);
|
||||
return WxMpOAuth2AccessToken.fromJson(responseText);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
@ -210,7 +211,7 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
|
||||
|
||||
try {
|
||||
RequestExecutor<String, String> executor = SimpleGetRequestExecutor.create(this);
|
||||
String responseText = executor.execute(url, null);
|
||||
String responseText = executor.execute(url, null, WxType.MP);
|
||||
return WxMpUser.fromJson(responseText);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
@ -222,7 +223,7 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
|
||||
String url = String.format(OAUTH2_VALIDATE_TOKEN_URL.getUrl(this.getWxMpConfigStorage()), token.getAccessToken(), token.getOpenId());
|
||||
|
||||
try {
|
||||
SimpleGetRequestExecutor.create(this).execute(url, null);
|
||||
SimpleGetRequestExecutor.create(this).execute(url, null, WxType.MP);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (WxErrorException e) {
|
||||
@ -333,11 +334,10 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
|
||||
}
|
||||
|
||||
String accessToken = getAccessToken(false);
|
||||
|
||||
String uriWithAccessToken = uri + (uri.contains("?") ? "&" : "?") + "access_token=" + accessToken;
|
||||
|
||||
try {
|
||||
T result = executor.execute(uriWithAccessToken, data);
|
||||
T result = executor.execute(uriWithAccessToken, data, WxType.MP);
|
||||
log.debug("\n【请求地址】: {}\n【请求参数】:{}\n【响应数据】:{}", uriWithAccessToken, dataForLog, result);
|
||||
return result;
|
||||
} catch (WxErrorException e) {
|
||||
|
||||
@ -26,7 +26,7 @@ public class MaterialDeleteApacheHttpRequestExecutor extends MaterialDeleteReque
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean execute(String uri, String materialId) throws WxErrorException, IOException {
|
||||
public Boolean execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
|
||||
|
||||
@ -22,7 +22,7 @@ public class MaterialDeleteJoddHttpRequestExecutor extends MaterialDeleteRequest
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean execute(String uri, String materialId) throws WxErrorException, IOException {
|
||||
public Boolean execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
|
||||
HttpRequest request = HttpRequest.post(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
|
||||
|
||||
@ -28,12 +28,12 @@ public class MaterialDeleteOkhttpRequestExecutor extends MaterialDeleteRequestEx
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String uri, String data, ResponseHandler<Boolean> handler) throws WxErrorException, IOException {
|
||||
handler.handle(this.execute(uri, data));
|
||||
public void execute(String uri, String data, ResponseHandler<Boolean> handler, WxType wxType) throws WxErrorException, IOException {
|
||||
handler.handle(this.execute(uri, data, wxType));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean execute(String uri, String materialId) throws WxErrorException, IOException {
|
||||
public Boolean execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
|
||||
logger.debug("MaterialDeleteOkhttpRequestExecutor is running");
|
||||
//得到httpClient
|
||||
OkHttpClient client = requestHttp.getRequestHttpClient();
|
||||
|
||||
@ -2,6 +2,7 @@ package me.chanjar.weixin.mp.util.requestexecuter.material;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import me.chanjar.weixin.common.WxType;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
@ -15,8 +16,8 @@ public abstract class MaterialDeleteRequestExecutor<H, P> implements RequestExec
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String uri, String data, ResponseHandler<Boolean> handler) throws WxErrorException, IOException {
|
||||
handler.handle(this.execute(uri, data));
|
||||
public void execute(String uri, String data, ResponseHandler<Boolean> handler, WxType wxType) throws WxErrorException, IOException {
|
||||
handler.handle(this.execute(uri, data, wxType));
|
||||
}
|
||||
|
||||
public static RequestExecutor<Boolean, String> create(RequestHttp requestHttp) {
|
||||
|
||||
@ -35,7 +35,7 @@ public class MaterialNewsInfoApacheHttpRequestExecutor
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpMaterialNews execute(String uri, String materialId) throws WxErrorException, IOException {
|
||||
public WxMpMaterialNews execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
|
||||
|
||||
@ -29,7 +29,7 @@ public class MaterialNewsInfoJoddHttpRequestExecutor extends MaterialNewsInfoReq
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpMaterialNews execute(String uri, String materialId) throws WxErrorException, IOException {
|
||||
public WxMpMaterialNews execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package me.chanjar.weixin.mp.util.requestexecuter.material;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
@ -10,24 +11,23 @@ import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews;
|
||||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
|
||||
import okhttp3.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/5/5.
|
||||
* .
|
||||
*
|
||||
* @author ecoolper
|
||||
* @date 2017/5/5
|
||||
*/
|
||||
@Slf4j
|
||||
public class MaterialNewsInfoOkhttpRequestExecutor extends MaterialNewsInfoRequestExecutor<OkHttpClient, OkHttpProxyInfo> {
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
public MaterialNewsInfoOkhttpRequestExecutor(RequestHttp requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpMaterialNews execute(String uri, String materialId) throws WxErrorException, IOException {
|
||||
|
||||
//得到httpClient
|
||||
public WxMpMaterialNews execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
|
||||
OkHttpClient client = requestHttp.getRequestHttpClient();
|
||||
|
||||
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"),
|
||||
@ -36,7 +36,7 @@ public class MaterialNewsInfoOkhttpRequestExecutor extends MaterialNewsInfoReque
|
||||
|
||||
Response response = client.newCall(request).execute();
|
||||
String responseContent = response.body().string();
|
||||
this.logger.debug("响应原始数据:{}", responseContent);
|
||||
log.debug("响应原始数据:{}", responseContent);
|
||||
|
||||
WxError error = WxError.fromJson(responseContent, WxType.MP);
|
||||
if (error.getErrorCode() != 0) {
|
||||
|
||||
@ -2,6 +2,7 @@ package me.chanjar.weixin.mp.util.requestexecuter.material;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import me.chanjar.weixin.common.WxType;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
@ -16,8 +17,8 @@ public abstract class MaterialNewsInfoRequestExecutor<H, P> implements RequestEx
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String uri, String data, ResponseHandler<WxMpMaterialNews> handler) throws WxErrorException, IOException {
|
||||
handler.handle(this.execute(uri, data));
|
||||
public void execute(String uri, String data, ResponseHandler<WxMpMaterialNews> handler, WxType wxType) throws WxErrorException, IOException {
|
||||
handler.handle(this.execute(uri, data, wxType));
|
||||
}
|
||||
|
||||
public static RequestExecutor<WxMpMaterialNews, String> create(RequestHttp requestHttp) {
|
||||
|
||||
@ -33,7 +33,7 @@ public class MaterialUploadApacheHttpRequestExecutor extends MaterialUploadReque
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpMaterialUploadResult execute(String uri, WxMpMaterial material) throws WxErrorException, IOException {
|
||||
public WxMpMaterialUploadResult execute(String uri, WxMpMaterial material, WxType wxType) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
RequestConfig response = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
|
||||
|
||||
@ -28,7 +28,7 @@ public class MaterialUploadJoddHttpRequestExecutor extends MaterialUploadRequest
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpMaterialUploadResult execute(String uri, WxMpMaterial material) throws WxErrorException, IOException {
|
||||
public WxMpMaterialUploadResult execute(String uri, WxMpMaterial material, WxType wxType) throws WxErrorException, IOException {
|
||||
HttpRequest request = HttpRequest.post(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
|
||||
|
||||
@ -28,7 +28,7 @@ public class MaterialUploadOkhttpRequestExecutor extends MaterialUploadRequestEx
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpMaterialUploadResult execute(String uri, WxMpMaterial material) throws WxErrorException, IOException {
|
||||
public WxMpMaterialUploadResult execute(String uri, WxMpMaterial material, WxType wxType) throws WxErrorException, IOException {
|
||||
logger.debug("MaterialUploadOkhttpRequestExecutor is running");
|
||||
if (material == null) {
|
||||
throw new WxErrorException(WxError.builder().errorCode(-1).errorMsg("非法请求,material参数为空").build());
|
||||
|
||||
@ -2,6 +2,7 @@ package me.chanjar.weixin.mp.util.requestexecuter.material;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import me.chanjar.weixin.common.WxType;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
@ -20,8 +21,8 @@ public abstract class MaterialUploadRequestExecutor<H, P> implements RequestExec
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String uri, WxMpMaterial data, ResponseHandler<WxMpMaterialUploadResult> handler) throws WxErrorException, IOException {
|
||||
handler.handle(this.execute(uri, data));
|
||||
public void execute(String uri, WxMpMaterial data, ResponseHandler<WxMpMaterialUploadResult> handler, WxType wxType) throws WxErrorException, IOException {
|
||||
handler.handle(this.execute(uri, data, wxType));
|
||||
}
|
||||
|
||||
public static RequestExecutor<WxMpMaterialUploadResult, WxMpMaterial> create(RequestHttp requestHttp) {
|
||||
|
||||
@ -27,7 +27,7 @@ public class MaterialVideoInfoApacheHttpRequestExecutor extends MaterialVideoInf
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpMaterialVideoInfoResult execute(String uri, String materialId) throws WxErrorException, IOException {
|
||||
public WxMpMaterialVideoInfoResult execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
|
||||
|
||||
@ -23,7 +23,7 @@ public class MaterialVideoInfoJoddHttpRequestExecutor extends MaterialVideoInfoR
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpMaterialVideoInfoResult execute(String uri, String materialId) throws WxErrorException, IOException {
|
||||
public WxMpMaterialVideoInfoResult execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
|
||||
HttpRequest request = HttpRequest.post(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
|
||||
|
||||
@ -23,7 +23,7 @@ public class MaterialVideoInfoOkhttpRequestExecutor extends MaterialVideoInfoReq
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpMaterialVideoInfoResult execute(String uri, String materialId) throws WxErrorException, IOException {
|
||||
public WxMpMaterialVideoInfoResult execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
|
||||
logger.debug("MaterialVideoInfoOkhttpRequestExecutor is running");
|
||||
//得到httpClient
|
||||
OkHttpClient client = requestHttp.getRequestHttpClient();
|
||||
|
||||
@ -3,6 +3,7 @@ package me.chanjar.weixin.mp.util.requestexecuter.material;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import me.chanjar.weixin.common.WxType;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
@ -17,8 +18,8 @@ public abstract class MaterialVideoInfoRequestExecutor<H, P> implements RequestE
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String uri, String data, ResponseHandler<WxMpMaterialVideoInfoResult> handler) throws WxErrorException, IOException {
|
||||
handler.handle(this.execute(uri, data));
|
||||
public void execute(String uri, String data, ResponseHandler<WxMpMaterialVideoInfoResult> handler, WxType wxType) throws WxErrorException, IOException {
|
||||
handler.handle(this.execute(uri, data, wxType));
|
||||
}
|
||||
|
||||
public static RequestExecutor<WxMpMaterialVideoInfoResult, String> create(RequestHttp requestHttp) {
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package me.chanjar.weixin.mp.util.requestexecuter.material;
|
||||
|
||||
import me.chanjar.weixin.common.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
@ -30,7 +31,7 @@ public class MaterialVoiceAndImageDownloadApacheHttpRequestExecutor extends Mate
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream execute(String uri, String materialId) throws WxErrorException, IOException {
|
||||
public InputStream execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
|
||||
|
||||
@ -6,6 +6,7 @@ import jodd.http.HttpResponse;
|
||||
import jodd.http.ProxyInfo;
|
||||
import jodd.util.StringPool;
|
||||
|
||||
import me.chanjar.weixin.common.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
@ -27,7 +28,7 @@ public class MaterialVoiceAndImageDownloadJoddHttpRequestExecutor extends Materi
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream execute(String uri, String materialId) throws WxErrorException, IOException {
|
||||
public InputStream execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
|
||||
HttpRequest request = HttpRequest.post(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
|
||||
|
||||
@ -24,7 +24,7 @@ public class MaterialVoiceAndImageDownloadOkhttpRequestExecutor extends Material
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream execute(String uri, String materialId) throws WxErrorException, IOException {
|
||||
public InputStream execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
|
||||
logger.debug("MaterialVoiceAndImageDownloadOkhttpRequestExecutor is running");
|
||||
OkHttpClient client = requestHttp.getRequestHttpClient();
|
||||
RequestBody requestBody = new FormBody.Builder().add("media_id", materialId).build();
|
||||
|
||||
@ -4,6 +4,7 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import me.chanjar.weixin.common.WxType;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
@ -19,8 +20,8 @@ public abstract class MaterialVoiceAndImageDownloadRequestExecutor<H, P> impleme
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String uri, String data, ResponseHandler<InputStream> handler) throws WxErrorException, IOException {
|
||||
handler.handle(this.execute(uri, data));
|
||||
public void execute(String uri, String data, ResponseHandler<InputStream> handler, WxType wxType) throws WxErrorException, IOException {
|
||||
handler.handle(this.execute(uri, data, wxType));
|
||||
}
|
||||
|
||||
public static RequestExecutor<InputStream, String> create(RequestHttp requestHttp, File tmpDirFile) {
|
||||
|
||||
@ -30,7 +30,7 @@ public class MediaImgUploadApacheHttpRequestExecutor extends MediaImgUploadReque
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMediaImgUploadResult execute(String uri, File data) throws WxErrorException, IOException {
|
||||
public WxMediaImgUploadResult execute(String uri, File data, WxType wxType) throws WxErrorException, IOException {
|
||||
if (data == null) {
|
||||
throw new WxErrorException(WxError.builder().errorCode(-1).errorMsg("文件对象为空").build());
|
||||
}
|
||||
|
||||
@ -26,7 +26,7 @@ public class MediaImgUploadHttpRequestExecutor extends MediaImgUploadRequestExec
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMediaImgUploadResult execute(String uri, File data) throws WxErrorException, IOException {
|
||||
public WxMediaImgUploadResult execute(String uri, File data, WxType wxType) throws WxErrorException, IOException {
|
||||
if (data == null) {
|
||||
throw new WxErrorException(WxError.builder().errorCode(-1).errorMsg("文件对象为空").build());
|
||||
}
|
||||
|
||||
@ -26,7 +26,7 @@ public class MediaImgUploadOkhttpRequestExecutor extends MediaImgUploadRequestEx
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMediaImgUploadResult execute(String uri, File file) throws WxErrorException, IOException {
|
||||
public WxMediaImgUploadResult execute(String uri, File file, WxType wxType) throws WxErrorException, IOException {
|
||||
logger.debug("MediaImgUploadOkhttpRequestExecutor is running");
|
||||
//得到httpClient
|
||||
OkHttpClient client = requestHttp.getRequestHttpClient();
|
||||
|
||||
@ -3,6 +3,7 @@ package me.chanjar.weixin.mp.util.requestexecuter.media;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import me.chanjar.weixin.common.WxType;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
@ -20,8 +21,8 @@ public abstract class MediaImgUploadRequestExecutor<H, P> implements RequestExec
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String uri, File data, ResponseHandler<WxMediaImgUploadResult> handler) throws WxErrorException, IOException {
|
||||
handler.handle(this.execute(uri, data));
|
||||
public void execute(String uri, File data, ResponseHandler<WxMediaImgUploadResult> handler, WxType wxType) throws WxErrorException, IOException {
|
||||
handler.handle(this.execute(uri, data, wxType));
|
||||
}
|
||||
|
||||
public static RequestExecutor<WxMediaImgUploadResult, File> create(RequestHttp requestHttp) {
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package me.chanjar.weixin.mp.util.requestexecuter.ocr;
|
||||
|
||||
import me.chanjar.weixin.common.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
@ -17,41 +18,40 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author : zhayueran
|
||||
* @Date: 2019/6/27 14:06
|
||||
* @Description:
|
||||
* @date 2019/6/27 14:06
|
||||
*/
|
||||
public class OcrDiscernApacheHttpRequestExecutor extends OcrDiscernRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
|
||||
|
||||
public OcrDiscernApacheHttpRequestExecutor(RequestHttp requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String execute(String uri, File file) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
|
||||
httpPost.setConfig(config);
|
||||
}
|
||||
if (file != null) {
|
||||
HttpEntity entity = MultipartEntityBuilder
|
||||
.create()
|
||||
.addBinaryBody("file", file)
|
||||
.setMode(HttpMultipartMode.RFC6532)
|
||||
.build();
|
||||
httpPost.setEntity(entity);
|
||||
}
|
||||
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return responseContent;
|
||||
} finally {
|
||||
httpPost.releaseConnection();
|
||||
}
|
||||
public String execute(String uri, File file, WxType wxType) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
|
||||
httpPost.setConfig(config);
|
||||
}
|
||||
if (file != null) {
|
||||
HttpEntity entity = MultipartEntityBuilder
|
||||
.create()
|
||||
.addBinaryBody("file", file)
|
||||
.setMode(HttpMultipartMode.RFC6532)
|
||||
.build();
|
||||
httpPost.setEntity(entity);
|
||||
}
|
||||
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
WxError error = WxError.fromJson(responseContent, wxType);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return responseContent;
|
||||
} finally {
|
||||
httpPost.releaseConnection();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package me.chanjar.weixin.mp.util.requestexecuter.ocr;
|
||||
|
||||
import me.chanjar.weixin.common.WxType;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
@ -9,28 +10,29 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author : zhayueran
|
||||
* @Date: 2019/6/27 15:06
|
||||
* @Description:
|
||||
* .
|
||||
*
|
||||
* @author zhayueran
|
||||
* @date 2019/6/27 15:06
|
||||
*/
|
||||
public abstract class OcrDiscernRequestExecutor<H, P> implements RequestExecutor<String, File> {
|
||||
protected RequestHttp<H, P> requestHttp;
|
||||
protected RequestHttp<H, P> requestHttp;
|
||||
|
||||
public OcrDiscernRequestExecutor(RequestHttp requestHttp) {
|
||||
this.requestHttp = requestHttp;
|
||||
}
|
||||
public OcrDiscernRequestExecutor(RequestHttp requestHttp) {
|
||||
this.requestHttp = requestHttp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String uri, File data, ResponseHandler<String> handler) throws WxErrorException, IOException {
|
||||
handler.handle(this.execute(uri, data));
|
||||
}
|
||||
@Override
|
||||
public void execute(String uri, File data, ResponseHandler<String> handler, WxType wxType) throws WxErrorException, IOException {
|
||||
handler.handle(this.execute(uri, data, wxType));
|
||||
}
|
||||
|
||||
public static RequestExecutor<String, File> create(RequestHttp requestHttp) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
return new OcrDiscernApacheHttpRequestExecutor(requestHttp);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
public static RequestExecutor<String, File> create(RequestHttp requestHttp) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
return new OcrDiscernApacheHttpRequestExecutor(requestHttp);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -31,7 +31,7 @@ public class QrCodeApacheHttpRequestExecutor extends QrCodeRequestExecutor<Close
|
||||
}
|
||||
|
||||
@Override
|
||||
public File execute(String uri, WxMpQrCodeTicket ticket) throws WxErrorException, IOException {
|
||||
public File execute(String uri, WxMpQrCodeTicket ticket, WxType wxType) throws WxErrorException, IOException {
|
||||
if (ticket != null) {
|
||||
if (uri.indexOf('?') == -1) {
|
||||
uri += '?';
|
||||
|
||||
@ -30,7 +30,7 @@ public class QrCodeJoddHttpRequestExecutor extends QrCodeRequestExecutor<HttpCon
|
||||
}
|
||||
|
||||
@Override
|
||||
public File execute(String uri, WxMpQrCodeTicket ticket) throws WxErrorException, IOException {
|
||||
public File execute(String uri, WxMpQrCodeTicket ticket, WxType wxType) throws WxErrorException, IOException {
|
||||
if (ticket != null) {
|
||||
if (uri.indexOf('?') == -1) {
|
||||
uri += '?';
|
||||
|
||||
@ -32,7 +32,7 @@ public class QrCodeOkhttpRequestExecutor extends QrCodeRequestExecutor<OkHttpCli
|
||||
}
|
||||
|
||||
@Override
|
||||
public File execute(String uri, WxMpQrCodeTicket ticket) throws WxErrorException, IOException {
|
||||
public File execute(String uri, WxMpQrCodeTicket ticket, WxType wxType) throws WxErrorException, IOException {
|
||||
logger.debug("QrCodeOkhttpRequestExecutor is running");
|
||||
|
||||
if (ticket != null) {
|
||||
|
||||
@ -3,6 +3,7 @@ package me.chanjar.weixin.mp.util.requestexecuter.qrcode;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import me.chanjar.weixin.common.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
@ -23,8 +24,8 @@ public abstract class QrCodeRequestExecutor<H, P> implements RequestExecutor<Fil
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String uri, WxMpQrCodeTicket data, ResponseHandler<File> handler) throws WxErrorException, IOException {
|
||||
handler.handle(this.execute(uri, data));
|
||||
public void execute(String uri, WxMpQrCodeTicket data, ResponseHandler<File> handler, WxType wxType) throws WxErrorException, IOException {
|
||||
handler.handle(this.execute(uri, data, wxType));
|
||||
}
|
||||
|
||||
public static RequestExecutor<File, WxMpQrCodeTicket> create(RequestHttp requestHttp) throws WxErrorException {
|
||||
|
||||
@ -31,7 +31,7 @@ public class VoiceUploadApacheHttpRequestExecutor extends VoiceUploadRequestExec
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean execute(String uri, File data) throws WxErrorException, IOException {
|
||||
public Boolean execute(String uri, File data, WxType wxType) throws WxErrorException, IOException {
|
||||
if (data == null) {
|
||||
throw new WxErrorException(WxError.builder().errorCode(-1).errorMsg("文件对象为空").build());
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ package me.chanjar.weixin.mp.util.requestexecuter.voice;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import me.chanjar.weixin.common.WxType;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
@ -23,8 +24,8 @@ public abstract class VoiceUploadRequestExecutor<H, P> implements RequestExecuto
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String uri, File data, ResponseHandler<Boolean> handler) throws WxErrorException, IOException {
|
||||
handler.handle(this.execute(uri, data));
|
||||
public void execute(String uri, File data, ResponseHandler<Boolean> handler, WxType wxType) throws WxErrorException, IOException {
|
||||
handler.handle(this.execute(uri, data, wxType));
|
||||
}
|
||||
|
||||
public static RequestExecutor<Boolean, File> create(RequestHttp requestHttp) {
|
||||
|
||||
Reference in New Issue
Block a user