装饰模式实现
This commit is contained in:
@@ -6,6 +6,7 @@ import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.session.WxSession;
|
||||
import me.chanjar.weixin.common.session.WxSessionManager;
|
||||
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.cp.bean.WxCpDepart;
|
||||
import me.chanjar.weixin.cp.bean.WxCpMessage;
|
||||
@@ -20,7 +21,7 @@ import java.util.List;
|
||||
/**
|
||||
* 微信API的Service
|
||||
*/
|
||||
public interface WxCpService<H, P> {
|
||||
public interface WxCpService {
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
@@ -469,7 +470,7 @@ public interface WxCpService<H, P> {
|
||||
* <pre>
|
||||
* Service没有实现某个API的时候,可以用这个,
|
||||
* 比{@link #get}和{@link #post}方法更灵活,可以自己构造RequestExecutor用来处理不同的参数和不同的返回类型。
|
||||
* 可以参考,{@link me.chanjar.weixin.common.util.http.jodd.MediaUploadRequestExecutor}的实现方法
|
||||
* 可以参考,{@link MediaUploadRequestExecutor}的实现方法
|
||||
* </pre>
|
||||
*
|
||||
* @param executor 执行器
|
||||
@@ -478,9 +479,7 @@ public interface WxCpService<H, P> {
|
||||
* @param <T> 请求值类型
|
||||
* @param <E> 返回值类型
|
||||
*/
|
||||
<T, E> T execute(RequestExecutor<T, H, P, E> executor, String uri, E data) throws WxErrorException;
|
||||
|
||||
<T, E> T executeInternal(RequestExecutor<T, H, P, E> executor, String uri, E data) throws WxErrorException;
|
||||
<T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 注入 {@link WxCpConfigStorage} 的实现
|
||||
|
||||
@@ -14,9 +14,9 @@ import me.chanjar.weixin.common.session.WxSessionManager;
|
||||
import me.chanjar.weixin.common.util.RandomUtils;
|
||||
import me.chanjar.weixin.common.util.crypto.SHA1;
|
||||
import me.chanjar.weixin.common.util.fs.FileUtils;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.URIUtil;
|
||||
import me.chanjar.weixin.common.util.http.apache.*;
|
||||
import me.chanjar.weixin.common.util.http.*;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
|
||||
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
|
||||
import me.chanjar.weixin.common.util.json.GsonHelper;
|
||||
import me.chanjar.weixin.cp.api.WxCpConfigStorage;
|
||||
import me.chanjar.weixin.cp.api.WxCpService;
|
||||
@@ -41,7 +41,7 @@ import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class WxCpServiceImpl implements WxCpService<CloseableHttpClient, HttpHost> {
|
||||
public class WxCpServiceImpl implements WxCpService, RequestHttp {
|
||||
|
||||
protected final Logger log = LoggerFactory.getLogger(WxCpServiceImpl.class);
|
||||
|
||||
@@ -538,7 +538,7 @@ public class WxCpServiceImpl implements WxCpService<CloseableHttpClient, HttpHos
|
||||
* 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求
|
||||
*/
|
||||
@Override
|
||||
public <T, E> T execute(RequestExecutor<T,CloseableHttpClient, HttpHost, E> executor, String uri, E data) throws WxErrorException {
|
||||
public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
|
||||
int retryTimes = 0;
|
||||
do {
|
||||
try {
|
||||
@@ -574,7 +574,7 @@ public class WxCpServiceImpl implements WxCpService<CloseableHttpClient, HttpHos
|
||||
throw new RuntimeException("微信服务端异常,超出重试次数");
|
||||
}
|
||||
|
||||
public synchronized <T, E> T executeInternal(RequestExecutor<T,CloseableHttpClient, HttpHost, E> executor, String uri, E data) throws WxErrorException {
|
||||
public synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
|
||||
if (uri.contains("access_token=")) {
|
||||
throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri);
|
||||
}
|
||||
@@ -584,7 +584,7 @@ public class WxCpServiceImpl implements WxCpService<CloseableHttpClient, HttpHos
|
||||
uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken;
|
||||
|
||||
try {
|
||||
return executor.execute(getHttpclient(), this.httpProxy, uriWithAccessToken, data);
|
||||
return executor.execute(this, uriWithAccessToken, data);
|
||||
} catch (WxErrorException e) {
|
||||
WxError error = e.getError();
|
||||
/*
|
||||
@@ -698,4 +698,13 @@ public class WxCpServiceImpl implements WxCpService<CloseableHttpClient, HttpHos
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object getRequestHttpClient() {
|
||||
return this.httpClient;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getRequestHttpProxy() {
|
||||
return this.httpProxy;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,6 @@ import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.cp.api.WxCpService;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
@@ -23,7 +21,7 @@ public class WxCpBusyRetryTest {
|
||||
|
||||
@Override
|
||||
public synchronized <T, E> T executeInternal(
|
||||
RequestExecutor<T,CloseableHttpClient, HttpHost, E> executor, String uri, E data)
|
||||
RequestExecutor<T, E> executor, String uri, E data)
|
||||
throws WxErrorException {
|
||||
this.log.info("Executed");
|
||||
WxError error = new WxError();
|
||||
|
||||
Reference in New Issue
Block a user