优化微信配置类
This commit is contained in:
parent
ce843c4f24
commit
f956c66481
@ -6,8 +6,8 @@ import javax.net.ssl.HostnameVerifier;
|
|||||||
import javax.net.ssl.SSLContext;
|
import javax.net.ssl.SSLContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Http 参数
|
* Http请求参数
|
||||||
*
|
*
|
||||||
* @className HttpParams
|
* @className HttpParams
|
||||||
* @author jinyu(foxinmy@gmail.com)
|
* @author jinyu(foxinmy@gmail.com)
|
||||||
* @date 2015年8月13日
|
* @date 2015年8月13日
|
||||||
@ -16,104 +16,204 @@ import javax.net.ssl.SSLContext;
|
|||||||
*/
|
*/
|
||||||
public final class HttpParams {
|
public final class HttpParams {
|
||||||
|
|
||||||
private boolean allowUserInteraction = true;
|
private final boolean allowUserInteraction;
|
||||||
private int connectTimeout = 5000;
|
private final int connectTimeout;
|
||||||
private int socketTimeout = 5000;
|
private final int socketTimeout;
|
||||||
private int readTimeout = 5000;
|
private final int readTimeout;
|
||||||
private int chunkSize = 4096;
|
private final int chunkSize;
|
||||||
private long ifModifiedSince = 0l;
|
private final boolean followRedirects;
|
||||||
private boolean followRedirects = false;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 代理对象
|
* 代理对象
|
||||||
*/
|
*/
|
||||||
private Proxy proxy;
|
private final Proxy proxy;
|
||||||
/**
|
/**
|
||||||
* SSL对象
|
* SSL对象
|
||||||
*/
|
*/
|
||||||
private SSLContext sslContext;
|
private final SSLContext sslContext;
|
||||||
/**
|
/**
|
||||||
* hostname对象
|
* 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() {
|
public boolean isAllowUserInteraction() {
|
||||||
return allowUserInteraction;
|
return allowUserInteraction;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAllowUserInteraction(boolean allowUserInteraction) {
|
|
||||||
this.allowUserInteraction = allowUserInteraction;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getConnectTimeout() {
|
public int getConnectTimeout() {
|
||||||
return connectTimeout;
|
return connectTimeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setConnectTimeout(int connectTimeout) {
|
|
||||||
this.connectTimeout = connectTimeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getSocketTimeout() {
|
public int getSocketTimeout() {
|
||||||
return socketTimeout;
|
return socketTimeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSocketTimeout(int socketTimeout) {
|
|
||||||
this.socketTimeout = socketTimeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getReadTimeout() {
|
public int getReadTimeout() {
|
||||||
return readTimeout;
|
return readTimeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setReadTimeout(int readTimeout) {
|
|
||||||
this.readTimeout = readTimeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getChunkSize() {
|
public int getChunkSize() {
|
||||||
return chunkSize;
|
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() {
|
public boolean isFollowRedirects() {
|
||||||
return followRedirects;
|
return followRedirects;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFollowRedirects(boolean followRedirects) {
|
|
||||||
this.followRedirects = followRedirects;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Proxy getProxy() {
|
public Proxy getProxy() {
|
||||||
return proxy;
|
return proxy;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setProxy(Proxy proxy) {
|
|
||||||
this.proxy = proxy;
|
|
||||||
}
|
|
||||||
|
|
||||||
public SSLContext getSSLContext() {
|
public SSLContext getSSLContext() {
|
||||||
return sslContext;
|
return sslContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSSLContext(SSLContext sslContext) {
|
|
||||||
this.sslContext = sslContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
public HostnameVerifier getHostnameVerifier() {
|
public HostnameVerifier getHostnameVerifier() {
|
||||||
return hostnameVerifier;
|
return hostnameVerifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHostnameVerifier(HostnameVerifier hostnameVerifier) {
|
public static HttpParams.Builder custom() {
|
||||||
this.hostnameVerifier = hostnameVerifier;
|
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 简单实现
|
* HTTP 简单实现
|
||||||
*
|
*
|
||||||
* @className SimpleHttpClient
|
* @className SimpleHttpClient
|
||||||
* @author jinyu(foxinmy@gmail.com)
|
* @author jinyu(foxinmy@gmail.com)
|
||||||
* @date 2015年5月29日
|
* @date 2015年5月29日
|
||||||
@ -87,7 +87,6 @@ public class SimpleHttpClient extends AbstractHttpClient implements HttpClient {
|
|||||||
.isAllowUserInteraction());
|
.isAllowUserInteraction());
|
||||||
connection.setConnectTimeout(params.getConnectTimeout());
|
connection.setConnectTimeout(params.getConnectTimeout());
|
||||||
connection.setReadTimeout(params.getReadTimeout());
|
connection.setReadTimeout(params.getReadTimeout());
|
||||||
connection.setIfModifiedSince(params.getIfModifiedSince());
|
|
||||||
connection.setInstanceFollowRedirects(params
|
connection.setInstanceFollowRedirects(params
|
||||||
.isFollowRedirects());
|
.isFollowRedirects());
|
||||||
}
|
}
|
||||||
|
|||||||
@ -38,16 +38,16 @@ public class WeixinRequestExecutor {
|
|||||||
protected final InternalLogger logger = InternalLoggerFactory
|
protected final InternalLogger logger = InternalLoggerFactory
|
||||||
.getInstance(getClass());
|
.getInstance(getClass());
|
||||||
|
|
||||||
protected final HttpClient httpClient;
|
private final HttpClient httpClient;
|
||||||
protected final HttpParams params;
|
private final HttpParams httpParams;
|
||||||
|
|
||||||
public WeixinRequestExecutor() {
|
public WeixinRequestExecutor() {
|
||||||
this(new HttpParams());
|
this(HttpParams.custom().build());
|
||||||
}
|
}
|
||||||
|
|
||||||
public WeixinRequestExecutor(HttpParams params) {
|
public WeixinRequestExecutor(HttpParams httpParams) {
|
||||||
this.httpClient = HttpClientFactory.getInstance();
|
this.httpClient = HttpClientFactory.getInstance();
|
||||||
this.params = params;
|
this.httpParams = httpParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
public WeixinResponse get(String url) throws WeixinException {
|
public WeixinResponse get(String url) throws WeixinException {
|
||||||
@ -95,7 +95,7 @@ public class WeixinRequestExecutor {
|
|||||||
|
|
||||||
protected WeixinResponse doRequest(HttpRequest request)
|
protected WeixinResponse doRequest(HttpRequest request)
|
||||||
throws WeixinException {
|
throws WeixinException {
|
||||||
request.setParams(params);
|
request.setParams(httpParams);
|
||||||
try {
|
try {
|
||||||
logger.info("weixin request >> " + request.getMethod() + " "
|
logger.info("weixin request >> " + request.getMethod() + " "
|
||||||
+ request.getURI().toString());
|
+ request.getURI().toString());
|
||||||
@ -180,6 +180,6 @@ public class WeixinRequestExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public HttpParams getExecuteParams() {
|
public HttpParams getExecuteParams() {
|
||||||
return params;
|
return httpParams;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,11 +7,13 @@ import javax.net.ssl.KeyManagerFactory;
|
|||||||
import javax.net.ssl.SSLContext;
|
import javax.net.ssl.SSLContext;
|
||||||
|
|
||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
|
import com.foxinmy.weixin4j.http.HttpParams;
|
||||||
|
import com.foxinmy.weixin4j.http.HttpRequest;
|
||||||
import com.foxinmy.weixin4j.model.Consts;
|
import com.foxinmy.weixin4j.model.Consts;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信ssl请求
|
* 微信ssl请求
|
||||||
*
|
*
|
||||||
* @className WeixinSSLRequestExecutor
|
* @className WeixinSSLRequestExecutor
|
||||||
* @author jinyu(foxinmy@gmail.com)
|
* @author jinyu(foxinmy@gmail.com)
|
||||||
* @date 2015年8月17日
|
* @date 2015年8月17日
|
||||||
@ -36,15 +38,27 @@ public class WeixinSSLRequestExecutor extends WeixinRequestExecutor {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new WeixinException("Key load error", e);
|
throw new WeixinException("Key load error", e);
|
||||||
}
|
}
|
||||||
params.setSSLContext(sslContext);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public WeixinSSLRequestExecutor(SSLContext sslContext) {
|
public WeixinSSLRequestExecutor(SSLContext sslContext) {
|
||||||
this.sslContext = sslContext;
|
this.sslContext = sslContext;
|
||||||
params.setSSLContext(sslContext);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public SSLContext getSSLContext() {
|
public SSLContext getSSLContext() {
|
||||||
return sslContext;
|
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.io.InputStream;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.foxinmy.weixin4j.api.CashApi;
|
import com.foxinmy.weixin4j.api.CashApi;
|
||||||
import com.foxinmy.weixin4j.api.CouponApi;
|
import com.foxinmy.weixin4j.api.CouponApi;
|
||||||
import com.foxinmy.weixin4j.api.CustomsApi;
|
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.CurrencyType;
|
||||||
import com.foxinmy.weixin4j.type.CustomsCity;
|
import com.foxinmy.weixin4j.type.CustomsCity;
|
||||||
import com.foxinmy.weixin4j.type.IdQuery;
|
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配置的支付账号信息
|
* 使用weixin4j.properties配置的支付账号信息
|
||||||
*/
|
*/
|
||||||
public WeixinPayProxy() {
|
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
|
* @see com.foxinmy.weixin4j.setting.Weixin4jSettings
|
||||||
*/
|
*/
|
||||||
public WeixinPayProxy(Weixin4jSettings settings) {
|
public WeixinPayProxy(Weixin4jSettings<WeixinPayAccount> settings) {
|
||||||
this.settings = settings;
|
this.settings = settings;
|
||||||
this.payApi = new PayApi(settings.getPayAccount());
|
this.payApi = new PayApi(settings.getAccount());
|
||||||
this.couponApi = new CouponApi(settings.getPayAccount());
|
this.couponApi = new CouponApi(settings.getAccount());
|
||||||
this.cashApi = new CashApi(settings.getPayAccount());
|
this.cashApi = new CashApi(settings.getAccount());
|
||||||
this.customsApi = new CustomsApi(settings.getPayAccount());
|
this.customsApi = new CustomsApi(settings.getAccount());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取微信商户支付信息
|
* 获取微信商户账号信息
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public WeixinPayAccount getWeixinPayAccount() {
|
public WeixinPayAccount getWeixinPayAccount() {
|
||||||
return this.settings.getPayAccount();
|
return settings.getAccount();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -111,7 +116,7 @@ public class WeixinPayProxy {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public WeixinSignature getWeixinSignature() {
|
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;
|
package com.foxinmy.weixin4j.setting;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
|
||||||
import com.foxinmy.weixin4j.cache.CacheStorager;
|
import com.foxinmy.weixin4j.cache.CacheStorager;
|
||||||
import com.foxinmy.weixin4j.cache.FileCacheStorager;
|
import com.foxinmy.weixin4j.cache.FileCacheStorager;
|
||||||
|
import com.foxinmy.weixin4j.http.HttpParams;
|
||||||
import com.foxinmy.weixin4j.model.Token;
|
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.StringUtil;
|
||||||
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
|
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
|
||||||
|
|
||||||
@ -18,11 +16,19 @@ import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
|
|||||||
* @since JDK 1.6
|
* @since JDK 1.6
|
||||||
* @see
|
* @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
|
* Token的存储方式 默认为FileCacheStorager
|
||||||
*/
|
*/
|
||||||
@ -32,60 +38,36 @@ public class Weixin4jSettings extends SystemSettings<WeixinAccount> {
|
|||||||
*/
|
*/
|
||||||
private String certificateFile;
|
private String certificateFile;
|
||||||
|
|
||||||
/**
|
public Weixin4jSettings(T account) {
|
||||||
* 默认使用weixin4j.properties配置的信息
|
this.account = account;
|
||||||
*/
|
|
||||||
public Weixin4jSettings() {
|
|
||||||
this(JSON.parseObject(Weixin4jConfigUtil.getValue("account"),
|
|
||||||
WeixinPayAccount.class), null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public T getAccount() {
|
||||||
* 支付代理接口
|
return account;
|
||||||
*
|
|
||||||
* @param weixinPayAccount
|
|
||||||
* 商户信息
|
|
||||||
* @param certificateFile
|
|
||||||
* 支付接口需要的证书文件(*.p12),比如退款接口
|
|
||||||
*/
|
|
||||||
public Weixin4jSettings(WeixinPayAccount weixinPayAccount,
|
|
||||||
String certificateFile) {
|
|
||||||
this(weixinPayAccount);
|
|
||||||
this.certificateFile = certificateFile;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public HttpParams getHttpParams() {
|
||||||
* 支付代理接口
|
return httpParams;
|
||||||
*
|
|
||||||
* @param weixinPayAccount
|
|
||||||
* 商户信息
|
|
||||||
*/
|
|
||||||
public Weixin4jSettings(WeixinPayAccount weixinPayAccount) {
|
|
||||||
this(new WeixinAccount(weixinPayAccount.getId(),
|
|
||||||
weixinPayAccount.getSecret()));
|
|
||||||
this.weixinPayAccount = weixinPayAccount;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public String getTmpdir() {
|
||||||
* 账号信息
|
return tmpdir;
|
||||||
*
|
|
||||||
* @param account
|
|
||||||
*/
|
|
||||||
public Weixin4jSettings(WeixinAccount account) {
|
|
||||||
super(account);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public WeixinPayAccount getPayAccount() {
|
public void setHttpParams(HttpParams httpParams) {
|
||||||
return weixinPayAccount;
|
this.httpParams = httpParams;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTmpdir(String tmpdir) {
|
||||||
|
this.tmpdir = tmpdir;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getTmpdir0() {
|
public String getTmpdir0() {
|
||||||
if (StringUtil.isBlank(getTmpdir())) {
|
if (StringUtil.isBlank(tmpdir)) {
|
||||||
return Weixin4jConfigUtil.getClassPathValue("weixin4j.tmpdir",
|
return Weixin4jConfigUtil.getValue("weixin4j.tmpdir",
|
||||||
System.getProperty("java.io.tmpdir"));
|
System.getProperty("java.io.tmpdir"));
|
||||||
}
|
}
|
||||||
return getTmpdir();
|
return tmpdir;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CacheStorager<Token> getCacheStorager() {
|
public CacheStorager<Token> getCacheStorager() {
|
||||||
@ -121,8 +103,8 @@ public class Weixin4jSettings extends SystemSettings<WeixinAccount> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Weixin4jSettings [weixinPayAccount=" + weixinPayAccount
|
return "Weixin4jSettings [account=" + account + ", httpParams="
|
||||||
+ ", certificateFile=" + certificateFile + ", "
|
+ httpParams + ", tmpdir=" + tmpdir + ", cacheStorager="
|
||||||
+ super.toString() + "]";
|
+ cacheStorager + ", certificateFile=" + certificateFile + "]";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -23,10 +23,12 @@ public class HttpClientTest {
|
|||||||
static HttpRequest request = new HttpRequest(HttpMethod.GET,
|
static HttpRequest request = new HttpRequest(HttpMethod.GET,
|
||||||
"http://www.iteye.com/");
|
"http://www.iteye.com/");
|
||||||
static {
|
static {
|
||||||
HttpParams params = new HttpParams();
|
HttpParams params = HttpParams
|
||||||
params.setProxy(new Proxy(Type.HTTP, new InetSocketAddress(
|
.custom()
|
||||||
"117.136.234.9", 80)));
|
.setProxy(
|
||||||
//request.setParams(params);
|
new Proxy(Type.HTTP, new InetSocketAddress(
|
||||||
|
"117.136.234.9", 80))).build();
|
||||||
|
// request.setParams(params);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void test1() throws HttpClientException {
|
public static void test1() throws HttpClientException {
|
||||||
@ -68,6 +70,6 @@ public class HttpClientTest {
|
|||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
test1();
|
test1();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -58,6 +58,7 @@ import com.foxinmy.weixin4j.tuple.MpVideo;
|
|||||||
import com.foxinmy.weixin4j.tuple.Tuple;
|
import com.foxinmy.weixin4j.tuple.Tuple;
|
||||||
import com.foxinmy.weixin4j.type.MediaType;
|
import com.foxinmy.weixin4j.type.MediaType;
|
||||||
import com.foxinmy.weixin4j.type.TicketType;
|
import com.foxinmy.weixin4j.type.TicketType;
|
||||||
|
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信公众平台接口实现
|
* 微信公众平台接口实现
|
||||||
@ -125,13 +126,14 @@ public class WeixinProxy {
|
|||||||
/**
|
/**
|
||||||
* 配置信息
|
* 配置信息
|
||||||
*/
|
*/
|
||||||
private Weixin4jSettings settings;
|
private Weixin4jSettings<WeixinAccount> settings;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 默认使用文件方式保存token、使用weixin4j.properties配置的账号信息
|
* 默认使用文件方式保存token、使用weixin4j.properties配置的账号信息
|
||||||
*/
|
*/
|
||||||
public WeixinProxy() {
|
public WeixinProxy() {
|
||||||
this(new Weixin4jSettings());
|
this(new Weixin4jSettings<WeixinAccount>(
|
||||||
|
Weixin4jConfigUtil.getWeixinAccount()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -140,7 +142,7 @@ public class WeixinProxy {
|
|||||||
* 微信配置信息
|
* 微信配置信息
|
||||||
* @see com.foxinmy.weixin4j.setting.Weixin4jSettings
|
* @see com.foxinmy.weixin4j.setting.Weixin4jSettings
|
||||||
*/
|
*/
|
||||||
public WeixinProxy(Weixin4jSettings settings) {
|
public WeixinProxy(Weixin4jSettings<WeixinAccount> settings) {
|
||||||
this(new TokenManager(new WeixinTokenCreator(settings.getAccount()
|
this(new TokenManager(new WeixinTokenCreator(settings.getAccount()
|
||||||
.getId(), settings.getAccount().getSecret()),
|
.getId(), settings.getAccount().getSecret()),
|
||||||
settings.getCacheStorager0()));
|
settings.getCacheStorager0()));
|
||||||
@ -152,6 +154,7 @@ public class WeixinProxy {
|
|||||||
*
|
*
|
||||||
* @see com.foxinmy.weixin4j.mp.token.WeixinTokenCreator
|
* @see com.foxinmy.weixin4j.mp.token.WeixinTokenCreator
|
||||||
* @param tokenManager
|
* @param tokenManager
|
||||||
|
* token管理
|
||||||
*/
|
*/
|
||||||
private WeixinProxy(TokenManager tokenManager) {
|
private WeixinProxy(TokenManager tokenManager) {
|
||||||
this.tokenManager = tokenManager;
|
this.tokenManager = tokenManager;
|
||||||
|
|||||||
@ -17,7 +17,6 @@ import com.foxinmy.weixin4j.http.ContentType;
|
|||||||
import com.foxinmy.weixin4j.http.HttpClientException;
|
import com.foxinmy.weixin4j.http.HttpClientException;
|
||||||
import com.foxinmy.weixin4j.http.HttpHeaders;
|
import com.foxinmy.weixin4j.http.HttpHeaders;
|
||||||
import com.foxinmy.weixin4j.http.HttpMethod;
|
import com.foxinmy.weixin4j.http.HttpMethod;
|
||||||
import com.foxinmy.weixin4j.http.HttpParams;
|
|
||||||
import com.foxinmy.weixin4j.http.HttpRequest;
|
import com.foxinmy.weixin4j.http.HttpRequest;
|
||||||
import com.foxinmy.weixin4j.http.HttpResponse;
|
import com.foxinmy.weixin4j.http.HttpResponse;
|
||||||
import com.foxinmy.weixin4j.http.apache.ByteArrayBody;
|
import com.foxinmy.weixin4j.http.apache.ByteArrayBody;
|
||||||
@ -252,8 +251,7 @@ public class MediaApi extends MpApi {
|
|||||||
request = new HttpRequest(HttpMethod.GET, String.format(
|
request = new HttpRequest(HttpMethod.GET, String.format(
|
||||||
meida_download_uri, token.getAccessToken(), mediaId));
|
meida_download_uri, token.getAccessToken(), mediaId));
|
||||||
}
|
}
|
||||||
HttpParams params = weixinExecutor.getExecuteParams();
|
request.setParams(weixinExecutor.getExecuteParams());
|
||||||
request.setParams(params);
|
|
||||||
logger.info("weixin request >> " + request.getMethod() + " "
|
logger.info("weixin request >> " + request.getMethod() + " "
|
||||||
+ request.getURI().toString());
|
+ request.getURI().toString());
|
||||||
HttpResponse response = weixinExecutor.getExecuteClient().execute(
|
HttpResponse response = weixinExecutor.getExecuteClient().execute(
|
||||||
|
|||||||
@ -5,9 +5,11 @@ import org.junit.Before;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
|
import com.foxinmy.weixin4j.model.WeixinAccount;
|
||||||
import com.foxinmy.weixin4j.mp.token.WeixinTokenCreator;
|
import com.foxinmy.weixin4j.mp.token.WeixinTokenCreator;
|
||||||
import com.foxinmy.weixin4j.setting.Weixin4jSettings;
|
import com.foxinmy.weixin4j.setting.Weixin4jSettings;
|
||||||
import com.foxinmy.weixin4j.token.TokenManager;
|
import com.foxinmy.weixin4j.token.TokenManager;
|
||||||
|
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* token测试
|
* token测试
|
||||||
@ -20,11 +22,12 @@ import com.foxinmy.weixin4j.token.TokenManager;
|
|||||||
public class TokenTest {
|
public class TokenTest {
|
||||||
|
|
||||||
protected TokenManager tokenManager;
|
protected TokenManager tokenManager;
|
||||||
protected Weixin4jSettings settings;
|
protected Weixin4jSettings<WeixinAccount> settings;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
this.settings = new Weixin4jSettings();
|
this.settings = new Weixin4jSettings<WeixinAccount>(
|
||||||
|
Weixin4jConfigUtil.getWeixinAccount());
|
||||||
tokenManager = new TokenManager(new WeixinTokenCreator(settings
|
tokenManager = new TokenManager(new WeixinTokenCreator(settings
|
||||||
.getAccount().getId(), settings.getAccount().getSecret()),
|
.getAccount().getId(), settings.getAccount().getSecret()),
|
||||||
settings.getCacheStorager0());
|
settings.getCacheStorager0());
|
||||||
|
|||||||
@ -51,6 +51,7 @@ import com.foxinmy.weixin4j.token.TokenManager;
|
|||||||
import com.foxinmy.weixin4j.tuple.MpArticle;
|
import com.foxinmy.weixin4j.tuple.MpArticle;
|
||||||
import com.foxinmy.weixin4j.type.MediaType;
|
import com.foxinmy.weixin4j.type.MediaType;
|
||||||
import com.foxinmy.weixin4j.type.TicketType;
|
import com.foxinmy.weixin4j.type.TicketType;
|
||||||
|
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信企业号接口实现
|
* 微信企业号接口实现
|
||||||
@ -110,13 +111,14 @@ public class WeixinProxy {
|
|||||||
/**
|
/**
|
||||||
* 配置信息
|
* 配置信息
|
||||||
*/
|
*/
|
||||||
private Weixin4jSettings settings;
|
private Weixin4jSettings<WeixinAccount> settings;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 默认使用文件方式保存token、使用weixin4j.properties配置的账号信息
|
* 默认使用文件方式保存token、使用weixin4j.properties配置的账号信息
|
||||||
*/
|
*/
|
||||||
public WeixinProxy() {
|
public WeixinProxy() {
|
||||||
this(new Weixin4jSettings());
|
this(new Weixin4jSettings<WeixinAccount>(
|
||||||
|
Weixin4jConfigUtil.getWeixinAccount()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -125,7 +127,7 @@ public class WeixinProxy {
|
|||||||
* 微信配置信息
|
* 微信配置信息
|
||||||
* @see com.foxinmy.weixin4j.setting.Weixin4jSettings
|
* @see com.foxinmy.weixin4j.setting.Weixin4jSettings
|
||||||
*/
|
*/
|
||||||
public WeixinProxy(Weixin4jSettings settings) {
|
public WeixinProxy(Weixin4jSettings<WeixinAccount> settings) {
|
||||||
this(new TokenManager(new WeixinTokenCreator(settings.getAccount()
|
this(new TokenManager(new WeixinTokenCreator(settings.getAccount()
|
||||||
.getId(), settings.getAccount().getSecret()),
|
.getId(), settings.getAccount().getSecret()),
|
||||||
settings.getCacheStorager0()));
|
settings.getCacheStorager0()));
|
||||||
@ -148,7 +150,7 @@ public class WeixinProxy {
|
|||||||
TokenManager suiteTokenManager) {
|
TokenManager suiteTokenManager) {
|
||||||
this(new TokenManager(new WeixinTokenSuiteCreator(perCodeManager,
|
this(new TokenManager(new WeixinTokenSuiteCreator(perCodeManager,
|
||||||
suiteTokenManager), perCodeManager.getCacheStorager()));
|
suiteTokenManager), perCodeManager.getCacheStorager()));
|
||||||
this.settings = new Weixin4jSettings(new WeixinAccount(
|
this.settings = new Weixin4jSettings<WeixinAccount>(new WeixinAccount(
|
||||||
perCodeManager.getAuthCorpId(), null));
|
perCodeManager.getAuthCorpId(), null));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -3,8 +3,10 @@ package com.foxinmy.weixin4j.qy;
|
|||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
import com.foxinmy.weixin4j.model.Consts;
|
import com.foxinmy.weixin4j.model.Consts;
|
||||||
import com.foxinmy.weixin4j.model.WeixinAccount;
|
import com.foxinmy.weixin4j.model.WeixinAccount;
|
||||||
@ -13,10 +15,10 @@ import com.foxinmy.weixin4j.qy.api.SuiteApi;
|
|||||||
import com.foxinmy.weixin4j.qy.model.OUserInfo;
|
import com.foxinmy.weixin4j.qy.model.OUserInfo;
|
||||||
import com.foxinmy.weixin4j.qy.model.WeixinQyAccount;
|
import com.foxinmy.weixin4j.qy.model.WeixinQyAccount;
|
||||||
import com.foxinmy.weixin4j.qy.suite.SuiteTicketManager;
|
import com.foxinmy.weixin4j.qy.suite.SuiteTicketManager;
|
||||||
import com.foxinmy.weixin4j.qy.suite.Weixin4jSuiteSettings;
|
|
||||||
import com.foxinmy.weixin4j.qy.token.WeixinProviderTokenCreator;
|
import com.foxinmy.weixin4j.qy.token.WeixinProviderTokenCreator;
|
||||||
import com.foxinmy.weixin4j.qy.type.LoginTargetType;
|
import com.foxinmy.weixin4j.qy.type.LoginTargetType;
|
||||||
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
||||||
|
import com.foxinmy.weixin4j.setting.Weixin4jSettings;
|
||||||
import com.foxinmy.weixin4j.token.TokenManager;
|
import com.foxinmy.weixin4j.token.TokenManager;
|
||||||
import com.foxinmy.weixin4j.util.StringUtil;
|
import com.foxinmy.weixin4j.util.StringUtil;
|
||||||
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
|
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
|
||||||
@ -46,41 +48,44 @@ public class WeixinSuiteProxy {
|
|||||||
/**
|
/**
|
||||||
* 配置相关
|
* 配置相关
|
||||||
*/
|
*/
|
||||||
private final Weixin4jSuiteSettings suiteSettings;
|
private final Weixin4jSettings<WeixinQyAccount> settings;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认使用文件方式保存token、使用weixin4j.properties配置的账号信息
|
||||||
|
*/
|
||||||
public WeixinSuiteProxy() {
|
public WeixinSuiteProxy() {
|
||||||
this(new Weixin4jSuiteSettings());
|
this(new Weixin4jSettings<WeixinQyAccount>(JSON.parseObject(
|
||||||
|
Weixin4jConfigUtil.getValue("account"), WeixinQyAccount.class)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param suiteSettings
|
* @param settings
|
||||||
* 套件信息配置
|
* 配置信息
|
||||||
*/
|
*/
|
||||||
public WeixinSuiteProxy(Weixin4jSuiteSettings suiteSettings) {
|
public WeixinSuiteProxy(Weixin4jSettings<WeixinQyAccount> settings) {
|
||||||
this.suiteSettings = suiteSettings;
|
this.settings = settings;
|
||||||
if (suiteSettings.getAccount().getSuiteAccounts() != null) {
|
List<WeixinAccount> suites = settings.getAccount().getSuites();
|
||||||
this.suiteMap = new HashMap<String, SuiteApi>();
|
if (suites != null && !suites.isEmpty()) {
|
||||||
for (WeixinAccount suite : suiteSettings.getAccount()
|
this.suiteMap = new HashMap<String, SuiteApi>(suites.size());
|
||||||
.getSuiteAccounts()) {
|
for (WeixinAccount suite : suites) {
|
||||||
this.suiteMap.put(suite.getId(), new SuiteApi(
|
|
||||||
new SuiteTicketManager(suite.getId(), suite.getSecret(),
|
|
||||||
suiteSettings.getCacheStorager0())));
|
|
||||||
this.suiteMap.put(
|
this.suiteMap.put(
|
||||||
null,
|
suite.getId(),
|
||||||
suiteMap.get(suiteSettings.getAccount()
|
new SuiteApi(
|
||||||
.getSuiteAccounts().get(0).getId()));
|
new SuiteTicketManager(suite.getId(), suite
|
||||||
|
.getSecret(), settings
|
||||||
|
.getCacheStorager0())));
|
||||||
}
|
}
|
||||||
|
this.suiteMap.put(null, suiteMap.get(suites.get(0).getId()));
|
||||||
}
|
}
|
||||||
if (StringUtil.isNotBlank(suiteSettings.getAccount().getId())
|
if (StringUtil.isNotBlank(settings.getAccount().getId())
|
||||||
&& StringUtil.isNotBlank(suiteSettings.getAccount()
|
&& StringUtil.isNotBlank(settings.getAccount()
|
||||||
.getProviderSecret())) {
|
.getProviderSecret())) {
|
||||||
this.providerApi = new ProviderApi(new TokenManager(
|
this.providerApi = new ProviderApi(
|
||||||
new WeixinProviderTokenCreator(suiteSettings
|
new TokenManager(new WeixinProviderTokenCreator(settings
|
||||||
.getAccount().getId(), suiteSettings
|
.getAccount().getId(), settings.getAccount()
|
||||||
.getAccount().getProviderSecret()),
|
.getProviderSecret()), settings.getCacheStorager0()),
|
||||||
suiteSettings.getCacheStorager0()),
|
settings.getCacheStorager0());
|
||||||
suiteSettings.getCacheStorager0());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,7 +95,7 @@ public class WeixinSuiteProxy {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public WeixinQyAccount getWeixinAccount() {
|
public WeixinQyAccount getWeixinAccount() {
|
||||||
return this.suiteSettings.getAccount();
|
return this.settings.getAccount();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -20,7 +20,6 @@ import com.foxinmy.weixin4j.http.ContentType;
|
|||||||
import com.foxinmy.weixin4j.http.HttpClientException;
|
import com.foxinmy.weixin4j.http.HttpClientException;
|
||||||
import com.foxinmy.weixin4j.http.HttpHeaders;
|
import com.foxinmy.weixin4j.http.HttpHeaders;
|
||||||
import com.foxinmy.weixin4j.http.HttpMethod;
|
import com.foxinmy.weixin4j.http.HttpMethod;
|
||||||
import com.foxinmy.weixin4j.http.HttpParams;
|
|
||||||
import com.foxinmy.weixin4j.http.HttpRequest;
|
import com.foxinmy.weixin4j.http.HttpRequest;
|
||||||
import com.foxinmy.weixin4j.http.HttpResponse;
|
import com.foxinmy.weixin4j.http.HttpResponse;
|
||||||
import com.foxinmy.weixin4j.http.apache.ByteArrayBody;
|
import com.foxinmy.weixin4j.http.apache.ByteArrayBody;
|
||||||
@ -50,7 +49,7 @@ import com.foxinmy.weixin4j.util.StringUtil;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 媒体相关API
|
* 媒体相关API
|
||||||
*
|
*
|
||||||
* @className MediaApi
|
* @className MediaApi
|
||||||
* @author jinyu(foxinmy@gmail.com)
|
* @author jinyu(foxinmy@gmail.com)
|
||||||
* @date 2014年9月25日
|
* @date 2014年9月25日
|
||||||
@ -70,7 +69,7 @@ public class MediaApi extends QyApi {
|
|||||||
/**
|
/**
|
||||||
* 上传图文消息内的图片:用于上传图片到企业号服务端,接口返回图片url,请注意,该url仅可用于图文消息的发送,
|
* 上传图文消息内的图片:用于上传图片到企业号服务端,接口返回图片url,请注意,该url仅可用于图文消息的发送,
|
||||||
* 且每个企业每天最多只能上传100张图片。
|
* 且每个企业每天最多只能上传100张图片。
|
||||||
*
|
*
|
||||||
* @param is
|
* @param is
|
||||||
* 图片数据
|
* 图片数据
|
||||||
* @param fileName
|
* @param fileName
|
||||||
@ -103,7 +102,7 @@ public class MediaApi extends QyApi {
|
|||||||
* 正常情况下返回{"type":"TYPE","media_id":"MEDIA_ID","created_at":123456789},
|
* 正常情况下返回{"type":"TYPE","media_id":"MEDIA_ID","created_at":123456789},
|
||||||
* 否则抛出异常.
|
* 否则抛出异常.
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param agentid
|
* @param agentid
|
||||||
* 企业应用ID(<font color="red">大于0时视为上传永久媒体文件</font>)
|
* 企业应用ID(<font color="red">大于0时视为上传永久媒体文件</font>)
|
||||||
* @param is
|
* @param is
|
||||||
@ -185,7 +184,7 @@ public class MediaApi extends QyApi {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 下载媒体文件
|
* 下载媒体文件
|
||||||
*
|
*
|
||||||
* @param agentid
|
* @param agentid
|
||||||
* 企业应用Id(<font color="red">大于0时视为获取永久媒体文件</font>)
|
* 企业应用Id(<font color="red">大于0时视为获取永久媒体文件</font>)
|
||||||
* @param mediaId
|
* @param mediaId
|
||||||
@ -213,8 +212,7 @@ public class MediaApi extends QyApi {
|
|||||||
request = new HttpRequest(HttpMethod.GET, String.format(
|
request = new HttpRequest(HttpMethod.GET, String.format(
|
||||||
media_download_uri, token.getAccessToken(), mediaId));
|
media_download_uri, token.getAccessToken(), mediaId));
|
||||||
}
|
}
|
||||||
HttpParams params = weixinExecutor.getExecuteParams();
|
request.setParams(weixinExecutor.getExecuteParams());
|
||||||
request.setParams(params);
|
|
||||||
logger.info("weixin request >> " + request.getMethod() + " "
|
logger.info("weixin request >> " + request.getMethod() + " "
|
||||||
+ request.getURI().toString());
|
+ request.getURI().toString());
|
||||||
HttpResponse response = weixinExecutor.getExecuteClient().execute(
|
HttpResponse response = weixinExecutor.getExecuteClient().execute(
|
||||||
@ -260,7 +258,7 @@ public class MediaApi extends QyApi {
|
|||||||
* 、新增的永久素材也可以在公众平台官网素材管理模块中看到,永久素材的数量是有上限的,请谨慎新增。图文消息素材和图片素材的上限为5000,
|
* 、新增的永久素材也可以在公众平台官网素材管理模块中看到,永久素材的数量是有上限的,请谨慎新增。图文消息素材和图片素材的上限为5000,
|
||||||
* 其他类型为1000
|
* 其他类型为1000
|
||||||
* </P>
|
* </P>
|
||||||
*
|
*
|
||||||
* @param agentid
|
* @param agentid
|
||||||
* 企业应用的id
|
* 企业应用的id
|
||||||
* @param articles
|
* @param articles
|
||||||
@ -289,7 +287,7 @@ public class MediaApi extends QyApi {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除永久媒体素材
|
* 删除永久媒体素材
|
||||||
*
|
*
|
||||||
* @param agentid
|
* @param agentid
|
||||||
* 企业应用ID
|
* 企业应用ID
|
||||||
* @param mediaId
|
* @param mediaId
|
||||||
@ -311,7 +309,7 @@ public class MediaApi extends QyApi {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 下载永久图文素材
|
* 下载永久图文素材
|
||||||
*
|
*
|
||||||
* @param agentid
|
* @param agentid
|
||||||
* 企业应用ID
|
* 企业应用ID
|
||||||
* @param mediaId
|
* @param mediaId
|
||||||
@ -333,7 +331,7 @@ public class MediaApi extends QyApi {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 修改永久图文素材
|
* 修改永久图文素材
|
||||||
*
|
*
|
||||||
* @param agentid
|
* @param agentid
|
||||||
* 企业应用的id
|
* 企业应用的id
|
||||||
* @param mediaId
|
* @param mediaId
|
||||||
@ -365,7 +363,7 @@ public class MediaApi extends QyApi {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取永久媒体素材的总数
|
* 获取永久媒体素材的总数
|
||||||
*
|
*
|
||||||
* @param agentid
|
* @param agentid
|
||||||
* 企业应用id
|
* 企业应用id
|
||||||
* @return 总数对象
|
* @return 总数对象
|
||||||
@ -387,7 +385,7 @@ public class MediaApi extends QyApi {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取媒体素材记录列表
|
* 获取媒体素材记录列表
|
||||||
*
|
*
|
||||||
* @param agentid
|
* @param agentid
|
||||||
* 企业应用ID
|
* 企业应用ID
|
||||||
* @param mediaType
|
* @param mediaType
|
||||||
@ -427,7 +425,7 @@ public class MediaApi extends QyApi {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取全部的媒体素材
|
* 获取全部的媒体素材
|
||||||
*
|
*
|
||||||
* @param agentid
|
* @param agentid
|
||||||
* 企业应用id
|
* 企业应用id
|
||||||
* @param mediaType
|
* @param mediaType
|
||||||
@ -458,7 +456,7 @@ public class MediaApi extends QyApi {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 批量上传成员
|
* 批量上传成员
|
||||||
*
|
*
|
||||||
* @param users
|
* @param users
|
||||||
* 成员列表
|
* 成员列表
|
||||||
* @see {@link BatchApi#syncUser(String,Callback)}
|
* @see {@link BatchApi#syncUser(String,Callback)}
|
||||||
@ -474,7 +472,7 @@ public class MediaApi extends QyApi {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 批量上传部门
|
* 批量上传部门
|
||||||
*
|
*
|
||||||
* @param parties
|
* @param parties
|
||||||
* 部门列表
|
* 部门列表
|
||||||
* @see {@link BatchApi#replaceParty(String,Callback)}
|
* @see {@link BatchApi#replaceParty(String,Callback)}
|
||||||
|
|||||||
@ -8,7 +8,7 @@ import com.foxinmy.weixin4j.model.WeixinAccount;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信企业号信息
|
* 微信企业号信息
|
||||||
*
|
*
|
||||||
* @className WeixinQyAccount
|
* @className WeixinQyAccount
|
||||||
* @author jinyu(foxinmy@gmail.com)
|
* @author jinyu(foxinmy@gmail.com)
|
||||||
* @date 2014年11月18日
|
* @date 2014年11月18日
|
||||||
@ -23,7 +23,7 @@ public class WeixinQyAccount extends WeixinAccount {
|
|||||||
/**
|
/**
|
||||||
* 多个应用套件信息
|
* 多个应用套件信息
|
||||||
*/
|
*/
|
||||||
private List<WeixinAccount> suiteAccounts;
|
private List<WeixinAccount> suites;
|
||||||
/**
|
/**
|
||||||
* 第三方提供商secret(企业号登陆)
|
* 第三方提供商secret(企业号登陆)
|
||||||
*/
|
*/
|
||||||
@ -34,7 +34,7 @@ public class WeixinQyAccount extends WeixinAccount {
|
|||||||
private String chatSecret;
|
private String chatSecret;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param corpid
|
* @param corpid
|
||||||
* 企业ID 必填
|
* 企业ID 必填
|
||||||
* @param corpsecret
|
* @param corpsecret
|
||||||
@ -49,17 +49,17 @@ public class WeixinQyAccount extends WeixinAccount {
|
|||||||
@JSONCreator
|
@JSONCreator
|
||||||
public WeixinQyAccount(@JSONField(name = "id") String corpid,
|
public WeixinQyAccount(@JSONField(name = "id") String corpid,
|
||||||
@JSONField(name = "secret") String corpsecret,
|
@JSONField(name = "secret") String corpsecret,
|
||||||
@JSONField(name = "suiteAccounts") List<WeixinAccount> suiteAccounts,
|
@JSONField(name = "suites") List<WeixinAccount> suites,
|
||||||
@JSONField(name = "providerSecret") String providerSecret,
|
@JSONField(name = "providerSecret") String providerSecret,
|
||||||
@JSONField(name = "chatSecret") String chatSecret) {
|
@JSONField(name = "chatSecret") String chatSecret) {
|
||||||
super(corpid, corpsecret);
|
super(corpid, corpsecret);
|
||||||
this.suiteAccounts = suiteAccounts;
|
this.suites = suites;
|
||||||
this.providerSecret = providerSecret;
|
this.providerSecret = providerSecret;
|
||||||
this.chatSecret = chatSecret;
|
this.chatSecret = chatSecret;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<WeixinAccount> getSuiteAccounts() {
|
public List<WeixinAccount> getSuites() {
|
||||||
return suiteAccounts;
|
return suites;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getProviderSecret() {
|
public String getProviderSecret() {
|
||||||
@ -70,15 +70,15 @@ public class WeixinQyAccount extends WeixinAccount {
|
|||||||
return chatSecret;
|
return chatSecret;
|
||||||
}
|
}
|
||||||
|
|
||||||
public WeixinAccount[] suiteAccountsToArray() {
|
public WeixinAccount[] suitesToArray() {
|
||||||
return suiteAccounts != null ? suiteAccounts
|
return suites != null ? suites
|
||||||
.toArray(new WeixinAccount[suiteAccounts.size()]) : null;
|
.toArray(new WeixinAccount[suites.size()]) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "WeixinQyAccount [" + super.toString() + ", suiteAccounts="
|
return "WeixinQyAccount [" + super.toString() + ", suiteAccounts="
|
||||||
+ suiteAccounts + ", providerSecret=" + providerSecret
|
+ suites + ", providerSecret=" + providerSecret
|
||||||
+ ", chatSecret=" + chatSecret + "]";
|
+ ", chatSecret=" + chatSecret + "]";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,86 +0,0 @@
|
|||||||
package com.foxinmy.weixin4j.qy.suite;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
|
||||||
import com.foxinmy.weixin4j.cache.CacheStorager;
|
|
||||||
import com.foxinmy.weixin4j.cache.FileCacheStorager;
|
|
||||||
import com.foxinmy.weixin4j.model.Token;
|
|
||||||
import com.foxinmy.weixin4j.model.WeixinAccount;
|
|
||||||
import com.foxinmy.weixin4j.qy.model.WeixinQyAccount;
|
|
||||||
import com.foxinmy.weixin4j.setting.SystemSettings;
|
|
||||||
import com.foxinmy.weixin4j.util.StringUtil;
|
|
||||||
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 微信第三方套件配置相关
|
|
||||||
*
|
|
||||||
* @className Weixin4jSuiteSettings
|
|
||||||
* @author jinyu(foxinmy@gmail.com)
|
|
||||||
* @date 2016年1月28日
|
|
||||||
* @since JDK 1.6
|
|
||||||
* @see
|
|
||||||
*/
|
|
||||||
public class Weixin4jSuiteSettings extends SystemSettings<WeixinQyAccount> {
|
|
||||||
/**
|
|
||||||
* Token的存储方式 默认为FileCacheStorager
|
|
||||||
*/
|
|
||||||
private CacheStorager<Token> cacheStorager;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 默认使用weixin4j.properties配置的信息
|
|
||||||
*/
|
|
||||||
public Weixin4jSuiteSettings() {
|
|
||||||
this(JSON.parseObject(Weixin4jConfigUtil.getValue("account"),
|
|
||||||
WeixinQyAccount.class));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param providerCorpId
|
|
||||||
* 服务商的企业号ID <font color="red">使用服务商API时必填项</font>
|
|
||||||
* @param providerSecret
|
|
||||||
* 服务商secret <font color="red">使用服务商API时必填项</font>
|
|
||||||
* @param suites
|
|
||||||
* 套件信息 <font color="red">使用套件API时必填项</font>
|
|
||||||
*/
|
|
||||||
public Weixin4jSuiteSettings(String providerCorpId, String providerSecret,
|
|
||||||
WeixinAccount... suites) {
|
|
||||||
this(new WeixinQyAccount(providerCorpId, null, Arrays.asList(suites),
|
|
||||||
providerSecret, null));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 账号信息
|
|
||||||
*
|
|
||||||
* @param account
|
|
||||||
*/
|
|
||||||
private Weixin4jSuiteSettings(WeixinQyAccount account) {
|
|
||||||
super(account);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getTmpdir0() {
|
|
||||||
if (StringUtil.isBlank(getTmpdir())) {
|
|
||||||
return Weixin4jConfigUtil.getClassPathValue("weixin4j.tmpdir",
|
|
||||||
System.getProperty("java.io.tmpdir"));
|
|
||||||
}
|
|
||||||
return getTmpdir();
|
|
||||||
}
|
|
||||||
|
|
||||||
public CacheStorager<Token> getCacheStorager() {
|
|
||||||
return cacheStorager;
|
|
||||||
}
|
|
||||||
|
|
||||||
public CacheStorager<Token> getCacheStorager0() {
|
|
||||||
if (cacheStorager == null) {
|
|
||||||
return new FileCacheStorager<Token>(getTmpdir0());
|
|
||||||
}
|
|
||||||
return cacheStorager;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "Weixin4jSuiteSettings [" + super.toString() + "]";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -5,9 +5,11 @@ import org.junit.Before;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
|
import com.foxinmy.weixin4j.model.WeixinAccount;
|
||||||
import com.foxinmy.weixin4j.qy.token.WeixinTokenCreator;
|
import com.foxinmy.weixin4j.qy.token.WeixinTokenCreator;
|
||||||
import com.foxinmy.weixin4j.setting.Weixin4jSettings;
|
import com.foxinmy.weixin4j.setting.Weixin4jSettings;
|
||||||
import com.foxinmy.weixin4j.token.TokenManager;
|
import com.foxinmy.weixin4j.token.TokenManager;
|
||||||
|
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* token测试
|
* token测试
|
||||||
@ -20,14 +22,15 @@ import com.foxinmy.weixin4j.token.TokenManager;
|
|||||||
public class TokenTest {
|
public class TokenTest {
|
||||||
|
|
||||||
protected TokenManager tokenManager;
|
protected TokenManager tokenManager;
|
||||||
protected Weixin4jSettings settings;
|
protected Weixin4jSettings<WeixinAccount> settings;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
this.settings = new Weixin4jSettings();
|
this.settings = new Weixin4jSettings<WeixinAccount>(
|
||||||
|
Weixin4jConfigUtil.getWeixinAccount());
|
||||||
tokenManager = new TokenManager(new WeixinTokenCreator(settings
|
tokenManager = new TokenManager(new WeixinTokenCreator(settings
|
||||||
.getAccount().getId(), settings.getAccount()
|
.getAccount().getId(), settings.getAccount().getSecret()),
|
||||||
.getSecret()), settings.getCacheStorager0());
|
settings.getCacheStorager0());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user