diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaDownloadRequestExecutor.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaDownloadRequestExecutor.java index cfe382e1..a61c54a7 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaDownloadRequestExecutor.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaDownloadRequestExecutor.java @@ -41,14 +41,14 @@ public class MediaDownloadRequestExecutor implements RequestExecutor @Override public String execute(RequestHttp requestHttp, String uri, String queryParam) throws WxErrorException, IOException { - if (requestHttp.getHttpClient() instanceof CloseableHttpClient) { - CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getHttpClient(); - HttpHost httpProxy = (HttpHost) requestHttp.getHttpProxy(); + if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) { + CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient(); + HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy(); return executeApache(httpClient, httpProxy, uri, queryParam); } - if (requestHttp.getHttpClient() instanceof HttpConnectionProvider) { - HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getHttpClient(); - ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getHttpProxy(); + if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) { + HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient(); + ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy(); return executeJodd(provider, proxyInfo, uri, queryParam); } else { //这里需要抛出异常,需要优化 diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/SimplePostRequestExecutor.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/SimplePostRequestExecutor.java index 39cb90d6..f35385de 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/SimplePostRequestExecutor.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/SimplePostRequestExecutor.java @@ -27,14 +27,14 @@ public class SimplePostRequestExecutor implements RequestExecutor { +public interface WxCpService { /** *
@@ -469,7 +470,7 @@ public interface WxCpService {
    * 
    * Service没有实现某个API的时候,可以用这个,
    * 比{@link #get}和{@link #post}方法更灵活,可以自己构造RequestExecutor用来处理不同的参数和不同的返回类型。
-   * 可以参考,{@link me.chanjar.weixin.common.util.http.jodd.MediaUploadRequestExecutor}的实现方法
+   * 可以参考,{@link MediaUploadRequestExecutor}的实现方法
    * 
* * @param executor 执行器 @@ -478,9 +479,7 @@ public interface WxCpService { * @param 请求值类型 * @param 返回值类型 */ - T execute(RequestExecutor executor, String uri, E data) throws WxErrorException; - - T executeInternal(RequestExecutor executor, String uri, E data) throws WxErrorException; + T execute(RequestExecutor executor, String uri, E data) throws WxErrorException; /** * 注入 {@link WxCpConfigStorage} 的实现 diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/apache/WxCpServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/apache/WxCpServiceImpl.java index 522c449f..4369868f 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/apache/WxCpServiceImpl.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/apache/WxCpServiceImpl.java @@ -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 { +public class WxCpServiceImpl implements WxCpService, RequestHttp { protected final Logger log = LoggerFactory.getLogger(WxCpServiceImpl.class); @@ -538,7 +538,7 @@ public class WxCpServiceImpl implements WxCpService T execute(RequestExecutor executor, String uri, E data) throws WxErrorException { + public T execute(RequestExecutor executor, String uri, E data) throws WxErrorException { int retryTimes = 0; do { try { @@ -574,7 +574,7 @@ public class WxCpServiceImpl implements WxCpService T executeInternal(RequestExecutor executor, String uri, E data) throws WxErrorException { + public synchronized T executeInternal(RequestExecutor 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 { * 得到WxMpService * @return */ - WxMpService getWxMpService(); + WxMpService getWxMpService(); /** * 获得卡券api_ticket,不强制刷新卡券api_ticket diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java index edaa72c1..0b968594 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java @@ -2,15 +2,15 @@ package me.chanjar.weixin.mp.api; import me.chanjar.weixin.common.bean.WxJsapiSignature; import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; import me.chanjar.weixin.common.util.http.RequestExecutor; -import me.chanjar.weixin.common.util.http.apache.MediaUploadRequestExecutor; import me.chanjar.weixin.mp.bean.*; import me.chanjar.weixin.mp.bean.result.*; /** * 微信API的Service */ -public interface WxMpService { +public interface WxMpService { /** *
@@ -133,7 +133,6 @@ public interface WxMpService {
    * 长链接转短链接接口
    * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=长链接转短链接接口
    * 
- * */ String shortUrl(String long_url) throws WxErrorException; @@ -153,8 +152,8 @@ public interface WxMpService { *
* * @param redirectURI 用户授权完成后的重定向链接,无需urlencode, 方法内会进行encode - * @param scope 应用授权作用域,拥有多个作用域用逗号(,)分隔,网页应用目前仅填写snsapi_login即可 - * @param state 非必填,用于保持请求和回调的状态,授权请求后原样带回给第三方。该参数可用于防止csrf攻击(跨站请求伪造攻击),建议第三方带上该参数,可设置为简单的随机数加session进行校验 + * @param scope 应用授权作用域,拥有多个作用域用逗号(,)分隔,网页应用目前仅填写snsapi_login即可 + * @param state 非必填,用于保持请求和回调的状态,授权请求后原样带回给第三方。该参数可用于防止csrf攻击(跨站请求伪造攻击),建议第三方带上该参数,可设置为简单的随机数加session进行校验 * @return url */ String buildQrConnectUrl(String redirectURI, String scope, String state); @@ -190,7 +189,7 @@ public interface WxMpService { * 用oauth2获取用户信息, 当前面引导授权时的scope是snsapi_userinfo的时候才可以 * * - * @param lang zh_CN, zh_TW, en + * @param lang zh_CN, zh_TW, en */ WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, String lang) throws WxErrorException; @@ -198,7 +197,6 @@ public interface WxMpService { *
    * 验证oauth2的access token是否有效
    * 
- * */ boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken); @@ -227,15 +225,12 @@ public interface WxMpService { * 可以参考,{@link MediaUploadRequestExecutor}的实现方法 * */ - T execute(RequestExecutor executor, String uri, E data) throws WxErrorException; - - T executeInternal(RequestExecutor executor, String uri, E data) throws WxErrorException; - + T execute(RequestExecutor executor, String uri, E data) throws WxErrorException; - /** - * 获取代理对象 - */ - //HttpHost getHttpProxy(); + /** + * 获取代理对象 + */ + //HttpHost getRequestHttpProxy(); /** * 注入 {@link WxMpConfigStorage} 的实现 @@ -350,15 +345,13 @@ public interface WxMpService { WxMpDeviceService getDeviceService(); /** - * * @return */ - H getHttpclient(); + //Object getHttpclient(); /** - * * @return */ - P getHttpProxy(); + //Object getHttpProxy(); } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpCardServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpCardServiceImpl.java similarity index 97% rename from weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpCardServiceImpl.java rename to weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpCardServiceImpl.java index e9d9c789..9e64d043 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpCardServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpCardServiceImpl.java @@ -1,4 +1,4 @@ -package me.chanjar.weixin.mp.api.impl.apache; +package me.chanjar.weixin.mp.api.impl; import com.google.gson.JsonElement; import com.google.gson.JsonObject; @@ -10,7 +10,7 @@ import me.chanjar.weixin.common.bean.result.WxError; import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.common.util.RandomUtils; import me.chanjar.weixin.common.util.crypto.SHA1; -import me.chanjar.weixin.common.util.http.apache.SimpleGetRequestExecutor; +import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor; import me.chanjar.weixin.mp.api.WxMpCardService; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.bean.result.WxMpCardResult; @@ -30,7 +30,7 @@ public class WxMpCardServiceImpl implements WxMpCardService wxMpService; + private WxMpService wxMpService; public WxMpCardServiceImpl(WxMpService wxMpService) { this.wxMpService = wxMpService; @@ -41,7 +41,7 @@ public class WxMpCardServiceImpl implements WxMpCardService getWxMpService(){ + public WxMpService getWxMpService(){ return this.wxMpService; } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpKefuServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpKefuServiceImpl.java similarity index 96% rename from weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpKefuServiceImpl.java rename to weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpKefuServiceImpl.java index 9e7685b5..4667f327 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpKefuServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpKefuServiceImpl.java @@ -1,12 +1,10 @@ -package me.chanjar.weixin.mp.api.impl.jodd; +package me.chanjar.weixin.mp.api.impl; import com.google.gson.JsonObject; -import jodd.http.HttpConnectionProvider; -import jodd.http.ProxyInfo; import me.chanjar.weixin.common.bean.result.WxError; import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; import me.chanjar.weixin.common.exception.WxErrorException; -import me.chanjar.weixin.common.util.http.jodd.MediaUploadRequestExecutor; +import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; import me.chanjar.weixin.mp.api.WxMpKefuService; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage; @@ -29,7 +27,7 @@ public class WxMpKefuServiceImpl implements WxMpKefuService { .getLogger(WxMpKefuServiceImpl.class); private static final String API_URL_PREFIX = "https://api.weixin.qq.com/customservice"; private static final String API_URL_PREFIX_WITH_CGI_BIN = "https://api.weixin.qq.com/cgi-bin/customservice"; - private WxMpService wxMpService; + private WxMpService wxMpService; public WxMpKefuServiceImpl(WxMpService wxMpService) { this.wxMpService = wxMpService; diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpMaterialServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpMaterialServiceImpl.java similarity index 94% rename from weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpMaterialServiceImpl.java rename to weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpMaterialServiceImpl.java index f6c48f9a..4242063b 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpMaterialServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpMaterialServiceImpl.java @@ -1,19 +1,17 @@ -package me.chanjar.weixin.mp.api.impl.jodd; +package me.chanjar.weixin.mp.api.impl; -import jodd.http.HttpConnectionProvider; -import jodd.http.ProxyInfo; import me.chanjar.weixin.common.api.WxConsts; import me.chanjar.weixin.common.bean.result.WxError; import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.common.util.fs.FileUtils; -import me.chanjar.weixin.common.util.http.jodd.MediaDownloadRequestExecutor; -import me.chanjar.weixin.common.util.http.jodd.MediaUploadRequestExecutor; +import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor; +import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; import me.chanjar.weixin.common.util.json.WxGsonBuilder; import me.chanjar.weixin.mp.api.WxMpMaterialService; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.bean.material.*; -import me.chanjar.weixin.mp.util.http.jodd.*; +import me.chanjar.weixin.mp.util.http.*; import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; import java.io.File; @@ -29,7 +27,7 @@ import java.util.UUID; public class WxMpMaterialServiceImpl implements WxMpMaterialService { private static final String MEDIA_API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/media"; private static final String MATERIAL_API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/material"; - private WxMpService wxMpService; + private WxMpService wxMpService; public WxMpMaterialServiceImpl(WxMpService wxMpService) { this.wxMpService = wxMpService; diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpQrcodeServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpQrcodeServiceImpl.java similarity index 94% rename from weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpQrcodeServiceImpl.java rename to weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpQrcodeServiceImpl.java index fba822b3..8659f2af 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpQrcodeServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpQrcodeServiceImpl.java @@ -1,14 +1,12 @@ -package me.chanjar.weixin.mp.api.impl.jodd; +package me.chanjar.weixin.mp.api.impl; import com.google.gson.JsonObject; -import jodd.http.HttpConnectionProvider; -import jodd.http.ProxyInfo; import me.chanjar.weixin.common.bean.result.WxError; import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.mp.api.WxMpQrcodeService; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket; -import me.chanjar.weixin.mp.util.http.jodd.QrCodeRequestExecutor; +import me.chanjar.weixin.mp.util.http.QrCodeRequestExecutor; import java.io.File; import java.io.UnsupportedEncodingException; @@ -20,7 +18,7 @@ import java.nio.charset.StandardCharsets; */ public class WxMpQrcodeServiceImpl implements WxMpQrcodeService { private static final String API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/qrcode"; - private WxMpService wxMpService; + private WxMpService wxMpService; public WxMpQrcodeServiceImpl(WxMpService wxMpService) { this.wxMpService = wxMpService; diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpUserBlacklistServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpUserBlacklistServiceImpl.java similarity index 87% rename from weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpUserBlacklistServiceImpl.java rename to weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpUserBlacklistServiceImpl.java index f310b3d4..b0b35343 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpUserBlacklistServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpUserBlacklistServiceImpl.java @@ -1,11 +1,9 @@ -package me.chanjar.weixin.mp.api.impl.jodd; +package me.chanjar.weixin.mp.api.impl; import com.google.gson.Gson; import com.google.gson.JsonObject; -import jodd.http.HttpConnectionProvider; -import jodd.http.ProxyInfo; import me.chanjar.weixin.common.exception.WxErrorException; -import me.chanjar.weixin.common.util.http.jodd.SimplePostRequestExecutor; +import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpUserBlacklistService; import me.chanjar.weixin.mp.bean.result.WxMpUserBlacklistGetResult; @@ -19,7 +17,7 @@ import java.util.Map; */ public class WxMpUserBlacklistServiceImpl implements WxMpUserBlacklistService { private static final String API_BLACKLIST_PREFIX = "https://api.weixin.qq.com/cgi-bin/tags/members"; - private WxMpService wxMpService; + private WxMpService wxMpService; public WxMpUserBlacklistServiceImpl(WxMpService wxMpService) { this.wxMpService = wxMpService; diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpKefuServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpKefuServiceImpl.java deleted file mode 100644 index 0dc921f1..00000000 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpKefuServiceImpl.java +++ /dev/null @@ -1,183 +0,0 @@ -package me.chanjar.weixin.mp.api.impl.apache; - -import com.google.gson.JsonObject; -import me.chanjar.weixin.common.bean.result.WxError; -import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; -import me.chanjar.weixin.common.exception.WxErrorException; -import me.chanjar.weixin.common.util.http.apache.MediaUploadRequestExecutor; -import me.chanjar.weixin.mp.api.WxMpKefuService; -import me.chanjar.weixin.mp.api.WxMpService; -import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage; -import me.chanjar.weixin.mp.bean.kefu.request.WxMpKfAccountRequest; -import me.chanjar.weixin.mp.bean.kefu.request.WxMpKfSessionRequest; -import me.chanjar.weixin.mp.bean.kefu.result.*; -import org.apache.http.HttpHost; -import org.apache.http.impl.client.CloseableHttpClient; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.File; -import java.util.Date; - -/** - * - * @author Binary Wang - * - */ -public class WxMpKefuServiceImpl implements WxMpKefuService { - protected final Logger log = LoggerFactory - .getLogger(WxMpKefuServiceImpl.class); - private static final String API_URL_PREFIX = "https://api.weixin.qq.com/customservice"; - private static final String API_URL_PREFIX_WITH_CGI_BIN = "https://api.weixin.qq.com/cgi-bin/customservice"; - private WxMpService wxMpService; - - public WxMpKefuServiceImpl(WxMpService wxMpService) { - this.wxMpService = wxMpService; - } - - @Override - public boolean sendKefuMessage(WxMpKefuMessage message) - throws WxErrorException { - String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send"; - String responseContent = this.wxMpService.post(url, message.toJson()); - return responseContent != null; - } - - @Override - public WxMpKfList kfList() throws WxErrorException { - String url = API_URL_PREFIX_WITH_CGI_BIN + "/getkflist"; - String responseContent = this.wxMpService.get(url, null); - return WxMpKfList.fromJson(responseContent); - } - - @Override - public WxMpKfOnlineList kfOnlineList() throws WxErrorException { - String url = API_URL_PREFIX_WITH_CGI_BIN + "/getonlinekflist"; - String responseContent = this.wxMpService.get(url, null); - return WxMpKfOnlineList.fromJson(responseContent); - } - - @Override - public boolean kfAccountAdd(WxMpKfAccountRequest request) - throws WxErrorException { - String url = API_URL_PREFIX + "/kfaccount/add"; - String responseContent = this.wxMpService.post(url, request.toJson()); - return responseContent != null; - } - - @Override - public boolean kfAccountUpdate(WxMpKfAccountRequest request) - throws WxErrorException { - String url = API_URL_PREFIX + "/kfaccount/update"; - String responseContent = this.wxMpService.post(url, request.toJson()); - return responseContent != null; - } - - @Override - public boolean kfAccountInviteWorker(WxMpKfAccountRequest request) throws WxErrorException { - String url = API_URL_PREFIX + "/kfaccount/inviteworker"; - String responseContent = this.wxMpService.post(url, request.toJson()); - return responseContent != null; - } - - @Override - public boolean kfAccountUploadHeadImg(String kfAccount, File imgFile) - throws WxErrorException { - String url = API_URL_PREFIX + "/kfaccount/uploadheadimg?kf_account=" + kfAccount; - WxMediaUploadResult responseContent = this.wxMpService - .execute(new MediaUploadRequestExecutor(), url, imgFile); - return responseContent != null; - } - - @Override - public boolean kfAccountDel(String kfAccount) throws WxErrorException { - String url = API_URL_PREFIX + "/kfaccount/del?kf_account=" + kfAccount; - String responseContent = this.wxMpService.get(url, null); - return responseContent != null; - } - - @Override - public boolean kfSessionCreate(String openid, String kfAccount) - throws WxErrorException { - WxMpKfSessionRequest request = new WxMpKfSessionRequest(kfAccount, openid); - String url = API_URL_PREFIX + "/kfsession/create"; - String responseContent = this.wxMpService.post(url, request.toJson()); - return responseContent != null; - } - - @Override - public boolean kfSessionClose(String openid, String kfAccount) - throws WxErrorException { - WxMpKfSessionRequest request = new WxMpKfSessionRequest(kfAccount, openid); - String url = API_URL_PREFIX + "/kfsession/close"; - String responseContent = this.wxMpService.post(url, request.toJson()); - return responseContent != null; - } - - @Override - public WxMpKfSessionGetResult kfSessionGet(String openid) - throws WxErrorException { - String url = API_URL_PREFIX + "/kfsession/getsession?openid=" + openid; - String responseContent = this.wxMpService.get(url, null); - return WxMpKfSessionGetResult.fromJson(responseContent); - } - - @Override - public WxMpKfSessionList kfSessionList(String kfAccount) - throws WxErrorException { - String url = API_URL_PREFIX + "/kfsession/getsessionlist?kf_account=" + kfAccount; - String responseContent = this.wxMpService.get(url, null); - return WxMpKfSessionList.fromJson(responseContent); - } - - @Override - public WxMpKfSessionWaitCaseList kfSessionGetWaitCase() - throws WxErrorException { - String url = API_URL_PREFIX + "/kfsession/getwaitcase"; - String responseContent = this.wxMpService.get(url, null); - return WxMpKfSessionWaitCaseList.fromJson(responseContent); - } - - @Override - public WxMpKfMsgList kfMsgList(Date startTime, Date endTime, Long msgId, Integer number) throws WxErrorException { - if(number > 10000){ - throw new WxErrorException(WxError.newBuilder().setErrorMsg("非法参数请求,每次最多查询10000条记录!").build()); - } - - if(startTime.after(endTime)){ - throw new WxErrorException(WxError.newBuilder().setErrorMsg("起始时间不能晚于结束时间!").build()); - } - - String url = API_URL_PREFIX + "/msgrecord/getmsglist"; - - JsonObject param = new JsonObject(); - param.addProperty("starttime", startTime.getTime() / 1000); //starttime 起始时间,unix时间戳 - param.addProperty("endtime", endTime.getTime() / 1000); //endtime 结束时间,unix时间戳,每次查询时段不能超过24小时 - param.addProperty("msgid", msgId); //msgid 消息id顺序从小到大,从1开始 - param.addProperty("number", number); //number 每次获取条数,最多10000条 - - String responseContent = this.wxMpService.post(url, param.toString()); - - return WxMpKfMsgList.fromJson(responseContent); - } - - @Override - public WxMpKfMsgList kfMsgList(Date startTime, Date endTime) throws WxErrorException { - int number = 10000; - WxMpKfMsgList result = this.kfMsgList(startTime,endTime, 1L, number); - - if(result != null && result.getNumber() == number){ - Long msgId = result.getMsgId(); - WxMpKfMsgList followingResult = this.kfMsgList(startTime,endTime, msgId, number); - while(followingResult != null && followingResult.getRecords().size() > 0){ - result.getRecords().addAll(followingResult.getRecords()); - result.setNumber(result.getNumber() + followingResult.getNumber()); - result.setMsgId(followingResult.getMsgId()); - followingResult = this.kfMsgList(startTime,endTime, followingResult.getMsgId(), number); - } - } - - return result; - } - -} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpMaterialServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpMaterialServiceImpl.java deleted file mode 100644 index 13d5a754..00000000 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpMaterialServiceImpl.java +++ /dev/null @@ -1,168 +0,0 @@ -package me.chanjar.weixin.mp.api.impl.apache; - -import me.chanjar.weixin.common.api.WxConsts; -import me.chanjar.weixin.common.bean.result.WxError; -import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; -import me.chanjar.weixin.common.exception.WxErrorException; -import me.chanjar.weixin.common.util.fs.FileUtils; -import me.chanjar.weixin.common.util.http.apache.MediaDownloadRequestExecutor; -import me.chanjar.weixin.common.util.http.apache.MediaUploadRequestExecutor; -import me.chanjar.weixin.common.util.json.WxGsonBuilder; -import me.chanjar.weixin.mp.api.WxMpMaterialService; -import me.chanjar.weixin.mp.api.WxMpService; -import me.chanjar.weixin.mp.bean.material.WxMpMaterial; -import me.chanjar.weixin.mp.bean.material.WxMpMaterialArticleUpdate; -import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews; -import me.chanjar.weixin.mp.bean.material.*; -import me.chanjar.weixin.mp.util.http.apache.*; -import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; -import org.apache.http.HttpHost; -import org.apache.http.impl.client.CloseableHttpClient; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; - -/** - * Created by Binary Wang on 2016/7/21. - */ -public class WxMpMaterialServiceImpl implements WxMpMaterialService { - private static final String MEDIA_API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/media"; - private static final String MATERIAL_API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/material"; - private WxMpService wxMpService; - - public WxMpMaterialServiceImpl(WxMpService wxMpService) { - this.wxMpService = wxMpService; - } - - @Override - public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException { - try { - return this.mediaUpload(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType)); - } catch (IOException e) { - e.printStackTrace(); - throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build()); - } - } - - @Override - public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException { - String url = MEDIA_API_URL_PREFIX + "/upload?type=" + mediaType; - return this.wxMpService.execute(new MediaUploadRequestExecutor(), url, file); - } - - @Override - public File mediaDownload(String media_id) throws WxErrorException { - String url = MEDIA_API_URL_PREFIX + "/get"; - return this.wxMpService.execute( - new MediaDownloadRequestExecutor(this.wxMpService.getWxMpConfigStorage().getTmpDirFile()), - url, - "media_id=" + media_id); - } - - @Override - public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException { - String url = MEDIA_API_URL_PREFIX + "/uploadimg"; - return this.wxMpService.execute(new MediaImgUploadRequestExecutor(), url, file); - } - - @Override - public WxMpMaterialUploadResult materialFileUpload(String mediaType, WxMpMaterial material) throws WxErrorException { - String url = MATERIAL_API_URL_PREFIX + "/add_material?type=" + mediaType; - return this.wxMpService.execute(new MaterialUploadRequestExecutor(), url, material); - } - - @Override - public WxMpMaterialUploadResult materialNewsUpload(WxMpMaterialNews news) throws WxErrorException { - if (news == null || news.isEmpty()) { - throw new IllegalArgumentException("news is empty!"); - } - String url = MATERIAL_API_URL_PREFIX + "/add_news"; - String responseContent = this.wxMpService.post(url, news.toJson()); - return WxMpMaterialUploadResult.fromJson(responseContent); - } - - @Override - public InputStream materialImageOrVoiceDownload(String media_id) throws WxErrorException { - String url = MATERIAL_API_URL_PREFIX + "/get_material"; - return this.wxMpService.execute(new MaterialVoiceAndImageDownloadRequestExecutor(this.wxMpService.getWxMpConfigStorage().getTmpDirFile()), url, media_id); - } - - @Override - public WxMpMaterialVideoInfoResult materialVideoInfo(String media_id) throws WxErrorException { - String url = MATERIAL_API_URL_PREFIX + "/get_material"; - return this.wxMpService.execute(new MaterialVideoInfoRequestExecutor(), url, media_id); - } - - @Override - public WxMpMaterialNews materialNewsInfo(String media_id) throws WxErrorException { - String url = MATERIAL_API_URL_PREFIX + "/get_material"; - return this.wxMpService.execute(new MaterialNewsInfoRequestExecutor(), url, media_id); - } - - @Override - public boolean materialNewsUpdate(WxMpMaterialArticleUpdate wxMpMaterialArticleUpdate) throws WxErrorException { - String url = MATERIAL_API_URL_PREFIX + "/update_news"; - String responseText = this.wxMpService.post(url, wxMpMaterialArticleUpdate.toJson()); - WxError wxError = WxError.fromJson(responseText); - if (wxError.getErrorCode() == 0) { - return true; - } else { - throw new WxErrorException(wxError); - } - } - - @Override - public boolean materialDelete(String media_id) throws WxErrorException { - String url = MATERIAL_API_URL_PREFIX + "/del_material"; - return this.wxMpService.execute(new MaterialDeleteRequestExecutor(), url, media_id); - } - - @Override - public WxMpMaterialCountResult materialCount() throws WxErrorException { - String url = MATERIAL_API_URL_PREFIX + "/get_materialcount"; - String responseText = this.wxMpService.get(url, null); - WxError wxError = WxError.fromJson(responseText); - if (wxError.getErrorCode() == 0) { - return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialCountResult.class); - } else { - throw new WxErrorException(wxError); - } - } - - @Override - public WxMpMaterialNewsBatchGetResult materialNewsBatchGet(int offset, int count) throws WxErrorException { - String url = MATERIAL_API_URL_PREFIX + "/batchget_material"; - Map params = new HashMap<>(); - params.put("type", WxConsts.MATERIAL_NEWS); - params.put("offset", offset); - params.put("count", count); - String responseText = this.wxMpService.post(url, WxGsonBuilder.create().toJson(params)); - WxError wxError = WxError.fromJson(responseText); - if (wxError.getErrorCode() == 0) { - return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialNewsBatchGetResult.class); - } else { - throw new WxErrorException(wxError); - } - } - - @Override - public WxMpMaterialFileBatchGetResult materialFileBatchGet(String type, int offset, int count) throws WxErrorException { - String url = MATERIAL_API_URL_PREFIX + "/batchget_material"; - Map params = new HashMap<>(); - params.put("type", type); - params.put("offset", offset); - params.put("count", count); - String responseText = this.wxMpService.post(url, WxGsonBuilder.create().toJson(params)); - WxError wxError = WxError.fromJson(responseText); - if (wxError.getErrorCode() == 0) { - return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialFileBatchGetResult.class); - } else { - throw new WxErrorException(wxError); - } - } - -} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpQrcodeServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpQrcodeServiceImpl.java deleted file mode 100644 index 5db81fe1..00000000 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpQrcodeServiceImpl.java +++ /dev/null @@ -1,120 +0,0 @@ -package me.chanjar.weixin.mp.api.impl.apache; - -import com.google.gson.JsonObject; -import me.chanjar.weixin.common.bean.result.WxError; -import me.chanjar.weixin.common.exception.WxErrorException; -import me.chanjar.weixin.mp.api.WxMpQrcodeService; -import me.chanjar.weixin.mp.api.WxMpService; -import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket; -import me.chanjar.weixin.mp.util.http.apache.QrCodeRequestExecutor; -import org.apache.http.HttpHost; -import org.apache.http.impl.client.CloseableHttpClient; - -import java.io.File; -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; -import java.nio.charset.StandardCharsets; - -/** - * Created by Binary Wang on 2016/7/21. - */ -public class WxMpQrcodeServiceImpl implements WxMpQrcodeService { - private static final String API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/qrcode"; - private WxMpService wxMpService; - - public WxMpQrcodeServiceImpl(WxMpService wxMpService) { - this.wxMpService = wxMpService; - } - - @Override - public WxMpQrCodeTicket qrCodeCreateTmpTicket(int sceneId, Integer expireSeconds) throws WxErrorException { - if (sceneId == 0) { - throw new WxErrorException(WxError.newBuilder().setErrorCode(-1).setErrorMsg("临时二维码场景值不能为0!").build()); - } - - //expireSeconds 该二维码有效时间,以秒为单位。 最大不超过2592000(即30天),此字段如果不填,则默认有效期为30秒。 - if (expireSeconds != null && expireSeconds > 2592000) { - throw new WxErrorException(WxError.newBuilder().setErrorCode(-1) - .setErrorMsg("临时二维码有效时间最大不能超过2592000(即30天)!").build()); - } - - if (expireSeconds == null) { - expireSeconds = 30; - } - - String url = API_URL_PREFIX + "/create"; - JsonObject json = new JsonObject(); - json.addProperty("action_name", "QR_SCENE"); - json.addProperty("expire_seconds", expireSeconds); - - JsonObject actionInfo = new JsonObject(); - JsonObject scene = new JsonObject(); - scene.addProperty("scene_id", sceneId); - actionInfo.add("scene", scene); - json.add("action_info", actionInfo); - String responseContent = this.wxMpService.post(url, json.toString()); - return WxMpQrCodeTicket.fromJson(responseContent); - } - - @Override - public WxMpQrCodeTicket qrCodeCreateLastTicket(int sceneId) throws WxErrorException { - if (sceneId < 1 || sceneId > 100000) { - throw new WxErrorException(WxError.newBuilder().setErrorCode(-1).setErrorMsg("永久二维码的场景值目前只支持1--100000!").build()); - } - - String url = API_URL_PREFIX + "/create"; - JsonObject json = new JsonObject(); - json.addProperty("action_name", "QR_LIMIT_SCENE"); - JsonObject actionInfo = new JsonObject(); - JsonObject scene = new JsonObject(); - scene.addProperty("scene_id", sceneId); - actionInfo.add("scene", scene); - json.add("action_info", actionInfo); - String responseContent = this.wxMpService.post(url, json.toString()); - return WxMpQrCodeTicket.fromJson(responseContent); - } - - @Override - public WxMpQrCodeTicket qrCodeCreateLastTicket(String sceneStr) throws WxErrorException { - String url = API_URL_PREFIX + "/create"; - JsonObject json = new JsonObject(); - json.addProperty("action_name", "QR_LIMIT_STR_SCENE"); - JsonObject actionInfo = new JsonObject(); - JsonObject scene = new JsonObject(); - scene.addProperty("scene_str", sceneStr); - actionInfo.add("scene", scene); - json.add("action_info", actionInfo); - String responseContent = this.wxMpService.post(url, json.toString()); - return WxMpQrCodeTicket.fromJson(responseContent); - } - - @Override - public File qrCodePicture(WxMpQrCodeTicket ticket) throws WxErrorException { - String url = "https://mp.weixin.qq.com/cgi-bin/showqrcode"; - return this.wxMpService.execute(new QrCodeRequestExecutor(), url, ticket); - } - - @Override - public String qrCodePictureUrl(String ticket, boolean needShortUrl) throws WxErrorException { - String url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%s"; - try { - String resultUrl = String.format(url, - URLEncoder.encode(ticket, StandardCharsets.UTF_8.name())); - if (needShortUrl) { - return this.wxMpService.shortUrl(resultUrl); - } - - return resultUrl; - } catch (UnsupportedEncodingException e) { - WxError error = WxError.newBuilder().setErrorCode(-1) - .setErrorMsg(e.getMessage()).build(); - throw new WxErrorException(error); - } - } - - @Override - public String qrCodePictureUrl(String ticket) throws WxErrorException { - return qrCodePictureUrl(ticket, false); - } - -} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpServiceImpl.java index 75a6bd96..5e6f863a 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpServiceImpl.java @@ -12,12 +12,9 @@ import me.chanjar.weixin.common.session.StandardSessionManager; 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.http.RequestExecutor; -import me.chanjar.weixin.common.util.http.URIUtil; +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.http.apache.SimpleGetRequestExecutor; -import me.chanjar.weixin.common.util.http.apache.SimplePostRequestExecutor; import me.chanjar.weixin.mp.api.*; import me.chanjar.weixin.mp.api.impl.*; import me.chanjar.weixin.mp.bean.*; @@ -34,7 +31,7 @@ import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.concurrent.locks.Lock; -public class WxMpServiceImpl implements WxMpService { +public class WxMpServiceImpl implements WxMpService,RequestHttp { private static final JsonParser JSON_PARSER = new JsonParser(); @@ -248,8 +245,8 @@ public class WxMpServiceImpl implements WxMpService executor = new SimpleGetRequestExecutor(); - String responseText = executor.execute(this.getHttpclient(), this.httpProxy, url.toString(), null); + RequestExecutor executor = new SimpleGetRequestExecutor(); + String responseText = executor.execute(this, url.toString(), null); return WxMpOAuth2AccessToken.fromJson(responseText); } catch (IOException e) { throw new RuntimeException(e); @@ -292,8 +289,8 @@ public class WxMpServiceImpl implements WxMpService executor = new SimpleGetRequestExecutor(); - String responseText = executor.execute(getHttpclient(), this.httpProxy, url.toString(), null); + RequestExecutor executor = new SimpleGetRequestExecutor(); + String responseText = executor.execute(this, url.toString(), null); return WxMpUser.fromJson(responseText); } catch (IOException e) { throw new RuntimeException(e); @@ -308,8 +305,8 @@ public class WxMpServiceImpl implements WxMpService T executeInternal(RequestExecutor executor, String uri, E data) throws WxErrorException { + public synchronized T executeInternal(RequestExecutor executor, String uri, E data) throws WxErrorException { if (uri.indexOf("access_token=") != -1) { throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri); } @@ -390,7 +386,7 @@ public class WxMpServiceImpl implements WxMpService wxMpService; - - public WxMpUserBlacklistServiceImpl(WxMpService wxMpService) { - this.wxMpService = wxMpService; - } - - @Override - public WxMpUserBlacklistGetResult getBlacklist(String nextOpenid) throws WxErrorException { - JsonObject jsonObject = new JsonObject(); - jsonObject.addProperty("begin_openid", nextOpenid); - String url = API_BLACKLIST_PREFIX + "/getblacklist"; - String responseContent = this.wxMpService.execute(new SimplePostRequestExecutor(), url, jsonObject.toString()); - return WxMpUserBlacklistGetResult.fromJson(responseContent); - } - - @Override - public void pushToBlacklist(List openidList) throws WxErrorException { - Map map = new HashMap<>(); - map.put("openid_list", openidList); - String url = API_BLACKLIST_PREFIX + "/batchblacklist"; - this.wxMpService.execute(new SimplePostRequestExecutor(), url, new Gson().toJson(map)); - } - - @Override - public void pullFromBlacklist(List openidList) throws WxErrorException { - Map map = new HashMap<>(); - map.put("openid_list", openidList); - String url = API_BLACKLIST_PREFIX + "/batchunblacklist"; - this.wxMpService.execute(new SimplePostRequestExecutor(), url, new Gson().toJson(map)); - } -} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpCardServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpCardServiceImpl.java deleted file mode 100644 index 0789b7ea..00000000 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpCardServiceImpl.java +++ /dev/null @@ -1,251 +0,0 @@ -package me.chanjar.weixin.mp.api.impl.jodd; - -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import com.google.gson.JsonPrimitive; -import com.google.gson.reflect.TypeToken; -import jodd.http.HttpConnectionProvider; -import jodd.http.ProxyInfo; -import me.chanjar.weixin.common.bean.WxCardApiSignature; -import me.chanjar.weixin.common.bean.result.WxError; -import me.chanjar.weixin.common.exception.WxErrorException; -import me.chanjar.weixin.common.util.RandomUtils; -import me.chanjar.weixin.common.util.crypto.SHA1; -import me.chanjar.weixin.common.util.http.jodd.SimpleGetRequestExecutor; -import me.chanjar.weixin.mp.api.WxMpCardService; -import me.chanjar.weixin.mp.api.WxMpService; -import me.chanjar.weixin.mp.bean.result.WxMpCardResult; -import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Arrays; -import java.util.concurrent.locks.Lock; - -/** - * Created by Binary Wang on 2016/7/27. - */ -public class WxMpCardServiceImpl implements WxMpCardService { - - private final Logger log = LoggerFactory.getLogger(WxMpCardServiceImpl.class); - - private WxMpService wxMpService; - - public WxMpCardServiceImpl(WxMpService wxMpService) { - this.wxMpService = wxMpService; - } - - /** - * 得到WxMpService - * @return - */ - @Override - public WxMpService getWxMpService(){ - return this.wxMpService; - } - - /** - * 获得卡券api_ticket,不强制刷新卡券api_ticket - * - * @return 卡券api_ticket - * @see #getCardApiTicket(boolean) - */ - @Override - public String getCardApiTicket() throws WxErrorException { - return getCardApiTicket(false); - } - - /** - *
-   * 获得卡券api_ticket
-   * 获得时会检查卡券apiToken是否过期,如果过期了,那么就刷新一下,否则就什么都不干
-   *
-   * 详情请见:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E9.99.84.E5.BD
-   * .954-.E5.8D.A1.E5.88.B8.E6.89.A9.E5.B1.95.E5.AD.97.E6.AE.B5.E5.8F.8A.E7.AD.BE.E5.90.8D.E7.94
-   * .9F.E6.88.90.E7.AE.97.E6.B3.95
-   * 
- * - * @param forceRefresh 强制刷新 - * @return 卡券api_ticket - */ - @Override - public String getCardApiTicket(boolean forceRefresh) throws WxErrorException { - Lock lock = getWxMpService().getWxMpConfigStorage().getCardApiTicketLock(); - try { - lock.lock(); - - if (forceRefresh) { - this.getWxMpService().getWxMpConfigStorage().expireCardApiTicket(); - } - - if (this.getWxMpService().getWxMpConfigStorage().isCardApiTicketExpired()) { - String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=wx_card"; - String responseContent = this.wxMpService.execute(new SimpleGetRequestExecutor(), url, null); - JsonElement tmpJsonElement = new JsonParser().parse(responseContent); - JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject(); - String cardApiTicket = tmpJsonObject.get("ticket").getAsString(); - int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt(); - this.getWxMpService().getWxMpConfigStorage().updateCardApiTicket(cardApiTicket, expiresInSeconds); - } - } finally { - lock.unlock(); - } - return this.getWxMpService().getWxMpConfigStorage().getCardApiTicket(); - } - - /** - *
-   * 创建调用卡券api时所需要的签名
-   *
-   * 详情请见:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E9.99.84.E5.BD
-   * .954-.E5.8D.A1.E5.88.B8.E6.89.A9.E5.B1.95.E5.AD.97.E6.AE.B5.E5.8F.8A.E7.AD.BE.E5.90.8D.E7.94
-   * .9F.E6.88.90.E7.AE.97.E6.B3.95
-   * 
- * - * @param optionalSignParam 参与签名的参数数组。 - * 可以为下列字段:app_id, card_id, card_type, code, openid, location_id - *
注意:当做wx.chooseCard调用时,必须传入app_id参与签名,否则会造成签名失败导致拉取卡券列表为空 - * @return 卡券Api签名对象 - */ - @Override - public WxCardApiSignature createCardApiSignature(String... optionalSignParam) throws - WxErrorException { - long timestamp = System.currentTimeMillis() / 1000; - String nonceStr = RandomUtils.getRandomStr(); - String cardApiTicket = getCardApiTicket(false); - - String[] signParam = Arrays.copyOf(optionalSignParam, optionalSignParam.length + 3); - signParam[optionalSignParam.length] = String.valueOf(timestamp); - signParam[optionalSignParam.length + 1] = nonceStr; - signParam[optionalSignParam.length + 2] = cardApiTicket; - String signature = SHA1.gen(signParam); - WxCardApiSignature cardApiSignature = new WxCardApiSignature(); - cardApiSignature.setTimestamp(timestamp); - cardApiSignature.setNonceStr(nonceStr); - cardApiSignature.setSignature(signature); - return cardApiSignature; - } - - /** - * 卡券Code解码 - * - * @param encryptCode 加密Code,通过JSSDK的chooseCard接口获得 - * @return 解密后的Code - */ - @Override - public String decryptCardCode(String encryptCode) throws WxErrorException { - String url = "https://api.weixin.qq.com/card/code/decrypt"; - JsonObject param = new JsonObject(); - param.addProperty("encrypt_code", encryptCode); - String responseContent = this.wxMpService.post(url, param.toString()); - JsonElement tmpJsonElement = new JsonParser().parse(responseContent); - JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject(); - JsonPrimitive jsonPrimitive = tmpJsonObject.getAsJsonPrimitive("code"); - return jsonPrimitive.getAsString(); - } - - /** - * 卡券Code查询 - * - * @param cardId 卡券ID代表一类卡券 - * @param code 单张卡券的唯一标准 - * @param checkConsume 是否校验code核销状态,填入true和false时的code异常状态返回数据不同 - * @return WxMpCardResult对象 - */ - @Override - public WxMpCardResult queryCardCode(String cardId, String code, boolean checkConsume) throws WxErrorException { - String url = "https://api.weixin.qq.com/card/code/get"; - JsonObject param = new JsonObject(); - param.addProperty("card_id", cardId); - param.addProperty("code", code); - param.addProperty("check_consume", checkConsume); - String responseContent = this.wxMpService.post(url, param.toString()); - JsonElement tmpJsonElement = new JsonParser().parse(responseContent); - return WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement, - new TypeToken() { - }.getType()); - } - - /** - * 卡券Code核销。核销失败会抛出异常 - * - * @param code 单张卡券的唯一标准 - * @return 调用返回的JSON字符串。 - *
可用 com.google.gson.JsonParser#parse 等方法直接取JSON串中的errcode等信息。 - */ - @Override - public String consumeCardCode(String code) throws WxErrorException { - return consumeCardCode(code, null); - } - - /** - * 卡券Code核销。核销失败会抛出异常 - * - * @param code 单张卡券的唯一标准 - * @param cardId 当自定义Code卡券时需要传入card_id - * @return 调用返回的JSON字符串。 - *
可用 com.google.gson.JsonParser#parse 等方法直接取JSON串中的errcode等信息。 - */ - @Override - public String consumeCardCode(String code, String cardId) throws WxErrorException { - String url = "https://api.weixin.qq.com/card/code/consume"; - JsonObject param = new JsonObject(); - param.addProperty("code", code); - - if (cardId != null && !"".equals(cardId)) { - param.addProperty("card_id", cardId); - } - - return this.wxMpService.post(url, param.toString()); - } - - /** - * 卡券Mark接口。 - * 开发者在帮助消费者核销卡券之前,必须帮助先将此code(卡券串码)与一个openid绑定(即mark住), - * 才能进一步调用核销接口,否则报错。 - * - * @param code 卡券的code码 - * @param cardId 卡券的ID - * @param openId 用券用户的openid - * @param isMark 是否要mark(占用)这个code,填写true或者false,表示占用或解除占用 - */ - @Override - public void markCardCode(String code, String cardId, String openId, boolean isMark) throws - WxErrorException { - String url = "https://api.weixin.qq.com/card/code/mark"; - JsonObject param = new JsonObject(); - param.addProperty("code", code); - param.addProperty("card_id", cardId); - param.addProperty("openid", openId); - param.addProperty("is_mark", isMark); - String responseContent = this.getWxMpService().post(url, param.toString()); - JsonElement tmpJsonElement = new JsonParser().parse(responseContent); - WxMpCardResult cardResult = WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement, - new TypeToken() { }.getType()); - if (!cardResult.getErrorCode().equals("0")) { - this.log.warn("朋友的券mark失败:{}", cardResult.getErrorMsg()); - } - } - - @Override - public String getCardDetail(String cardId) throws WxErrorException { - String url = "https://api.weixin.qq.com/card/get"; - JsonObject param = new JsonObject(); - param.addProperty("card_id", cardId); - String responseContent = this.wxMpService.post(url, param.toString()); - - // 判断返回值 - JsonObject json = (new JsonParser()).parse(responseContent).getAsJsonObject(); - String errcode = json.get("errcode").getAsString(); - if (!"0".equals(errcode)) { - String errmsg = json.get("errmsg").getAsString(); - WxError error = new WxError(); - error.setErrorCode(Integer.valueOf(errcode)); - error.setErrorMsg(errmsg); - throw new WxErrorException(error); - } - - return responseContent; - } -} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpServiceImpl.java index 1480270c..fa342f66 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpServiceImpl.java @@ -14,10 +14,7 @@ import me.chanjar.weixin.common.session.StandardSessionManager; 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.http.RequestExecutor; -import me.chanjar.weixin.common.util.http.URIUtil; -import me.chanjar.weixin.common.util.http.jodd.SimpleGetRequestExecutor; -import me.chanjar.weixin.common.util.http.jodd.SimplePostRequestExecutor; +import me.chanjar.weixin.common.util.http.*; import me.chanjar.weixin.mp.api.*; import me.chanjar.weixin.mp.api.impl.*; import me.chanjar.weixin.mp.bean.*; @@ -28,7 +25,7 @@ import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.concurrent.locks.Lock; -public class WxMpServiceImpl implements WxMpService { +public class WxMpServiceImpl implements WxMpService,RequestHttp { private static final JsonParser JSON_PARSER = new JsonParser(); @@ -248,8 +245,8 @@ public class WxMpServiceImpl implements WxMpService executor = new SimpleGetRequestExecutor(); - String responseText = executor.execute(getHttpclient(), getHttpProxy(), url.toString(), null); + RequestExecutor executor = new SimpleGetRequestExecutor(); + String responseText = executor.execute(this, url.toString(), null); return WxMpOAuth2AccessToken.fromJson(responseText); } catch (IOException e) { throw new RuntimeException(e); @@ -292,8 +289,8 @@ public class WxMpServiceImpl implements WxMpService executor = new SimpleGetRequestExecutor(); - String responseText = executor.execute(getHttpclient(), getHttpProxy(), url.toString(), null); + RequestExecutor executor = new SimpleGetRequestExecutor(); + String responseText = executor.execute(this, url.toString(), null); return WxMpUser.fromJson(responseText); } catch (IOException e) { throw new RuntimeException(e); @@ -308,8 +305,8 @@ public class WxMpServiceImpl implements WxMpService T executeInternal(RequestExecutor executor, String uri, E data) throws WxErrorException { + public synchronized T executeInternal(RequestExecutor executor, String uri, E data) throws WxErrorException { if (uri.indexOf("access_token=") != -1) { throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri); } @@ -394,7 +395,7 @@ public class WxMpServiceImpl implements WxMpService params = new HashMap<>(); + params.put("media_id", materialId); + httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); + try (CloseableHttpResponse response = httpclient.execute(httpPost)) { + String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); + WxError error = WxError.fromJson(responseContent); + if (error.getErrorCode() != 0) { + throw new WxErrorException(error); + } else { + return true; + } + } finally { + httpPost.releaseConnection(); + } + } + + +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialNewsInfoRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialNewsInfoRequestExecutor.java new file mode 100644 index 00000000..87180067 --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialNewsInfoRequestExecutor.java @@ -0,0 +1,93 @@ +package me.chanjar.weixin.mp.util.http; + +import jodd.http.HttpConnectionProvider; +import jodd.http.HttpRequest; +import jodd.http.HttpResponse; +import jodd.http.ProxyInfo; +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.common.util.http.RequestHttp; +import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; +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 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.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +public class MaterialNewsInfoRequestExecutor implements RequestExecutor { + + public MaterialNewsInfoRequestExecutor() { + super(); + } + + @Override + public WxMpMaterialNews execute(RequestHttp requestHttp, String uri, + String materialId) throws WxErrorException, IOException { + if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) { + CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient(); + HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy(); + return executeApache(httpClient, httpProxy, uri, materialId); + } + if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) { + HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient(); + ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy(); + return executeJodd(provider, proxyInfo, uri, materialId); + } else { + //这里需要抛出异常,需要优化 + return null; + } + } + + public WxMpMaterialNews executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, IOException { + HttpPost httpPost = new HttpPost(uri); + if (httpProxy != null) { + RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); + httpPost.setConfig(config); + } + + Map params = new HashMap<>(); + params.put("media_id", materialId); + httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); + try (CloseableHttpResponse response = httpclient.execute(httpPost)) { + String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); + WxError error = WxError.fromJson(responseContent); + if (error.getErrorCode() != 0) { + throw new WxErrorException(error); + } else { + return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class); + } + } finally { + httpPost.releaseConnection(); + } + } + + + public WxMpMaterialNews executeJodd(HttpConnectionProvider httpclient, ProxyInfo httpProxy, String uri, String materialId) throws WxErrorException, IOException { + HttpRequest request = HttpRequest.post(uri); + if (httpProxy != null) { + httpclient.useProxy(httpProxy); + } + request.withConnectionProvider(httpclient); + + request.query("media_id", materialId); + HttpResponse response = request.send(); + + String responseContent = request.bodyText(); + WxError error = WxError.fromJson(responseContent); + if (error.getErrorCode() != 0) { + throw new WxErrorException(error); + } else { + return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class); + } + } + +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialUploadRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialUploadRequestExecutor.java new file mode 100644 index 00000000..a1e1a91b --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialUploadRequestExecutor.java @@ -0,0 +1,121 @@ +package me.chanjar.weixin.mp.util.http; + +import jodd.http.HttpConnectionProvider; +import jodd.http.HttpRequest; +import jodd.http.HttpResponse; +import jodd.http.ProxyInfo; +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.common.util.http.RequestHttp; +import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; +import me.chanjar.weixin.common.util.json.WxGsonBuilder; +import me.chanjar.weixin.mp.bean.material.WxMpMaterial; +import me.chanjar.weixin.mp.bean.material.WxMpMaterialUploadResult; +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.mime.HttpMultipartMode; +import org.apache.http.entity.mime.MultipartEntityBuilder; +import org.apache.http.impl.client.CloseableHttpClient; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Map; + +public class MaterialUploadRequestExecutor implements RequestExecutor { + + @Override + public WxMpMaterialUploadResult execute(RequestHttp requestHttp, String uri, WxMpMaterial material) throws WxErrorException, IOException { + if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) { + CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient(); + HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy(); + return executeApache(httpClient, httpProxy, uri, material); + } + if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) { + HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient(); + ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy(); + return executeJodd(provider, proxyInfo, uri, material); + } else { + //这里需要抛出异常,需要优化 + return null; + } + } + + private WxMpMaterialUploadResult executeJodd(HttpConnectionProvider provider, ProxyInfo httpProxy, String uri, WxMpMaterial material) throws WxErrorException, IOException { + HttpRequest request = HttpRequest.post(uri); + if (httpProxy != null) { + provider.useProxy(httpProxy); + } + request.withConnectionProvider(provider); + + if (material == null) { + throw new WxErrorException(WxError.newBuilder().setErrorMsg("非法请求,material参数为空").build()); + } + + File file = material.getFile(); + if (file == null || !file.exists()) { + throw new FileNotFoundException(); + } + request.form("media", file); + Map form = material.getForm(); + if (material.getForm() != null) { + request.form("description", WxGsonBuilder.create().toJson(form)); + } + + HttpResponse response = request.send(); + String responseContent = response.bodyText(); + WxError error = WxError.fromJson(responseContent); + if (error.getErrorCode() != 0) { + throw new WxErrorException(error); + } else { + return WxMpMaterialUploadResult.fromJson(responseContent); + } + } + + private WxMpMaterialUploadResult executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, + WxMpMaterial material) throws WxErrorException, IOException { + HttpPost httpPost = new HttpPost(uri); + if (httpProxy != null) { + RequestConfig response = RequestConfig.custom().setProxy(httpProxy).build(); + httpPost.setConfig(response); + } + + if (material == null) { + throw new WxErrorException(WxError.newBuilder().setErrorMsg("非法请求,material参数为空").build()); + } + + File file = material.getFile(); + if (file == null || !file.exists()) { + throw new FileNotFoundException(); + } + + MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); + multipartEntityBuilder + .addBinaryBody("media", file) + .setMode(HttpMultipartMode.RFC6532); + Map form = material.getForm(); + if (material.getForm() != null) { + multipartEntityBuilder.addTextBody("description", WxGsonBuilder.create().toJson(form)); + } + + httpPost.setEntity(multipartEntityBuilder.build()); + httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString()); + + try (CloseableHttpResponse response = httpclient.execute(httpPost)) { + String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); + WxError error = WxError.fromJson(responseContent); + if (error.getErrorCode() != 0) { + throw new WxErrorException(error); + } else { + return WxMpMaterialUploadResult.fromJson(responseContent); + } + } finally { + httpPost.releaseConnection(); + } + } + +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialVideoInfoRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialVideoInfoRequestExecutor.java new file mode 100644 index 00000000..fb581892 --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialVideoInfoRequestExecutor.java @@ -0,0 +1,92 @@ +package me.chanjar.weixin.mp.util.http; + +import jodd.http.HttpConnectionProvider; +import jodd.http.HttpRequest; +import jodd.http.HttpResponse; +import jodd.http.ProxyInfo; +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.common.util.http.RequestHttp; +import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; +import me.chanjar.weixin.common.util.json.WxGsonBuilder; +import me.chanjar.weixin.mp.bean.material.WxMpMaterialVideoInfoResult; +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.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +public class MaterialVideoInfoRequestExecutor implements RequestExecutor { + + public MaterialVideoInfoRequestExecutor() { + super(); + } + + @Override + public WxMpMaterialVideoInfoResult execute(RequestHttp requestHttp, String uri, String materialId) throws WxErrorException, IOException { + if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) { + CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient(); + HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy(); + return executeApache(httpClient, httpProxy, uri, materialId); + } + if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) { + HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient(); + ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy(); + return executeJodd(provider, proxyInfo, uri, materialId); + } else { + //这里需要抛出异常,需要优化 + return null; + } + } + + + private WxMpMaterialVideoInfoResult executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, String materialId) throws WxErrorException, IOException { + HttpRequest request = HttpRequest.post(uri); + if (proxyInfo != null) { + provider.useProxy(proxyInfo); + } + request.withConnectionProvider(provider); + + request.query("media_id", materialId); + HttpResponse response = request.send(); + String responseContent = response.bodyText(); + WxError error = WxError.fromJson(responseContent); + if (error.getErrorCode() != 0) { + throw new WxErrorException(error); + } else { + return WxMpMaterialVideoInfoResult.fromJson(responseContent); + } + } + + private WxMpMaterialVideoInfoResult executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, + String materialId) throws WxErrorException, IOException { + HttpPost httpPost = new HttpPost(uri); + if (httpProxy != null) { + RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); + httpPost.setConfig(config); + } + + Map params = new HashMap<>(); + params.put("media_id", materialId); + httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); + try (CloseableHttpResponse response = httpclient.execute(httpPost)) { + String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); + WxError error = WxError.fromJson(responseContent); + if (error.getErrorCode() != 0) { + throw new WxErrorException(error); + } else { + return WxMpMaterialVideoInfoResult.fromJson(responseContent); + } + } finally { + httpPost.releaseConnection(); + } + } + + +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialVoiceAndImageDownloadRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialVoiceAndImageDownloadRequestExecutor.java new file mode 100644 index 00000000..913ca086 --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialVoiceAndImageDownloadRequestExecutor.java @@ -0,0 +1,117 @@ +package me.chanjar.weixin.mp.util.http; + +import jodd.http.HttpConnectionProvider; +import jodd.http.HttpRequest; +import jodd.http.HttpResponse; +import jodd.http.ProxyInfo; +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.common.util.http.RequestHttp; +import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler; +import me.chanjar.weixin.common.util.json.WxGsonBuilder; +import org.apache.commons.io.IOUtils; +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.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +public class MaterialVoiceAndImageDownloadRequestExecutor implements RequestExecutor { + + + public MaterialVoiceAndImageDownloadRequestExecutor() { + super(); + } + + public MaterialVoiceAndImageDownloadRequestExecutor(File tmpDirFile) { + super(); + } + + @Override + public InputStream execute(RequestHttp requestHttp, String uri, String materialId) throws WxErrorException, IOException { + if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) { + CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient(); + HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy(); + return executeApache(httpClient, httpProxy, uri, materialId); + } + if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) { + HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient(); + ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy(); + return executeJodd(provider, proxyInfo, uri, materialId); + } else { + //这里需要抛出异常,需要优化 + return null; + } + } + + private InputStream executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, String materialId) throws WxErrorException, IOException { + HttpRequest request = HttpRequest.post(uri); + if (proxyInfo != null) { + provider.useProxy(proxyInfo); + } + request.withConnectionProvider(provider); + + request.query("media_id", materialId); + HttpResponse response = request.send(); + try (InputStream inputStream = new ByteArrayInputStream(response.bodyBytes())) { + // 下载媒体文件出错 + byte[] responseContent = IOUtils.toByteArray(inputStream); + String responseContentString = new String(responseContent, "UTF-8"); + if (responseContentString.length() < 100) { + try { + WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class); + if (wxError.getErrorCode() != 0) { + throw new WxErrorException(wxError); + } + } catch (com.google.gson.JsonSyntaxException ex) { + return new ByteArrayInputStream(responseContent); + } + } + return new ByteArrayInputStream(responseContent); + } + + } + + private InputStream executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, + String materialId) throws WxErrorException, IOException { + HttpPost httpPost = new HttpPost(uri); + if (httpProxy != null) { + RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); + httpPost.setConfig(config); + } + + Map params = new HashMap<>(); + params.put("media_id", materialId); + httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); + try (CloseableHttpResponse response = httpclient.execute(httpPost); + InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);) { + // 下载媒体文件出错 + byte[] responseContent = IOUtils.toByteArray(inputStream); + String responseContentString = new String(responseContent, "UTF-8"); + if (responseContentString.length() < 100) { + try { + WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class); + if (wxError.getErrorCode() != 0) { + throw new WxErrorException(wxError); + } + } catch (com.google.gson.JsonSyntaxException ex) { + return new ByteArrayInputStream(responseContent); + } + } + return new ByteArrayInputStream(responseContent); + } finally { + httpPost.releaseConnection(); + } + } + + +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MediaImgUploadRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MediaImgUploadRequestExecutor.java new file mode 100644 index 00000000..c22bc310 --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MediaImgUploadRequestExecutor.java @@ -0,0 +1,103 @@ +package me.chanjar.weixin.mp.util.http; + +import jodd.http.HttpConnectionProvider; +import jodd.http.HttpRequest; +import jodd.http.HttpResponse; +import jodd.http.ProxyInfo; +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.common.util.http.RequestHttp; +import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; +import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult; +import org.apache.http.HttpEntity; +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.mime.HttpMultipartMode; +import org.apache.http.entity.mime.MultipartEntityBuilder; +import org.apache.http.impl.client.CloseableHttpClient; + +import java.io.File; +import java.io.IOException; + +/** + * @author miller + */ +public class MediaImgUploadRequestExecutor implements RequestExecutor { + + @Override + public WxMediaImgUploadResult execute(RequestHttp requestHttp, String uri, File data) throws WxErrorException, IOException { + if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) { + CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient(); + HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy(); + return executeApache(httpClient, httpProxy, uri, data); + } + if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) { + HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient(); + ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy(); + return executeJodd(provider, proxyInfo, uri, data); + } else { + //这里需要抛出异常,需要优化 + return null; + } + + } + + + private WxMediaImgUploadResult executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, File data) throws WxErrorException, IOException { + if (data == null) { + throw new WxErrorException(WxError.newBuilder().setErrorMsg("文件对象为空").build()); + } + + HttpRequest request = HttpRequest.post(uri); + if (proxyInfo != null) { + provider.useProxy(proxyInfo); + } + request.withConnectionProvider(provider); + + request.form("media", data); + HttpResponse response = request.send(); + String responseContent = response.bodyText(); + WxError error = WxError.fromJson(responseContent); + if (error.getErrorCode() != 0) { + throw new WxErrorException(error); + } + + return WxMediaImgUploadResult.fromJson(responseContent); + } + + private WxMediaImgUploadResult executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, + File data) throws WxErrorException, IOException { + if (data == null) { + throw new WxErrorException(WxError.newBuilder().setErrorMsg("文件对象为空").build()); + } + + HttpPost httpPost = new HttpPost(uri); + if (httpProxy != null) { + RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); + httpPost.setConfig(config); + } + + HttpEntity entity = MultipartEntityBuilder + .create() + .addBinaryBody("media", data) + .setMode(HttpMultipartMode.RFC6532) + .build(); + httpPost.setEntity(entity); + httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString()); + + try (CloseableHttpResponse response = httpclient.execute(httpPost)) { + String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); + WxError error = WxError.fromJson(responseContent); + if (error.getErrorCode() != 0) { + throw new WxErrorException(error); + } + + return WxMediaImgUploadResult.fromJson(responseContent); + } + } + +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/QrCodeRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/QrCodeRequestExecutor.java new file mode 100644 index 00000000..c9ae525c --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/QrCodeRequestExecutor.java @@ -0,0 +1,116 @@ +package me.chanjar.weixin.mp.util.http; + +import jodd.http.HttpConnectionProvider; +import jodd.http.HttpRequest; +import jodd.http.HttpResponse; +import jodd.http.ProxyInfo; +import jodd.util.MimeTypes; +import me.chanjar.weixin.common.bean.result.WxError; +import me.chanjar.weixin.common.exception.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.apache.InputStreamResponseHandler; +import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; +import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket; +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.HttpGet; +import org.apache.http.entity.ContentType; +import org.apache.http.impl.client.CloseableHttpClient; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URLEncoder; +import java.util.UUID; + +/** + * 获得QrCode图片 请求执行器 + * + * @author chanjarster + */ +public class QrCodeRequestExecutor implements RequestExecutor { + + @Override + public File execute(RequestHttp requestHttp, String uri, + WxMpQrCodeTicket ticket) throws WxErrorException, IOException { + if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) { + CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient(); + HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy(); + return executeApache(httpClient, httpProxy, uri, ticket); + } + if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) { + HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient(); + ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy(); + return executeJodd(provider, proxyInfo, uri, ticket); + } else { + //这里需要抛出异常,需要优化 + return null; + } + } + + private File executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, WxMpQrCodeTicket ticket) throws WxErrorException, IOException { + if (ticket != null) { + if (uri.indexOf('?') == -1) { + uri += '?'; + } + uri += uri.endsWith("?") + ? "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8") + : "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8"); + } + + HttpRequest request = HttpRequest.get(uri); + if (proxyInfo != null) { + provider.useProxy(proxyInfo); + } + request.withConnectionProvider(provider); + + HttpResponse response = request.send(); + String contentTypeHeader = response.header("Content-Type"); + if (MimeTypes.MIME_TEXT_PLAIN.equals(contentTypeHeader)) { + String responseContent = response.bodyText(); + throw new WxErrorException(WxError.fromJson(responseContent)); + } + try (InputStream inputStream = new ByteArrayInputStream(response.bodyBytes())) { + return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg"); + } + } + + private File executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, + WxMpQrCodeTicket ticket) throws WxErrorException, IOException { + if (ticket != null) { + if (uri.indexOf('?') == -1) { + uri += '?'; + } + uri += uri.endsWith("?") + ? "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8") + : "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8"); + } + + HttpGet httpGet = new HttpGet(uri); + if (httpProxy != null) { + RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); + httpGet.setConfig(config); + } + + try (CloseableHttpResponse response = httpclient.execute(httpGet); + InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);) { + Header[] contentTypeHeader = response.getHeaders("Content-Type"); + if (contentTypeHeader != null && contentTypeHeader.length > 0) { + // 出错 + if (ContentType.TEXT_PLAIN.getMimeType().equals(contentTypeHeader[0].getValue())) { + String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); + throw new WxErrorException(WxError.fromJson(responseContent)); + } + } + return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg"); + } finally { + httpGet.releaseConnection(); + } + } + +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MaterialDeleteRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MaterialDeleteRequestExecutor.java deleted file mode 100644 index b25d668d..00000000 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MaterialDeleteRequestExecutor.java +++ /dev/null @@ -1,50 +0,0 @@ -package me.chanjar.weixin.mp.util.http.apache; - -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.common.util.http.apache.Utf8ResponseHandler; -import me.chanjar.weixin.common.util.json.WxGsonBuilder; -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.StringEntity; -import org.apache.http.impl.client.CloseableHttpClient; - -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -public class MaterialDeleteRequestExecutor implements RequestExecutor { - - - public MaterialDeleteRequestExecutor() { - super(); - } - - @Override - public Boolean execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, IOException { - HttpPost httpPost = new HttpPost(uri); - if (httpProxy != null) { - RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); - httpPost.setConfig(config); - } - - Map params = new HashMap<>(); - params.put("media_id", materialId); - httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); - try(CloseableHttpResponse response = httpclient.execute(httpPost)){ - String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); - WxError error = WxError.fromJson(responseContent); - if (error.getErrorCode() != 0) { - throw new WxErrorException(error); - } else { - return true; - } - }finally { - httpPost.releaseConnection(); - } - } - -} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MaterialNewsInfoRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MaterialNewsInfoRequestExecutor.java deleted file mode 100644 index f29de11f..00000000 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MaterialNewsInfoRequestExecutor.java +++ /dev/null @@ -1,52 +0,0 @@ -package me.chanjar.weixin.mp.util.http.apache; - -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.common.util.http.apache.Utf8ResponseHandler; -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 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.StringEntity; -import org.apache.http.impl.client.CloseableHttpClient; - -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -public class MaterialNewsInfoRequestExecutor implements RequestExecutor { - - public MaterialNewsInfoRequestExecutor() { - super(); - } - - @Override - public WxMpMaterialNews execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, IOException { - HttpPost httpPost = new HttpPost(uri); - if (httpProxy != null) { - RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); - httpPost.setConfig(config); - } - - Map params = new HashMap<>(); - params.put("media_id", materialId); - httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); - try(CloseableHttpResponse response = httpclient.execute(httpPost)){ - String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); - WxError error = WxError.fromJson(responseContent); - if (error.getErrorCode() != 0) { - throw new WxErrorException(error); - } else { - return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class); - } - }finally { - httpPost.releaseConnection(); - } - - } - -} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MaterialUploadRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MaterialUploadRequestExecutor.java deleted file mode 100644 index d0c16f2d..00000000 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MaterialUploadRequestExecutor.java +++ /dev/null @@ -1,68 +0,0 @@ -package me.chanjar.weixin.mp.util.http.apache; - -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.common.util.http.apache.Utf8ResponseHandler; -import me.chanjar.weixin.common.util.json.WxGsonBuilder; -import me.chanjar.weixin.mp.bean.material.WxMpMaterial; -import me.chanjar.weixin.mp.bean.material.WxMpMaterialUploadResult; -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.mime.HttpMultipartMode; -import org.apache.http.entity.mime.MultipartEntityBuilder; -import org.apache.http.impl.client.CloseableHttpClient; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.util.Map; - -public class MaterialUploadRequestExecutor implements RequestExecutor { - - @Override - public WxMpMaterialUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, WxMpMaterial material) throws WxErrorException, IOException { - HttpPost httpPost = new HttpPost(uri); - if (httpProxy != null) { - RequestConfig response = RequestConfig.custom().setProxy(httpProxy).build(); - httpPost.setConfig(response); - } - - if (material == null) { - throw new WxErrorException(WxError.newBuilder().setErrorMsg("非法请求,material参数为空").build()); - } - - File file = material.getFile(); - if (file == null || !file.exists()) { - throw new FileNotFoundException(); - } - - MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); - multipartEntityBuilder - .addBinaryBody("media", file) - .setMode(HttpMultipartMode.RFC6532); - Map form = material.getForm(); - if (material.getForm() != null) { - multipartEntityBuilder.addTextBody("description", WxGsonBuilder.create().toJson(form)); - } - - httpPost.setEntity(multipartEntityBuilder.build()); - httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString()); - - try (CloseableHttpResponse response = httpclient.execute(httpPost)) { - String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); - WxError error = WxError.fromJson(responseContent); - if (error.getErrorCode() != 0) { - throw new WxErrorException(error); - } else { - return WxMpMaterialUploadResult.fromJson(responseContent); - } - } finally { - httpPost.releaseConnection(); - } - } - -} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MaterialVideoInfoRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MaterialVideoInfoRequestExecutor.java deleted file mode 100644 index 16176755..00000000 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MaterialVideoInfoRequestExecutor.java +++ /dev/null @@ -1,50 +0,0 @@ -package me.chanjar.weixin.mp.util.http.apache; - -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.common.util.http.apache.Utf8ResponseHandler; -import me.chanjar.weixin.common.util.json.WxGsonBuilder; -import me.chanjar.weixin.mp.bean.material.WxMpMaterialVideoInfoResult; -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.StringEntity; -import org.apache.http.impl.client.CloseableHttpClient; - -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -public class MaterialVideoInfoRequestExecutor implements RequestExecutor { - - public MaterialVideoInfoRequestExecutor() { - super(); - } - - @Override - public WxMpMaterialVideoInfoResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, IOException { - HttpPost httpPost = new HttpPost(uri); - if (httpProxy != null) { - RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); - httpPost.setConfig(config); - } - - Map params = new HashMap<>(); - params.put("media_id", materialId); - httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); - try(CloseableHttpResponse response = httpclient.execute(httpPost)){ - String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); - WxError error = WxError.fromJson(responseContent); - if (error.getErrorCode() != 0) { - throw new WxErrorException(error); - } else { - return WxMpMaterialVideoInfoResult.fromJson(responseContent); - } - }finally { - httpPost.releaseConnection(); - } - } - -} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MaterialVoiceAndImageDownloadRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MaterialVoiceAndImageDownloadRequestExecutor.java deleted file mode 100644 index 3a29ccfa..00000000 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MaterialVoiceAndImageDownloadRequestExecutor.java +++ /dev/null @@ -1,66 +0,0 @@ -package me.chanjar.weixin.mp.util.http.apache; - -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.common.util.http.apache.InputStreamResponseHandler; -import me.chanjar.weixin.common.util.json.WxGsonBuilder; -import org.apache.commons.io.IOUtils; -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.StringEntity; -import org.apache.http.impl.client.CloseableHttpClient; - -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.HashMap; -import java.util.Map; - -public class MaterialVoiceAndImageDownloadRequestExecutor implements RequestExecutor { - - - public MaterialVoiceAndImageDownloadRequestExecutor() { - super(); - } - - public MaterialVoiceAndImageDownloadRequestExecutor(File tmpDirFile) { - super(); - } - - @Override - public InputStream execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, IOException { - HttpPost httpPost = new HttpPost(uri); - if (httpProxy != null) { - RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); - httpPost.setConfig(config); - } - - Map params = new HashMap<>(); - params.put("media_id", materialId); - httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); - try (CloseableHttpResponse response = httpclient.execute(httpPost); - InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);){ - // 下载媒体文件出错 - byte[] responseContent = IOUtils.toByteArray(inputStream); - String responseContentString = new String(responseContent, "UTF-8"); - if (responseContentString.length() < 100) { - try { - WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class); - if (wxError.getErrorCode() != 0) { - throw new WxErrorException(wxError); - } - } catch (com.google.gson.JsonSyntaxException ex) { - return new ByteArrayInputStream(responseContent); - } - } - return new ByteArrayInputStream(responseContent); - }finally { - httpPost.releaseConnection(); - } - } - -} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MediaImgUploadRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MediaImgUploadRequestExecutor.java deleted file mode 100644 index e253f4ee..00000000 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MediaImgUploadRequestExecutor.java +++ /dev/null @@ -1,55 +0,0 @@ -package me.chanjar.weixin.mp.util.http.apache; - -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.common.util.http.apache.Utf8ResponseHandler; -import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult; -import org.apache.http.HttpEntity; -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.mime.HttpMultipartMode; -import org.apache.http.entity.mime.MultipartEntityBuilder; -import org.apache.http.impl.client.CloseableHttpClient; - -import java.io.File; -import java.io.IOException; - -/** - * @author miller - */ -public class MediaImgUploadRequestExecutor implements RequestExecutor { - @Override - public WxMediaImgUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File data) throws WxErrorException, IOException { - if (data == null) { - throw new WxErrorException(WxError.newBuilder().setErrorMsg("文件对象为空").build()); - } - - HttpPost httpPost = new HttpPost(uri); - if (httpProxy != null) { - RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); - httpPost.setConfig(config); - } - - HttpEntity entity = MultipartEntityBuilder - .create() - .addBinaryBody("media", data) - .setMode(HttpMultipartMode.RFC6532) - .build(); - httpPost.setEntity(entity); - httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString()); - - try (CloseableHttpResponse response = httpclient.execute(httpPost)) { - String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); - WxError error = WxError.fromJson(responseContent); - if (error.getErrorCode() != 0) { - throw new WxErrorException(error); - } - - return WxMediaImgUploadResult.fromJson(responseContent); - } - } -} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/QrCodeRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/QrCodeRequestExecutor.java deleted file mode 100644 index b0823365..00000000 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/QrCodeRequestExecutor.java +++ /dev/null @@ -1,66 +0,0 @@ -package me.chanjar.weixin.mp.util.http.apache; - -import me.chanjar.weixin.common.bean.result.WxError; -import me.chanjar.weixin.common.exception.WxErrorException; -import me.chanjar.weixin.common.util.fs.FileUtils; -import me.chanjar.weixin.common.util.http.RequestExecutor; -import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler; -import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; -import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket; -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.HttpGet; -import org.apache.http.entity.ContentType; -import org.apache.http.impl.client.CloseableHttpClient; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.net.URLEncoder; -import java.util.UUID; - -/** - * 获得QrCode图片 请求执行器 - * @author chanjarster - * - */ -public class QrCodeRequestExecutor implements RequestExecutor { - - @Override - public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, - WxMpQrCodeTicket ticket) throws WxErrorException, IOException { - if (ticket != null) { - if (uri.indexOf('?') == -1) { - uri += '?'; - } - uri += uri.endsWith("?") - ? "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8") - : "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8"); - } - - HttpGet httpGet = new HttpGet(uri); - if (httpProxy != null) { - RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); - httpGet.setConfig(config); - } - - try (CloseableHttpResponse response = httpclient.execute(httpGet); - InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);) { - Header[] contentTypeHeader = response.getHeaders("Content-Type"); - if (contentTypeHeader != null && contentTypeHeader.length > 0) { - // 出错 - if (ContentType.TEXT_PLAIN.getMimeType().equals(contentTypeHeader[0].getValue())) { - String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); - throw new WxErrorException(WxError.fromJson(responseContent)); - } - } - return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg"); - } finally { - httpGet.releaseConnection(); - } - - } - -} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MaterialDeleteRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MaterialDeleteRequestExecutor.java deleted file mode 100644 index 26485742..00000000 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MaterialDeleteRequestExecutor.java +++ /dev/null @@ -1,39 +0,0 @@ -package me.chanjar.weixin.mp.util.http.jodd; - -import jodd.http.HttpConnectionProvider; -import jodd.http.HttpRequest; -import jodd.http.HttpResponse; -import jodd.http.ProxyInfo; -import me.chanjar.weixin.common.bean.result.WxError; -import me.chanjar.weixin.common.exception.WxErrorException; -import me.chanjar.weixin.common.util.http.RequestExecutor; - -import java.io.IOException; - -public class MaterialDeleteRequestExecutor implements RequestExecutor { - - - public MaterialDeleteRequestExecutor() { - super(); - } - - @Override - public Boolean execute(HttpConnectionProvider httpclient, ProxyInfo httpProxy, String uri, String materialId) throws WxErrorException, IOException { - HttpRequest request = HttpRequest.post(uri); - if (httpProxy != null) { - httpclient.useProxy(httpProxy); - } - request.withConnectionProvider(httpclient); - - request.query("media_id", materialId); - HttpResponse response = request.send(); - String responseContent = response.bodyText(); - WxError error = WxError.fromJson(responseContent); - if (error.getErrorCode() != 0) { - throw new WxErrorException(error); - } else { - return true; - } - } - -} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MaterialNewsInfoRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MaterialNewsInfoRequestExecutor.java deleted file mode 100644 index 93b3a735..00000000 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MaterialNewsInfoRequestExecutor.java +++ /dev/null @@ -1,41 +0,0 @@ -package me.chanjar.weixin.mp.util.http.jodd; - -import jodd.http.HttpConnectionProvider; -import jodd.http.HttpRequest; -import jodd.http.HttpResponse; -import jodd.http.ProxyInfo; -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.mp.bean.material.WxMpMaterialNews; -import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; - -import java.io.IOException; - -public class MaterialNewsInfoRequestExecutor implements RequestExecutor { - - public MaterialNewsInfoRequestExecutor() { - super(); - } - - @Override - public WxMpMaterialNews execute(HttpConnectionProvider httpclient, ProxyInfo httpProxy, String uri, String materialId) throws WxErrorException, IOException { - HttpRequest request = HttpRequest.post(uri); - if (httpProxy != null) { - httpclient.useProxy(httpProxy); - } - request.withConnectionProvider(httpclient); - - request.query("media_id", materialId); - HttpResponse response = request.send(); - - String responseContent = request.bodyText(); - WxError error = WxError.fromJson(responseContent); - if (error.getErrorCode() != 0) { - throw new WxErrorException(error); - } else { - return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class); - } - } - -} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MaterialUploadRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MaterialUploadRequestExecutor.java deleted file mode 100644 index 85b6665e..00000000 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MaterialUploadRequestExecutor.java +++ /dev/null @@ -1,53 +0,0 @@ -package me.chanjar.weixin.mp.util.http.jodd; - -import jodd.http.HttpConnectionProvider; -import jodd.http.HttpRequest; -import jodd.http.HttpResponse; -import jodd.http.ProxyInfo; -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.common.util.json.WxGsonBuilder; -import me.chanjar.weixin.mp.bean.material.WxMpMaterial; -import me.chanjar.weixin.mp.bean.material.WxMpMaterialUploadResult; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.util.Map; - -public class MaterialUploadRequestExecutor implements RequestExecutor { - - @Override - public WxMpMaterialUploadResult execute(HttpConnectionProvider provider, ProxyInfo httpProxy, String uri, WxMpMaterial material) throws WxErrorException, IOException { - HttpRequest request = HttpRequest.post(uri); - if (httpProxy != null) { - provider.useProxy(httpProxy); - } - request.withConnectionProvider(provider); - - if (material == null) { - throw new WxErrorException(WxError.newBuilder().setErrorMsg("非法请求,material参数为空").build()); - } - - File file = material.getFile(); - if (file == null || !file.exists()) { - throw new FileNotFoundException(); - } - request.form("media", file); - Map form = material.getForm(); - if (material.getForm() != null) { - request.form("description", WxGsonBuilder.create().toJson(form)); - } - - HttpResponse response = request.send(); - String responseContent = response.bodyText(); - WxError error = WxError.fromJson(responseContent); - if (error.getErrorCode() != 0) { - throw new WxErrorException(error); - } else { - return WxMpMaterialUploadResult.fromJson(responseContent); - } - } - -} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MaterialVideoInfoRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MaterialVideoInfoRequestExecutor.java deleted file mode 100644 index 7b7272b7..00000000 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MaterialVideoInfoRequestExecutor.java +++ /dev/null @@ -1,39 +0,0 @@ -package me.chanjar.weixin.mp.util.http.jodd; - -import jodd.http.HttpConnectionProvider; -import jodd.http.HttpRequest; -import jodd.http.HttpResponse; -import jodd.http.ProxyInfo; -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.mp.bean.material.WxMpMaterialVideoInfoResult; - -import java.io.IOException; - -public class MaterialVideoInfoRequestExecutor implements RequestExecutor { - - public MaterialVideoInfoRequestExecutor() { - super(); - } - - @Override - public WxMpMaterialVideoInfoResult execute(HttpConnectionProvider provider, ProxyInfo httpProxy, String uri, String materialId) throws WxErrorException, IOException { - HttpRequest request = HttpRequest.post(uri); - if (httpProxy != null) { - provider.useProxy(httpProxy); - } - request.withConnectionProvider(provider); - - request.query("media_id", materialId); - HttpResponse response =request.send(); - String responseContent = response.bodyText(); - WxError error = WxError.fromJson(responseContent); - if (error.getErrorCode() != 0) { - throw new WxErrorException(error); - } else { - return WxMpMaterialVideoInfoResult.fromJson(responseContent); - } - } - -} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MaterialVoiceAndImageDownloadRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MaterialVoiceAndImageDownloadRequestExecutor.java deleted file mode 100644 index 4d988bad..00000000 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MaterialVoiceAndImageDownloadRequestExecutor.java +++ /dev/null @@ -1,57 +0,0 @@ -package me.chanjar.weixin.mp.util.http.jodd; - -import jodd.http.HttpConnectionProvider; -import jodd.http.HttpRequest; -import jodd.http.HttpResponse; -import jodd.http.ProxyInfo; -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.common.util.json.WxGsonBuilder; -import org.apache.commons.io.IOUtils; - -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; - -public class MaterialVoiceAndImageDownloadRequestExecutor implements RequestExecutor { - - - public MaterialVoiceAndImageDownloadRequestExecutor() { - super(); - } - - public MaterialVoiceAndImageDownloadRequestExecutor(File tmpDirFile) { - super(); - } - - @Override - public InputStream execute(HttpConnectionProvider provider, ProxyInfo httpProxy, String uri, String materialId) throws WxErrorException, IOException { - HttpRequest request = HttpRequest.post(uri); - if (httpProxy != null) { - provider.useProxy(httpProxy); - } - request.withConnectionProvider(provider); - - request.query("media_id", materialId); - HttpResponse response = request.send(); - - InputStream inputStream = new ByteArrayInputStream(response.bodyBytes()); - // 下载媒体文件出错 - byte[] responseContent = IOUtils.toByteArray(inputStream); - String responseContentString = new String(responseContent, "UTF-8"); - if (responseContentString.length() < 100) { - try { - WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class); - if (wxError.getErrorCode() != 0) { - throw new WxErrorException(wxError); - } - } catch (com.google.gson.JsonSyntaxException ex) { - return new ByteArrayInputStream(responseContent); - } - } - return new ByteArrayInputStream(responseContent); - } - -} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MediaImgUploadRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MediaImgUploadRequestExecutor.java deleted file mode 100644 index 7c4b13f9..00000000 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MediaImgUploadRequestExecutor.java +++ /dev/null @@ -1,42 +0,0 @@ -package me.chanjar.weixin.mp.util.http.jodd; - -import jodd.http.HttpConnectionProvider; -import jodd.http.HttpRequest; -import jodd.http.HttpResponse; -import jodd.http.ProxyInfo; -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.mp.bean.material.WxMediaImgUploadResult; - -import java.io.File; -import java.io.IOException; - -/** - * @author miller - */ -public class MediaImgUploadRequestExecutor implements RequestExecutor { - @Override - public WxMediaImgUploadResult execute(HttpConnectionProvider provider, ProxyInfo httpProxy, String uri, File data) throws WxErrorException, IOException { - if (data == null) { - throw new WxErrorException(WxError.newBuilder().setErrorMsg("文件对象为空").build()); - } - - HttpRequest request = HttpRequest.post(uri); - if (httpProxy != null) { - provider.useProxy(httpProxy); - } - request.withConnectionProvider(provider); - - request.form("media", data); - HttpResponse response =request.send(); - - String responseContent =response.bodyText(); - WxError error = WxError.fromJson(responseContent); - if (error.getErrorCode() != 0) { - throw new WxErrorException(error); - } - - return WxMediaImgUploadResult.fromJson(responseContent); - } -} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/QrCodeRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/QrCodeRequestExecutor.java deleted file mode 100644 index d1b6f2c5..00000000 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/QrCodeRequestExecutor.java +++ /dev/null @@ -1,60 +0,0 @@ -package me.chanjar.weixin.mp.util.http.jodd; - -import jodd.http.HttpConnectionProvider; -import jodd.http.HttpRequest; -import jodd.http.HttpResponse; -import jodd.http.ProxyInfo; -import jodd.util.MimeTypes; -import me.chanjar.weixin.common.bean.result.WxError; -import me.chanjar.weixin.common.exception.WxErrorException; -import me.chanjar.weixin.common.util.fs.FileUtils; -import me.chanjar.weixin.common.util.http.RequestExecutor; -import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket; - -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.net.URLEncoder; -import java.util.UUID; - -/** - * 获得QrCode图片 请求执行器 - * - * @author chanjarster - */ -public class QrCodeRequestExecutor implements RequestExecutor { - - @Override - public File execute(HttpConnectionProvider provider, ProxyInfo httpProxy, String uri, - WxMpQrCodeTicket ticket) throws WxErrorException, IOException { - if (ticket != null) { - if (uri.indexOf('?') == -1) { - uri += '?'; - } - uri += uri.endsWith("?") - ? "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8") - : "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8"); - } - - - HttpRequest request = HttpRequest.get(uri); - if (httpProxy != null) { - provider.useProxy(httpProxy); - } - request.withConnectionProvider(provider); - - HttpResponse response = request.send(); - try ( - InputStream inputStream = new ByteArrayInputStream(response.bodyBytes());) { - String contentTypeHeader = response.header("Content-Type"); - // 出错 - if (MimeTypes.MIME_TEXT_PLAIN.equals(contentTypeHeader)) { - String responseContent = response.bodyText(); - throw new WxErrorException(WxError.fromJson(responseContent)); - } - return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg"); - } - } - -} diff --git a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpBusyRetryTest.java b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpBusyRetryTest.java index 25dedbaa..74777cae 100644 --- a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpBusyRetryTest.java +++ b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpBusyRetryTest.java @@ -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.mp.api.impl.apache.WxMpServiceImpl; -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 WxMpBusyRetryTest { @Override public synchronized T executeInternal( - RequestExecutor executor, String uri, E data) + RequestExecutor executor, String uri, E data) throws WxErrorException { this.log.info("Executed"); WxError error = new WxError();