weixin4j-qy:新增第三方应用多个API接口

This commit is contained in:
jinyu
2015-06-22 17:12:40 +08:00
parent dd1fcaa598
commit 66bc364b33
19 changed files with 641 additions and 222 deletions
@@ -0,0 +1,36 @@
package com.foxinmy.weixin4j.token;
import com.foxinmy.weixin4j.exception.WeixinException;
/**
* cache存储
*
* @className CacheStorager
* @author jy
* @date 2015年6月22日
* @since JDK 1.7
* @see
*/
public interface CacheStorager<T> {
/**
* 查找缓存中的对象
*
* @param cacheKey
* 缓存名称
* @return
* @throws WeixinException
*/
T lookup(String cacheKey) throws WeixinException;
/**
* 缓存新的对象
*
* @param cacheKey
* 缓存名称
*
* @param t
* 将要缓存的对象
* @throws WeixinException
*/
void caching(String cacheKey, T t) throws WeixinException;
}
@@ -20,27 +20,29 @@ import com.foxinmy.weixin4j.xml.XmlStream;
*/
public class FileTokenStorager implements TokenStorager {
private final String tokenPath;
private final String cachePath;
public FileTokenStorager() {
this(ConfigUtil.getValue("token_path"));
}
public FileTokenStorager(String tokenPath) {
this.tokenPath = tokenPath;
public FileTokenStorager(String cachePath) {
this.cachePath = cachePath;
}
@Override
public Token lookupToken(String cacheKey) throws WeixinException {
File token_file = new File(String.format("%s/%s.xml", tokenPath,
public Token lookup(String cacheKey) throws WeixinException {
File token_file = new File(String.format("%s/%s.xml", cachePath,
cacheKey));
try {
if (token_file.exists()) {
Token token = XmlStream.fromXML(
new FileInputStream(token_file), Token.class);
long expire_time = token.getTime()
+ (token.getExpiresIn() * 1000) - 2;
if (expire_time > System.currentTimeMillis()) {
if (token.getTime() < 0) {
return token;
}
if ((token.getTime() + (token.getExpiresIn() * 1000l) - 2) > System
.currentTimeMillis()) {
return token;
}
}
@@ -51,13 +53,12 @@ public class FileTokenStorager implements TokenStorager {
}
@Override
public void cachingToken(String cacheKey, Token token)
throws WeixinException {
public void caching(String cacheKey, Token token) throws WeixinException {
try {
XmlStream.toXML(
token,
new FileOutputStream(new File(String.format("%s/%s.xml",
tokenPath, cacheKey))));
cachePath, cacheKey))));
} catch (IOException e) {
throw new WeixinException(e.getMessage());
}
@@ -44,10 +44,10 @@ public final class TokenHolder {
*/
public Token getToken() throws WeixinException {
String cacheKey = tokenCreator.getCacheKey();
Token token = tokenStorager.lookupToken(cacheKey);
Token token = tokenStorager.lookup(cacheKey);
if (token == null) {
token = tokenCreator.createToken();
tokenStorager.cachingToken(cacheKey, token);
tokenStorager.caching(cacheKey, token);
}
return token;
}
@@ -1,6 +1,5 @@
package com.foxinmy.weixin4j.token;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.model.Token;
/**
@@ -14,27 +13,5 @@ import com.foxinmy.weixin4j.model.Token;
* @see com.foxinmy.weixin4j.token.FileTokenStorager
* @see com.foxinmy.weixin4j.token.RedisTokenStorager
*/
public interface TokenStorager {
/**
* 查找缓存的token
*
* @param cacheKey
* 缓存的名称
* @return 查找结果
* @throws WeixinException
*/
public Token lookupToken(String cacheKey) throws WeixinException;
/**
* 缓存新的token
*
* @param cacheKey
* 缓存的名称
*
* @param token
* 新产生的token
* @throws WeixinException
*/
public void cachingToken(String cacheKey, Token token)
throws WeixinException;
public interface TokenStorager extends CacheStorager<Token> {
}