@@ -0,0 +1,51 @@ | |||
package me.chanjar.weixin.common.util.http; | |||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; | |||
import org.apache.http.impl.client.CloseableHttpClient; | |||
/** | |||
* httpclient build interface | |||
*/ | |||
public interface ApacheHttpClientBuilder { | |||
/** | |||
* 构建httpclient实例 | |||
* @return new instance of CloseableHttpClient | |||
*/ | |||
CloseableHttpClient build(); | |||
/** | |||
* 代理服务器地址 | |||
* @param httpProxyHost | |||
* @return | |||
*/ | |||
ApacheHttpClientBuilder httpProxyHost(String httpProxyHost); | |||
/** | |||
* 代理服务器端口 | |||
* @param httpProxyPort | |||
* @return | |||
*/ | |||
ApacheHttpClientBuilder httpProxyPort(int httpProxyPort); | |||
/** | |||
* 代理服务器用户名 | |||
* @param httpProxyUsername | |||
* @return | |||
*/ | |||
ApacheHttpClientBuilder httpProxyUsername(String httpProxyUsername); | |||
/** | |||
* 代理服务器密码 | |||
* @param httpProxyPassword | |||
* @return | |||
*/ | |||
ApacheHttpClientBuilder httpProxyPassword(String httpProxyPassword); | |||
/** | |||
* ssl连接socket工厂 | |||
* @param sslConnectionSocketFactory | |||
* @return | |||
*/ | |||
ApacheHttpClientBuilder sslConnectionSocketFactory(SSLConnectionSocketFactory sslConnectionSocketFactory); | |||
} |
@@ -0,0 +1,199 @@ | |||
package me.chanjar.weixin.common.util.http; | |||
import me.chanjar.weixin.common.util.StringUtils; | |||
import org.apache.http.annotation.NotThreadSafe; | |||
import org.apache.http.auth.AuthScope; | |||
import org.apache.http.auth.UsernamePasswordCredentials; | |||
import org.apache.http.client.CredentialsProvider; | |||
import org.apache.http.client.HttpRequestRetryHandler; | |||
import org.apache.http.client.config.RequestConfig; | |||
import org.apache.http.config.Registry; | |||
import org.apache.http.config.RegistryBuilder; | |||
import org.apache.http.config.SocketConfig; | |||
import org.apache.http.conn.HttpClientConnectionManager; | |||
import org.apache.http.conn.socket.ConnectionSocketFactory; | |||
import org.apache.http.conn.socket.PlainConnectionSocketFactory; | |||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; | |||
import org.apache.http.impl.client.BasicCredentialsProvider; | |||
import org.apache.http.impl.client.CloseableHttpClient; | |||
import org.apache.http.impl.client.HttpClientBuilder; | |||
import org.apache.http.impl.client.HttpClients; | |||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; | |||
import org.apache.http.protocol.HttpContext; | |||
import java.io.IOException; | |||
import java.util.concurrent.TimeUnit; | |||
/** | |||
* httpclient 连接管理器 | |||
*/ | |||
@NotThreadSafe | |||
public class DefaultApacheHttpHttpClientBuilder implements ApacheHttpClientBuilder { | |||
private int connectionRequestTimeout = 3000; | |||
private int connectionTimeout = 5000; | |||
private int soTimeout = 5000; | |||
private int idleConnTimeout = 60000; | |||
private int checkWaitTime = 5000; | |||
private int maxConnPerHost = 10; | |||
private int maxTotalConn = 50; | |||
private String userAgent; | |||
private HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() { | |||
@Override | |||
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { | |||
return false; | |||
} | |||
}; | |||
private SSLConnectionSocketFactory sslConnectionSocketFactory = SSLConnectionSocketFactory.getSocketFactory(); | |||
private PlainConnectionSocketFactory plainConnectionSocketFactory = PlainConnectionSocketFactory.getSocketFactory(); | |||
private String httpProxyHost; | |||
private int httpProxyPort; | |||
private String httpProxyUsername; | |||
private String httpProxyPassword; | |||
/** | |||
* 连接管理器 | |||
*/ | |||
private PoolingHttpClientConnectionManager connectionManager; | |||
/** | |||
* 闲置连接监控线程 | |||
*/ | |||
private IdleConnectionMonitorThread idleConnectionMonitorThread; | |||
/** | |||
* httpClientBuilder | |||
*/ | |||
private HttpClientBuilder httpClientBuilder; | |||
private boolean prepared = false; | |||
private DefaultApacheHttpHttpClientBuilder() { | |||
} | |||
public static DefaultApacheHttpHttpClientBuilder get() { | |||
return new DefaultApacheHttpHttpClientBuilder(); | |||
} | |||
public ApacheHttpClientBuilder httpProxyHost(String httpProxyHost) { | |||
this.httpProxyHost = httpProxyHost; | |||
return this; | |||
} | |||
public ApacheHttpClientBuilder httpProxyPort(int httpProxyPort) { | |||
this.httpProxyPort = httpProxyPort; | |||
return this; | |||
} | |||
public ApacheHttpClientBuilder httpProxyUsername(String httpProxyUsername) { | |||
this.httpProxyUsername = httpProxyUsername; | |||
return this; | |||
} | |||
public ApacheHttpClientBuilder httpProxyPassword(String httpProxyPassword) { | |||
this.httpProxyPassword = httpProxyPassword; | |||
return this; | |||
} | |||
public ApacheHttpClientBuilder sslConnectionSocketFactory(SSLConnectionSocketFactory sslConnectionSocketFactory){ | |||
this.sslConnectionSocketFactory = sslConnectionSocketFactory; | |||
return this; | |||
} | |||
public IdleConnectionMonitorThread getIdleConnectionMonitorThread() { | |||
return idleConnectionMonitorThread; | |||
} | |||
private void prepare(){ | |||
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() | |||
.register("http", plainConnectionSocketFactory) | |||
.register("https", sslConnectionSocketFactory) | |||
.build(); | |||
connectionManager = new PoolingHttpClientConnectionManager(registry); | |||
connectionManager.setMaxTotal(maxTotalConn); | |||
connectionManager.setDefaultMaxPerRoute(maxConnPerHost); | |||
connectionManager.setDefaultSocketConfig( | |||
SocketConfig.copy(SocketConfig.DEFAULT) | |||
.setSoTimeout(soTimeout) | |||
.build() | |||
); | |||
idleConnectionMonitorThread = new IdleConnectionMonitorThread(connectionManager, idleConnTimeout, checkWaitTime); | |||
idleConnectionMonitorThread.setDaemon(true); | |||
idleConnectionMonitorThread.start(); | |||
httpClientBuilder = HttpClients.custom() | |||
.setConnectionManager(connectionManager) | |||
.setDefaultRequestConfig( | |||
RequestConfig.custom() | |||
.setSocketTimeout(soTimeout) | |||
.setConnectTimeout(connectionTimeout) | |||
.setConnectionRequestTimeout(connectionRequestTimeout) | |||
.build() | |||
) | |||
.setRetryHandler(httpRequestRetryHandler); | |||
if (StringUtils.isNotBlank(httpProxyHost) && StringUtils.isNotBlank(httpProxyUsername)) { | |||
// 使用代理服务器 需要用户认证的代理服务器 | |||
CredentialsProvider credsProvider = new BasicCredentialsProvider(); | |||
credsProvider.setCredentials( | |||
new AuthScope(httpProxyHost, httpProxyPort), | |||
new UsernamePasswordCredentials(httpProxyUsername, httpProxyPassword)); | |||
httpClientBuilder.setDefaultCredentialsProvider(credsProvider); | |||
} | |||
if (StringUtils.isNotBlank(userAgent)) { | |||
httpClientBuilder.setUserAgent(userAgent); | |||
} | |||
} | |||
public CloseableHttpClient build() { | |||
if(!prepared){ | |||
prepare(); | |||
prepared = true; | |||
} | |||
return httpClientBuilder.build(); | |||
} | |||
public static class IdleConnectionMonitorThread extends Thread { | |||
private final HttpClientConnectionManager connMgr; | |||
private final int idleConnTimeout; | |||
private final int checkWaitTime; | |||
private volatile boolean shutdown; | |||
public IdleConnectionMonitorThread(HttpClientConnectionManager connMgr, int idleConnTimeout, int checkWaitTime) { | |||
super("IdleConnectionMonitorThread"); | |||
this.connMgr = connMgr; | |||
this.idleConnTimeout = idleConnTimeout; | |||
this.checkWaitTime = checkWaitTime; | |||
} | |||
@Override | |||
public void run() { | |||
try { | |||
while (!shutdown) { | |||
synchronized (this) { | |||
wait(checkWaitTime); | |||
connMgr.closeExpiredConnections(); | |||
connMgr.closeIdleConnections(idleConnTimeout, TimeUnit.MILLISECONDS); | |||
} | |||
} | |||
} catch (InterruptedException ignore) { | |||
} | |||
} | |||
public void trigger() { | |||
synchronized (this) { | |||
notifyAll(); | |||
} | |||
} | |||
public void shutdown() { | |||
shutdown = true; | |||
synchronized (this) { | |||
notifyAll(); | |||
} | |||
} | |||
} | |||
} |
@@ -74,6 +74,8 @@ public class MediaDownloadRequestExecutor implements RequestExecutor<File, Strin | |||
File localFile = FileUtils.createTmpFile(inputStream, name_ext[0], name_ext[1], tmpDirFile); | |||
return localFile; | |||
}finally { | |||
httpGet.releaseConnection(); | |||
} | |||
} | |||
@@ -1,9 +1,8 @@ | |||
package me.chanjar.weixin.common.util.http; | |||
import java.io.File; | |||
import java.io.IOException; | |||
import me.chanjar.weixin.common.bean.result.WxError; | |||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import org.apache.http.HttpEntity; | |||
import org.apache.http.HttpHost; | |||
import org.apache.http.client.ClientProtocolException; | |||
@@ -13,11 +12,11 @@ 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 me.chanjar.weixin.common.bean.result.WxError; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import org.apache.http.impl.client.CloseableHttpClient; | |||
import java.io.File; | |||
import java.io.IOException; | |||
/** | |||
* 上传媒体文件请求执行器,请求的参数是File, 返回的结果是String | |||
* @author Daniel Qian | |||
@@ -48,6 +47,8 @@ public class MediaUploadRequestExecutor implements RequestExecutor<WxMediaUpload | |||
throw new WxErrorException(error); | |||
} | |||
return WxMediaUploadResult.fromJson(responseContent); | |||
}finally { | |||
httpPost.releaseConnection(); | |||
} | |||
} | |||
@@ -1,17 +1,16 @@ | |||
package me.chanjar.weixin.common.util.http; | |||
import java.io.IOException; | |||
import me.chanjar.weixin.common.bean.result.WxError; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import org.apache.http.HttpHost; | |||
import org.apache.http.client.ClientProtocolException; | |||
import org.apache.http.client.config.RequestConfig; | |||
import org.apache.http.client.methods.CloseableHttpResponse; | |||
import org.apache.http.client.methods.HttpGet; | |||
import me.chanjar.weixin.common.bean.result.WxError; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import org.apache.http.impl.client.CloseableHttpClient; | |||
import java.io.IOException; | |||
/** | |||
* 简单的GET请求执行器,请求的参数是String, 返回的结果也是String | |||
* @author Daniel Qian | |||
@@ -40,6 +39,8 @@ public class SimpleGetRequestExecutor implements RequestExecutor<String, String> | |||
throw new WxErrorException(error); | |||
} | |||
return responseContent; | |||
}finally { | |||
httpGet.releaseConnection(); | |||
} | |||
} | |||
@@ -1,24 +1,17 @@ | |||
package me.chanjar.weixin.common.util.http; | |||
import java.io.IOException; | |||
import me.chanjar.weixin.common.bean.result.WxError; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import org.apache.http.Consts; | |||
import org.apache.http.HttpHost; | |||
import org.apache.http.auth.AuthScope; | |||
import org.apache.http.auth.UsernamePasswordCredentials; | |||
import org.apache.http.client.ClientProtocolException; | |||
import org.apache.http.client.CredentialsProvider; | |||
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.client.methods.HttpPost; | |||
import org.apache.http.entity.StringEntity; | |||
import org.apache.http.impl.client.BasicCredentialsProvider; | |||
import org.apache.http.impl.client.CloseableHttpClient; | |||
import org.apache.http.impl.client.HttpClients; | |||
import org.apache.http.util.EntityUtils; | |||
import java.io.IOException; | |||
/** | |||
* 简单的POST请求执行器,请求的参数是String, 返回的结果也是String | |||
@@ -47,6 +40,8 @@ public class SimplePostRequestExecutor implements RequestExecutor<String, String | |||
throw new WxErrorException(error); | |||
} | |||
return responseContent; | |||
}finally { | |||
httpPost.releaseConnection(); | |||
} | |||
} | |||
@@ -1,8 +1,9 @@ | |||
package me.chanjar.weixin.cp.api; | |||
import java.io.File; | |||
import me.chanjar.weixin.common.bean.WxAccessToken; | |||
import me.chanjar.weixin.common.util.http.ApacheHttpClientBuilder; | |||
import java.io.File; | |||
/** | |||
* 微信客户端配置存储 | |||
@@ -63,4 +64,9 @@ public interface WxCpConfigStorage { | |||
public File getTmpDirFile(); | |||
/** | |||
* http client builder | |||
* @return ApacheHttpClientBuilder | |||
*/ | |||
public ApacheHttpClientBuilder getApacheHttpClientBuilder(); | |||
} |
@@ -1,8 +1,9 @@ | |||
package me.chanjar.weixin.cp.api; | |||
import java.io.File; | |||
import me.chanjar.weixin.common.bean.WxAccessToken; | |||
import me.chanjar.weixin.common.util.http.ApacheHttpClientBuilder; | |||
import java.io.File; | |||
/** | |||
* 基于内存的微信配置provider,在实际生产环境中应该将这些配置持久化 | |||
@@ -32,6 +33,8 @@ public class WxCpInMemoryConfigStorage implements WxCpConfigStorage { | |||
protected volatile File tmpDirFile; | |||
private volatile ApacheHttpClientBuilder apacheHttpClientBuilder; | |||
public String getAccessToken() { | |||
return this.accessToken; | |||
} | |||
@@ -205,4 +208,12 @@ public class WxCpInMemoryConfigStorage implements WxCpConfigStorage { | |||
this.tmpDirFile = tmpDirFile; | |||
} | |||
@Override | |||
public ApacheHttpClientBuilder getApacheHttpClientBuilder() { | |||
return this.apacheHttpClientBuilder; | |||
} | |||
public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) { | |||
this.apacheHttpClientBuilder = apacheHttpClientBuilder; | |||
} | |||
} |
@@ -1,14 +1,12 @@ | |||
package me.chanjar.weixin.cp.api; | |||
import java.io.File; | |||
import java.io.IOException; | |||
import java.io.InputStream; | |||
import java.io.StringReader; | |||
import java.math.BigDecimal; | |||
import java.security.NoSuchAlgorithmException; | |||
import java.util.List; | |||
import java.util.UUID; | |||
import com.google.gson.JsonArray; | |||
import com.google.gson.JsonElement; | |||
import com.google.gson.JsonObject; | |||
import com.google.gson.JsonPrimitive; | |||
import com.google.gson.internal.Streams; | |||
import com.google.gson.reflect.TypeToken; | |||
import com.google.gson.stream.JsonReader; | |||
import me.chanjar.weixin.common.bean.WxAccessToken; | |||
import me.chanjar.weixin.common.bean.WxJsapiSignature; | |||
import me.chanjar.weixin.common.bean.WxMenu; | |||
@@ -22,41 +20,30 @@ import me.chanjar.weixin.common.util.RandomUtils; | |||
import me.chanjar.weixin.common.util.StringUtils; | |||
import me.chanjar.weixin.common.util.crypto.SHA1; | |||
import me.chanjar.weixin.common.util.fs.FileUtils; | |||
import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor; | |||
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; | |||
import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
import me.chanjar.weixin.common.util.http.JoddGetRequestExecutor; | |||
import me.chanjar.weixin.common.util.http.JoddPostRequestExecutor; | |||
import me.chanjar.weixin.common.util.http.URIUtil; | |||
import me.chanjar.weixin.common.util.http.*; | |||
import me.chanjar.weixin.common.util.json.GsonHelper; | |||
import me.chanjar.weixin.cp.bean.WxCpDepart; | |||
import me.chanjar.weixin.cp.bean.WxCpMessage; | |||
import me.chanjar.weixin.cp.bean.WxCpTag; | |||
import me.chanjar.weixin.cp.bean.WxCpUser; | |||
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | |||
import org.apache.http.HttpHost; | |||
import org.apache.http.auth.AuthScope; | |||
import org.apache.http.auth.UsernamePasswordCredentials; | |||
import org.apache.http.client.ClientProtocolException; | |||
import org.apache.http.client.CredentialsProvider; | |||
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.impl.client.BasicCredentialsProvider; | |||
import org.apache.http.impl.client.BasicResponseHandler; | |||
import org.apache.http.impl.client.CloseableHttpClient; | |||
import org.apache.http.impl.client.HttpClients; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
import com.google.gson.JsonArray; | |||
import com.google.gson.JsonElement; | |||
import com.google.gson.JsonObject; | |||
import com.google.gson.JsonPrimitive; | |||
import com.google.gson.internal.Streams; | |||
import com.google.gson.reflect.TypeToken; | |||
import com.google.gson.stream.JsonReader; | |||
import java.io.File; | |||
import java.io.IOException; | |||
import java.io.InputStream; | |||
import java.io.StringReader; | |||
import java.security.NoSuchAlgorithmException; | |||
import java.util.List; | |||
import java.util.UUID; | |||
public class WxCpServiceImpl implements WxCpService { | |||
@@ -126,6 +113,8 @@ public class WxCpServiceImpl implements WxCpService { | |||
String resultContent = null; | |||
try (CloseableHttpResponse response = httpclient.execute(httpGet)) { | |||
resultContent = new BasicResponseHandler().handleResponse(response); | |||
}finally { | |||
httpGet.releaseConnection(); | |||
} | |||
WxError error = WxError.fromJson(resultContent); | |||
if (error.getErrorCode() != 0) { | |||
@@ -156,7 +145,7 @@ public class WxCpServiceImpl implements WxCpService { | |||
synchronized (globalJsapiTicketRefreshLock) { | |||
if (wxCpConfigStorage.isJsapiTicketExpired()) { | |||
String url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket"; | |||
String responseContent = execute(new JoddGetRequestExecutor(), url, null); | |||
String responseContent = execute(new SimpleGetRequestExecutor(), url, null); | |||
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); | |||
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject(); | |||
String jsapiTicket = tmpJsonObject.get("ticket").getAsString(); | |||
@@ -256,9 +245,9 @@ public class WxCpServiceImpl implements WxCpService { | |||
public Integer departCreate(WxCpDepart depart) throws WxErrorException { | |||
String url = "https://qyapi.weixin.qq.com/cgi-bin/department/create"; | |||
String responseContent = execute( | |||
new JoddPostRequestExecutor(), | |||
url, | |||
depart.toJson()); | |||
new SimplePostRequestExecutor(), | |||
url, | |||
depart.toJson()); | |||
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); | |||
return GsonHelper.getAsInteger(tmpJsonElement.getAsJsonObject().get("id")); | |||
} | |||
@@ -512,11 +501,11 @@ public class WxCpServiceImpl implements WxCpService { | |||
} | |||
public String get(String url, String queryParam) throws WxErrorException { | |||
return execute(new JoddGetRequestExecutor(), url, queryParam); | |||
return execute(new SimpleGetRequestExecutor(), url, queryParam); | |||
} | |||
public String post(String url, String postData) throws WxErrorException { | |||
return execute(new JoddPostRequestExecutor(), url, postData); | |||
return execute(new SimplePostRequestExecutor(), url, postData); | |||
} | |||
/** | |||
@@ -594,32 +583,16 @@ public class WxCpServiceImpl implements WxCpService { | |||
public void setWxCpConfigStorage(WxCpConfigStorage wxConfigProvider) { | |||
this.wxCpConfigStorage = wxConfigProvider; | |||
String http_proxy_host = wxCpConfigStorage.getHttp_proxy_host(); | |||
int http_proxy_port = wxCpConfigStorage.getHttp_proxy_port(); | |||
String http_proxy_username = wxCpConfigStorage.getHttp_proxy_username(); | |||
String http_proxy_password = wxCpConfigStorage.getHttp_proxy_password(); | |||
if(StringUtils.isNotBlank(http_proxy_host)) { | |||
// 使用代理服务器 | |||
if(StringUtils.isNotBlank(http_proxy_username)) { | |||
// 需要用户认证的代理服务器 | |||
CredentialsProvider credsProvider = new BasicCredentialsProvider(); | |||
credsProvider.setCredentials( | |||
new AuthScope(http_proxy_host, http_proxy_port), | |||
new UsernamePasswordCredentials(http_proxy_username, http_proxy_password)); | |||
httpClient = HttpClients | |||
.custom() | |||
.setDefaultCredentialsProvider(credsProvider) | |||
.build(); | |||
} else { | |||
// 无需用户认证的代理服务器 | |||
httpClient = HttpClients.createDefault(); | |||
} | |||
httpProxy = new HttpHost(http_proxy_host, http_proxy_port); | |||
} else { | |||
httpClient = HttpClients.createDefault(); | |||
ApacheHttpClientBuilder apacheHttpClientBuilder = wxCpConfigStorage.getApacheHttpClientBuilder(); | |||
if (null == apacheHttpClientBuilder) { | |||
apacheHttpClientBuilder = DefaultApacheHttpHttpClientBuilder.get(); | |||
} | |||
apacheHttpClientBuilder.httpProxyHost(wxCpConfigStorage.getHttp_proxy_host()) | |||
.httpProxyPort(wxCpConfigStorage.getHttp_proxy_port()) | |||
.httpProxyUsername(wxCpConfigStorage.getHttp_proxy_username()) | |||
.httpProxyPassword(wxCpConfigStorage.getHttp_proxy_password()); | |||
httpClient = apacheHttpClientBuilder.build(); | |||
} | |||
@Override | |||
@@ -1,10 +1,10 @@ | |||
package me.chanjar.weixin.mp.api; | |||
import java.io.File; | |||
import me.chanjar.weixin.common.bean.WxAccessToken; | |||
import me.chanjar.weixin.common.util.http.ApacheHttpClientBuilder; | |||
import javax.net.ssl.SSLContext; | |||
import me.chanjar.weixin.common.bean.WxAccessToken; | |||
import java.io.File; | |||
/** | |||
* 微信客户端配置存储 | |||
@@ -92,4 +92,10 @@ public interface WxMpConfigStorage { | |||
public File getTmpDirFile(); | |||
public SSLContext getSSLContext(); | |||
/** | |||
* http client builder | |||
* @return ApacheHttpClientBuilder | |||
*/ | |||
public ApacheHttpClientBuilder getApacheHttpClientBuilder(); | |||
} |
@@ -1,10 +1,10 @@ | |||
package me.chanjar.weixin.mp.api; | |||
import java.io.File; | |||
import me.chanjar.weixin.common.bean.WxAccessToken; | |||
import me.chanjar.weixin.common.util.http.ApacheHttpClientBuilder; | |||
import javax.net.ssl.SSLContext; | |||
import me.chanjar.weixin.common.bean.WxAccessToken; | |||
import java.io.File; | |||
/** | |||
* 基于内存的微信配置provider,在实际生产环境中应该将这些配置持久化 | |||
@@ -41,7 +41,9 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage { | |||
protected volatile File tmpDirFile; | |||
protected volatile SSLContext sslContext; | |||
protected volatile ApacheHttpClientBuilder apacheHttpClientBuilder; | |||
public String getAccessToken() { | |||
return this.accessToken; | |||
} | |||
@@ -258,4 +260,12 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage { | |||
sslContext = context; | |||
} | |||
@Override | |||
public ApacheHttpClientBuilder getApacheHttpClientBuilder() { | |||
return this.apacheHttpClientBuilder; | |||
} | |||
public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) { | |||
this.apacheHttpClientBuilder = apacheHttpClientBuilder; | |||
} | |||
} |
@@ -1,21 +1,10 @@ | |||
package me.chanjar.weixin.mp.api; | |||
import java.io.File; | |||
import java.io.IOException; | |||
import java.io.InputStream; | |||
import java.io.StringReader; | |||
import java.security.NoSuchAlgorithmException; | |||
import java.util.Arrays; | |||
import java.util.Date; | |||
import java.util.HashMap; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.Map.Entry; | |||
import java.util.SortedMap; | |||
import java.util.TreeMap; | |||
import java.util.UUID; | |||
import com.google.gson.JsonPrimitive; | |||
import com.google.gson.*; | |||
import com.google.gson.internal.Streams; | |||
import com.google.gson.reflect.TypeToken; | |||
import com.google.gson.stream.JsonReader; | |||
import com.thoughtworks.xstream.XStream; | |||
import me.chanjar.weixin.common.api.WxConsts; | |||
import me.chanjar.weixin.common.bean.WxAccessToken; | |||
import me.chanjar.weixin.common.bean.WxCardApiSignature; | |||
@@ -27,7 +16,6 @@ import me.chanjar.weixin.common.exception.WxErrorException; | |||
import me.chanjar.weixin.common.session.StandardSessionManager; | |||
import me.chanjar.weixin.common.session.WxSessionManager; | |||
import me.chanjar.weixin.common.util.RandomUtils; | |||
import me.chanjar.weixin.common.util.StringUtils; | |||
import me.chanjar.weixin.common.util.crypto.SHA1; | |||
import me.chanjar.weixin.common.util.crypto.WxCryptUtil; | |||
import me.chanjar.weixin.common.util.fs.FileUtils; | |||
@@ -35,74 +23,32 @@ import me.chanjar.weixin.common.util.http.*; | |||
import me.chanjar.weixin.common.util.json.GsonHelper; | |||
import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||
import me.chanjar.weixin.common.util.xml.XStreamInitializer; | |||
import me.chanjar.weixin.mp.bean.WxMpCustomMessage; | |||
import me.chanjar.weixin.mp.bean.WxMpGroup; | |||
import me.chanjar.weixin.mp.bean.WxMpMassGroupMessage; | |||
import me.chanjar.weixin.mp.bean.WxMpMassNews; | |||
import me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage; | |||
import me.chanjar.weixin.mp.bean.WxMpMassVideo; | |||
import me.chanjar.weixin.mp.bean.WxMpMaterial; | |||
import me.chanjar.weixin.mp.bean.WxMpMaterialArticleUpdate; | |||
import me.chanjar.weixin.mp.bean.WxMpMaterialNews; | |||
import me.chanjar.weixin.mp.bean.WxMpSemanticQuery; | |||
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage; | |||
import me.chanjar.weixin.mp.bean.result.WxMpCardResult; | |||
import me.chanjar.weixin.mp.bean.result.WxMpMassSendResult; | |||
import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult; | |||
import me.chanjar.weixin.mp.bean.result.WxMpMaterialCountResult; | |||
import me.chanjar.weixin.mp.bean.result.WxMpMaterialFileBatchGetResult; | |||
import me.chanjar.weixin.mp.bean.result.WxMpMaterialNewsBatchGetResult; | |||
import me.chanjar.weixin.mp.bean.result.WxMpMaterialUploadResult; | |||
import me.chanjar.weixin.mp.bean.result.WxMpMaterialVideoInfoResult; | |||
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken; | |||
import me.chanjar.weixin.mp.bean.result.WxMpPayCallback; | |||
import me.chanjar.weixin.mp.bean.result.WxMpPayRefundResult; | |||
import me.chanjar.weixin.mp.bean.result.WxMpPayResult; | |||
import me.chanjar.weixin.mp.bean.result.WxMpPrepayIdResult; | |||
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket; | |||
import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult; | |||
import me.chanjar.weixin.mp.bean.result.WxMpUser; | |||
import me.chanjar.weixin.mp.bean.result.WxMpUserCumulate; | |||
import me.chanjar.weixin.mp.bean.result.WxMpUserList; | |||
import me.chanjar.weixin.mp.bean.result.WxMpUserSummary; | |||
import me.chanjar.weixin.mp.bean.result.WxRedpackResult; | |||
import me.chanjar.weixin.mp.util.http.MaterialDeleteRequestExecutor; | |||
import me.chanjar.weixin.mp.util.http.MaterialNewsInfoRequestExecutor; | |||
import me.chanjar.weixin.mp.util.http.MaterialUploadRequestExecutor; | |||
import me.chanjar.weixin.mp.util.http.MaterialVideoInfoRequestExecutor; | |||
import me.chanjar.weixin.mp.util.http.MaterialVoiceAndImageDownloadRequestExecutor; | |||
import me.chanjar.weixin.mp.util.http.QrCodeRequestExecutor; | |||
import me.chanjar.weixin.mp.bean.*; | |||
import me.chanjar.weixin.mp.bean.result.*; | |||
import me.chanjar.weixin.mp.util.http.*; | |||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
import org.apache.http.Consts; | |||
import org.apache.http.HttpHost; | |||
import org.apache.http.auth.AuthScope; | |||
import org.apache.http.auth.UsernamePasswordCredentials; | |||
import org.apache.http.client.ClientProtocolException; | |||
import org.apache.http.client.CredentialsProvider; | |||
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.client.methods.HttpPost; | |||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; | |||
import org.apache.http.entity.StringEntity; | |||
import org.apache.http.impl.client.BasicCredentialsProvider; | |||
import org.apache.http.impl.client.BasicResponseHandler; | |||
import org.apache.http.impl.client.CloseableHttpClient; | |||
import org.apache.http.impl.client.HttpClientBuilder; | |||
import org.apache.http.impl.client.HttpClients; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
import org.slf4j.helpers.MessageFormatter; | |||
import com.google.gson.JsonArray; | |||
import com.google.gson.JsonElement; | |||
import com.google.gson.JsonObject; | |||
import com.google.gson.JsonParser; | |||
import com.google.gson.internal.Streams; | |||
import com.google.gson.reflect.TypeToken; | |||
import com.google.gson.stream.JsonReader; | |||
import com.thoughtworks.xstream.XStream; | |||
import java.io.File; | |||
import java.io.IOException; | |||
import java.io.InputStream; | |||
import java.io.StringReader; | |||
import java.security.NoSuchAlgorithmException; | |||
import java.util.*; | |||
import java.util.Map.Entry; | |||
public class WxMpServiceImpl implements WxMpService { | |||
@@ -171,6 +117,8 @@ public class WxMpServiceImpl implements WxMpService { | |||
} | |||
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); | |||
wxMpConfigStorage.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); | |||
}finally { | |||
httpGet.releaseConnection(); | |||
} | |||
} catch (ClientProtocolException e) { | |||
throw new RuntimeException(e); | |||
@@ -195,7 +143,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
synchronized (globalJsapiTicketRefreshLock) { | |||
if (wxMpConfigStorage.isJsapiTicketExpired()) { | |||
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi"; | |||
String responseContent = execute(new JoddGetRequestExecutor(), url, null); | |||
String responseContent = execute(new SimpleGetRequestExecutor(), url, null); | |||
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); | |||
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject(); | |||
String jsapiTicket = tmpJsonObject.get("ticket").getAsString(); | |||
@@ -232,33 +180,33 @@ public class WxMpServiceImpl implements WxMpService { | |||
public void customMessageSend(WxMpCustomMessage message) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send"; | |||
execute(new JoddPostRequestExecutor(), url, message.toJson()); | |||
execute(new SimplePostRequestExecutor(), url, message.toJson()); | |||
} | |||
public void menuCreate(WxMenu menu) throws WxErrorException { | |||
if (menu.getMatchRule() != null) { | |||
String url = "https://api.weixin.qq.com/cgi-bin/menu/addconditional"; | |||
execute(new JoddPostRequestExecutor(), url, menu.toJson()); | |||
execute(new SimplePostRequestExecutor(), url, menu.toJson()); | |||
} else { | |||
String url = "https://api.weixin.qq.com/cgi-bin/menu/create"; | |||
execute(new JoddPostRequestExecutor(), url, menu.toJson()); | |||
execute(new SimplePostRequestExecutor(), url, menu.toJson()); | |||
} | |||
} | |||
public void menuDelete() throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/menu/delete"; | |||
execute(new JoddGetRequestExecutor(), url, null); | |||
execute(new SimpleGetRequestExecutor(), url, null); | |||
} | |||
public void menuDelete(String menuid) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/menu/delconditional"; | |||
execute(new JoddGetRequestExecutor(), url, "menuid=" + menuid); | |||
execute(new SimpleGetRequestExecutor(), url, "menuid=" + menuid); | |||
} | |||
public WxMenu menuGet() throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/menu/get"; | |||
try { | |||
String resultContent = execute(new JoddGetRequestExecutor(), url, null); | |||
String resultContent = execute(new SimpleGetRequestExecutor(), url, null); | |||
return WxMenu.fromJson(resultContent); | |||
} catch (WxErrorException e) { | |||
// 46003 不存在的菜单数据 | |||
@@ -272,7 +220,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
public WxMenu menuTryMatch(String userid) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/menu/trymatch"; | |||
try { | |||
String resultContent = execute(new JoddGetRequestExecutor(), url, "user_id=" + userid); | |||
String resultContent = execute(new SimpleGetRequestExecutor(), url, "user_id=" + userid); | |||
return WxMenu.fromJson(resultContent); | |||
} catch (WxErrorException e) { | |||
// 46003 不存在的菜单数据 46002 不存在的菜单版本 | |||
@@ -385,25 +333,25 @@ public class WxMpServiceImpl implements WxMpService { | |||
public WxMpMassUploadResult massNewsUpload(WxMpMassNews news) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/media/uploadnews"; | |||
String responseContent = execute(new JoddPostRequestExecutor(), url, news.toJson()); | |||
String responseContent = execute(new SimplePostRequestExecutor(), url, news.toJson()); | |||
return WxMpMassUploadResult.fromJson(responseContent); | |||
} | |||
public WxMpMassUploadResult massVideoUpload(WxMpMassVideo video) throws WxErrorException { | |||
String url = "http://file.api.weixin.qq.com/cgi-bin/media/uploadvideo"; | |||
String responseContent = execute(new JoddPostRequestExecutor(), url, video.toJson()); | |||
String responseContent = execute(new SimplePostRequestExecutor(), url, video.toJson()); | |||
return WxMpMassUploadResult.fromJson(responseContent); | |||
} | |||
public WxMpMassSendResult massGroupMessageSend(WxMpMassGroupMessage message) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall"; | |||
String responseContent = execute(new JoddPostRequestExecutor(), url, message.toJson()); | |||
String responseContent = execute(new SimplePostRequestExecutor(), url, message.toJson()); | |||
return WxMpMassSendResult.fromJson(responseContent); | |||
} | |||
public WxMpMassSendResult massOpenIdsMessageSend(WxMpMassOpenIdsMessage message) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/message/mass/send"; | |||
String responseContent = execute(new JoddPostRequestExecutor(), url, message.toJson()); | |||
String responseContent = execute(new SimplePostRequestExecutor(), url, message.toJson()); | |||
return WxMpMassSendResult.fromJson(responseContent); | |||
} | |||
@@ -415,7 +363,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
groupJson.addProperty("name", name); | |||
String responseContent = execute( | |||
new JoddPostRequestExecutor(), | |||
new SimplePostRequestExecutor(), | |||
url, | |||
json.toString()); | |||
return WxMpGroup.fromJson(responseContent); | |||
@@ -423,7 +371,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
public List<WxMpGroup> groupGet() throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/groups/get"; | |||
String responseContent = execute(new JoddGetRequestExecutor(), url, null); | |||
String responseContent = execute(new SimpleGetRequestExecutor(), url, null); | |||
/* | |||
* 操蛋的微信API,创建时返回的是 { group : { id : ..., name : ...} } | |||
* 查询时返回的是 { groups : [ { id : ..., name : ..., count : ... }, ... ] } | |||
@@ -438,14 +386,14 @@ public class WxMpServiceImpl implements WxMpService { | |||
String url = "https://api.weixin.qq.com/cgi-bin/groups/getid"; | |||
JsonObject o = new JsonObject(); | |||
o.addProperty("openid", openid); | |||
String responseContent = execute(new JoddPostRequestExecutor(), url, o.toString()); | |||
String responseContent = execute(new SimplePostRequestExecutor(), url, o.toString()); | |||
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); | |||
return GsonHelper.getAsLong(tmpJsonElement.getAsJsonObject().get("groupid")); | |||
} | |||
public void groupUpdate(WxMpGroup group) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/groups/update"; | |||
execute(new JoddPostRequestExecutor(), url, group.toJson()); | |||
execute(new SimplePostRequestExecutor(), url, group.toJson()); | |||
} | |||
public void userUpdateGroup(String openid, long to_groupid) throws WxErrorException { | |||
@@ -453,7 +401,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
JsonObject json = new JsonObject(); | |||
json.addProperty("openid", openid); | |||
json.addProperty("to_groupid", to_groupid); | |||
execute(new JoddPostRequestExecutor(), url, json.toString()); | |||
execute(new SimplePostRequestExecutor(), url, json.toString()); | |||
} | |||
public void userUpdateRemark(String openid, String remark) throws WxErrorException { | |||
@@ -461,19 +409,19 @@ public class WxMpServiceImpl implements WxMpService { | |||
JsonObject json = new JsonObject(); | |||
json.addProperty("openid", openid); | |||
json.addProperty("remark", remark); | |||
execute(new JoddPostRequestExecutor(), url, json.toString()); | |||
execute(new SimplePostRequestExecutor(), url, json.toString()); | |||
} | |||
public WxMpUser userInfo(String openid, String lang) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/user/info"; | |||
lang = lang == null ? "zh_CN" : lang; | |||
String responseContent = execute(new JoddGetRequestExecutor(), url, "openid=" + openid + "&lang=" + lang); | |||
String responseContent = execute(new SimpleGetRequestExecutor(), url, "openid=" + openid + "&lang=" + lang); | |||
return WxMpUser.fromJson(responseContent); | |||
} | |||
public WxMpUserList userList(String next_openid) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/user/get"; | |||
String responseContent = execute(new JoddGetRequestExecutor(), url, next_openid == null ? null : "next_openid=" + next_openid); | |||
String responseContent = execute(new SimpleGetRequestExecutor(), url, next_openid == null ? null : "next_openid=" + next_openid); | |||
return WxMpUserList.fromJson(responseContent); | |||
} | |||
@@ -489,7 +437,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
scene.addProperty("scene_id", scene_id); | |||
actionInfo.add("scene", scene); | |||
json.add("action_info", actionInfo); | |||
String responseContent = execute(new JoddPostRequestExecutor(), url, json.toString()); | |||
String responseContent = execute(new SimplePostRequestExecutor(), url, json.toString()); | |||
return WxMpQrCodeTicket.fromJson(responseContent); | |||
} | |||
@@ -502,7 +450,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
scene.addProperty("scene_id", scene_id); | |||
actionInfo.add("scene", scene); | |||
json.add("action_info", actionInfo); | |||
String responseContent = execute(new JoddPostRequestExecutor(), url, json.toString()); | |||
String responseContent = execute(new SimplePostRequestExecutor(), url, json.toString()); | |||
return WxMpQrCodeTicket.fromJson(responseContent); | |||
} | |||
@@ -515,7 +463,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
scene.addProperty("scene_str", scene_str); | |||
actionInfo.add("scene", scene); | |||
json.add("action_info", actionInfo); | |||
String responseContent = execute(new JoddPostRequestExecutor(), url, json.toString()); | |||
String responseContent = execute(new SimplePostRequestExecutor(), url, json.toString()); | |||
return WxMpQrCodeTicket.fromJson(responseContent); | |||
} | |||
@@ -529,14 +477,14 @@ public class WxMpServiceImpl implements WxMpService { | |||
JsonObject o = new JsonObject(); | |||
o.addProperty("action", "long2short"); | |||
o.addProperty("long_url", long_url); | |||
String responseContent = execute(new JoddPostRequestExecutor(), url, o.toString()); | |||
String responseContent = execute(new SimplePostRequestExecutor(), url, o.toString()); | |||
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); | |||
return tmpJsonElement.getAsJsonObject().get("short_url").getAsString(); | |||
} | |||
public String templateSend(WxMpTemplateMessage templateMessage) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send"; | |||
String responseContent = execute(new JoddPostRequestExecutor(), url, templateMessage.toJson()); | |||
String responseContent = execute(new SimplePostRequestExecutor(), url, templateMessage.toJson()); | |||
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); | |||
final JsonObject jsonObject = tmpJsonElement.getAsJsonObject(); | |||
if (jsonObject.get("errcode").getAsInt() == 0) | |||
@@ -546,7 +494,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
public WxMpSemanticQueryResult semanticQuery(WxMpSemanticQuery semanticQuery) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/semantic/semproxy/search"; | |||
String responseContent = execute(new JoddPostRequestExecutor(), url, semanticQuery.toJson()); | |||
String responseContent = execute(new SimplePostRequestExecutor(), url, semanticQuery.toJson()); | |||
return WxMpSemanticQueryResult.fromJson(responseContent); | |||
} | |||
@@ -578,7 +526,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
url += "&grant_type=authorization_code"; | |||
try { | |||
RequestExecutor<String, String> executor = new JoddGetRequestExecutor(); | |||
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); | |||
String responseText = executor.execute(getHttpclient(), httpProxy, url, null); | |||
return WxMpOAuth2AccessToken.fromJson(responseText); | |||
} catch (ClientProtocolException e) { | |||
@@ -596,7 +544,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
url += "&refresh_token=" + refreshToken; | |||
try { | |||
RequestExecutor<String, String> executor = new JoddGetRequestExecutor(); | |||
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); | |||
String responseText = executor.execute(getHttpclient(), httpProxy, url, null); | |||
return WxMpOAuth2AccessToken.fromJson(responseText); | |||
} catch (ClientProtocolException e) { | |||
@@ -618,7 +566,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
} | |||
try { | |||
RequestExecutor<String, String> executor = new JoddGetRequestExecutor(); | |||
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); | |||
String responseText = executor.execute(getHttpclient(), httpProxy, url, null); | |||
return WxMpUser.fromJson(responseText); | |||
} catch (ClientProtocolException e) { | |||
@@ -635,7 +583,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
url += "&openid=" + oAuth2AccessToken.getOpenId(); | |||
try { | |||
RequestExecutor<String, String> executor = new JoddGetRequestExecutor(); | |||
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); | |||
executor.execute(getHttpclient(), httpProxy, url, null); | |||
} catch (ClientProtocolException e) { | |||
throw new RuntimeException(e); | |||
@@ -688,11 +636,11 @@ public class WxMpServiceImpl implements WxMpService { | |||
} | |||
public String get(String url, String queryParam) throws WxErrorException { | |||
return execute(new JoddGetRequestExecutor(), url, queryParam); | |||
return execute(new SimpleGetRequestExecutor(), url, queryParam); | |||
} | |||
public String post(String url, String postData) throws WxErrorException { | |||
return execute(new JoddPostRequestExecutor(), url, postData); | |||
return execute(new SimplePostRequestExecutor(), url, postData); | |||
} | |||
/** | |||
@@ -772,36 +720,25 @@ public class WxMpServiceImpl implements WxMpService { | |||
public void setWxMpConfigStorage(WxMpConfigStorage wxConfigProvider) { | |||
this.wxMpConfigStorage = wxConfigProvider; | |||
String http_proxy_host = wxMpConfigStorage.getHttp_proxy_host(); | |||
int http_proxy_port = wxMpConfigStorage.getHttp_proxy_port(); | |||
String http_proxy_username = wxMpConfigStorage.getHttp_proxy_username(); | |||
String http_proxy_password = wxMpConfigStorage.getHttp_proxy_password(); | |||
final HttpClientBuilder builder = HttpClients.custom(); | |||
if (StringUtils.isNotBlank(http_proxy_host)) { | |||
// 使用代理服务器 | |||
if (StringUtils.isNotBlank(http_proxy_username)) { | |||
// 需要用户认证的代理服务器 | |||
CredentialsProvider credsProvider = new BasicCredentialsProvider(); | |||
credsProvider.setCredentials( | |||
new AuthScope(http_proxy_host, http_proxy_port), | |||
new UsernamePasswordCredentials(http_proxy_username, http_proxy_password)); | |||
builder | |||
.setDefaultCredentialsProvider(credsProvider); | |||
} else { | |||
// 无需用户认证的代理服务器 | |||
} | |||
httpProxy = new HttpHost(http_proxy_host, http_proxy_port); | |||
ApacheHttpClientBuilder apacheHttpClientBuilder = wxMpConfigStorage.getApacheHttpClientBuilder(); | |||
if (null == apacheHttpClientBuilder) { | |||
apacheHttpClientBuilder = DefaultApacheHttpHttpClientBuilder.get(); | |||
} | |||
apacheHttpClientBuilder.httpProxyHost(wxMpConfigStorage.getHttp_proxy_host()) | |||
.httpProxyPort(wxMpConfigStorage.getHttp_proxy_port()) | |||
.httpProxyUsername(wxMpConfigStorage.getHttp_proxy_username()) | |||
.httpProxyPassword(wxMpConfigStorage.getHttp_proxy_password()); | |||
if (wxConfigProvider.getSSLContext() != null){ | |||
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( | |||
wxConfigProvider.getSSLContext(), | |||
new String[] { "TLSv1" }, | |||
null, | |||
SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); | |||
builder.setSSLSocketFactory(sslsf); | |||
apacheHttpClientBuilder.sslConnectionSocketFactory(sslsf); | |||
} | |||
httpClient = builder.build(); | |||
httpClient = apacheHttpClientBuilder.build(); | |||
} | |||
@Override | |||
@@ -865,6 +802,8 @@ public class WxMpServiceImpl implements WxMpService { | |||
return wxMpPrepayIdResult; | |||
} catch (IOException e) { | |||
throw new RuntimeException("Failed to get prepay id due to IO exception.", e); | |||
}finally { | |||
httpPost.releaseConnection(); | |||
} | |||
} | |||
@@ -1037,6 +976,8 @@ public class WxMpServiceImpl implements WxMpService { | |||
error.setErrorCode(-1); | |||
error.setErrorMsg("incorrect response."); | |||
throw new WxErrorException(error); | |||
}finally { | |||
httpPost.releaseConnection(); | |||
} | |||
} | |||
@@ -1082,6 +1023,8 @@ public class WxMpServiceImpl implements WxMpService { | |||
WxError error = new WxError(); | |||
error.setErrorCode(-1); | |||
throw new WxErrorException(error); | |||
}finally { | |||
httpPost.releaseConnection(); | |||
} | |||
} | |||
@@ -1120,7 +1063,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
synchronized (globalCardApiTicketRefreshLock) { | |||
if (wxMpConfigStorage.isCardApiTicketExpired()) { | |||
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=wx_card"; | |||
String responseContent = execute(new JoddGetRequestExecutor(), url, null); | |||
String responseContent = execute(new SimpleGetRequestExecutor(), url, null); | |||
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); | |||
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject(); | |||
String cardApiTicket = tmpJsonObject.get("ticket").getAsString(); | |||
@@ -34,13 +34,16 @@ public class MaterialDeleteRequestExecutor implements RequestExecutor<Boolean, S | |||
Map<String, String> params = new HashMap<>(); | |||
params.put("media_id", materialId); | |||
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); | |||
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; | |||
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(); | |||
} | |||
} | |||
@@ -35,14 +35,18 @@ public class MaterialNewsInfoRequestExecutor implements RequestExecutor<WxMpMate | |||
Map<String, String> params = new HashMap<>(); | |||
params.put("media_id", materialId); | |||
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); | |||
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); | |||
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(); | |||
} | |||
} | |||
} |
@@ -48,13 +48,16 @@ public class MaterialUploadRequestExecutor implements RequestExecutor<WxMpMateri | |||
httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString()); | |||
} | |||
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); | |||
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(); | |||
} | |||
} | |||
@@ -34,13 +34,16 @@ public class MaterialVideoInfoRequestExecutor implements RequestExecutor<WxMpMat | |||
Map<String, String> params = new HashMap<>(); | |||
params.put("media_id", materialId); | |||
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); | |||
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); | |||
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(); | |||
} | |||
} | |||
@@ -44,22 +44,25 @@ public class MaterialVoiceAndImageDownloadRequestExecutor implements RequestExec | |||
Map<String, String> params = new HashMap<>(); | |||
params.put("media_id", materialId); | |||
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); | |||
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); | |||
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); | |||
} | |||
} catch (com.google.gson.JsonSyntaxException ex) { | |||
return new ByteArrayInputStream(responseContent); | |||
} | |||
return new ByteArrayInputStream(responseContent); | |||
}finally { | |||
httpPost.releaseConnection(); | |||
} | |||
return new ByteArrayInputStream(responseContent); | |||
} | |||
} |
@@ -58,8 +58,9 @@ public class QrCodeRequestExecutor implements RequestExecutor<File, WxMpQrCodeTi | |||
} | |||
InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response); | |||
File localFile = FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg"); | |||
return localFile; | |||
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg"); | |||
}finally { | |||
httpGet.releaseConnection(); | |||
} | |||
} | |||