@@ -1,74 +1,74 @@ | |||
package me.chanjar.weixin.cp.api; | |||
import me.chanjar.weixin.common.bean.WxAccessToken; | |||
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; | |||
import java.io.File; | |||
/** | |||
* 微信客户端配置存储 | |||
* | |||
* @author Daniel Qian | |||
*/ | |||
public interface WxCpConfigStorage { | |||
String getAccessToken(); | |||
boolean isAccessTokenExpired(); | |||
/** | |||
* 强制将access token过期掉 | |||
*/ | |||
void expireAccessToken(); | |||
void updateAccessToken(WxAccessToken accessToken); | |||
void updateAccessToken(String accessToken, int expiresIn); | |||
String getJsapiTicket(); | |||
boolean isJsapiTicketExpired(); | |||
/** | |||
* 强制将jsapi ticket过期掉 | |||
*/ | |||
void expireJsapiTicket(); | |||
/** | |||
* 应该是线程安全的 | |||
* | |||
* @param jsapiTicket | |||
*/ | |||
void updateJsapiTicket(String jsapiTicket, int expiresInSeconds); | |||
String getCorpId(); | |||
String getCorpSecret(); | |||
Integer getAgentId(); | |||
String getToken(); | |||
String getAesKey(); | |||
long getExpiresTime(); | |||
String getOauth2redirectUri(); | |||
String getHttpProxyHost(); | |||
int getHttpProxyPort(); | |||
String getHttpProxyUsername(); | |||
String getHttpProxyPassword(); | |||
File getTmpDirFile(); | |||
/** | |||
* http client builder | |||
* | |||
* @return ApacheHttpClientBuilder | |||
*/ | |||
ApacheHttpClientBuilder getApacheHttpClientBuilder(); | |||
} | |||
package me.chanjar.weixin.cp.api; | |||
import me.chanjar.weixin.common.bean.WxAccessToken; | |||
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; | |||
import java.io.File; | |||
/** | |||
* 微信客户端配置存储 | |||
* | |||
* @author Daniel Qian | |||
*/ | |||
public interface WxCpConfigStorage { | |||
String getAccessToken(); | |||
boolean isAccessTokenExpired(); | |||
/** | |||
* 强制将access token过期掉 | |||
*/ | |||
void expireAccessToken(); | |||
void updateAccessToken(WxAccessToken accessToken); | |||
void updateAccessToken(String accessToken, int expiresIn); | |||
String getJsapiTicket(); | |||
boolean isJsapiTicketExpired(); | |||
/** | |||
* 强制将jsapi ticket过期掉 | |||
*/ | |||
void expireJsapiTicket(); | |||
/** | |||
* 应该是线程安全的 | |||
* | |||
* @param jsapiTicket | |||
*/ | |||
void updateJsapiTicket(String jsapiTicket, int expiresInSeconds); | |||
String getCorpId(); | |||
String getCorpSecret(); | |||
Integer getAgentId(); | |||
String getToken(); | |||
String getAesKey(); | |||
long getExpiresTime(); | |||
String getOauth2redirectUri(); | |||
String getHttpProxyHost(); | |||
int getHttpProxyPort(); | |||
String getHttpProxyUsername(); | |||
String getHttpProxyPassword(); | |||
File getTmpDirFile(); | |||
/** | |||
* http client builder | |||
* | |||
* @return ApacheHttpClientBuilder | |||
*/ | |||
ApacheHttpClientBuilder getApacheHttpClientBuilder(); | |||
} |
@@ -1,224 +1,224 @@ | |||
package me.chanjar.weixin.cp.api; | |||
import me.chanjar.weixin.common.bean.WxAccessToken; | |||
import me.chanjar.weixin.common.util.ToStringUtils; | |||
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; | |||
import java.io.File; | |||
/** | |||
* 基于内存的微信配置provider,在实际生产环境中应该将这些配置持久化 | |||
* | |||
* @author Daniel Qian | |||
*/ | |||
public class WxCpInMemoryConfigStorage implements WxCpConfigStorage { | |||
protected volatile String corpId; | |||
protected volatile String corpSecret; | |||
protected volatile String token; | |||
protected volatile String accessToken; | |||
protected volatile String aesKey; | |||
protected volatile Integer agentId; | |||
protected volatile long expiresTime; | |||
protected volatile String oauth2redirectUri; | |||
protected volatile String httpProxyHost; | |||
protected volatile int httpProxyPort; | |||
protected volatile String httpProxyUsername; | |||
protected volatile String httpProxyPassword; | |||
protected volatile String jsapiTicket; | |||
protected volatile long jsapiTicketExpiresTime; | |||
protected volatile File tmpDirFile; | |||
private volatile ApacheHttpClientBuilder apacheHttpClientBuilder; | |||
@Override | |||
public String getAccessToken() { | |||
return this.accessToken; | |||
} | |||
public void setAccessToken(String accessToken) { | |||
this.accessToken = accessToken; | |||
} | |||
@Override | |||
public boolean isAccessTokenExpired() { | |||
return System.currentTimeMillis() > this.expiresTime; | |||
} | |||
@Override | |||
public void expireAccessToken() { | |||
this.expiresTime = 0; | |||
} | |||
@Override | |||
public synchronized void updateAccessToken(WxAccessToken accessToken) { | |||
updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); | |||
} | |||
@Override | |||
public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) { | |||
this.accessToken = accessToken; | |||
this.expiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000l; | |||
} | |||
@Override | |||
public String getJsapiTicket() { | |||
return this.jsapiTicket; | |||
} | |||
public void setJsapiTicket(String jsapiTicket) { | |||
this.jsapiTicket = jsapiTicket; | |||
} | |||
public long getJsapiTicketExpiresTime() { | |||
return this.jsapiTicketExpiresTime; | |||
} | |||
public void setJsapiTicketExpiresTime(long jsapiTicketExpiresTime) { | |||
this.jsapiTicketExpiresTime = jsapiTicketExpiresTime; | |||
} | |||
@Override | |||
public boolean isJsapiTicketExpired() { | |||
return System.currentTimeMillis() > this.jsapiTicketExpiresTime; | |||
} | |||
@Override | |||
public synchronized void updateJsapiTicket(String jsapiTicket, int expiresInSeconds) { | |||
this.jsapiTicket = jsapiTicket; | |||
// 预留200秒的时间 | |||
this.jsapiTicketExpiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000l; | |||
} | |||
@Override | |||
public void expireJsapiTicket() { | |||
this.jsapiTicketExpiresTime = 0; | |||
} | |||
@Override | |||
public String getCorpId() { | |||
return this.corpId; | |||
} | |||
public void setCorpId(String corpId) { | |||
this.corpId = corpId; | |||
} | |||
@Override | |||
public String getCorpSecret() { | |||
return this.corpSecret; | |||
} | |||
public void setCorpSecret(String corpSecret) { | |||
this.corpSecret = corpSecret; | |||
} | |||
@Override | |||
public String getToken() { | |||
return this.token; | |||
} | |||
public void setToken(String token) { | |||
this.token = token; | |||
} | |||
@Override | |||
public long getExpiresTime() { | |||
return this.expiresTime; | |||
} | |||
public void setExpiresTime(long expiresTime) { | |||
this.expiresTime = expiresTime; | |||
} | |||
@Override | |||
public String getAesKey() { | |||
return this.aesKey; | |||
} | |||
public void setAesKey(String aesKey) { | |||
this.aesKey = aesKey; | |||
} | |||
@Override | |||
public Integer getAgentId() { | |||
return this.agentId; | |||
} | |||
public void setAgentId(Integer agentId) { | |||
this.agentId = agentId; | |||
} | |||
@Override | |||
public String getOauth2redirectUri() { | |||
return this.oauth2redirectUri; | |||
} | |||
public void setOauth2redirectUri(String oauth2redirectUri) { | |||
this.oauth2redirectUri = oauth2redirectUri; | |||
} | |||
@Override | |||
public String getHttpProxyHost() { | |||
return this.httpProxyHost; | |||
} | |||
public void setHttpProxyHost(String httpProxyHost) { | |||
this.httpProxyHost = httpProxyHost; | |||
} | |||
@Override | |||
public int getHttpProxyPort() { | |||
return this.httpProxyPort; | |||
} | |||
public void setHttpProxyPort(int httpProxyPort) { | |||
this.httpProxyPort = httpProxyPort; | |||
} | |||
@Override | |||
public String getHttpProxyUsername() { | |||
return this.httpProxyUsername; | |||
} | |||
public void setHttpProxyUsername(String httpProxyUsername) { | |||
this.httpProxyUsername = httpProxyUsername; | |||
} | |||
@Override | |||
public String getHttpProxyPassword() { | |||
return this.httpProxyPassword; | |||
} | |||
public void setHttpProxyPassword(String httpProxyPassword) { | |||
this.httpProxyPassword = httpProxyPassword; | |||
} | |||
@Override | |||
public String toString() { | |||
return ToStringUtils.toSimpleString(this); | |||
} | |||
@Override | |||
public File getTmpDirFile() { | |||
return this.tmpDirFile; | |||
} | |||
public void setTmpDirFile(File tmpDirFile) { | |||
this.tmpDirFile = tmpDirFile; | |||
} | |||
@Override | |||
public ApacheHttpClientBuilder getApacheHttpClientBuilder() { | |||
return this.apacheHttpClientBuilder; | |||
} | |||
public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) { | |||
this.apacheHttpClientBuilder = apacheHttpClientBuilder; | |||
} | |||
} | |||
package me.chanjar.weixin.cp.api; | |||
import me.chanjar.weixin.common.bean.WxAccessToken; | |||
import me.chanjar.weixin.common.util.ToStringUtils; | |||
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; | |||
import java.io.File; | |||
/** | |||
* 基于内存的微信配置provider,在实际生产环境中应该将这些配置持久化 | |||
* | |||
* @author Daniel Qian | |||
*/ | |||
public class WxCpInMemoryConfigStorage implements WxCpConfigStorage { | |||
protected volatile String corpId; | |||
protected volatile String corpSecret; | |||
protected volatile String token; | |||
protected volatile String accessToken; | |||
protected volatile String aesKey; | |||
protected volatile Integer agentId; | |||
protected volatile long expiresTime; | |||
protected volatile String oauth2redirectUri; | |||
protected volatile String httpProxyHost; | |||
protected volatile int httpProxyPort; | |||
protected volatile String httpProxyUsername; | |||
protected volatile String httpProxyPassword; | |||
protected volatile String jsapiTicket; | |||
protected volatile long jsapiTicketExpiresTime; | |||
protected volatile File tmpDirFile; | |||
private volatile ApacheHttpClientBuilder apacheHttpClientBuilder; | |||
@Override | |||
public String getAccessToken() { | |||
return this.accessToken; | |||
} | |||
public void setAccessToken(String accessToken) { | |||
this.accessToken = accessToken; | |||
} | |||
@Override | |||
public boolean isAccessTokenExpired() { | |||
return System.currentTimeMillis() > this.expiresTime; | |||
} | |||
@Override | |||
public void expireAccessToken() { | |||
this.expiresTime = 0; | |||
} | |||
@Override | |||
public synchronized void updateAccessToken(WxAccessToken accessToken) { | |||
updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); | |||
} | |||
@Override | |||
public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) { | |||
this.accessToken = accessToken; | |||
this.expiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000l; | |||
} | |||
@Override | |||
public String getJsapiTicket() { | |||
return this.jsapiTicket; | |||
} | |||
public void setJsapiTicket(String jsapiTicket) { | |||
this.jsapiTicket = jsapiTicket; | |||
} | |||
public long getJsapiTicketExpiresTime() { | |||
return this.jsapiTicketExpiresTime; | |||
} | |||
public void setJsapiTicketExpiresTime(long jsapiTicketExpiresTime) { | |||
this.jsapiTicketExpiresTime = jsapiTicketExpiresTime; | |||
} | |||
@Override | |||
public boolean isJsapiTicketExpired() { | |||
return System.currentTimeMillis() > this.jsapiTicketExpiresTime; | |||
} | |||
@Override | |||
public synchronized void updateJsapiTicket(String jsapiTicket, int expiresInSeconds) { | |||
this.jsapiTicket = jsapiTicket; | |||
// 预留200秒的时间 | |||
this.jsapiTicketExpiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000l; | |||
} | |||
@Override | |||
public void expireJsapiTicket() { | |||
this.jsapiTicketExpiresTime = 0; | |||
} | |||
@Override | |||
public String getCorpId() { | |||
return this.corpId; | |||
} | |||
public void setCorpId(String corpId) { | |||
this.corpId = corpId; | |||
} | |||
@Override | |||
public String getCorpSecret() { | |||
return this.corpSecret; | |||
} | |||
public void setCorpSecret(String corpSecret) { | |||
this.corpSecret = corpSecret; | |||
} | |||
@Override | |||
public String getToken() { | |||
return this.token; | |||
} | |||
public void setToken(String token) { | |||
this.token = token; | |||
} | |||
@Override | |||
public long getExpiresTime() { | |||
return this.expiresTime; | |||
} | |||
public void setExpiresTime(long expiresTime) { | |||
this.expiresTime = expiresTime; | |||
} | |||
@Override | |||
public String getAesKey() { | |||
return this.aesKey; | |||
} | |||
public void setAesKey(String aesKey) { | |||
this.aesKey = aesKey; | |||
} | |||
@Override | |||
public Integer getAgentId() { | |||
return this.agentId; | |||
} | |||
public void setAgentId(Integer agentId) { | |||
this.agentId = agentId; | |||
} | |||
@Override | |||
public String getOauth2redirectUri() { | |||
return this.oauth2redirectUri; | |||
} | |||
public void setOauth2redirectUri(String oauth2redirectUri) { | |||
this.oauth2redirectUri = oauth2redirectUri; | |||
} | |||
@Override | |||
public String getHttpProxyHost() { | |||
return this.httpProxyHost; | |||
} | |||
public void setHttpProxyHost(String httpProxyHost) { | |||
this.httpProxyHost = httpProxyHost; | |||
} | |||
@Override | |||
public int getHttpProxyPort() { | |||
return this.httpProxyPort; | |||
} | |||
public void setHttpProxyPort(int httpProxyPort) { | |||
this.httpProxyPort = httpProxyPort; | |||
} | |||
@Override | |||
public String getHttpProxyUsername() { | |||
return this.httpProxyUsername; | |||
} | |||
public void setHttpProxyUsername(String httpProxyUsername) { | |||
this.httpProxyUsername = httpProxyUsername; | |||
} | |||
@Override | |||
public String getHttpProxyPassword() { | |||
return this.httpProxyPassword; | |||
} | |||
public void setHttpProxyPassword(String httpProxyPassword) { | |||
this.httpProxyPassword = httpProxyPassword; | |||
} | |||
@Override | |||
public String toString() { | |||
return ToStringUtils.toSimpleString(this); | |||
} | |||
@Override | |||
public File getTmpDirFile() { | |||
return this.tmpDirFile; | |||
} | |||
public void setTmpDirFile(File tmpDirFile) { | |||
this.tmpDirFile = tmpDirFile; | |||
} | |||
@Override | |||
public ApacheHttpClientBuilder getApacheHttpClientBuilder() { | |||
return this.apacheHttpClientBuilder; | |||
} | |||
public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) { | |||
this.apacheHttpClientBuilder = apacheHttpClientBuilder; | |||
} | |||
} |
@@ -1,269 +1,269 @@ | |||
package me.chanjar.weixin.cp.api; | |||
import me.chanjar.weixin.common.bean.WxAccessToken; | |||
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; | |||
import redis.clients.jedis.Jedis; | |||
import redis.clients.jedis.JedisPool; | |||
import redis.clients.jedis.JedisPoolConfig; | |||
import java.io.File; | |||
/** | |||
* Jedis client implementor for wechat config storage | |||
* | |||
* @author gaigeshen | |||
*/ | |||
public class WxCpJedisConfigStorage implements WxCpConfigStorage { | |||
/* Redis keys here */ | |||
private static final String ACCESS_TOKEN_KEY = "WX_CP_ACCESS_TOKEN"; | |||
private static final String ACCESS_TOKEN_EXPIRES_TIME_KEY = "WX_CP_ACCESS_TOKEN_EXPIRES_TIME"; | |||
private static final String JS_API_TICKET_KEY = "WX_CP_JS_API_TICKET"; | |||
private static final String JS_API_TICKET_EXPIRES_TIME_KEY = "WX_CP_JS_API_TICKET_EXPIRES_TIME"; | |||
/* Redis clients pool */ | |||
private final JedisPool jedisPool; | |||
private volatile String corpId; | |||
private volatile String corpSecret; | |||
private volatile String token; | |||
private volatile String aesKey; | |||
private volatile Integer agentId; | |||
private volatile String oauth2redirectUri; | |||
private volatile String httpProxyHost; | |||
private volatile int httpProxyPort; | |||
private volatile String httpProxyUsername; | |||
private volatile String httpProxyPassword; | |||
private volatile File tmpDirFile; | |||
private volatile ApacheHttpClientBuilder apacheHttpClientBuilder; | |||
public WxCpJedisConfigStorage(String host, int port) { | |||
this.jedisPool = new JedisPool(host, port); | |||
} | |||
public WxCpJedisConfigStorage(JedisPoolConfig poolConfig, String host, int port) { | |||
this.jedisPool = new JedisPool(poolConfig, host, port); | |||
} | |||
public WxCpJedisConfigStorage(JedisPoolConfig poolConfig, String host, int port, int timeout, final String password) { | |||
this.jedisPool = new JedisPool(poolConfig, host, port, timeout, password); | |||
} | |||
/** | |||
* This method will be destroy jedis pool | |||
*/ | |||
public void destroy() { | |||
this.jedisPool.destroy(); | |||
} | |||
@Override | |||
public String getAccessToken() { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
return jedis.get(ACCESS_TOKEN_KEY); | |||
} | |||
} | |||
@Override | |||
public boolean isAccessTokenExpired() { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
String expiresTimeStr = jedis.get(ACCESS_TOKEN_EXPIRES_TIME_KEY); | |||
if (expiresTimeStr != null) { | |||
Long expiresTime = Long.parseLong(expiresTimeStr); | |||
return System.currentTimeMillis() > expiresTime; | |||
} | |||
return true; | |||
} | |||
} | |||
@Override | |||
public void expireAccessToken() { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
jedis.set(ACCESS_TOKEN_EXPIRES_TIME_KEY, "0"); | |||
} | |||
} | |||
@Override | |||
public synchronized void updateAccessToken(WxAccessToken accessToken) { | |||
this.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); | |||
} | |||
@Override | |||
public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
jedis.set(ACCESS_TOKEN_KEY, accessToken); | |||
jedis.set(ACCESS_TOKEN_EXPIRES_TIME_KEY, | |||
(System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L) + ""); | |||
} | |||
} | |||
@Override | |||
public String getJsapiTicket() { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
return jedis.get(JS_API_TICKET_KEY); | |||
} | |||
} | |||
@Override | |||
public boolean isJsapiTicketExpired() { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
String expiresTimeStr = jedis.get(JS_API_TICKET_EXPIRES_TIME_KEY); | |||
if (expiresTimeStr != null) { | |||
Long expiresTime = Long.parseLong(expiresTimeStr); | |||
return System.currentTimeMillis() > expiresTime; | |||
} | |||
return true; | |||
} | |||
} | |||
@Override | |||
public void expireJsapiTicket() { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
jedis.set(JS_API_TICKET_EXPIRES_TIME_KEY, "0"); | |||
} | |||
} | |||
@Override | |||
public synchronized void updateJsapiTicket(String jsapiTicket, int expiresInSeconds) { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
jedis.set(JS_API_TICKET_KEY, jsapiTicket); | |||
jedis.set(JS_API_TICKET_EXPIRES_TIME_KEY, | |||
(System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L + "")); | |||
} | |||
} | |||
@Override | |||
public String getCorpId() { | |||
return this.corpId; | |||
} | |||
public void setCorpId(String corpId) { | |||
this.corpId = corpId; | |||
} | |||
@Override | |||
public String getCorpSecret() { | |||
return this.corpSecret; | |||
} | |||
public void setCorpSecret(String corpSecret) { | |||
this.corpSecret = corpSecret; | |||
} | |||
@Override | |||
public Integer getAgentId() { | |||
return this.agentId; | |||
} | |||
public void setAgentId(Integer agentId) { | |||
this.agentId = agentId; | |||
} | |||
@Override | |||
public String getToken() { | |||
return this.token; | |||
} | |||
public void setToken(String token) { | |||
this.token = token; | |||
} | |||
@Override | |||
public String getAesKey() { | |||
return this.aesKey; | |||
} | |||
public void setAesKey(String aesKey) { | |||
this.aesKey = aesKey; | |||
} | |||
@Override | |||
public long getExpiresTime() { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
String expiresTimeStr = jedis.get(ACCESS_TOKEN_EXPIRES_TIME_KEY); | |||
if (expiresTimeStr != null) { | |||
Long expiresTime = Long.parseLong(expiresTimeStr); | |||
return expiresTime; | |||
} | |||
return 0L; | |||
} | |||
} | |||
@Override | |||
public String getOauth2redirectUri() { | |||
return this.oauth2redirectUri; | |||
} | |||
public void setOauth2redirectUri(String oauth2redirectUri) { | |||
this.oauth2redirectUri = oauth2redirectUri; | |||
} | |||
@Override | |||
public String getHttpProxyHost() { | |||
return this.httpProxyHost; | |||
} | |||
public void setHttpProxyHost(String httpProxyHost) { | |||
this.httpProxyHost = httpProxyHost; | |||
} | |||
@Override | |||
public int getHttpProxyPort() { | |||
return this.httpProxyPort; | |||
} | |||
public void setHttpProxyPort(int httpProxyPort) { | |||
this.httpProxyPort = httpProxyPort; | |||
} | |||
@Override | |||
public String getHttpProxyUsername() { | |||
return this.httpProxyUsername; | |||
} | |||
// ============================ Setters below | |||
public void setHttpProxyUsername(String httpProxyUsername) { | |||
this.httpProxyUsername = httpProxyUsername; | |||
} | |||
@Override | |||
public String getHttpProxyPassword() { | |||
return this.httpProxyPassword; | |||
} | |||
public void setHttpProxyPassword(String httpProxyPassword) { | |||
this.httpProxyPassword = httpProxyPassword; | |||
} | |||
@Override | |||
public File getTmpDirFile() { | |||
return this.tmpDirFile; | |||
} | |||
public void setTmpDirFile(File tmpDirFile) { | |||
this.tmpDirFile = tmpDirFile; | |||
} | |||
@Override | |||
public ApacheHttpClientBuilder getApacheHttpClientBuilder() { | |||
return this.apacheHttpClientBuilder; | |||
} | |||
public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) { | |||
this.apacheHttpClientBuilder = apacheHttpClientBuilder; | |||
} | |||
} | |||
package me.chanjar.weixin.cp.api; | |||
import me.chanjar.weixin.common.bean.WxAccessToken; | |||
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; | |||
import redis.clients.jedis.Jedis; | |||
import redis.clients.jedis.JedisPool; | |||
import redis.clients.jedis.JedisPoolConfig; | |||
import java.io.File; | |||
/** | |||
* Jedis client implementor for wechat config storage | |||
* | |||
* @author gaigeshen | |||
*/ | |||
public class WxCpJedisConfigStorage implements WxCpConfigStorage { | |||
/* Redis keys here */ | |||
private static final String ACCESS_TOKEN_KEY = "WX_CP_ACCESS_TOKEN"; | |||
private static final String ACCESS_TOKEN_EXPIRES_TIME_KEY = "WX_CP_ACCESS_TOKEN_EXPIRES_TIME"; | |||
private static final String JS_API_TICKET_KEY = "WX_CP_JS_API_TICKET"; | |||
private static final String JS_API_TICKET_EXPIRES_TIME_KEY = "WX_CP_JS_API_TICKET_EXPIRES_TIME"; | |||
/* Redis clients pool */ | |||
private final JedisPool jedisPool; | |||
private volatile String corpId; | |||
private volatile String corpSecret; | |||
private volatile String token; | |||
private volatile String aesKey; | |||
private volatile Integer agentId; | |||
private volatile String oauth2redirectUri; | |||
private volatile String httpProxyHost; | |||
private volatile int httpProxyPort; | |||
private volatile String httpProxyUsername; | |||
private volatile String httpProxyPassword; | |||
private volatile File tmpDirFile; | |||
private volatile ApacheHttpClientBuilder apacheHttpClientBuilder; | |||
public WxCpJedisConfigStorage(String host, int port) { | |||
this.jedisPool = new JedisPool(host, port); | |||
} | |||
public WxCpJedisConfigStorage(JedisPoolConfig poolConfig, String host, int port) { | |||
this.jedisPool = new JedisPool(poolConfig, host, port); | |||
} | |||
public WxCpJedisConfigStorage(JedisPoolConfig poolConfig, String host, int port, int timeout, final String password) { | |||
this.jedisPool = new JedisPool(poolConfig, host, port, timeout, password); | |||
} | |||
/** | |||
* This method will be destroy jedis pool | |||
*/ | |||
public void destroy() { | |||
this.jedisPool.destroy(); | |||
} | |||
@Override | |||
public String getAccessToken() { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
return jedis.get(ACCESS_TOKEN_KEY); | |||
} | |||
} | |||
@Override | |||
public boolean isAccessTokenExpired() { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
String expiresTimeStr = jedis.get(ACCESS_TOKEN_EXPIRES_TIME_KEY); | |||
if (expiresTimeStr != null) { | |||
Long expiresTime = Long.parseLong(expiresTimeStr); | |||
return System.currentTimeMillis() > expiresTime; | |||
} | |||
return true; | |||
} | |||
} | |||
@Override | |||
public void expireAccessToken() { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
jedis.set(ACCESS_TOKEN_EXPIRES_TIME_KEY, "0"); | |||
} | |||
} | |||
@Override | |||
public synchronized void updateAccessToken(WxAccessToken accessToken) { | |||
this.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); | |||
} | |||
@Override | |||
public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
jedis.set(ACCESS_TOKEN_KEY, accessToken); | |||
jedis.set(ACCESS_TOKEN_EXPIRES_TIME_KEY, | |||
(System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L) + ""); | |||
} | |||
} | |||
@Override | |||
public String getJsapiTicket() { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
return jedis.get(JS_API_TICKET_KEY); | |||
} | |||
} | |||
@Override | |||
public boolean isJsapiTicketExpired() { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
String expiresTimeStr = jedis.get(JS_API_TICKET_EXPIRES_TIME_KEY); | |||
if (expiresTimeStr != null) { | |||
Long expiresTime = Long.parseLong(expiresTimeStr); | |||
return System.currentTimeMillis() > expiresTime; | |||
} | |||
return true; | |||
} | |||
} | |||
@Override | |||
public void expireJsapiTicket() { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
jedis.set(JS_API_TICKET_EXPIRES_TIME_KEY, "0"); | |||
} | |||
} | |||
@Override | |||
public synchronized void updateJsapiTicket(String jsapiTicket, int expiresInSeconds) { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
jedis.set(JS_API_TICKET_KEY, jsapiTicket); | |||
jedis.set(JS_API_TICKET_EXPIRES_TIME_KEY, | |||
(System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L + "")); | |||
} | |||
} | |||
@Override | |||
public String getCorpId() { | |||
return this.corpId; | |||
} | |||
public void setCorpId(String corpId) { | |||
this.corpId = corpId; | |||
} | |||
@Override | |||
public String getCorpSecret() { | |||
return this.corpSecret; | |||
} | |||
public void setCorpSecret(String corpSecret) { | |||
this.corpSecret = corpSecret; | |||
} | |||
@Override | |||
public Integer getAgentId() { | |||
return this.agentId; | |||
} | |||
public void setAgentId(Integer agentId) { | |||
this.agentId = agentId; | |||
} | |||
@Override | |||
public String getToken() { | |||
return this.token; | |||
} | |||
public void setToken(String token) { | |||
this.token = token; | |||
} | |||
@Override | |||
public String getAesKey() { | |||
return this.aesKey; | |||
} | |||
public void setAesKey(String aesKey) { | |||
this.aesKey = aesKey; | |||
} | |||
@Override | |||
public long getExpiresTime() { | |||
try (Jedis jedis = this.jedisPool.getResource()) { | |||
String expiresTimeStr = jedis.get(ACCESS_TOKEN_EXPIRES_TIME_KEY); | |||
if (expiresTimeStr != null) { | |||
Long expiresTime = Long.parseLong(expiresTimeStr); | |||
return expiresTime; | |||
} | |||
return 0L; | |||
} | |||
} | |||
@Override | |||
public String getOauth2redirectUri() { | |||
return this.oauth2redirectUri; | |||
} | |||
public void setOauth2redirectUri(String oauth2redirectUri) { | |||
this.oauth2redirectUri = oauth2redirectUri; | |||
} | |||
@Override | |||
public String getHttpProxyHost() { | |||
return this.httpProxyHost; | |||
} | |||
public void setHttpProxyHost(String httpProxyHost) { | |||
this.httpProxyHost = httpProxyHost; | |||
} | |||
@Override | |||
public int getHttpProxyPort() { | |||
return this.httpProxyPort; | |||
} | |||
public void setHttpProxyPort(int httpProxyPort) { | |||
this.httpProxyPort = httpProxyPort; | |||
} | |||
@Override | |||
public String getHttpProxyUsername() { | |||
return this.httpProxyUsername; | |||
} | |||
// ============================ Setters below | |||
public void setHttpProxyUsername(String httpProxyUsername) { | |||
this.httpProxyUsername = httpProxyUsername; | |||
} | |||
@Override | |||
public String getHttpProxyPassword() { | |||
return this.httpProxyPassword; | |||
} | |||
public void setHttpProxyPassword(String httpProxyPassword) { | |||
this.httpProxyPassword = httpProxyPassword; | |||
} | |||
@Override | |||
public File getTmpDirFile() { | |||
return this.tmpDirFile; | |||
} | |||
public void setTmpDirFile(File tmpDirFile) { | |||
this.tmpDirFile = tmpDirFile; | |||
} | |||
@Override | |||
public ApacheHttpClientBuilder getApacheHttpClientBuilder() { | |||
return this.apacheHttpClientBuilder; | |||
} | |||
public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) { | |||
this.apacheHttpClientBuilder = apacheHttpClientBuilder; | |||
} | |||
} |
@@ -1,29 +1,29 @@ | |||
package me.chanjar.weixin.cp.api; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import me.chanjar.weixin.common.session.WxSessionManager; | |||
import me.chanjar.weixin.cp.bean.WxCpXmlMessage; | |||
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage; | |||
import java.util.Map; | |||
/** | |||
* 处理微信推送消息的处理器接口 | |||
* | |||
* @author Daniel Qian | |||
*/ | |||
public interface WxCpMessageHandler { | |||
/** | |||
* @param wxMessage | |||
* @param context 上下文,如果handler或interceptor之间有信息要传递,可以用这个 | |||
* @param wxCpService | |||
* @param sessionManager | |||
* @return xml格式的消息,如果在异步规则里处理的话,可以返回null | |||
*/ | |||
WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, | |||
Map<String, Object> context, | |||
WxCpService wxCpService, | |||
WxSessionManager sessionManager) throws WxErrorException; | |||
} | |||
package me.chanjar.weixin.cp.api; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import me.chanjar.weixin.common.session.WxSessionManager; | |||
import me.chanjar.weixin.cp.bean.WxCpXmlMessage; | |||
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage; | |||
import java.util.Map; | |||
/** | |||
* 处理微信推送消息的处理器接口 | |||
* | |||
* @author Daniel Qian | |||
*/ | |||
public interface WxCpMessageHandler { | |||
/** | |||
* @param wxMessage | |||
* @param context 上下文,如果handler或interceptor之间有信息要传递,可以用这个 | |||
* @param wxCpService | |||
* @param sessionManager | |||
* @return xml格式的消息,如果在异步规则里处理的话,可以返回null | |||
*/ | |||
WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, | |||
Map<String, Object> context, | |||
WxCpService wxCpService, | |||
WxSessionManager sessionManager) throws WxErrorException; | |||
} |
@@ -1,30 +1,30 @@ | |||
package me.chanjar.weixin.cp.api; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import me.chanjar.weixin.common.session.WxSessionManager; | |||
import me.chanjar.weixin.cp.bean.WxCpXmlMessage; | |||
import java.util.Map; | |||
/** | |||
* 微信消息拦截器,可以用来做验证 | |||
* | |||
* @author Daniel Qian | |||
*/ | |||
public interface WxCpMessageInterceptor { | |||
/** | |||
* 拦截微信消息 | |||
* | |||
* @param wxMessage | |||
* @param context 上下文,如果handler或interceptor之间有信息要传递,可以用这个 | |||
* @param wxCpService | |||
* @param sessionManager | |||
* @return true代表OK,false代表不OK | |||
*/ | |||
boolean intercept(WxCpXmlMessage wxMessage, | |||
Map<String, Object> context, | |||
WxCpService wxCpService, | |||
WxSessionManager sessionManager) throws WxErrorException; | |||
} | |||
package me.chanjar.weixin.cp.api; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import me.chanjar.weixin.common.session.WxSessionManager; | |||
import me.chanjar.weixin.cp.bean.WxCpXmlMessage; | |||
import java.util.Map; | |||
/** | |||
* 微信消息拦截器,可以用来做验证 | |||
* | |||
* @author Daniel Qian | |||
*/ | |||
public interface WxCpMessageInterceptor { | |||
/** | |||
* 拦截微信消息 | |||
* | |||
* @param wxMessage | |||
* @param context 上下文,如果handler或interceptor之间有信息要传递,可以用这个 | |||
* @param wxCpService | |||
* @param sessionManager | |||
* @return true代表OK,false代表不OK | |||
*/ | |||
boolean intercept(WxCpXmlMessage wxMessage, | |||
Map<String, Object> context, | |||
WxCpService wxCpService, | |||
WxSessionManager sessionManager) throws WxErrorException; | |||
} |
@@ -1,15 +1,15 @@ | |||
package me.chanjar.weixin.cp.api; | |||
import me.chanjar.weixin.cp.bean.WxCpXmlMessage; | |||
/** | |||
* 消息匹配器,用在消息路由的时候 | |||
*/ | |||
public interface WxCpMessageMatcher { | |||
/** | |||
* 消息是否匹配某种模式 | |||
*/ | |||
boolean match(WxCpXmlMessage message); | |||
} | |||
package me.chanjar.weixin.cp.api; | |||
import me.chanjar.weixin.cp.bean.WxCpXmlMessage; | |||
/** | |||
* 消息匹配器,用在消息路由的时候 | |||
*/ | |||
public interface WxCpMessageMatcher { | |||
/** | |||
* 消息是否匹配某种模式 | |||
*/ | |||
boolean match(WxCpXmlMessage message); | |||
} |
@@ -1,249 +1,249 @@ | |||
package me.chanjar.weixin.cp.api; | |||
import me.chanjar.weixin.common.api.WxErrorExceptionHandler; | |||
import me.chanjar.weixin.common.api.WxMessageDuplicateChecker; | |||
import me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker; | |||
import me.chanjar.weixin.common.session.InternalSession; | |||
import me.chanjar.weixin.common.session.InternalSessionManager; | |||
import me.chanjar.weixin.common.session.StandardSessionManager; | |||
import me.chanjar.weixin.common.session.WxSessionManager; | |||
import me.chanjar.weixin.common.util.LogExceptionHandler; | |||
import me.chanjar.weixin.cp.bean.WxCpXmlMessage; | |||
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
import java.util.ArrayList; | |||
import java.util.HashMap; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.concurrent.ExecutionException; | |||
import java.util.concurrent.ExecutorService; | |||
import java.util.concurrent.Executors; | |||
import java.util.concurrent.Future; | |||
/** | |||
* <pre> | |||
* 微信消息路由器,通过代码化的配置,把来自微信的消息交给handler处理 | |||
* | |||
* 说明: | |||
* 1. 配置路由规则时要按照从细到粗的原则,否则可能消息可能会被提前处理 | |||
* 2. 默认情况下消息只会被处理一次,除非使用 {@link WxCpMessageRouterRule#next()} | |||
* 3. 规则的结束必须用{@link WxCpMessageRouterRule#end()}或者{@link WxCpMessageRouterRule#next()},否则不会生效 | |||
* | |||
* 使用方法: | |||
* WxCpMessageRouter router = new WxCpMessageRouter(); | |||
* router | |||
* .rule() | |||
* .msgType("MSG_TYPE").event("EVENT").eventKey("EVENT_KEY").content("CONTENT") | |||
* .interceptor(interceptor, ...).handler(handler, ...) | |||
* .end() | |||
* .rule() | |||
* // 另外一个匹配规则 | |||
* .end() | |||
* ; | |||
* | |||
* // 将WxXmlMessage交给消息路由器 | |||
* router.route(message); | |||
* | |||
* </pre> | |||
* | |||
* @author Daniel Qian | |||
*/ | |||
public class WxCpMessageRouter { | |||
private static final int DEFAULT_THREAD_POOL_SIZE = 100; | |||
protected final Logger log = LoggerFactory.getLogger(WxCpMessageRouter.class); | |||
private final List<WxCpMessageRouterRule> rules = new ArrayList<>(); | |||
private final WxCpService wxCpService; | |||
private ExecutorService executorService; | |||
private WxMessageDuplicateChecker messageDuplicateChecker; | |||
private WxSessionManager sessionManager; | |||
private WxErrorExceptionHandler exceptionHandler; | |||
public WxCpMessageRouter(WxCpService wxCpService) { | |||
this.wxCpService = wxCpService; | |||
this.executorService = Executors.newFixedThreadPool(DEFAULT_THREAD_POOL_SIZE); | |||
this.messageDuplicateChecker = new WxMessageInMemoryDuplicateChecker(); | |||
this.sessionManager = new StandardSessionManager(); | |||
this.exceptionHandler = new LogExceptionHandler(); | |||
} | |||
/** | |||
* <pre> | |||
* 设置自定义的 {@link ExecutorService} | |||
* 如果不调用该方法,默认使用 Executors.newFixedThreadPool(100) | |||
* </pre> | |||
* | |||
* @param executorService | |||
*/ | |||
public void setExecutorService(ExecutorService executorService) { | |||
this.executorService = executorService; | |||
} | |||
/** | |||
* <pre> | |||
* 设置自定义的 {@link me.chanjar.weixin.common.api.WxMessageDuplicateChecker} | |||
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker} | |||
* </pre> | |||
* | |||
* @param messageDuplicateChecker | |||
*/ | |||
public void setMessageDuplicateChecker(WxMessageDuplicateChecker messageDuplicateChecker) { | |||
this.messageDuplicateChecker = messageDuplicateChecker; | |||
} | |||
/** | |||
* <pre> | |||
* 设置自定义的{@link me.chanjar.weixin.common.session.WxSessionManager} | |||
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.session.StandardSessionManager} | |||
* </pre> | |||
* | |||
* @param sessionManager | |||
*/ | |||
public void setSessionManager(WxSessionManager sessionManager) { | |||
this.sessionManager = sessionManager; | |||
} | |||
/** | |||
* <pre> | |||
* 设置自定义的{@link me.chanjar.weixin.common.api.WxErrorExceptionHandler} | |||
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.util.LogExceptionHandler} | |||
* </pre> | |||
* | |||
* @param exceptionHandler | |||
*/ | |||
public void setExceptionHandler(WxErrorExceptionHandler exceptionHandler) { | |||
this.exceptionHandler = exceptionHandler; | |||
} | |||
List<WxCpMessageRouterRule> getRules() { | |||
return this.rules; | |||
} | |||
/** | |||
* 开始一个新的Route规则 | |||
*/ | |||
public WxCpMessageRouterRule rule() { | |||
return new WxCpMessageRouterRule(this); | |||
} | |||
/** | |||
* 处理微信消息 | |||
* | |||
* @param wxMessage | |||
* @param context | |||
*/ | |||
public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage, final Map<String, Object> context) { | |||
if (isDuplicateMessage(wxMessage)) { | |||
// 如果是重复消息,那么就不做处理 | |||
return null; | |||
} | |||
final List<WxCpMessageRouterRule> matchRules = new ArrayList<>(); | |||
// 收集匹配的规则 | |||
for (final WxCpMessageRouterRule rule : this.rules) { | |||
if (rule.test(wxMessage)) { | |||
matchRules.add(rule); | |||
if (!rule.isReEnter()) { | |||
break; | |||
} | |||
} | |||
} | |||
if (matchRules.size() == 0) { | |||
return null; | |||
} | |||
WxCpXmlOutMessage res = null; | |||
final List<Future> futures = new ArrayList<>(); | |||
for (final WxCpMessageRouterRule rule : matchRules) { | |||
// 返回最后一个非异步的rule的执行结果 | |||
if (rule.isAsync()) { | |||
futures.add( | |||
this.executorService.submit(new Runnable() { | |||
@Override | |||
public void run() { | |||
rule.service(wxMessage, context, WxCpMessageRouter.this.wxCpService, WxCpMessageRouter.this.sessionManager, WxCpMessageRouter.this.exceptionHandler); | |||
} | |||
}) | |||
); | |||
} else { | |||
res = rule.service(wxMessage, context, this.wxCpService, this.sessionManager, this.exceptionHandler); | |||
// 在同步操作结束,session访问结束 | |||
this.log.debug("End session access: async=false, sessionId={}", wxMessage.getFromUserName()); | |||
sessionEndAccess(wxMessage); | |||
} | |||
} | |||
if (futures.size() > 0) { | |||
this.executorService.submit(new Runnable() { | |||
@Override | |||
public void run() { | |||
for (Future future : futures) { | |||
try { | |||
future.get(); | |||
WxCpMessageRouter.this.log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUserName()); | |||
// 异步操作结束,session访问结束 | |||
sessionEndAccess(wxMessage); | |||
} catch (InterruptedException e) { | |||
WxCpMessageRouter.this.log.error("Error happened when wait task finish", e); | |||
} catch (ExecutionException e) { | |||
WxCpMessageRouter.this.log.error("Error happened when wait task finish", e); | |||
} | |||
} | |||
} | |||
}); | |||
} | |||
return res; | |||
} | |||
/** | |||
* 处理微信消息 | |||
* | |||
* @param wxMessage | |||
*/ | |||
public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage) { | |||
return this.route(wxMessage, new HashMap<String, Object>()); | |||
} | |||
protected boolean isDuplicateMessage(WxCpXmlMessage wxMessage) { | |||
String messageId = ""; | |||
if (wxMessage.getMsgId() == null) { | |||
messageId = String.valueOf(wxMessage.getCreateTime()) | |||
+ "-" + String.valueOf(wxMessage.getAgentId() == null ? "" : wxMessage.getAgentId()) | |||
+ "-" + wxMessage.getFromUserName() | |||
+ "-" + String.valueOf(wxMessage.getEventKey() == null ? "" : wxMessage.getEventKey()) | |||
+ "-" + String.valueOf(wxMessage.getEvent() == null ? "" : wxMessage.getEvent()) | |||
; | |||
} else { | |||
messageId = String.valueOf(wxMessage.getMsgId()); | |||
} | |||
return this.messageDuplicateChecker.isDuplicate(messageId); | |||
} | |||
/** | |||
* 对session的访问结束 | |||
* | |||
* @param wxMessage | |||
*/ | |||
protected void sessionEndAccess(WxCpXmlMessage wxMessage) { | |||
InternalSession session = ((InternalSessionManager) this.sessionManager).findSession(wxMessage.getFromUserName()); | |||
if (session != null) { | |||
session.endAccess(); | |||
} | |||
} | |||
} | |||
package me.chanjar.weixin.cp.api; | |||
import me.chanjar.weixin.common.api.WxErrorExceptionHandler; | |||
import me.chanjar.weixin.common.api.WxMessageDuplicateChecker; | |||
import me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker; | |||
import me.chanjar.weixin.common.session.InternalSession; | |||
import me.chanjar.weixin.common.session.InternalSessionManager; | |||
import me.chanjar.weixin.common.session.StandardSessionManager; | |||
import me.chanjar.weixin.common.session.WxSessionManager; | |||
import me.chanjar.weixin.common.util.LogExceptionHandler; | |||
import me.chanjar.weixin.cp.bean.WxCpXmlMessage; | |||
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
import java.util.ArrayList; | |||
import java.util.HashMap; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.concurrent.ExecutionException; | |||
import java.util.concurrent.ExecutorService; | |||
import java.util.concurrent.Executors; | |||
import java.util.concurrent.Future; | |||
/** | |||
* <pre> | |||
* 微信消息路由器,通过代码化的配置,把来自微信的消息交给handler处理 | |||
* | |||
* 说明: | |||
* 1. 配置路由规则时要按照从细到粗的原则,否则可能消息可能会被提前处理 | |||
* 2. 默认情况下消息只会被处理一次,除非使用 {@link WxCpMessageRouterRule#next()} | |||
* 3. 规则的结束必须用{@link WxCpMessageRouterRule#end()}或者{@link WxCpMessageRouterRule#next()},否则不会生效 | |||
* | |||
* 使用方法: | |||
* WxCpMessageRouter router = new WxCpMessageRouter(); | |||
* router | |||
* .rule() | |||
* .msgType("MSG_TYPE").event("EVENT").eventKey("EVENT_KEY").content("CONTENT") | |||
* .interceptor(interceptor, ...).handler(handler, ...) | |||
* .end() | |||
* .rule() | |||
* // 另外一个匹配规则 | |||
* .end() | |||
* ; | |||
* | |||
* // 将WxXmlMessage交给消息路由器 | |||
* router.route(message); | |||
* | |||
* </pre> | |||
* | |||
* @author Daniel Qian | |||
*/ | |||
public class WxCpMessageRouter { | |||
private static final int DEFAULT_THREAD_POOL_SIZE = 100; | |||
protected final Logger log = LoggerFactory.getLogger(WxCpMessageRouter.class); | |||
private final List<WxCpMessageRouterRule> rules = new ArrayList<>(); | |||
private final WxCpService wxCpService; | |||
private ExecutorService executorService; | |||
private WxMessageDuplicateChecker messageDuplicateChecker; | |||
private WxSessionManager sessionManager; | |||
private WxErrorExceptionHandler exceptionHandler; | |||
public WxCpMessageRouter(WxCpService wxCpService) { | |||
this.wxCpService = wxCpService; | |||
this.executorService = Executors.newFixedThreadPool(DEFAULT_THREAD_POOL_SIZE); | |||
this.messageDuplicateChecker = new WxMessageInMemoryDuplicateChecker(); | |||
this.sessionManager = new StandardSessionManager(); | |||
this.exceptionHandler = new LogExceptionHandler(); | |||
} | |||
/** | |||
* <pre> | |||
* 设置自定义的 {@link ExecutorService} | |||
* 如果不调用该方法,默认使用 Executors.newFixedThreadPool(100) | |||
* </pre> | |||
* | |||
* @param executorService | |||
*/ | |||
public void setExecutorService(ExecutorService executorService) { | |||
this.executorService = executorService; | |||
} | |||
/** | |||
* <pre> | |||
* 设置自定义的 {@link me.chanjar.weixin.common.api.WxMessageDuplicateChecker} | |||
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker} | |||
* </pre> | |||
* | |||
* @param messageDuplicateChecker | |||
*/ | |||
public void setMessageDuplicateChecker(WxMessageDuplicateChecker messageDuplicateChecker) { | |||
this.messageDuplicateChecker = messageDuplicateChecker; | |||
} | |||
/** | |||
* <pre> | |||
* 设置自定义的{@link me.chanjar.weixin.common.session.WxSessionManager} | |||
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.session.StandardSessionManager} | |||
* </pre> | |||
* | |||
* @param sessionManager | |||
*/ | |||
public void setSessionManager(WxSessionManager sessionManager) { | |||
this.sessionManager = sessionManager; | |||
} | |||
/** | |||
* <pre> | |||
* 设置自定义的{@link me.chanjar.weixin.common.api.WxErrorExceptionHandler} | |||
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.util.LogExceptionHandler} | |||
* </pre> | |||
* | |||
* @param exceptionHandler | |||
*/ | |||
public void setExceptionHandler(WxErrorExceptionHandler exceptionHandler) { | |||
this.exceptionHandler = exceptionHandler; | |||
} | |||
List<WxCpMessageRouterRule> getRules() { | |||
return this.rules; | |||
} | |||
/** | |||
* 开始一个新的Route规则 | |||
*/ | |||
public WxCpMessageRouterRule rule() { | |||
return new WxCpMessageRouterRule(this); | |||
} | |||
/** | |||
* 处理微信消息 | |||
* | |||
* @param wxMessage | |||
* @param context | |||
*/ | |||
public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage, final Map<String, Object> context) { | |||
if (isDuplicateMessage(wxMessage)) { | |||
// 如果是重复消息,那么就不做处理 | |||
return null; | |||
} | |||
final List<WxCpMessageRouterRule> matchRules = new ArrayList<>(); | |||
// 收集匹配的规则 | |||
for (final WxCpMessageRouterRule rule : this.rules) { | |||
if (rule.test(wxMessage)) { | |||
matchRules.add(rule); | |||
if (!rule.isReEnter()) { | |||
break; | |||
} | |||
} | |||
} | |||
if (matchRules.size() == 0) { | |||
return null; | |||
} | |||
WxCpXmlOutMessage res = null; | |||
final List<Future> futures = new ArrayList<>(); | |||
for (final WxCpMessageRouterRule rule : matchRules) { | |||
// 返回最后一个非异步的rule的执行结果 | |||
if (rule.isAsync()) { | |||
futures.add( | |||
this.executorService.submit(new Runnable() { | |||
@Override | |||
public void run() { | |||
rule.service(wxMessage, context, WxCpMessageRouter.this.wxCpService, WxCpMessageRouter.this.sessionManager, WxCpMessageRouter.this.exceptionHandler); | |||
} | |||
}) | |||
); | |||
} else { | |||
res = rule.service(wxMessage, context, this.wxCpService, this.sessionManager, this.exceptionHandler); | |||
// 在同步操作结束,session访问结束 | |||
this.log.debug("End session access: async=false, sessionId={}", wxMessage.getFromUserName()); | |||
sessionEndAccess(wxMessage); | |||
} | |||
} | |||
if (futures.size() > 0) { | |||
this.executorService.submit(new Runnable() { | |||
@Override | |||
public void run() { | |||
for (Future future : futures) { | |||
try { | |||
future.get(); | |||
WxCpMessageRouter.this.log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUserName()); | |||
// 异步操作结束,session访问结束 | |||
sessionEndAccess(wxMessage); | |||
} catch (InterruptedException e) { | |||
WxCpMessageRouter.this.log.error("Error happened when wait task finish", e); | |||
} catch (ExecutionException e) { | |||
WxCpMessageRouter.this.log.error("Error happened when wait task finish", e); | |||
} | |||
} | |||
} | |||
}); | |||
} | |||
return res; | |||
} | |||
/** | |||
* 处理微信消息 | |||
* | |||
* @param wxMessage | |||
*/ | |||
public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage) { | |||
return this.route(wxMessage, new HashMap<String, Object>()); | |||
} | |||
protected boolean isDuplicateMessage(WxCpXmlMessage wxMessage) { | |||
String messageId = ""; | |||
if (wxMessage.getMsgId() == null) { | |||
messageId = String.valueOf(wxMessage.getCreateTime()) | |||
+ "-" + String.valueOf(wxMessage.getAgentId() == null ? "" : wxMessage.getAgentId()) | |||
+ "-" + wxMessage.getFromUserName() | |||
+ "-" + String.valueOf(wxMessage.getEventKey() == null ? "" : wxMessage.getEventKey()) | |||
+ "-" + String.valueOf(wxMessage.getEvent() == null ? "" : wxMessage.getEvent()) | |||
; | |||
} else { | |||
messageId = String.valueOf(wxMessage.getMsgId()); | |||
} | |||
return this.messageDuplicateChecker.isDuplicate(messageId); | |||
} | |||
/** | |||
* 对session的访问结束 | |||
* | |||
* @param wxMessage | |||
*/ | |||
protected void sessionEndAccess(WxCpXmlMessage wxMessage) { | |||
InternalSession session = ((InternalSessionManager) this.sessionManager).findSession(wxMessage.getFromUserName()); | |||
if (session != null) { | |||
session.endAccess(); | |||
} | |||
} | |||
} |
@@ -1,321 +1,321 @@ | |||
package me.chanjar.weixin.cp.api; | |||
import me.chanjar.weixin.common.api.WxErrorExceptionHandler; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import me.chanjar.weixin.common.session.WxSessionManager; | |||
import me.chanjar.weixin.cp.bean.WxCpXmlMessage; | |||
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage; | |||
import java.util.ArrayList; | |||
import java.util.HashMap; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.regex.Pattern; | |||
public class WxCpMessageRouterRule { | |||
private final WxCpMessageRouter routerBuilder; | |||
private boolean async = true; | |||
private String fromUser; | |||
private String msgType; | |||
private String event; | |||
private String eventKey; | |||
private String content; | |||
private String rContent; | |||
private WxCpMessageMatcher matcher; | |||
private boolean reEnter = false; | |||
private Integer agentId; | |||
private List<WxCpMessageHandler> handlers = new ArrayList<>(); | |||
private List<WxCpMessageInterceptor> interceptors = new ArrayList<>(); | |||
protected WxCpMessageRouterRule(WxCpMessageRouter routerBuilder) { | |||
this.routerBuilder = routerBuilder; | |||
} | |||
/** | |||
* 设置是否异步执行,默认是true | |||
* | |||
* @param async | |||
*/ | |||
public WxCpMessageRouterRule async(boolean async) { | |||
this.async = async; | |||
return this; | |||
} | |||
/** | |||
* 如果agentId匹配 | |||
* | |||
* @param agentId | |||
*/ | |||
public WxCpMessageRouterRule agentId(Integer agentId) { | |||
this.agentId = agentId; | |||
return this; | |||
} | |||
/** | |||
* 如果msgType等于某值 | |||
* | |||
* @param msgType | |||
*/ | |||
public WxCpMessageRouterRule msgType(String msgType) { | |||
this.msgType = msgType; | |||
return this; | |||
} | |||
/** | |||
* 如果event等于某值 | |||
* | |||
* @param event | |||
*/ | |||
public WxCpMessageRouterRule event(String event) { | |||
this.event = event; | |||
return this; | |||
} | |||
/** | |||
* 如果eventKey等于某值 | |||
* | |||
* @param eventKey | |||
*/ | |||
public WxCpMessageRouterRule eventKey(String eventKey) { | |||
this.eventKey = eventKey; | |||
return this; | |||
} | |||
/** | |||
* 如果content等于某值 | |||
* | |||
* @param content | |||
*/ | |||
public WxCpMessageRouterRule content(String content) { | |||
this.content = content; | |||
return this; | |||
} | |||
/** | |||
* 如果content匹配该正则表达式 | |||
* | |||
* @param regex | |||
*/ | |||
public WxCpMessageRouterRule rContent(String regex) { | |||
this.rContent = regex; | |||
return this; | |||
} | |||
/** | |||
* 如果fromUser等于某值 | |||
* | |||
* @param fromUser | |||
*/ | |||
public WxCpMessageRouterRule fromUser(String fromUser) { | |||
this.fromUser = fromUser; | |||
return this; | |||
} | |||
/** | |||
* 如果消息匹配某个matcher,用在用户需要自定义更复杂的匹配规则的时候 | |||
* | |||
* @param matcher | |||
*/ | |||
public WxCpMessageRouterRule matcher(WxCpMessageMatcher matcher) { | |||
this.matcher = matcher; | |||
return this; | |||
} | |||
/** | |||
* 设置微信消息拦截器 | |||
* | |||
* @param interceptor | |||
*/ | |||
public WxCpMessageRouterRule interceptor(WxCpMessageInterceptor interceptor) { | |||
return interceptor(interceptor, (WxCpMessageInterceptor[]) null); | |||
} | |||
/** | |||
* 设置微信消息拦截器 | |||
* | |||
* @param interceptor | |||
* @param otherInterceptors | |||
*/ | |||
public WxCpMessageRouterRule interceptor(WxCpMessageInterceptor interceptor, WxCpMessageInterceptor... otherInterceptors) { | |||
this.interceptors.add(interceptor); | |||
if (otherInterceptors != null && otherInterceptors.length > 0) { | |||
for (WxCpMessageInterceptor i : otherInterceptors) { | |||
this.interceptors.add(i); | |||
} | |||
} | |||
return this; | |||
} | |||
/** | |||
* 设置微信消息处理器 | |||
* | |||
* @param handler | |||
*/ | |||
public WxCpMessageRouterRule handler(WxCpMessageHandler handler) { | |||
return handler(handler, (WxCpMessageHandler[]) null); | |||
} | |||
/** | |||
* 设置微信消息处理器 | |||
* | |||
* @param handler | |||
* @param otherHandlers | |||
*/ | |||
public WxCpMessageRouterRule handler(WxCpMessageHandler handler, WxCpMessageHandler... otherHandlers) { | |||
this.handlers.add(handler); | |||
if (otherHandlers != null && otherHandlers.length > 0) { | |||
for (WxCpMessageHandler i : otherHandlers) { | |||
this.handlers.add(i); | |||
} | |||
} | |||
return this; | |||
} | |||
/** | |||
* 规则结束,代表如果一个消息匹配该规则,那么它将不再会进入其他规则 | |||
*/ | |||
public WxCpMessageRouter end() { | |||
this.routerBuilder.getRules().add(this); | |||
return this.routerBuilder; | |||
} | |||
/** | |||
* 规则结束,但是消息还会进入其他规则 | |||
*/ | |||
public WxCpMessageRouter next() { | |||
this.reEnter = true; | |||
return end(); | |||
} | |||
protected boolean test(WxCpXmlMessage wxMessage) { | |||
return | |||
(this.fromUser == null || this.fromUser.equals(wxMessage.getFromUserName())) | |||
&& | |||
(this.agentId == null || this.agentId.equals(wxMessage.getAgentId())) | |||
&& | |||
(this.msgType == null || this.msgType.equals(wxMessage.getMsgType())) | |||
&& | |||
(this.event == null || this.event.equals(wxMessage.getEvent())) | |||
&& | |||
(this.eventKey == null || this.eventKey.equals(wxMessage.getEventKey())) | |||
&& | |||
(this.content == null || this.content | |||
.equals(wxMessage.getContent() == null ? null : wxMessage.getContent().trim())) | |||
&& | |||
(this.rContent == null || Pattern | |||
.matches(this.rContent, wxMessage.getContent() == null ? "" : wxMessage.getContent().trim())) | |||
&& | |||
(this.matcher == null || this.matcher.match(wxMessage)) | |||
; | |||
} | |||
/** | |||
* 处理微信推送过来的消息 | |||
* | |||
* @param wxMessage | |||
* @return true 代表继续执行别的router,false 代表停止执行别的router | |||
*/ | |||
protected WxCpXmlOutMessage service(WxCpXmlMessage wxMessage, | |||
Map<String, Object> context, | |||
WxCpService wxCpService, | |||
WxSessionManager sessionManager, | |||
WxErrorExceptionHandler exceptionHandler) { | |||
if (context == null) { | |||
context = new HashMap<>(); | |||
} | |||
try { | |||
// 如果拦截器不通过 | |||
for (WxCpMessageInterceptor interceptor : this.interceptors) { | |||
if (!interceptor.intercept(wxMessage, context, wxCpService, sessionManager)) { | |||
return null; | |||
} | |||
} | |||
// 交给handler处理 | |||
WxCpXmlOutMessage res = null; | |||
for (WxCpMessageHandler handler : this.handlers) { | |||
// 返回最后handler的结果 | |||
res = handler.handle(wxMessage, context, wxCpService, sessionManager); | |||
} | |||
return res; | |||
} catch (WxErrorException e) { | |||
exceptionHandler.handle(e); | |||
} | |||
return null; | |||
} | |||
public void setFromUser(String fromUser) { | |||
this.fromUser = fromUser; | |||
} | |||
public void setMsgType(String msgType) { | |||
this.msgType = msgType; | |||
} | |||
public void setEvent(String event) { | |||
this.event = event; | |||
} | |||
public void setEventKey(String eventKey) { | |||
this.eventKey = eventKey; | |||
} | |||
public void setContent(String content) { | |||
this.content = content; | |||
} | |||
public void setrContent(String rContent) { | |||
this.rContent = rContent; | |||
} | |||
public void setMatcher(WxCpMessageMatcher matcher) { | |||
this.matcher = matcher; | |||
} | |||
public void setAgentId(Integer agentId) { | |||
this.agentId = agentId; | |||
} | |||
public void setHandlers(List<WxCpMessageHandler> handlers) { | |||
this.handlers = handlers; | |||
} | |||
public void setInterceptors(List<WxCpMessageInterceptor> interceptors) { | |||
this.interceptors = interceptors; | |||
} | |||
public boolean isAsync() { | |||
return this.async; | |||
} | |||
public void setAsync(boolean async) { | |||
this.async = async; | |||
} | |||
public boolean isReEnter() { | |||
return this.reEnter; | |||
} | |||
public void setReEnter(boolean reEnter) { | |||
this.reEnter = reEnter; | |||
} | |||
} | |||
package me.chanjar.weixin.cp.api; | |||
import me.chanjar.weixin.common.api.WxErrorExceptionHandler; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import me.chanjar.weixin.common.session.WxSessionManager; | |||
import me.chanjar.weixin.cp.bean.WxCpXmlMessage; | |||
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage; | |||
import java.util.ArrayList; | |||
import java.util.HashMap; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.regex.Pattern; | |||
public class WxCpMessageRouterRule { | |||
private final WxCpMessageRouter routerBuilder; | |||
private boolean async = true; | |||
private String fromUser; | |||
private String msgType; | |||
private String event; | |||
private String eventKey; | |||
private String content; | |||
private String rContent; | |||
private WxCpMessageMatcher matcher; | |||
private boolean reEnter = false; | |||
private Integer agentId; | |||
private List<WxCpMessageHandler> handlers = new ArrayList<>(); | |||
private List<WxCpMessageInterceptor> interceptors = new ArrayList<>(); | |||
protected WxCpMessageRouterRule(WxCpMessageRouter routerBuilder) { | |||
this.routerBuilder = routerBuilder; | |||
} | |||
/** | |||
* 设置是否异步执行,默认是true | |||
* | |||
* @param async | |||
*/ | |||
public WxCpMessageRouterRule async(boolean async) { | |||
this.async = async; | |||
return this; | |||
} | |||
/** | |||
* 如果agentId匹配 | |||
* | |||
* @param agentId | |||
*/ | |||
public WxCpMessageRouterRule agentId(Integer agentId) { | |||
this.agentId = agentId; | |||
return this; | |||
} | |||
/** | |||
* 如果msgType等于某值 | |||
* | |||
* @param msgType | |||
*/ | |||
public WxCpMessageRouterRule msgType(String msgType) { | |||
this.msgType = msgType; | |||
return this; | |||
} | |||
/** | |||
* 如果event等于某值 | |||
* | |||
* @param event | |||
*/ | |||
public WxCpMessageRouterRule event(String event) { | |||
this.event = event; | |||
return this; | |||
} | |||
/** | |||
* 如果eventKey等于某值 | |||
* | |||
* @param eventKey | |||
*/ | |||
public WxCpMessageRouterRule eventKey(String eventKey) { | |||
this.eventKey = eventKey; | |||
return this; | |||
} | |||
/** | |||
* 如果content等于某值 | |||
* | |||
* @param content | |||
*/ | |||
public WxCpMessageRouterRule content(String content) { | |||
this.content = content; | |||
return this; | |||
} | |||
/** | |||
* 如果content匹配该正则表达式 | |||
* | |||
* @param regex | |||
*/ | |||
public WxCpMessageRouterRule rContent(String regex) { | |||
this.rContent = regex; | |||
return this; | |||
} | |||
/** | |||
* 如果fromUser等于某值 | |||
* | |||
* @param fromUser | |||
*/ | |||
public WxCpMessageRouterRule fromUser(String fromUser) { | |||
this.fromUser = fromUser; | |||
return this; | |||
} | |||
/** | |||
* 如果消息匹配某个matcher,用在用户需要自定义更复杂的匹配规则的时候 | |||
* | |||
* @param matcher | |||
*/ | |||
public WxCpMessageRouterRule matcher(WxCpMessageMatcher matcher) { | |||
this.matcher = matcher; | |||
return this; | |||
} | |||
/** | |||
* 设置微信消息拦截器 | |||
* | |||
* @param interceptor | |||
*/ | |||
public WxCpMessageRouterRule interceptor(WxCpMessageInterceptor interceptor) { | |||
return interceptor(interceptor, (WxCpMessageInterceptor[]) null); | |||
} | |||
/** | |||
* 设置微信消息拦截器 | |||
* | |||
* @param interceptor | |||
* @param otherInterceptors | |||
*/ | |||
public WxCpMessageRouterRule interceptor(WxCpMessageInterceptor interceptor, WxCpMessageInterceptor... otherInterceptors) { | |||
this.interceptors.add(interceptor); | |||
if (otherInterceptors != null && otherInterceptors.length > 0) { | |||
for (WxCpMessageInterceptor i : otherInterceptors) { | |||
this.interceptors.add(i); | |||
} | |||
} | |||
return this; | |||
} | |||
/** | |||
* 设置微信消息处理器 | |||
* | |||
* @param handler | |||
*/ | |||
public WxCpMessageRouterRule handler(WxCpMessageHandler handler) { | |||
return handler(handler, (WxCpMessageHandler[]) null); | |||
} | |||
/** | |||
* 设置微信消息处理器 | |||
* | |||
* @param handler | |||
* @param otherHandlers | |||
*/ | |||
public WxCpMessageRouterRule handler(WxCpMessageHandler handler, WxCpMessageHandler... otherHandlers) { | |||
this.handlers.add(handler); | |||
if (otherHandlers != null && otherHandlers.length > 0) { | |||
for (WxCpMessageHandler i : otherHandlers) { | |||
this.handlers.add(i); | |||
} | |||
} | |||
return this; | |||
} | |||
/** | |||
* 规则结束,代表如果一个消息匹配该规则,那么它将不再会进入其他规则 | |||
*/ | |||
public WxCpMessageRouter end() { | |||
this.routerBuilder.getRules().add(this); | |||
return this.routerBuilder; | |||
} | |||
/** | |||
* 规则结束,但是消息还会进入其他规则 | |||
*/ | |||
public WxCpMessageRouter next() { | |||
this.reEnter = true; | |||
return end(); | |||
} | |||
protected boolean test(WxCpXmlMessage wxMessage) { | |||
return | |||
(this.fromUser == null || this.fromUser.equals(wxMessage.getFromUserName())) | |||
&& | |||
(this.agentId == null || this.agentId.equals(wxMessage.getAgentId())) | |||
&& | |||
(this.msgType == null || this.msgType.equals(wxMessage.getMsgType())) | |||
&& | |||
(this.event == null || this.event.equals(wxMessage.getEvent())) | |||
&& | |||
(this.eventKey == null || this.eventKey.equals(wxMessage.getEventKey())) | |||
&& | |||
(this.content == null || this.content | |||
.equals(wxMessage.getContent() == null ? null : wxMessage.getContent().trim())) | |||
&& | |||
(this.rContent == null || Pattern | |||
.matches(this.rContent, wxMessage.getContent() == null ? "" : wxMessage.getContent().trim())) | |||
&& | |||
(this.matcher == null || this.matcher.match(wxMessage)) | |||
; | |||
} | |||
/** | |||
* 处理微信推送过来的消息 | |||
* | |||
* @param wxMessage | |||
* @return true 代表继续执行别的router,false 代表停止执行别的router | |||
*/ | |||
protected WxCpXmlOutMessage service(WxCpXmlMessage wxMessage, | |||
Map<String, Object> context, | |||
WxCpService wxCpService, | |||
WxSessionManager sessionManager, | |||
WxErrorExceptionHandler exceptionHandler) { | |||
if (context == null) { | |||
context = new HashMap<>(); | |||
} | |||
try { | |||
// 如果拦截器不通过 | |||
for (WxCpMessageInterceptor interceptor : this.interceptors) { | |||
if (!interceptor.intercept(wxMessage, context, wxCpService, sessionManager)) { | |||
return null; | |||
} | |||
} | |||
// 交给handler处理 | |||
WxCpXmlOutMessage res = null; | |||
for (WxCpMessageHandler handler : this.handlers) { | |||
// 返回最后handler的结果 | |||
res = handler.handle(wxMessage, context, wxCpService, sessionManager); | |||
} | |||
return res; | |||
} catch (WxErrorException e) { | |||
exceptionHandler.handle(e); | |||
} | |||
return null; | |||
} | |||
public void setFromUser(String fromUser) { | |||
this.fromUser = fromUser; | |||
} | |||
public void setMsgType(String msgType) { | |||
this.msgType = msgType; | |||
} | |||
public void setEvent(String event) { | |||
this.event = event; | |||
} | |||
public void setEventKey(String eventKey) { | |||
this.eventKey = eventKey; | |||
} | |||
public void setContent(String content) { | |||
this.content = content; | |||
} | |||
public void setrContent(String rContent) { | |||
this.rContent = rContent; | |||
} | |||
public void setMatcher(WxCpMessageMatcher matcher) { | |||
this.matcher = matcher; | |||
} | |||
public void setAgentId(Integer agentId) { | |||
this.agentId = agentId; | |||
} | |||
public void setHandlers(List<WxCpMessageHandler> handlers) { | |||
this.handlers = handlers; | |||
} | |||
public void setInterceptors(List<WxCpMessageInterceptor> interceptors) { | |||
this.interceptors = interceptors; | |||
} | |||
public boolean isAsync() { | |||
return this.async; | |||
} | |||
public void setAsync(boolean async) { | |||
this.async = async; | |||
} | |||
public boolean isReEnter() { | |||
return this.reEnter; | |||
} | |||
public void setReEnter(boolean reEnter) { | |||
this.reEnter = reEnter; | |||
} | |||
} |
@@ -1,67 +0,0 @@ | |||
package me.chanjar.weixin.cp.api; | |||
import com.google.inject.Inject; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl; | |||
import me.chanjar.weixin.cp.bean.WxCpDepart; | |||
import org.testng.Assert; | |||
import org.testng.annotations.Guice; | |||
import org.testng.annotations.Test; | |||
import java.util.List; | |||
import static org.testng.Assert.assertNotNull; | |||
import static org.testng.Assert.assertTrue; | |||
/** | |||
* 测试部门接口 | |||
* | |||
* @author Daniel Qian | |||
*/ | |||
@Test(groups = "departAPI") | |||
@Guice(modules = ApiTestModule.class) | |||
public class WxCpDepartAPITest { | |||
@Inject | |||
protected WxCpServiceImpl wxCpService; | |||
protected WxCpDepart depart; | |||
@Test | |||
public void testDepartCreate() throws WxErrorException { | |||
WxCpDepart cpDepart = new WxCpDepart(); | |||
cpDepart.setName("子部门" + System.currentTimeMillis()); | |||
cpDepart.setParentId(1); | |||
cpDepart.setOrder(1); | |||
Integer departId = this.wxCpService.departCreate(cpDepart); | |||
System.out.println(departId); | |||
} | |||
@Test//(dependsOnMethods = "testDepartCreate") | |||
public void testDepartGet() throws WxErrorException { | |||
System.out.println("=================获取部门"); | |||
List<WxCpDepart> departList = this.wxCpService.departGet(); | |||
assertNotNull(departList); | |||
assertTrue(departList.size() > 0); | |||
for (WxCpDepart g : departList) { | |||
this.depart = g; | |||
System.out.println(this.depart.getId() + ":" + this.depart.getName()); | |||
assertNotNull(g.getName()); | |||
} | |||
} | |||
@Test(dependsOnMethods = {"testDepartGet", "testDepartCreate"}) | |||
public void testDepartUpdate() throws WxErrorException { | |||
System.out.println("=================更新部门"); | |||
this.depart.setName("子部门改名" + System.currentTimeMillis()); | |||
this.wxCpService.departUpdate(this.depart); | |||
} | |||
@Test(dependsOnMethods = "testDepartUpdate") | |||
public void testDepartDelete() throws WxErrorException { | |||
System.out.println("=================删除部门"); | |||
System.out.println(this.depart.getId() + ":" + this.depart.getName()); | |||
this.wxCpService.departDelete(this.depart.getId()); | |||
} | |||
} |