优化微信配置类
This commit is contained in:
@@ -6,8 +6,8 @@ import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.SSLContext;
|
||||
|
||||
/**
|
||||
* Http 参数
|
||||
*
|
||||
* Http请求参数
|
||||
*
|
||||
* @className HttpParams
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2015年8月13日
|
||||
@@ -16,104 +16,204 @@ import javax.net.ssl.SSLContext;
|
||||
*/
|
||||
public final class HttpParams {
|
||||
|
||||
private boolean allowUserInteraction = true;
|
||||
private int connectTimeout = 5000;
|
||||
private int socketTimeout = 5000;
|
||||
private int readTimeout = 5000;
|
||||
private int chunkSize = 4096;
|
||||
private long ifModifiedSince = 0l;
|
||||
private boolean followRedirects = false;
|
||||
|
||||
private final boolean allowUserInteraction;
|
||||
private final int connectTimeout;
|
||||
private final int socketTimeout;
|
||||
private final int readTimeout;
|
||||
private final int chunkSize;
|
||||
private final boolean followRedirects;
|
||||
/**
|
||||
* 代理对象
|
||||
*/
|
||||
private Proxy proxy;
|
||||
private final Proxy proxy;
|
||||
/**
|
||||
* SSL对象
|
||||
*/
|
||||
private SSLContext sslContext;
|
||||
private final SSLContext sslContext;
|
||||
/**
|
||||
* hostname对象
|
||||
*/
|
||||
private HostnameVerifier hostnameVerifier;
|
||||
private final HostnameVerifier hostnameVerifier;
|
||||
|
||||
HttpParams(boolean allowUserInteraction, int connectTimeout,
|
||||
int socketTimeout, int readTimeout, int chunkSize,
|
||||
boolean followRedirects, Proxy proxy, SSLContext sslContext,
|
||||
HostnameVerifier hostnameVerifier) {
|
||||
this.allowUserInteraction = allowUserInteraction;
|
||||
this.connectTimeout = connectTimeout;
|
||||
this.socketTimeout = socketTimeout;
|
||||
this.readTimeout = readTimeout;
|
||||
this.chunkSize = chunkSize;
|
||||
this.followRedirects = followRedirects;
|
||||
this.proxy = proxy;
|
||||
this.sslContext = sslContext;
|
||||
this.hostnameVerifier = hostnameVerifier;
|
||||
}
|
||||
|
||||
public boolean isAllowUserInteraction() {
|
||||
return allowUserInteraction;
|
||||
}
|
||||
|
||||
public void setAllowUserInteraction(boolean allowUserInteraction) {
|
||||
this.allowUserInteraction = allowUserInteraction;
|
||||
}
|
||||
|
||||
public int getConnectTimeout() {
|
||||
return connectTimeout;
|
||||
}
|
||||
|
||||
public void setConnectTimeout(int connectTimeout) {
|
||||
this.connectTimeout = connectTimeout;
|
||||
}
|
||||
|
||||
public int getSocketTimeout() {
|
||||
return socketTimeout;
|
||||
}
|
||||
|
||||
public void setSocketTimeout(int socketTimeout) {
|
||||
this.socketTimeout = socketTimeout;
|
||||
}
|
||||
|
||||
public int getReadTimeout() {
|
||||
return readTimeout;
|
||||
}
|
||||
|
||||
public void setReadTimeout(int readTimeout) {
|
||||
this.readTimeout = readTimeout;
|
||||
}
|
||||
|
||||
public int getChunkSize() {
|
||||
return chunkSize;
|
||||
}
|
||||
|
||||
public void setChunkSize(int chunkSize) {
|
||||
this.chunkSize = chunkSize;
|
||||
}
|
||||
|
||||
public long getIfModifiedSince() {
|
||||
return ifModifiedSince;
|
||||
}
|
||||
|
||||
public void setIfModifiedSince(long ifModifiedSince) {
|
||||
this.ifModifiedSince = ifModifiedSince;
|
||||
}
|
||||
|
||||
public boolean isFollowRedirects() {
|
||||
return followRedirects;
|
||||
}
|
||||
|
||||
public void setFollowRedirects(boolean followRedirects) {
|
||||
this.followRedirects = followRedirects;
|
||||
}
|
||||
|
||||
public Proxy getProxy() {
|
||||
return proxy;
|
||||
}
|
||||
|
||||
public void setProxy(Proxy proxy) {
|
||||
this.proxy = proxy;
|
||||
}
|
||||
|
||||
public SSLContext getSSLContext() {
|
||||
return sslContext;
|
||||
}
|
||||
|
||||
public void setSSLContext(SSLContext sslContext) {
|
||||
this.sslContext = sslContext;
|
||||
}
|
||||
|
||||
public HostnameVerifier getHostnameVerifier() {
|
||||
return hostnameVerifier;
|
||||
}
|
||||
|
||||
public void setHostnameVerifier(HostnameVerifier hostnameVerifier) {
|
||||
this.hostnameVerifier = hostnameVerifier;
|
||||
public static HttpParams.Builder custom() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public static HttpParams.Builder copy(final HttpParams params) {
|
||||
return new Builder()
|
||||
.setAllowUserInteraction(params.isAllowUserInteraction())
|
||||
.setConnectTimeout(params.getConnectTimeout())
|
||||
.setSocketTimeout(params.getSocketTimeout())
|
||||
.setReadTimeout(params.getReadTimeout())
|
||||
.setChunkSize(params.getChunkSize())
|
||||
.setFollowRedirects(params.isFollowRedirects());
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private boolean allowUserInteraction;
|
||||
private int connectTimeout;
|
||||
private int socketTimeout;
|
||||
private int readTimeout;
|
||||
private int chunkSize;
|
||||
private boolean followRedirects;
|
||||
/**
|
||||
* 代理对象
|
||||
*/
|
||||
private Proxy proxy;
|
||||
/**
|
||||
* SSL对象
|
||||
*/
|
||||
private SSLContext sslContext;
|
||||
/**
|
||||
* hostname对象
|
||||
*/
|
||||
private HostnameVerifier hostnameVerifier;
|
||||
|
||||
Builder() {
|
||||
this.allowUserInteraction = true;
|
||||
this.connectTimeout = 5000;
|
||||
this.socketTimeout = 5000;
|
||||
this.readTimeout = 5000;
|
||||
this.chunkSize = 4096;
|
||||
this.followRedirects = false;
|
||||
}
|
||||
|
||||
public boolean isAllowUserInteraction() {
|
||||
return allowUserInteraction;
|
||||
}
|
||||
|
||||
public Builder setAllowUserInteraction(boolean allowUserInteraction) {
|
||||
this.allowUserInteraction = allowUserInteraction;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int getConnectTimeout() {
|
||||
return connectTimeout;
|
||||
}
|
||||
|
||||
public Builder setConnectTimeout(int connectTimeout) {
|
||||
this.connectTimeout = connectTimeout;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int getSocketTimeout() {
|
||||
return socketTimeout;
|
||||
}
|
||||
|
||||
public Builder setSocketTimeout(int socketTimeout) {
|
||||
this.socketTimeout = socketTimeout;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int getReadTimeout() {
|
||||
return readTimeout;
|
||||
}
|
||||
|
||||
public Builder setReadTimeout(int readTimeout) {
|
||||
this.readTimeout = readTimeout;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int getChunkSize() {
|
||||
return chunkSize;
|
||||
}
|
||||
|
||||
public Builder setChunkSize(int chunkSize) {
|
||||
this.chunkSize = chunkSize;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isFollowRedirects() {
|
||||
return followRedirects;
|
||||
}
|
||||
|
||||
public Builder setFollowRedirects(boolean followRedirects) {
|
||||
this.followRedirects = followRedirects;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Proxy getProxy() {
|
||||
return proxy;
|
||||
}
|
||||
|
||||
public Builder setProxy(Proxy proxy) {
|
||||
this.proxy = proxy;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SSLContext getSslContext() {
|
||||
return sslContext;
|
||||
}
|
||||
|
||||
public Builder setSslContext(SSLContext sslContext) {
|
||||
this.sslContext = sslContext;
|
||||
return this;
|
||||
}
|
||||
|
||||
public HostnameVerifier getHostnameVerifier() {
|
||||
return hostnameVerifier;
|
||||
}
|
||||
|
||||
public Builder setHostnameVerifier(HostnameVerifier hostnameVerifier) {
|
||||
this.hostnameVerifier = hostnameVerifier;
|
||||
return this;
|
||||
}
|
||||
|
||||
public HttpParams build() {
|
||||
return new HttpParams(allowUserInteraction, connectTimeout,
|
||||
socketTimeout, readTimeout, chunkSize, followRedirects,
|
||||
proxy, sslContext, hostnameVerifier);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ import com.foxinmy.weixin4j.util.StringUtil;
|
||||
|
||||
/**
|
||||
* HTTP 简单实现
|
||||
*
|
||||
*
|
||||
* @className SimpleHttpClient
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2015年5月29日
|
||||
@@ -87,7 +87,6 @@ public class SimpleHttpClient extends AbstractHttpClient implements HttpClient {
|
||||
.isAllowUserInteraction());
|
||||
connection.setConnectTimeout(params.getConnectTimeout());
|
||||
connection.setReadTimeout(params.getReadTimeout());
|
||||
connection.setIfModifiedSince(params.getIfModifiedSince());
|
||||
connection.setInstanceFollowRedirects(params
|
||||
.isFollowRedirects());
|
||||
}
|
||||
|
||||
+7
-7
@@ -38,16 +38,16 @@ public class WeixinRequestExecutor {
|
||||
protected final InternalLogger logger = InternalLoggerFactory
|
||||
.getInstance(getClass());
|
||||
|
||||
protected final HttpClient httpClient;
|
||||
protected final HttpParams params;
|
||||
private final HttpClient httpClient;
|
||||
private final HttpParams httpParams;
|
||||
|
||||
public WeixinRequestExecutor() {
|
||||
this(new HttpParams());
|
||||
this(HttpParams.custom().build());
|
||||
}
|
||||
|
||||
public WeixinRequestExecutor(HttpParams params) {
|
||||
public WeixinRequestExecutor(HttpParams httpParams) {
|
||||
this.httpClient = HttpClientFactory.getInstance();
|
||||
this.params = params;
|
||||
this.httpParams = httpParams;
|
||||
}
|
||||
|
||||
public WeixinResponse get(String url) throws WeixinException {
|
||||
@@ -95,7 +95,7 @@ public class WeixinRequestExecutor {
|
||||
|
||||
protected WeixinResponse doRequest(HttpRequest request)
|
||||
throws WeixinException {
|
||||
request.setParams(params);
|
||||
request.setParams(httpParams);
|
||||
try {
|
||||
logger.info("weixin request >> " + request.getMethod() + " "
|
||||
+ request.getURI().toString());
|
||||
@@ -180,6 +180,6 @@ public class WeixinRequestExecutor {
|
||||
}
|
||||
|
||||
public HttpParams getExecuteParams() {
|
||||
return params;
|
||||
return httpParams;
|
||||
}
|
||||
}
|
||||
|
||||
+17
-3
@@ -7,11 +7,13 @@ import javax.net.ssl.KeyManagerFactory;
|
||||
import javax.net.ssl.SSLContext;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.HttpParams;
|
||||
import com.foxinmy.weixin4j.http.HttpRequest;
|
||||
import com.foxinmy.weixin4j.model.Consts;
|
||||
|
||||
/**
|
||||
* 微信ssl请求
|
||||
*
|
||||
*
|
||||
* @className WeixinSSLRequestExecutor
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2015年8月17日
|
||||
@@ -36,15 +38,27 @@ public class WeixinSSLRequestExecutor extends WeixinRequestExecutor {
|
||||
} catch (Exception e) {
|
||||
throw new WeixinException("Key load error", e);
|
||||
}
|
||||
params.setSSLContext(sslContext);
|
||||
}
|
||||
|
||||
public WeixinSSLRequestExecutor(SSLContext sslContext) {
|
||||
this.sslContext = sslContext;
|
||||
params.setSSLContext(sslContext);
|
||||
}
|
||||
|
||||
public SSLContext getSSLContext() {
|
||||
return sslContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected WeixinResponse doRequest(HttpRequest request)
|
||||
throws WeixinException {
|
||||
HttpParams params = null;
|
||||
if (request.getParams() != null) {
|
||||
params = HttpParams.copy(request.getParams())
|
||||
.setSslContext(sslContext).build();
|
||||
} else {
|
||||
params = HttpParams.custom().setSslContext(sslContext).build();
|
||||
}
|
||||
request.setParams(params);
|
||||
return super.doRequest(request);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Date;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.foxinmy.weixin4j.api.CashApi;
|
||||
import com.foxinmy.weixin4j.api.CouponApi;
|
||||
import com.foxinmy.weixin4j.api.CustomsApi;
|
||||
@@ -42,6 +43,7 @@ import com.foxinmy.weixin4j.type.BillType;
|
||||
import com.foxinmy.weixin4j.type.CurrencyType;
|
||||
import com.foxinmy.weixin4j.type.CustomsCity;
|
||||
import com.foxinmy.weixin4j.type.IdQuery;
|
||||
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
|
||||
|
||||
/**
|
||||
* 微信支付接口实现
|
||||
@@ -73,13 +75,16 @@ public class WeixinPayProxy {
|
||||
/**
|
||||
* 配置信息
|
||||
*/
|
||||
private final Weixin4jSettings settings;
|
||||
private final Weixin4jSettings<WeixinPayAccount> settings;
|
||||
|
||||
/**
|
||||
* 使用weixin4j.properties配置的支付账号信息
|
||||
*/
|
||||
public WeixinPayProxy() {
|
||||
this(new Weixin4jSettings());
|
||||
this(
|
||||
new Weixin4jSettings<WeixinPayAccount>(JSON.parseObject(
|
||||
Weixin4jConfigUtil.getValue("account"),
|
||||
WeixinPayAccount.class)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,21 +93,21 @@ public class WeixinPayProxy {
|
||||
* 支付相关配置信息
|
||||
* @see com.foxinmy.weixin4j.setting.Weixin4jSettings
|
||||
*/
|
||||
public WeixinPayProxy(Weixin4jSettings settings) {
|
||||
public WeixinPayProxy(Weixin4jSettings<WeixinPayAccount> settings) {
|
||||
this.settings = settings;
|
||||
this.payApi = new PayApi(settings.getPayAccount());
|
||||
this.couponApi = new CouponApi(settings.getPayAccount());
|
||||
this.cashApi = new CashApi(settings.getPayAccount());
|
||||
this.customsApi = new CustomsApi(settings.getPayAccount());
|
||||
this.payApi = new PayApi(settings.getAccount());
|
||||
this.couponApi = new CouponApi(settings.getAccount());
|
||||
this.cashApi = new CashApi(settings.getAccount());
|
||||
this.customsApi = new CustomsApi(settings.getAccount());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信商户支付信息
|
||||
* 获取微信商户账号信息
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public WeixinPayAccount getWeixinPayAccount() {
|
||||
return this.settings.getPayAccount();
|
||||
return settings.getAccount();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -111,7 +116,7 @@ public class WeixinPayProxy {
|
||||
* @return
|
||||
*/
|
||||
public WeixinSignature getWeixinSignature() {
|
||||
return this.payApi.getWeixinSignature();
|
||||
return payApi.getWeixinSignature();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
package com.foxinmy.weixin4j.setting;
|
||||
|
||||
import com.foxinmy.weixin4j.http.HttpParams;
|
||||
|
||||
/**
|
||||
* 系统配置相关
|
||||
*
|
||||
* @className SystemSettings
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2016年1月28日
|
||||
* @since JDK 1.6
|
||||
* @see
|
||||
*/
|
||||
public abstract class SystemSettings<T> {
|
||||
/**
|
||||
* 账号信息
|
||||
*/
|
||||
private T account;
|
||||
/**
|
||||
* Http参数
|
||||
*/
|
||||
private HttpParams httpParams;
|
||||
/**
|
||||
* 系统临时目录
|
||||
*/
|
||||
private String tmpdir;
|
||||
|
||||
/**
|
||||
* @param account
|
||||
*/
|
||||
public SystemSettings(T account) {
|
||||
this.account = account;
|
||||
}
|
||||
|
||||
public T getAccount() {
|
||||
return account;
|
||||
}
|
||||
|
||||
public HttpParams getHttpParams() {
|
||||
return httpParams;
|
||||
}
|
||||
|
||||
public HttpParams getHttpParams0() {
|
||||
if (httpParams == null) {
|
||||
return new HttpParams();
|
||||
}
|
||||
return httpParams;
|
||||
}
|
||||
|
||||
public String getTmpdir() {
|
||||
return tmpdir;
|
||||
}
|
||||
|
||||
public abstract String getTmpdir0();
|
||||
|
||||
public void setHttpParams(HttpParams httpParams) {
|
||||
this.httpParams = httpParams;
|
||||
}
|
||||
|
||||
public void setTmpdir(String tmpdir) {
|
||||
this.tmpdir = tmpdir;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "account=" + account + ", httpParams=" + httpParams
|
||||
+ ", tmpdir=" + tmpdir;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,9 @@
|
||||
package com.foxinmy.weixin4j.setting;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.foxinmy.weixin4j.cache.CacheStorager;
|
||||
import com.foxinmy.weixin4j.cache.FileCacheStorager;
|
||||
import com.foxinmy.weixin4j.http.HttpParams;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.model.WeixinAccount;
|
||||
import com.foxinmy.weixin4j.model.WeixinPayAccount;
|
||||
import com.foxinmy.weixin4j.util.StringUtil;
|
||||
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
|
||||
|
||||
@@ -18,11 +16,19 @@ import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
|
||||
* @since JDK 1.6
|
||||
* @see
|
||||
*/
|
||||
public class Weixin4jSettings extends SystemSettings<WeixinAccount> {
|
||||
public class Weixin4jSettings<T> {
|
||||
/**
|
||||
* 微信支付账号信息
|
||||
* 账号信息
|
||||
*/
|
||||
private WeixinPayAccount weixinPayAccount;
|
||||
private final T account;
|
||||
/**
|
||||
* Http参数
|
||||
*/
|
||||
private HttpParams httpParams;
|
||||
/**
|
||||
* 系统临时目录
|
||||
*/
|
||||
private String tmpdir;
|
||||
/**
|
||||
* Token的存储方式 默认为FileCacheStorager
|
||||
*/
|
||||
@@ -32,60 +38,36 @@ public class Weixin4jSettings extends SystemSettings<WeixinAccount> {
|
||||
*/
|
||||
private String certificateFile;
|
||||
|
||||
/**
|
||||
* 默认使用weixin4j.properties配置的信息
|
||||
*/
|
||||
public Weixin4jSettings() {
|
||||
this(JSON.parseObject(Weixin4jConfigUtil.getValue("account"),
|
||||
WeixinPayAccount.class), null);
|
||||
public Weixin4jSettings(T account) {
|
||||
this.account = account;
|
||||
}
|
||||
|
||||
/**
|
||||
* 支付代理接口
|
||||
*
|
||||
* @param weixinPayAccount
|
||||
* 商户信息
|
||||
* @param certificateFile
|
||||
* 支付接口需要的证书文件(*.p12),比如退款接口
|
||||
*/
|
||||
public Weixin4jSettings(WeixinPayAccount weixinPayAccount,
|
||||
String certificateFile) {
|
||||
this(weixinPayAccount);
|
||||
this.certificateFile = certificateFile;
|
||||
public T getAccount() {
|
||||
return account;
|
||||
}
|
||||
|
||||
/**
|
||||
* 支付代理接口
|
||||
*
|
||||
* @param weixinPayAccount
|
||||
* 商户信息
|
||||
*/
|
||||
public Weixin4jSettings(WeixinPayAccount weixinPayAccount) {
|
||||
this(new WeixinAccount(weixinPayAccount.getId(),
|
||||
weixinPayAccount.getSecret()));
|
||||
this.weixinPayAccount = weixinPayAccount;
|
||||
public HttpParams getHttpParams() {
|
||||
return httpParams;
|
||||
}
|
||||
|
||||
/**
|
||||
* 账号信息
|
||||
*
|
||||
* @param account
|
||||
*/
|
||||
public Weixin4jSettings(WeixinAccount account) {
|
||||
super(account);
|
||||
public String getTmpdir() {
|
||||
return tmpdir;
|
||||
}
|
||||
|
||||
public WeixinPayAccount getPayAccount() {
|
||||
return weixinPayAccount;
|
||||
public void setHttpParams(HttpParams httpParams) {
|
||||
this.httpParams = httpParams;
|
||||
}
|
||||
|
||||
public void setTmpdir(String tmpdir) {
|
||||
this.tmpdir = tmpdir;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTmpdir0() {
|
||||
if (StringUtil.isBlank(getTmpdir())) {
|
||||
return Weixin4jConfigUtil.getClassPathValue("weixin4j.tmpdir",
|
||||
if (StringUtil.isBlank(tmpdir)) {
|
||||
return Weixin4jConfigUtil.getValue("weixin4j.tmpdir",
|
||||
System.getProperty("java.io.tmpdir"));
|
||||
}
|
||||
return getTmpdir();
|
||||
return tmpdir;
|
||||
}
|
||||
|
||||
public CacheStorager<Token> getCacheStorager() {
|
||||
@@ -121,8 +103,8 @@ public class Weixin4jSettings extends SystemSettings<WeixinAccount> {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Weixin4jSettings [weixinPayAccount=" + weixinPayAccount
|
||||
+ ", certificateFile=" + certificateFile + ", "
|
||||
+ super.toString() + "]";
|
||||
return "Weixin4jSettings [account=" + account + ", httpParams="
|
||||
+ httpParams + ", tmpdir=" + tmpdir + ", cacheStorager="
|
||||
+ cacheStorager + ", certificateFile=" + certificateFile + "]";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user