删除AbstractTokenCreator,引入CacheCreator<T>类
This commit is contained in:
parent
eb54ea9079
commit
f6c12e07f3
14
CHANGE.md
14
CHANGE.md
@ -687,4 +687,16 @@
|
||||
|
||||
* 2016-05-12
|
||||
|
||||
+ 添加MemcacheTokenStorager支持
|
||||
+ weixin4j-base:添加MemcacheTokenStorager支持
|
||||
|
||||
* 2016-05-24
|
||||
|
||||
+ weixin4j-mp:优化OauthApi授权
|
||||
|
||||
+ openid 修正为 openId
|
||||
|
||||
+ 修改@author注释为jinyu(foxinmy@gmail.com)
|
||||
|
||||
+ weixin4j-base:删除AbstractTokenCreator,引入CacheCreator<T>类
|
||||
|
||||
+ weixin4j-base:修改Memcached-Java-Client的依赖
|
||||
@ -1,36 +0,0 @@
|
||||
package com.foxinmy.weixin4j.token;
|
||||
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
|
||||
|
||||
/**
|
||||
*
|
||||
* @className: AbstractTokenCreator
|
||||
* @author jinyu
|
||||
* @date 2016年4月21日
|
||||
* @since JDK 1.6
|
||||
* @see
|
||||
*/
|
||||
public abstract class AbstractTokenCreator implements TokenCreator {
|
||||
|
||||
protected final WeixinRequestExecutor weixinExecutor;
|
||||
|
||||
public AbstractTokenCreator() {
|
||||
this.weixinExecutor = new WeixinRequestExecutor();
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存key:附加weixin4j_前缀
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getCacheKey() {
|
||||
return String.format("weixin4j_%s", getCacheKey0());
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回缓存KEY的名称:建议接口类型命名 如 mp_token_{appid}
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public abstract String getCacheKey0();
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
package com.foxinmy.weixin4j.token;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
|
||||
/**
|
||||
* Cache的创建
|
||||
*
|
||||
* @className CacheCreator
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2016年5月24日
|
||||
* @since JDK 1.6
|
||||
* @see
|
||||
*/
|
||||
public interface CacheCreator<T> {
|
||||
/**
|
||||
* CacheKey
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String key();
|
||||
|
||||
/**
|
||||
* 创建Cache
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public T create() throws WeixinException;
|
||||
}
|
||||
@ -3,7 +3,7 @@ package com.foxinmy.weixin4j.token;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
|
||||
/**
|
||||
* cache存储
|
||||
* Cache的存储
|
||||
*
|
||||
* @className CacheStorager
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
@ -15,33 +15,33 @@ public interface CacheStorager<T> {
|
||||
/**
|
||||
* 查找缓存中的对象
|
||||
*
|
||||
* @param cacheKey
|
||||
* @param key
|
||||
* 缓存key
|
||||
* @return 缓存对象
|
||||
* @throws WeixinException
|
||||
*/
|
||||
T lookup(String cacheKey) throws WeixinException;
|
||||
T lookup(String key) throws WeixinException;
|
||||
|
||||
/**
|
||||
* 缓存新的对象
|
||||
*
|
||||
* @param cacheKey
|
||||
* @param key
|
||||
* 缓存key
|
||||
*
|
||||
* @param t
|
||||
* @param cache
|
||||
* 将要缓存的对象
|
||||
* @throws WeixinException
|
||||
*/
|
||||
void caching(String cacheKey, T t) throws WeixinException;
|
||||
void caching(String key, T cache) throws WeixinException;
|
||||
|
||||
/**
|
||||
* 移除缓存对象
|
||||
*
|
||||
* @param cacheKey
|
||||
* @param key
|
||||
* 缓存key
|
||||
* @return 移除的对象
|
||||
*/
|
||||
T evict(String cacheKey) throws WeixinException;
|
||||
T evict(String key) throws WeixinException;
|
||||
|
||||
/**
|
||||
* 清除所有缓存对象(<font color="red">请慎重</font>)
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
package com.foxinmy.weixin4j.token;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
|
||||
/**
|
||||
* TOKEN创建者
|
||||
* Token的创建
|
||||
*
|
||||
* @className TokenCreator
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
@ -12,19 +12,27 @@ import com.foxinmy.weixin4j.model.Token;
|
||||
* @since JDK 1.6
|
||||
* @see
|
||||
*/
|
||||
public interface TokenCreator {
|
||||
/**
|
||||
* 返回缓存KEY的名称
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getCacheKey();
|
||||
public abstract class TokenCreator implements CacheCreator<Token> {
|
||||
protected final WeixinRequestExecutor weixinExecutor;
|
||||
|
||||
public TokenCreator() {
|
||||
this.weixinExecutor = new WeixinRequestExecutor();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建token
|
||||
* 缓存key:附加weixin4j_前缀
|
||||
*
|
||||
* @return
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public Token createToken() throws WeixinException;
|
||||
@Override
|
||||
public String key() {
|
||||
return String.format("weixin4j_%s", key0());
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回缓存KEY的名称:建议接口类型命名 如 mp_token_{appid}
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public abstract String key0();
|
||||
}
|
||||
|
||||
@ -43,10 +43,10 @@ public class TokenHolder {
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public Token getToken() throws WeixinException {
|
||||
String cacheKey = tokenCreator.getCacheKey();
|
||||
String cacheKey = tokenCreator.key();
|
||||
Token token = tokenStorager.lookup(cacheKey);
|
||||
if (token == null) {
|
||||
token = tokenCreator.createToken();
|
||||
token = tokenCreator.create();
|
||||
tokenStorager.caching(cacheKey, token);
|
||||
}
|
||||
return token;
|
||||
@ -69,8 +69,8 @@ public class TokenHolder {
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public Token refreshToken() throws WeixinException {
|
||||
String cacheKey = tokenCreator.getCacheKey();
|
||||
Token token = tokenCreator.createToken();
|
||||
String cacheKey = tokenCreator.key();
|
||||
Token token = tokenCreator.create();
|
||||
tokenStorager.caching(cacheKey, token);
|
||||
return token;
|
||||
}
|
||||
@ -82,7 +82,7 @@ public class TokenHolder {
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public Token evictToken() throws WeixinException {
|
||||
String cacheKey = tokenCreator.getCacheKey();
|
||||
String cacheKey = tokenCreator.key();
|
||||
return tokenStorager.evict(cacheKey);
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,7 +3,7 @@ package com.foxinmy.weixin4j.token;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
|
||||
/**
|
||||
* token的存储
|
||||
* Token的存储
|
||||
*
|
||||
* @className TokenStorager
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
|
||||
@ -5,7 +5,7 @@ import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.mp.type.URLConsts;
|
||||
import com.foxinmy.weixin4j.token.AbstractTokenCreator;
|
||||
import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
import com.foxinmy.weixin4j.type.TicketType;
|
||||
|
||||
@ -20,7 +20,7 @@ import com.foxinmy.weixin4j.type.TicketType;
|
||||
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115&token=&lang=zh_CN">
|
||||
* JS TICKET</a>
|
||||
*/
|
||||
public class WeixinTicketCreator extends AbstractTokenCreator {
|
||||
public class WeixinTicketCreator extends TokenCreator {
|
||||
|
||||
private final String appid;
|
||||
private final TicketType ticketType;
|
||||
@ -43,12 +43,12 @@ public class WeixinTicketCreator extends AbstractTokenCreator {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCacheKey0() {
|
||||
public String key0() {
|
||||
return String.format("mp_ticket_%s_%s", ticketType.name(), appid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token createToken() throws WeixinException {
|
||||
public Token create() throws WeixinException {
|
||||
WeixinResponse response = weixinExecutor.get(
|
||||
String.format(URLConsts.JS_TICKET_URL, weixinTokenHolder.getToken().getAccessToken(), ticketType.name()));
|
||||
JSONObject result = response.getAsJson();
|
||||
|
||||
@ -5,7 +5,7 @@ import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.mp.type.URLConsts;
|
||||
import com.foxinmy.weixin4j.token.AbstractTokenCreator;
|
||||
import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
|
||||
/**
|
||||
* 微信公众平台TOKEN创建者
|
||||
@ -18,7 +18,7 @@ import com.foxinmy.weixin4j.token.AbstractTokenCreator;
|
||||
* href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183&token=&lang=zh_CN">微信公众平台获取token说明</a>
|
||||
* @see com.foxinmy.weixin4j.model.Token
|
||||
*/
|
||||
public class WeixinTokenCreator extends AbstractTokenCreator {
|
||||
public class WeixinTokenCreator extends TokenCreator {
|
||||
|
||||
private final String appid;
|
||||
private final String secret;
|
||||
@ -36,12 +36,12 @@ public class WeixinTokenCreator extends AbstractTokenCreator {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCacheKey0() {
|
||||
public String key0() {
|
||||
return String.format("mp_token_%s", appid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token createToken() throws WeixinException {
|
||||
public Token create() throws WeixinException {
|
||||
String tokenUrl = String.format(URLConsts.ASSESS_TOKEN_URL, appid,
|
||||
secret);
|
||||
WeixinResponse response = weixinExecutor.get(tokenUrl);
|
||||
|
||||
@ -168,7 +168,7 @@ public class SuiteApi extends QyApi {
|
||||
Token token = new Token(obj.getString("access_token"));
|
||||
token.setExpiresIn(obj.getIntValue("expires_in"));
|
||||
suiteTicketHolder.getTokenStorager().caching(
|
||||
tokenCreator.getCacheKey(), token);
|
||||
tokenCreator.key(), token);
|
||||
// 缓存微信企业号永久授权码
|
||||
suitePerCodeHolder
|
||||
.cachingPermanentCode(obj.getString("permanent_code"));
|
||||
|
||||
@ -5,7 +5,7 @@ import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
||||
import com.foxinmy.weixin4j.token.AbstractTokenCreator;
|
||||
import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
|
||||
/**
|
||||
@ -20,7 +20,7 @@ import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
* 获取应用套件预授权码</a>
|
||||
* @see com.foxinmy.weixin4j.model.Token
|
||||
*/
|
||||
public class WeixinSuitePreCodeCreator extends AbstractTokenCreator {
|
||||
public class WeixinSuitePreCodeCreator extends TokenCreator {
|
||||
|
||||
private final TokenHolder suiteTokenHolder;
|
||||
private final String suiteId;
|
||||
@ -38,12 +38,12 @@ public class WeixinSuitePreCodeCreator extends AbstractTokenCreator {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCacheKey0() {
|
||||
public String key0() {
|
||||
return String.format("qy_suite_precode_%s", suiteId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token createToken() throws WeixinException {
|
||||
public Token create() throws WeixinException {
|
||||
WeixinResponse response = weixinExecutor.post(
|
||||
String.format(URLConsts.SUITE_PRE_CODE_URL, suiteTokenHolder.getAccessToken()),
|
||||
String.format("{\"suite_id\":\"%s\"}", suiteId));
|
||||
|
||||
@ -5,7 +5,7 @@ import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
||||
import com.foxinmy.weixin4j.token.AbstractTokenCreator;
|
||||
import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
|
||||
/**
|
||||
* 微信企业号应用套件凭证创建
|
||||
@ -18,7 +18,7 @@ import com.foxinmy.weixin4j.token.AbstractTokenCreator;
|
||||
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E8.8E.B7.E5.8F.96.E5.BA.94.E7.94.A8.E5.A5.97.E4.BB.B6.E4.BB.A4.E7.89.8C">获取应用套件凭证</a>
|
||||
* @see com.foxinmy.weixin4j.model.Token
|
||||
*/
|
||||
public class WeixinSuiteTokenCreator extends AbstractTokenCreator {
|
||||
public class WeixinSuiteTokenCreator extends TokenCreator {
|
||||
|
||||
private final SuiteTicketHolder ticketHolder;
|
||||
|
||||
@ -32,12 +32,12 @@ public class WeixinSuiteTokenCreator extends AbstractTokenCreator {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCacheKey0() {
|
||||
public String key0() {
|
||||
return String.format("qy_suite_token_%s", ticketHolder.getSuiteId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token createToken() throws WeixinException {
|
||||
public Token create() throws WeixinException {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("suite_id", ticketHolder.getSuiteId());
|
||||
obj.put("suite_secret", ticketHolder.getSuiteSecret());
|
||||
|
||||
@ -5,7 +5,7 @@ import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
||||
import com.foxinmy.weixin4j.token.AbstractTokenCreator;
|
||||
import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
|
||||
/**
|
||||
@ -20,7 +20,7 @@ import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
* 获取企业号access_token</a>
|
||||
* @see com.foxinmy.weixin4j.model.Token
|
||||
*/
|
||||
public class WeixinTokenSuiteCreator extends AbstractTokenCreator {
|
||||
public class WeixinTokenSuiteCreator extends TokenCreator {
|
||||
|
||||
private final SuitePerCodeHolder perCodeHolder;
|
||||
private final TokenHolder suiteTokenHolder;
|
||||
@ -38,12 +38,12 @@ public class WeixinTokenSuiteCreator extends AbstractTokenCreator {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCacheKey0() {
|
||||
public String key0() {
|
||||
return String.format("qy_token_suite_%s_%s", perCodeHolder.getSuiteId(), perCodeHolder.getAuthCorpId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token createToken() throws WeixinException {
|
||||
public Token create() throws WeixinException {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("suite_id", perCodeHolder.getSuiteId());
|
||||
obj.put("auth_corpid", perCodeHolder.getAuthCorpId());
|
||||
|
||||
@ -5,7 +5,7 @@ import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
||||
import com.foxinmy.weixin4j.token.AbstractTokenCreator;
|
||||
import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
|
||||
/**
|
||||
* 微信企业号应用提供商凭证创建
|
||||
@ -19,7 +19,7 @@ import com.foxinmy.weixin4j.token.AbstractTokenCreator;
|
||||
* 获取应用提供商凭证</a>
|
||||
* @see com.foxinmy.weixin4j.model.Token
|
||||
*/
|
||||
public class WeixinProviderTokenCreator extends AbstractTokenCreator {
|
||||
public class WeixinProviderTokenCreator extends TokenCreator {
|
||||
|
||||
private final String corpid;
|
||||
private final String providersecret;
|
||||
@ -37,12 +37,12 @@ public class WeixinProviderTokenCreator extends AbstractTokenCreator {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCacheKey0() {
|
||||
public String key0() {
|
||||
return String.format("qy_provider_token_%s", corpid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token createToken() throws WeixinException {
|
||||
public Token create() throws WeixinException {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("corpid", corpid);
|
||||
obj.put("provider_secret", providersecret);
|
||||
|
||||
@ -5,7 +5,7 @@ import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
||||
import com.foxinmy.weixin4j.token.AbstractTokenCreator;
|
||||
import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
import com.foxinmy.weixin4j.type.TicketType;
|
||||
|
||||
@ -19,7 +19,7 @@ import com.foxinmy.weixin4j.type.TicketType;
|
||||
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E5%BE%AE%E4%BF%A1JS-SDK%E6%8E%A5%E5%8F%A3#.E9.99.84.E5.BD.951-JS-SDK.E4.BD.BF.E7.94.A8.E6.9D.83.E9.99.90.E7.AD.BE.E5.90.8D.E7.AE.97.E6.B3.95"
|
||||
* >JSTICKET</a>
|
||||
*/
|
||||
public class WeixinTicketCreator extends AbstractTokenCreator {
|
||||
public class WeixinTicketCreator extends TokenCreator {
|
||||
|
||||
private final String corpid;
|
||||
private final TicketType ticketType;
|
||||
@ -40,12 +40,12 @@ public class WeixinTicketCreator extends AbstractTokenCreator {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCacheKey0() {
|
||||
public String key0() {
|
||||
return String.format("qy_ticket_%s_%s", ticketType.name(), corpid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token createToken() throws WeixinException {
|
||||
public Token create() throws WeixinException {
|
||||
WeixinResponse response = null;
|
||||
if (ticketType == TicketType.jsapi) {
|
||||
response = weixinExecutor
|
||||
|
||||
@ -5,7 +5,7 @@ import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
||||
import com.foxinmy.weixin4j.token.AbstractTokenCreator;
|
||||
import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
|
||||
/**
|
||||
* 微信企业号TOKEN创建
|
||||
@ -19,7 +19,7 @@ import com.foxinmy.weixin4j.token.AbstractTokenCreator;
|
||||
* 微信企业号获取token说明</a>
|
||||
* @see com.foxinmy.weixin4j.model.Token
|
||||
*/
|
||||
public class WeixinTokenCreator extends AbstractTokenCreator {
|
||||
public class WeixinTokenCreator extends TokenCreator {
|
||||
|
||||
private final String corpid;
|
||||
private final String corpsecret;
|
||||
@ -37,12 +37,12 @@ public class WeixinTokenCreator extends AbstractTokenCreator {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCacheKey0() {
|
||||
public String key0() {
|
||||
return String.format("qy_token_%s", corpid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token createToken() throws WeixinException {
|
||||
public Token create() throws WeixinException {
|
||||
String tokenUrl = String.format(URLConsts.ASSESS_TOKEN_URL, corpid,
|
||||
corpsecret);
|
||||
WeixinResponse response = weixinExecutor.get(tokenUrl);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user