@@ -41,14 +41,14 @@ public class MediaDownloadRequestExecutor implements RequestExecutor<File, Strin | |||||
@Override | @Override | ||||
public File execute(RequestHttp requestHttp, String uri, String queryParam) throws WxErrorException, IOException { | public File 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); | 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); | return executeJodd(provider, proxyInfo, uri, queryParam); | ||||
} else { | } else { | ||||
//这里需要抛出异常,需要优化 | //这里需要抛出异常,需要优化 | ||||
@@ -30,14 +30,14 @@ public class MediaUploadRequestExecutor implements RequestExecutor<WxMediaUpload | |||||
@Override | @Override | ||||
public WxMediaUploadResult execute(RequestHttp requestHttp, String uri, File file) throws WxErrorException, IOException { | public WxMediaUploadResult execute(RequestHttp requestHttp, String uri, File file) 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, file); | return executeApache(httpClient, httpProxy, uri, file); | ||||
} | } | ||||
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, file); | return executeJodd(provider, proxyInfo, uri, file); | ||||
} else { | } else { | ||||
//这里需要抛出异常,需要优化 | //这里需要抛出异常,需要优化 | ||||
@@ -9,12 +9,12 @@ public interface RequestHttp { | |||||
* httpClient | * httpClient | ||||
* @return | * @return | ||||
*/ | */ | ||||
Object getHttpClient(); | |||||
Object getRequestHttpClient(); | |||||
/** | /** | ||||
* httpProxy | * httpProxy | ||||
* @return | * @return | ||||
*/ | */ | ||||
Object getHttpProxy(); | |||||
Object getRequestHttpProxy(); | |||||
} | } |
@@ -24,14 +24,14 @@ public class SimpleGetRequestExecutor implements RequestExecutor<String, String> | |||||
@Override | @Override | ||||
public String execute(RequestHttp requestHttp, String uri, String queryParam) throws WxErrorException, IOException { | 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); | 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); | return executeJodd(provider, proxyInfo, uri, queryParam); | ||||
} else { | } else { | ||||
//这里需要抛出异常,需要优化 | //这里需要抛出异常,需要优化 | ||||
@@ -27,14 +27,14 @@ public class SimplePostRequestExecutor implements RequestExecutor<String, String | |||||
@Override | @Override | ||||
public String execute(RequestHttp requestHttp, String uri, String postEntity) throws WxErrorException, IOException { | public String execute(RequestHttp requestHttp, String uri, String postEntity) 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, postEntity); | return executeApache(httpClient, httpProxy, uri, postEntity); | ||||
} | } | ||||
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, postEntity); | return executeJodd(provider, proxyInfo, uri, postEntity); | ||||
} else { | } else { | ||||
//这里需要抛出异常,需要优化 | //这里需要抛出异常,需要优化 | ||||
@@ -6,6 +6,7 @@ import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | |||||
import me.chanjar.weixin.common.exception.WxErrorException; | import me.chanjar.weixin.common.exception.WxErrorException; | ||||
import me.chanjar.weixin.common.session.WxSession; | import me.chanjar.weixin.common.session.WxSession; | ||||
import me.chanjar.weixin.common.session.WxSessionManager; | import me.chanjar.weixin.common.session.WxSessionManager; | ||||
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; | |||||
import me.chanjar.weixin.common.util.http.RequestExecutor; | import me.chanjar.weixin.common.util.http.RequestExecutor; | ||||
import me.chanjar.weixin.cp.bean.WxCpDepart; | import me.chanjar.weixin.cp.bean.WxCpDepart; | ||||
import me.chanjar.weixin.cp.bean.WxCpMessage; | import me.chanjar.weixin.cp.bean.WxCpMessage; | ||||
@@ -20,7 +21,7 @@ import java.util.List; | |||||
/** | /** | ||||
* 微信API的Service | * 微信API的Service | ||||
*/ | */ | ||||
public interface WxCpService<H, P> { | |||||
public interface WxCpService { | |||||
/** | /** | ||||
* <pre> | * <pre> | ||||
@@ -469,7 +470,7 @@ public interface WxCpService<H, P> { | |||||
* <pre> | * <pre> | ||||
* Service没有实现某个API的时候,可以用这个, | * Service没有实现某个API的时候,可以用这个, | ||||
* 比{@link #get}和{@link #post}方法更灵活,可以自己构造RequestExecutor用来处理不同的参数和不同的返回类型。 | * 比{@link #get}和{@link #post}方法更灵活,可以自己构造RequestExecutor用来处理不同的参数和不同的返回类型。 | ||||
* 可以参考,{@link me.chanjar.weixin.common.util.http.jodd.MediaUploadRequestExecutor}的实现方法 | |||||
* 可以参考,{@link MediaUploadRequestExecutor}的实现方法 | |||||
* </pre> | * </pre> | ||||
* | * | ||||
* @param executor 执行器 | * @param executor 执行器 | ||||
@@ -478,9 +479,7 @@ public interface WxCpService<H, P> { | |||||
* @param <T> 请求值类型 | * @param <T> 请求值类型 | ||||
* @param <E> 返回值类型 | * @param <E> 返回值类型 | ||||
*/ | */ | ||||
<T, E> T execute(RequestExecutor<T, H, P, E> executor, String uri, E data) throws WxErrorException; | |||||
<T, E> T executeInternal(RequestExecutor<T, H, P, E> executor, String uri, E data) throws WxErrorException; | |||||
<T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException; | |||||
/** | /** | ||||
* 注入 {@link WxCpConfigStorage} 的实现 | * 注入 {@link WxCpConfigStorage} 的实现 | ||||
@@ -14,9 +14,9 @@ import me.chanjar.weixin.common.session.WxSessionManager; | |||||
import me.chanjar.weixin.common.util.RandomUtils; | import me.chanjar.weixin.common.util.RandomUtils; | ||||
import me.chanjar.weixin.common.util.crypto.SHA1; | import me.chanjar.weixin.common.util.crypto.SHA1; | ||||
import me.chanjar.weixin.common.util.fs.FileUtils; | 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.common.util.json.GsonHelper; | ||||
import me.chanjar.weixin.cp.api.WxCpConfigStorage; | import me.chanjar.weixin.cp.api.WxCpConfigStorage; | ||||
import me.chanjar.weixin.cp.api.WxCpService; | import me.chanjar.weixin.cp.api.WxCpService; | ||||
@@ -41,7 +41,7 @@ import java.io.InputStream; | |||||
import java.util.List; | import java.util.List; | ||||
import java.util.UUID; | import java.util.UUID; | ||||
public class WxCpServiceImpl implements WxCpService<CloseableHttpClient, HttpHost> { | |||||
public class WxCpServiceImpl implements WxCpService, RequestHttp { | |||||
protected final Logger log = LoggerFactory.getLogger(WxCpServiceImpl.class); | protected final Logger log = LoggerFactory.getLogger(WxCpServiceImpl.class); | ||||
@@ -538,7 +538,7 @@ public class WxCpServiceImpl implements WxCpService<CloseableHttpClient, HttpHos | |||||
* 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求 | * 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求 | ||||
*/ | */ | ||||
@Override | @Override | ||||
public <T, E> T execute(RequestExecutor<T,CloseableHttpClient, HttpHost, E> executor, String uri, E data) throws WxErrorException { | |||||
public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException { | |||||
int retryTimes = 0; | int retryTimes = 0; | ||||
do { | do { | ||||
try { | try { | ||||
@@ -574,7 +574,7 @@ public class WxCpServiceImpl implements WxCpService<CloseableHttpClient, HttpHos | |||||
throw new RuntimeException("微信服务端异常,超出重试次数"); | throw new RuntimeException("微信服务端异常,超出重试次数"); | ||||
} | } | ||||
public synchronized <T, E> T executeInternal(RequestExecutor<T,CloseableHttpClient, HttpHost, E> executor, String uri, E data) throws WxErrorException { | |||||
public synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException { | |||||
if (uri.contains("access_token=")) { | if (uri.contains("access_token=")) { | ||||
throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri); | throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri); | ||||
} | } | ||||
@@ -584,7 +584,7 @@ public class WxCpServiceImpl implements WxCpService<CloseableHttpClient, HttpHos | |||||
uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken; | uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken; | ||||
try { | try { | ||||
return executor.execute(getHttpclient(), this.httpProxy, uriWithAccessToken, data); | |||||
return executor.execute(this, uriWithAccessToken, data); | |||||
} catch (WxErrorException e) { | } catch (WxErrorException e) { | ||||
WxError error = e.getError(); | WxError error = e.getError(); | ||||
/* | /* | ||||
@@ -698,4 +698,13 @@ public class WxCpServiceImpl implements WxCpService<CloseableHttpClient, HttpHos | |||||
} | } | ||||
@Override | |||||
public Object getRequestHttpClient() { | |||||
return this.httpClient; | |||||
} | |||||
@Override | |||||
public Object getRequestHttpProxy() { | |||||
return this.httpProxy; | |||||
} | |||||
} | } |
@@ -4,8 +4,6 @@ import me.chanjar.weixin.common.bean.result.WxError; | |||||
import me.chanjar.weixin.common.exception.WxErrorException; | import me.chanjar.weixin.common.exception.WxErrorException; | ||||
import me.chanjar.weixin.common.util.http.RequestExecutor; | import me.chanjar.weixin.common.util.http.RequestExecutor; | ||||
import me.chanjar.weixin.cp.api.WxCpService; | import me.chanjar.weixin.cp.api.WxCpService; | ||||
import org.apache.http.HttpHost; | |||||
import org.apache.http.impl.client.CloseableHttpClient; | |||||
import org.testng.annotations.DataProvider; | import org.testng.annotations.DataProvider; | ||||
import org.testng.annotations.Test; | import org.testng.annotations.Test; | ||||
@@ -23,7 +21,7 @@ public class WxCpBusyRetryTest { | |||||
@Override | @Override | ||||
public synchronized <T, E> T executeInternal( | public synchronized <T, E> T executeInternal( | ||||
RequestExecutor<T,CloseableHttpClient, HttpHost, E> executor, String uri, E data) | |||||
RequestExecutor<T, E> executor, String uri, E data) | |||||
throws WxErrorException { | throws WxErrorException { | ||||
this.log.info("Executed"); | this.log.info("Executed"); | ||||
WxError error = new WxError(); | WxError error = new WxError(); | ||||
@@ -14,7 +14,7 @@ public interface WxMpCardService<H, P> { | |||||
* 得到WxMpService | * 得到WxMpService | ||||
* @return | * @return | ||||
*/ | */ | ||||
WxMpService<H, P> getWxMpService(); | |||||
WxMpService getWxMpService(); | |||||
/** | /** | ||||
* 获得卡券api_ticket,不强制刷新卡券api_ticket | * 获得卡券api_ticket,不强制刷新卡券api_ticket | ||||
@@ -2,15 +2,15 @@ package me.chanjar.weixin.mp.api; | |||||
import me.chanjar.weixin.common.bean.WxJsapiSignature; | import me.chanjar.weixin.common.bean.WxJsapiSignature; | ||||
import me.chanjar.weixin.common.exception.WxErrorException; | 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.RequestExecutor; | ||||
import me.chanjar.weixin.common.util.http.apache.MediaUploadRequestExecutor; | |||||
import me.chanjar.weixin.mp.bean.*; | import me.chanjar.weixin.mp.bean.*; | ||||
import me.chanjar.weixin.mp.bean.result.*; | import me.chanjar.weixin.mp.bean.result.*; | ||||
/** | /** | ||||
* 微信API的Service | * 微信API的Service | ||||
*/ | */ | ||||
public interface WxMpService<H, P> { | |||||
public interface WxMpService { | |||||
/** | /** | ||||
* <pre> | * <pre> | ||||
@@ -133,7 +133,6 @@ public interface WxMpService<H, P> { | |||||
* 长链接转短链接接口 | * 长链接转短链接接口 | ||||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=长链接转短链接接口 | * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=长链接转短链接接口 | ||||
* </pre> | * </pre> | ||||
* | |||||
*/ | */ | ||||
String shortUrl(String long_url) throws WxErrorException; | String shortUrl(String long_url) throws WxErrorException; | ||||
@@ -153,8 +152,8 @@ public interface WxMpService<H, P> { | |||||
* </pre> | * </pre> | ||||
* | * | ||||
* @param redirectURI 用户授权完成后的重定向链接,无需urlencode, 方法内会进行encode | * @param redirectURI 用户授权完成后的重定向链接,无需urlencode, 方法内会进行encode | ||||
* @param scope 应用授权作用域,拥有多个作用域用逗号(,)分隔,网页应用目前仅填写snsapi_login即可 | |||||
* @param state 非必填,用于保持请求和回调的状态,授权请求后原样带回给第三方。该参数可用于防止csrf攻击(跨站请求伪造攻击),建议第三方带上该参数,可设置为简单的随机数加session进行校验 | |||||
* @param scope 应用授权作用域,拥有多个作用域用逗号(,)分隔,网页应用目前仅填写snsapi_login即可 | |||||
* @param state 非必填,用于保持请求和回调的状态,授权请求后原样带回给第三方。该参数可用于防止csrf攻击(跨站请求伪造攻击),建议第三方带上该参数,可设置为简单的随机数加session进行校验 | |||||
* @return url | * @return url | ||||
*/ | */ | ||||
String buildQrConnectUrl(String redirectURI, String scope, String state); | String buildQrConnectUrl(String redirectURI, String scope, String state); | ||||
@@ -190,7 +189,7 @@ public interface WxMpService<H, P> { | |||||
* 用oauth2获取用户信息, 当前面引导授权时的scope是snsapi_userinfo的时候才可以 | * 用oauth2获取用户信息, 当前面引导授权时的scope是snsapi_userinfo的时候才可以 | ||||
* </pre> | * </pre> | ||||
* | * | ||||
* @param lang zh_CN, zh_TW, en | |||||
* @param lang zh_CN, zh_TW, en | |||||
*/ | */ | ||||
WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, String lang) throws WxErrorException; | WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, String lang) throws WxErrorException; | ||||
@@ -198,7 +197,6 @@ public interface WxMpService<H, P> { | |||||
* <pre> | * <pre> | ||||
* 验证oauth2的access token是否有效 | * 验证oauth2的access token是否有效 | ||||
* </pre> | * </pre> | ||||
* | |||||
*/ | */ | ||||
boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken); | boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken); | ||||
@@ -227,15 +225,12 @@ public interface WxMpService<H, P> { | |||||
* 可以参考,{@link MediaUploadRequestExecutor}的实现方法 | * 可以参考,{@link MediaUploadRequestExecutor}的实现方法 | ||||
* </pre> | * </pre> | ||||
*/ | */ | ||||
<T, E> T execute(RequestExecutor<T, H, P, E> executor, String uri, E data) throws WxErrorException; | |||||
<T, E> T executeInternal(RequestExecutor<T, H, P, E> executor, String uri, E data) throws WxErrorException; | |||||
<T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException; | |||||
/** | |||||
* 获取代理对象 | |||||
*/ | |||||
//HttpHost getHttpProxy(); | |||||
/** | |||||
* 获取代理对象 | |||||
*/ | |||||
//HttpHost getRequestHttpProxy(); | |||||
/** | /** | ||||
* 注入 {@link WxMpConfigStorage} 的实现 | * 注入 {@link WxMpConfigStorage} 的实现 | ||||
@@ -350,15 +345,13 @@ public interface WxMpService<H, P> { | |||||
WxMpDeviceService getDeviceService(); | WxMpDeviceService getDeviceService(); | ||||
/** | /** | ||||
* | |||||
* @return | * @return | ||||
*/ | */ | ||||
H getHttpclient(); | |||||
//Object getHttpclient(); | |||||
/** | /** | ||||
* | |||||
* @return | * @return | ||||
*/ | */ | ||||
P getHttpProxy(); | |||||
//Object getHttpProxy(); | |||||
} | } |
@@ -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.JsonElement; | ||||
import com.google.gson.JsonObject; | 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.exception.WxErrorException; | ||||
import me.chanjar.weixin.common.util.RandomUtils; | import me.chanjar.weixin.common.util.RandomUtils; | ||||
import me.chanjar.weixin.common.util.crypto.SHA1; | 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.WxMpCardService; | ||||
import me.chanjar.weixin.mp.api.WxMpService; | import me.chanjar.weixin.mp.api.WxMpService; | ||||
import me.chanjar.weixin.mp.bean.result.WxMpCardResult; | import me.chanjar.weixin.mp.bean.result.WxMpCardResult; | ||||
@@ -30,7 +30,7 @@ public class WxMpCardServiceImpl implements WxMpCardService<CloseableHttpClient, | |||||
private final Logger log = LoggerFactory.getLogger(WxMpCardServiceImpl.class); | private final Logger log = LoggerFactory.getLogger(WxMpCardServiceImpl.class); | ||||
private WxMpService<CloseableHttpClient, HttpHost> wxMpService; | |||||
private WxMpService wxMpService; | |||||
public WxMpCardServiceImpl(WxMpService wxMpService) { | public WxMpCardServiceImpl(WxMpService wxMpService) { | ||||
this.wxMpService = wxMpService; | this.wxMpService = wxMpService; | ||||
@@ -41,7 +41,7 @@ public class WxMpCardServiceImpl implements WxMpCardService<CloseableHttpClient, | |||||
* @return | * @return | ||||
*/ | */ | ||||
@Override | @Override | ||||
public WxMpService<CloseableHttpClient, HttpHost> getWxMpService(){ | |||||
public WxMpService getWxMpService(){ | |||||
return this.wxMpService; | return this.wxMpService; | ||||
} | } | ||||
@@ -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 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.WxError; | ||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | ||||
import me.chanjar.weixin.common.exception.WxErrorException; | 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.WxMpKefuService; | ||||
import me.chanjar.weixin.mp.api.WxMpService; | import me.chanjar.weixin.mp.api.WxMpService; | ||||
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage; | import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage; | ||||
@@ -29,7 +27,7 @@ public class WxMpKefuServiceImpl implements WxMpKefuService { | |||||
.getLogger(WxMpKefuServiceImpl.class); | .getLogger(WxMpKefuServiceImpl.class); | ||||
private static final String API_URL_PREFIX = "https://api.weixin.qq.com/customservice"; | 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 static final String API_URL_PREFIX_WITH_CGI_BIN = "https://api.weixin.qq.com/cgi-bin/customservice"; | ||||
private WxMpService<HttpConnectionProvider, ProxyInfo> wxMpService; | |||||
private WxMpService wxMpService; | |||||
public WxMpKefuServiceImpl(WxMpService wxMpService) { | public WxMpKefuServiceImpl(WxMpService wxMpService) { | ||||
this.wxMpService = wxMpService; | this.wxMpService = wxMpService; |
@@ -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.api.WxConsts; | ||||
import me.chanjar.weixin.common.bean.result.WxError; | import me.chanjar.weixin.common.bean.result.WxError; | ||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | ||||
import me.chanjar.weixin.common.exception.WxErrorException; | import me.chanjar.weixin.common.exception.WxErrorException; | ||||
import me.chanjar.weixin.common.util.fs.FileUtils; | 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.common.util.json.WxGsonBuilder; | ||||
import me.chanjar.weixin.mp.api.WxMpMaterialService; | import me.chanjar.weixin.mp.api.WxMpMaterialService; | ||||
import me.chanjar.weixin.mp.api.WxMpService; | import me.chanjar.weixin.mp.api.WxMpService; | ||||
import me.chanjar.weixin.mp.bean.material.*; | 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 me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | ||||
import java.io.File; | import java.io.File; | ||||
@@ -29,7 +27,7 @@ import java.util.UUID; | |||||
public class WxMpMaterialServiceImpl implements WxMpMaterialService { | 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 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 static final String MATERIAL_API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/material"; | ||||
private WxMpService<HttpConnectionProvider, ProxyInfo> wxMpService; | |||||
private WxMpService wxMpService; | |||||
public WxMpMaterialServiceImpl(WxMpService wxMpService) { | public WxMpMaterialServiceImpl(WxMpService wxMpService) { | ||||
this.wxMpService = wxMpService; | this.wxMpService = wxMpService; |
@@ -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 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.WxError; | ||||
import me.chanjar.weixin.common.exception.WxErrorException; | import me.chanjar.weixin.common.exception.WxErrorException; | ||||
import me.chanjar.weixin.mp.api.WxMpQrcodeService; | import me.chanjar.weixin.mp.api.WxMpQrcodeService; | ||||
import me.chanjar.weixin.mp.api.WxMpService; | import me.chanjar.weixin.mp.api.WxMpService; | ||||
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket; | 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.File; | ||||
import java.io.UnsupportedEncodingException; | import java.io.UnsupportedEncodingException; | ||||
@@ -20,7 +18,7 @@ import java.nio.charset.StandardCharsets; | |||||
*/ | */ | ||||
public class WxMpQrcodeServiceImpl implements WxMpQrcodeService { | public class WxMpQrcodeServiceImpl implements WxMpQrcodeService { | ||||
private static final String API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/qrcode"; | private static final String API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/qrcode"; | ||||
private WxMpService<HttpConnectionProvider, ProxyInfo> wxMpService; | |||||
private WxMpService wxMpService; | |||||
public WxMpQrcodeServiceImpl(WxMpService wxMpService) { | public WxMpQrcodeServiceImpl(WxMpService wxMpService) { | ||||
this.wxMpService = wxMpService; | this.wxMpService = wxMpService; |
@@ -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.Gson; | ||||
import com.google.gson.JsonObject; | 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.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.WxMpService; | ||||
import me.chanjar.weixin.mp.api.WxMpUserBlacklistService; | import me.chanjar.weixin.mp.api.WxMpUserBlacklistService; | ||||
import me.chanjar.weixin.mp.bean.result.WxMpUserBlacklistGetResult; | import me.chanjar.weixin.mp.bean.result.WxMpUserBlacklistGetResult; | ||||
@@ -19,7 +17,7 @@ import java.util.Map; | |||||
*/ | */ | ||||
public class WxMpUserBlacklistServiceImpl implements WxMpUserBlacklistService { | public class WxMpUserBlacklistServiceImpl implements WxMpUserBlacklistService { | ||||
private static final String API_BLACKLIST_PREFIX = "https://api.weixin.qq.com/cgi-bin/tags/members"; | private static final String API_BLACKLIST_PREFIX = "https://api.weixin.qq.com/cgi-bin/tags/members"; | ||||
private WxMpService<HttpConnectionProvider, ProxyInfo> wxMpService; | |||||
private WxMpService wxMpService; | |||||
public WxMpUserBlacklistServiceImpl(WxMpService wxMpService) { | public WxMpUserBlacklistServiceImpl(WxMpService wxMpService) { | ||||
this.wxMpService = wxMpService; | this.wxMpService = wxMpService; |
@@ -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<CloseableHttpClient, HttpHost> 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; | |||||
} | |||||
} |
@@ -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<CloseableHttpClient, HttpHost> 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<String, Object> 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<String, Object> 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); | |||||
} | |||||
} | |||||
} |
@@ -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<CloseableHttpClient, HttpHost> 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); | |||||
} | |||||
} |
@@ -12,12 +12,9 @@ import me.chanjar.weixin.common.session.StandardSessionManager; | |||||
import me.chanjar.weixin.common.session.WxSessionManager; | import me.chanjar.weixin.common.session.WxSessionManager; | ||||
import me.chanjar.weixin.common.util.RandomUtils; | import me.chanjar.weixin.common.util.RandomUtils; | ||||
import me.chanjar.weixin.common.util.crypto.SHA1; | 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.ApacheHttpClientBuilder; | ||||
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder; | 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.*; | ||||
import me.chanjar.weixin.mp.api.impl.*; | import me.chanjar.weixin.mp.api.impl.*; | ||||
import me.chanjar.weixin.mp.bean.*; | import me.chanjar.weixin.mp.bean.*; | ||||
@@ -34,7 +31,7 @@ import org.slf4j.LoggerFactory; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.util.concurrent.locks.Lock; | import java.util.concurrent.locks.Lock; | ||||
public class WxMpServiceImpl implements WxMpService<CloseableHttpClient, HttpHost> { | |||||
public class WxMpServiceImpl implements WxMpService,RequestHttp { | |||||
private static final JsonParser JSON_PARSER = new JsonParser(); | private static final JsonParser JSON_PARSER = new JsonParser(); | ||||
@@ -248,8 +245,8 @@ public class WxMpServiceImpl implements WxMpService<CloseableHttpClient, HttpHos | |||||
private WxMpOAuth2AccessToken getOAuth2AccessToken(StringBuilder url) throws WxErrorException { | private WxMpOAuth2AccessToken getOAuth2AccessToken(StringBuilder url) throws WxErrorException { | ||||
try { | try { | ||||
RequestExecutor<String, CloseableHttpClient, HttpHost, String> executor = new SimpleGetRequestExecutor(); | |||||
String responseText = executor.execute(this.getHttpclient(), this.httpProxy, url.toString(), null); | |||||
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); | |||||
String responseText = executor.execute(this, url.toString(), null); | |||||
return WxMpOAuth2AccessToken.fromJson(responseText); | return WxMpOAuth2AccessToken.fromJson(responseText); | ||||
} catch (IOException e) { | } catch (IOException e) { | ||||
throw new RuntimeException(e); | throw new RuntimeException(e); | ||||
@@ -292,8 +289,8 @@ public class WxMpServiceImpl implements WxMpService<CloseableHttpClient, HttpHos | |||||
} | } | ||||
try { | try { | ||||
RequestExecutor<String, CloseableHttpClient, HttpHost, String> executor = new SimpleGetRequestExecutor(); | |||||
String responseText = executor.execute(getHttpclient(), this.httpProxy, url.toString(), null); | |||||
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); | |||||
String responseText = executor.execute(this, url.toString(), null); | |||||
return WxMpUser.fromJson(responseText); | return WxMpUser.fromJson(responseText); | ||||
} catch (IOException e) { | } catch (IOException e) { | ||||
throw new RuntimeException(e); | throw new RuntimeException(e); | ||||
@@ -308,8 +305,8 @@ public class WxMpServiceImpl implements WxMpService<CloseableHttpClient, HttpHos | |||||
url.append("&openid=").append(oAuth2AccessToken.getOpenId()); | url.append("&openid=").append(oAuth2AccessToken.getOpenId()); | ||||
try { | try { | ||||
RequestExecutor<String, CloseableHttpClient, HttpHost, String> executor = new SimpleGetRequestExecutor(); | |||||
executor.execute(getHttpclient(), this.httpProxy, url.toString(), null); | |||||
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); | |||||
executor.execute(this, url.toString(), null); | |||||
} catch (IOException e) { | } catch (IOException e) { | ||||
throw new RuntimeException(e); | throw new RuntimeException(e); | ||||
} catch (WxErrorException e) { | } catch (WxErrorException e) { | ||||
@@ -345,7 +342,7 @@ public class WxMpServiceImpl implements WxMpService<CloseableHttpClient, HttpHos | |||||
* 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求 | * 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求 | ||||
*/ | */ | ||||
@Override | @Override | ||||
public <T, E> T execute(RequestExecutor<T, CloseableHttpClient, HttpHost, E> executor, String uri, E data) throws WxErrorException { | |||||
public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException { | |||||
int retryTimes = 0; | int retryTimes = 0; | ||||
do { | do { | ||||
try { | try { | ||||
@@ -379,8 +376,7 @@ public class WxMpServiceImpl implements WxMpService<CloseableHttpClient, HttpHos | |||||
throw new RuntimeException("微信服务端异常,超出重试次数"); | throw new RuntimeException("微信服务端异常,超出重试次数"); | ||||
} | } | ||||
@Override | |||||
public synchronized <T, E> T executeInternal(RequestExecutor<T, CloseableHttpClient, HttpHost, E> executor, String uri, E data) throws WxErrorException { | |||||
public synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException { | |||||
if (uri.indexOf("access_token=") != -1) { | if (uri.indexOf("access_token=") != -1) { | ||||
throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri); | throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri); | ||||
} | } | ||||
@@ -390,7 +386,7 @@ public class WxMpServiceImpl implements WxMpService<CloseableHttpClient, HttpHos | |||||
uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken; | uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken; | ||||
try { | try { | ||||
return executor.execute(getHttpclient(), getHttpProxy(), uriWithAccessToken, data); | |||||
return executor.execute(this, uriWithAccessToken, data); | |||||
} catch (WxErrorException e) { | } catch (WxErrorException e) { | ||||
WxError error = e.getError(); | WxError error = e.getError(); | ||||
/* | /* | ||||
@@ -417,12 +413,12 @@ public class WxMpServiceImpl implements WxMpService<CloseableHttpClient, HttpHos | |||||
} | } | ||||
} | } | ||||
@Override | |||||
//@Override | |||||
public HttpHost getHttpProxy() { | public HttpHost getHttpProxy() { | ||||
return this.httpProxy; | return this.httpProxy; | ||||
} | } | ||||
@Override | |||||
//@Override | |||||
public CloseableHttpClient getHttpclient() { | public CloseableHttpClient getHttpclient() { | ||||
return this.httpClient; | return this.httpClient; | ||||
} | } | ||||
@@ -526,4 +522,14 @@ public class WxMpServiceImpl implements WxMpService<CloseableHttpClient, HttpHos | |||||
public WxMpDeviceService getDeviceService() { | public WxMpDeviceService getDeviceService() { | ||||
return this.deviceService; | return this.deviceService; | ||||
} | } | ||||
@Override | |||||
public Object getRequestHttpClient() { | |||||
return this.httpClient; | |||||
} | |||||
@Override | |||||
public Object getRequestHttpProxy() { | |||||
return this.httpProxy; | |||||
} | |||||
} | } |
@@ -1,52 +0,0 @@ | |||||
package me.chanjar.weixin.mp.api.impl.apache; | |||||
import com.google.gson.Gson; | |||||
import com.google.gson.JsonObject; | |||||
import me.chanjar.weixin.common.exception.WxErrorException; | |||||
import me.chanjar.weixin.common.util.http.apache.SimplePostRequestExecutor; | |||||
import me.chanjar.weixin.mp.api.WxMpService; | |||||
import me.chanjar.weixin.mp.api.WxMpUserBlacklistService; | |||||
import me.chanjar.weixin.mp.bean.result.WxMpUserBlacklistGetResult; | |||||
import org.apache.http.HttpHost; | |||||
import org.apache.http.impl.client.CloseableHttpClient; | |||||
import java.util.HashMap; | |||||
import java.util.List; | |||||
import java.util.Map; | |||||
/** | |||||
* @author miller | |||||
*/ | |||||
public class WxMpUserBlacklistServiceImpl implements WxMpUserBlacklistService { | |||||
private static final String API_BLACKLIST_PREFIX = "https://api.weixin.qq.com/cgi-bin/tags/members"; | |||||
private WxMpService<CloseableHttpClient, HttpHost> 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<String> openidList) throws WxErrorException { | |||||
Map<String, Object> 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<String> openidList) throws WxErrorException { | |||||
Map<String, Object> map = new HashMap<>(); | |||||
map.put("openid_list", openidList); | |||||
String url = API_BLACKLIST_PREFIX + "/batchunblacklist"; | |||||
this.wxMpService.execute(new SimplePostRequestExecutor(), url, new Gson().toJson(map)); | |||||
} | |||||
} |
@@ -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<HttpConnectionProvider, ProxyInfo> { | |||||
private final Logger log = LoggerFactory.getLogger(WxMpCardServiceImpl.class); | |||||
private WxMpService<HttpConnectionProvider, ProxyInfo> wxMpService; | |||||
public WxMpCardServiceImpl(WxMpService wxMpService) { | |||||
this.wxMpService = wxMpService; | |||||
} | |||||
/** | |||||
* 得到WxMpService | |||||
* @return | |||||
*/ | |||||
@Override | |||||
public WxMpService<HttpConnectionProvider, ProxyInfo> getWxMpService(){ | |||||
return this.wxMpService; | |||||
} | |||||
/** | |||||
* 获得卡券api_ticket,不强制刷新卡券api_ticket | |||||
* | |||||
* @return 卡券api_ticket | |||||
* @see #getCardApiTicket(boolean) | |||||
*/ | |||||
@Override | |||||
public String getCardApiTicket() throws WxErrorException { | |||||
return getCardApiTicket(false); | |||||
} | |||||
/** | |||||
* <pre> | |||||
* 获得卡券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 | |||||
* </pre> | |||||
* | |||||
* @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(); | |||||
} | |||||
/** | |||||
* <pre> | |||||
* 创建调用卡券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 | |||||
* </pre> | |||||
* | |||||
* @param optionalSignParam 参与签名的参数数组。 | |||||
* 可以为下列字段:app_id, card_id, card_type, code, openid, location_id | |||||
* </br>注意:当做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<WxMpCardResult>() { | |||||
}.getType()); | |||||
} | |||||
/** | |||||
* 卡券Code核销。核销失败会抛出异常 | |||||
* | |||||
* @param code 单张卡券的唯一标准 | |||||
* @return 调用返回的JSON字符串。 | |||||
* <br>可用 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字符串。 | |||||
* <br>可用 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<WxMpCardResult>() { }.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; | |||||
} | |||||
} |
@@ -14,10 +14,7 @@ import me.chanjar.weixin.common.session.StandardSessionManager; | |||||
import me.chanjar.weixin.common.session.WxSessionManager; | import me.chanjar.weixin.common.session.WxSessionManager; | ||||
import me.chanjar.weixin.common.util.RandomUtils; | import me.chanjar.weixin.common.util.RandomUtils; | ||||
import me.chanjar.weixin.common.util.crypto.SHA1; | 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.*; | ||||
import me.chanjar.weixin.mp.api.impl.*; | import me.chanjar.weixin.mp.api.impl.*; | ||||
import me.chanjar.weixin.mp.bean.*; | import me.chanjar.weixin.mp.bean.*; | ||||
@@ -28,7 +25,7 @@ import org.slf4j.LoggerFactory; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.util.concurrent.locks.Lock; | import java.util.concurrent.locks.Lock; | ||||
public class WxMpServiceImpl implements WxMpService<HttpConnectionProvider, ProxyInfo> { | |||||
public class WxMpServiceImpl implements WxMpService,RequestHttp { | |||||
private static final JsonParser JSON_PARSER = new JsonParser(); | private static final JsonParser JSON_PARSER = new JsonParser(); | ||||
@@ -248,8 +245,8 @@ public class WxMpServiceImpl implements WxMpService<HttpConnectionProvider, Prox | |||||
private WxMpOAuth2AccessToken getOAuth2AccessToken(StringBuilder url) throws WxErrorException { | private WxMpOAuth2AccessToken getOAuth2AccessToken(StringBuilder url) throws WxErrorException { | ||||
try { | try { | ||||
RequestExecutor<String, HttpConnectionProvider, ProxyInfo, String> executor = new SimpleGetRequestExecutor(); | |||||
String responseText = executor.execute(getHttpclient(), getHttpProxy(), url.toString(), null); | |||||
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); | |||||
String responseText = executor.execute(this, url.toString(), null); | |||||
return WxMpOAuth2AccessToken.fromJson(responseText); | return WxMpOAuth2AccessToken.fromJson(responseText); | ||||
} catch (IOException e) { | } catch (IOException e) { | ||||
throw new RuntimeException(e); | throw new RuntimeException(e); | ||||
@@ -292,8 +289,8 @@ public class WxMpServiceImpl implements WxMpService<HttpConnectionProvider, Prox | |||||
} | } | ||||
try { | try { | ||||
RequestExecutor<String, HttpConnectionProvider, ProxyInfo, String> executor = new SimpleGetRequestExecutor(); | |||||
String responseText = executor.execute(getHttpclient(), getHttpProxy(), url.toString(), null); | |||||
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); | |||||
String responseText = executor.execute(this, url.toString(), null); | |||||
return WxMpUser.fromJson(responseText); | return WxMpUser.fromJson(responseText); | ||||
} catch (IOException e) { | } catch (IOException e) { | ||||
throw new RuntimeException(e); | throw new RuntimeException(e); | ||||
@@ -308,8 +305,8 @@ public class WxMpServiceImpl implements WxMpService<HttpConnectionProvider, Prox | |||||
url.append("&openid=").append(oAuth2AccessToken.getOpenId()); | url.append("&openid=").append(oAuth2AccessToken.getOpenId()); | ||||
try { | try { | ||||
RequestExecutor<String, HttpConnectionProvider, ProxyInfo, String> executor = new SimpleGetRequestExecutor(); | |||||
executor.execute(getHttpclient(), getHttpProxy(), url.toString(), null); | |||||
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); | |||||
executor.execute(this, url.toString(), null); | |||||
} catch (IOException e) { | } catch (IOException e) { | ||||
throw new RuntimeException(e); | throw new RuntimeException(e); | ||||
} catch (WxErrorException e) { | } catch (WxErrorException e) { | ||||
@@ -341,15 +338,20 @@ public class WxMpServiceImpl implements WxMpService<HttpConnectionProvider, Prox | |||||
return execute(new SimplePostRequestExecutor(), url, postData); | return execute(new SimplePostRequestExecutor(), url, postData); | ||||
} | } | ||||
@Override | |||||
//@Override | |||||
public HttpConnectionProvider getHttpclient() { | public HttpConnectionProvider getHttpclient() { | ||||
return this.httpClient; | return this.httpClient; | ||||
} | } | ||||
//@Override | |||||
public Object getHttpProxy() { | |||||
return null; | |||||
} | |||||
/** | /** | ||||
* 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求 | * 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求 | ||||
*/ | */ | ||||
public <T, E> T execute(RequestExecutor<T, HttpConnectionProvider, ProxyInfo, E> executor, String uri, E data) throws WxErrorException { | |||||
public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException { | |||||
int retryTimes = 0; | int retryTimes = 0; | ||||
do { | do { | ||||
try { | try { | ||||
@@ -383,8 +385,7 @@ public class WxMpServiceImpl implements WxMpService<HttpConnectionProvider, Prox | |||||
throw new RuntimeException("微信服务端异常,超出重试次数"); | throw new RuntimeException("微信服务端异常,超出重试次数"); | ||||
} | } | ||||
@Override | |||||
public synchronized <T, E> T executeInternal(RequestExecutor<T, HttpConnectionProvider, ProxyInfo, E> executor, String uri, E data) throws WxErrorException { | |||||
public synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException { | |||||
if (uri.indexOf("access_token=") != -1) { | if (uri.indexOf("access_token=") != -1) { | ||||
throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri); | throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri); | ||||
} | } | ||||
@@ -394,7 +395,7 @@ public class WxMpServiceImpl implements WxMpService<HttpConnectionProvider, Prox | |||||
uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken; | uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken; | ||||
try { | try { | ||||
return executor.execute(getHttpclient(), getHttpProxy(), uriWithAccessToken, data); | |||||
return executor.execute(this, uriWithAccessToken, data); | |||||
} catch (WxErrorException e) { | } catch (WxErrorException e) { | ||||
WxError error = e.getError(); | WxError error = e.getError(); | ||||
/* | /* | ||||
@@ -422,7 +423,12 @@ public class WxMpServiceImpl implements WxMpService<HttpConnectionProvider, Prox | |||||
} | } | ||||
@Override | @Override | ||||
public ProxyInfo getHttpProxy() { | |||||
public Object getRequestHttpClient() { | |||||
return this.httpClient; | |||||
} | |||||
@Override | |||||
public Object getRequestHttpProxy() { | |||||
return this.httpProxy; | return this.httpProxy; | ||||
} | } | ||||
@@ -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 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<Boolean, String> { | |||||
public MaterialDeleteRequestExecutor() { | |||||
super(); | |||||
} | |||||
@Override | |||||
public Boolean 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 Boolean 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 true; | |||||
} | |||||
} | |||||
private Boolean 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<String, String> 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(); | |||||
} | |||||
} | |||||
} |
@@ -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<WxMpMaterialNews, String> { | |||||
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<String, String> 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); | |||||
} | |||||
} | |||||
} |
@@ -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<WxMpMaterialUploadResult, WxMpMaterial> { | |||||
@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<String, String> 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<String, String> 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(); | |||||
} | |||||
} | |||||
} |
@@ -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<WxMpMaterialVideoInfoResult, String> { | |||||
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<String, String> 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(); | |||||
} | |||||
} | |||||
} |
@@ -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<InputStream, String> { | |||||
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<String, String> 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(); | |||||
} | |||||
} | |||||
} |
@@ -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<WxMediaImgUploadResult, File> { | |||||
@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); | |||||
} | |||||
} | |||||
} |
@@ -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<File, WxMpQrCodeTicket> { | |||||
@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(); | |||||
} | |||||
} | |||||
} |
@@ -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<Boolean,CloseableHttpClient, HttpHost, String> { | |||||
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<String, String> 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(); | |||||
} | |||||
} | |||||
} |
@@ -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<WxMpMaterialNews,CloseableHttpClient, HttpHost, String> { | |||||
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<String, String> 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(); | |||||
} | |||||
} | |||||
} |
@@ -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<WxMpMaterialUploadResult,CloseableHttpClient, HttpHost, WxMpMaterial> { | |||||
@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<String, String> 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(); | |||||
} | |||||
} | |||||
} |
@@ -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<WxMpMaterialVideoInfoResult, CloseableHttpClient, HttpHost, String> { | |||||
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<String, String> 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(); | |||||
} | |||||
} | |||||
} |
@@ -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<InputStream,CloseableHttpClient, HttpHost, String> { | |||||
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<String, String> 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(); | |||||
} | |||||
} | |||||
} |
@@ -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<WxMediaImgUploadResult, CloseableHttpClient, HttpHost, File> { | |||||
@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); | |||||
} | |||||
} | |||||
} |
@@ -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<File, CloseableHttpClient, HttpHost, WxMpQrCodeTicket> { | |||||
@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(); | |||||
} | |||||
} | |||||
} |
@@ -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<Boolean, HttpConnectionProvider, ProxyInfo, String> { | |||||
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; | |||||
} | |||||
} | |||||
} |
@@ -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<WxMpMaterialNews, HttpConnectionProvider, ProxyInfo, String> { | |||||
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); | |||||
} | |||||
} | |||||
} |
@@ -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<WxMpMaterialUploadResult, HttpConnectionProvider, ProxyInfo, WxMpMaterial> { | |||||
@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<String, String> 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); | |||||
} | |||||
} | |||||
} |
@@ -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<WxMpMaterialVideoInfoResult, HttpConnectionProvider, ProxyInfo, String> { | |||||
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); | |||||
} | |||||
} | |||||
} |
@@ -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<InputStream, HttpConnectionProvider, ProxyInfo, String> { | |||||
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); | |||||
} | |||||
} |
@@ -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<WxMediaImgUploadResult, HttpConnectionProvider, ProxyInfo, File> { | |||||
@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); | |||||
} | |||||
} |
@@ -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<File, HttpConnectionProvider, ProxyInfo, WxMpQrCodeTicket> { | |||||
@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"); | |||||
} | |||||
} | |||||
} |
@@ -4,8 +4,6 @@ import me.chanjar.weixin.common.bean.result.WxError; | |||||
import me.chanjar.weixin.common.exception.WxErrorException; | import me.chanjar.weixin.common.exception.WxErrorException; | ||||
import me.chanjar.weixin.common.util.http.RequestExecutor; | import me.chanjar.weixin.common.util.http.RequestExecutor; | ||||
import me.chanjar.weixin.mp.api.impl.apache.WxMpServiceImpl; | 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.DataProvider; | ||||
import org.testng.annotations.Test; | import org.testng.annotations.Test; | ||||
@@ -23,7 +21,7 @@ public class WxMpBusyRetryTest { | |||||
@Override | @Override | ||||
public synchronized <T, E> T executeInternal( | public synchronized <T, E> T executeInternal( | ||||
RequestExecutor<T, CloseableHttpClient, HttpHost, E> executor, String uri, E data) | |||||
RequestExecutor<T, E> executor, String uri, E data) | |||||
throws WxErrorException { | throws WxErrorException { | ||||
this.log.info("Executed"); | this.log.info("Executed"); | ||||
WxError error = new WxError(); | WxError error = new WxError(); | ||||