diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/util/Weixin4jConfigUtil.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/util/Weixin4jConfigUtil.java index 8add27c8..65a06191 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/util/Weixin4jConfigUtil.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/util/Weixin4jConfigUtil.java @@ -21,8 +21,7 @@ public class Weixin4jConfigUtil { private final static String CLASSPATH_VALUE; private static ResourceBundle weixinBundle; static { - CLASSPATH_VALUE = Thread.currentThread().getContextClassLoader() - .getResource("").getPath(); + CLASSPATH_VALUE = Thread.currentThread().getContextClassLoader().getResource("").getPath(); try { weixinBundle = ResourceBundle.getBundle(Consts.WEIXIN4J); } catch (MissingResourceException e) { @@ -89,21 +88,22 @@ public class Weixin4jConfigUtil { * @return */ public static String getClassPathValue(String key, String defaultValue) { - return getValue(key, defaultValue).replaceFirst(CLASSPATH_PREFIX, - CLASSPATH_VALUE); + return getValue(key, defaultValue).replaceFirst(CLASSPATH_PREFIX, CLASSPATH_VALUE); } + /** + * 获取微信账号信息 + * + * @return 微信账号信息 + */ public static WeixinAccount getWeixinAccount() { WeixinAccount account = null; try { - account = JSON - .parseObject(getValue("account"), WeixinAccount.class); + account = JSON.parseObject(getValue("account"), WeixinAccount.class); } catch (NullPointerException e) { - System.err - .println("'weixin4j.account' key not found in weixin4j.properties."); + System.err.println("'weixin4j.account' key not found in weixin4j.properties."); } catch (MissingResourceException e) { - System.err - .println("'weixin4j.account' key not found in weixin4j.properties."); + System.err.println("'weixin4j.account' key not found in weixin4j.properties."); } return account; } diff --git a/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/ComponentApi.java b/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/ComponentApi.java index d285875c..b0cbc936 100644 --- a/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/ComponentApi.java +++ b/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/ComponentApi.java @@ -114,7 +114,7 @@ public class ComponentApi extends MpApi { * @see com.foxinmy.weixin4j.mp.model.ComponentAuthInfo * @throws WeixinException */ - public ComponentAuthInfo exchangeComponentAuthInfo(String authCode) + public ComponentAuthInfo exchangeAuthInfo(String authCode) throws WeixinException { String component_exchange_authorizer_uri = getRequestUri("component_exchange_authorizer_uri"); JSONObject obj = new JSONObject(); @@ -143,7 +143,7 @@ public class ComponentApi extends MpApi { Token token = new Token(authObj.getString("authorizer_access_token"), authObj.getLongValue("expires_in") * 1000l); ticketManager.getCacheStorager().caching(tokenCreator.key(), token); - // 缓存微信企业号的永久授权码 + // 缓存微信公众号的永久授权码(refresh_token) perTicketManager.cachingTicket(authObj .getString("authorizer_refresh_token")); return info; @@ -160,7 +160,7 @@ public class ComponentApi extends MpApi { * @see com.foxinmy.weixin4j.mp.model.ComponentAuthInfo * @throws WeixinException */ - public ComponentAuthInfo getComponentAuthInfo(String authAppId) + public ComponentAuthInfo getAuthInfo(String authAppId) throws WeixinException { String component_get_authorizer_uri = getRequestUri("component_get_authorizer_uri"); JSONObject obj = new JSONObject(); @@ -222,7 +222,7 @@ public class ComponentApi extends MpApi { * @see com.foxinmy.weixin4j.mp.model.AuthorizerOption * @throws WeixinException */ - public JsonResult getAuthorizerOption(String authAppId, + public JsonResult setAuthorizerOption(String authAppId, AuthorizerOption option) throws WeixinException { String component_set_authorizer_option_uri = getRequestUri("component_set_authorizer_option_uri"); JSONObject obj = new JSONObject(); diff --git a/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/api/SuiteApi.java b/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/api/SuiteApi.java index cfb6db60..73064e38 100644 --- a/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/api/SuiteApi.java +++ b/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/api/SuiteApi.java @@ -25,8 +25,8 @@ import com.foxinmy.weixin4j.token.TokenManager; * @author jinyu(foxinmy@gmail.com) * @date 2015年6月17日 * @since JDK 1.6 - * @see 第三方应用授权 + * @see 第三方应用授权 */ public class SuiteApi extends QyApi { /** @@ -49,10 +49,9 @@ public class SuiteApi extends QyApi { */ public SuiteApi(TicketManager ticketManager) { this.ticketManager = ticketManager; - this.tokenManager = new TokenManager(new WeixinSuiteTokenCreator( - ticketManager), ticketManager.getCacheStorager()); - this.preCodeManager = new TokenManager(new WeixinSuitePreCodeCreator( - tokenManager, ticketManager.getThirdId()), + this.tokenManager = new TokenManager(new WeixinSuiteTokenCreator(ticketManager), + ticketManager.getCacheStorager()); + this.preCodeManager = new TokenManager(new WeixinSuitePreCodeCreator(tokenManager, ticketManager.getThirdId()), ticketManager.getCacheStorager()); } @@ -91,8 +90,7 @@ public class SuiteApi extends QyApi { * @return 应用套件的preticket管理 */ public PerTicketManager getPerTicketManager(String authCorpId) { - return new PerTicketManager(authCorpId, ticketManager.getThirdId(), - ticketManager.getThirdSecret(), + return new PerTicketManager(authCorpId, ticketManager.getThirdId(), ticketManager.getThirdSecret(), ticketManager.getCacheStorager()); } @@ -104,8 +102,7 @@ public class SuiteApi extends QyApi { * @return 企业号token */ public TokenManager getPerTokenManager(String authCorpId) { - return new TokenManager(new WeixinTokenSuiteCreator( - getPerTicketManager(authCorpId), tokenManager), + return new TokenManager(new WeixinTokenSuiteCreator(getPerTicketManager(authCorpId), tokenManager), ticketManager.getCacheStorager()); } @@ -127,14 +124,13 @@ public class SuiteApi extends QyApi { JSONObject appid = new JSONObject(); appid.put("appid", appids); para.put("session_info", appid); - WeixinResponse response = weixinExecutor.post( - String.format(suite_set_session_uri, - tokenManager.getAccessToken()), para.toJSONString()); + WeixinResponse response = weixinExecutor + .post(String.format(suite_set_session_uri, tokenManager.getAccessToken()), para.toJSONString()); return response.getAsJsonResult(); } /** - * 获取企业号的永久授权码 + * 临时授权码换取授权方的永久授权码,并换取授权信息、企业access_token * * @param authCode * 临时授权码会在授权成功时附加在redirect_uri中跳转回应用提供商网站。 @@ -145,27 +141,22 @@ public class SuiteApi extends QyApi { * "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.E4.BC.81.E4.B8.9A.E5.8F.B7.E7.9A.84.E6.B0.B8.E4.B9.85.E6.8E.88.E6.9D.83.E7.A0.81" * >获取企业号的永久授权码 */ - public OUserInfo exchangePermanentCode(String authCode) - throws WeixinException { + public OUserInfo exchangeAuthInfo(String authCode) throws WeixinException { String suite_get_permanent_uri = getRequestUri("suite_get_permanent_uri"); JSONObject obj = new JSONObject(); obj.put("suite_id", ticketManager.getThirdId()); obj.put("auth_code", authCode); - WeixinResponse response = weixinExecutor.post( - String.format(suite_get_permanent_uri, - tokenManager.getAccessToken()), obj.toJSONString()); + WeixinResponse response = weixinExecutor + .post(String.format(suite_get_permanent_uri, tokenManager.getAccessToken()), obj.toJSONString()); obj = response.getAsJson(); obj.put("corp_info", obj.remove("auth_corp_info")); obj.put("user_info", obj.remove("auth_user_info")); OUserInfo oInfo = JSON.toJavaObject(obj, OUserInfo.class); // 微信授权企业号的永久授权码 - PerTicketManager perTicketManager = getPerTicketManager(oInfo - .getCorpInfo().getCorpId()); + PerTicketManager perTicketManager = getPerTicketManager(oInfo.getCorpInfo().getCorpId()); // 缓存微信企业号的access_token - TokenCreator tokenCreator = new WeixinTokenSuiteCreator( - perTicketManager, tokenManager); - Token token = new Token(obj.getString("access_token"), - obj.getLongValue("expires_in") * 1000l); + TokenCreator tokenCreator = new WeixinTokenSuiteCreator(perTicketManager, tokenManager); + Token token = new Token(obj.getString("access_token"), obj.getLongValue("expires_in") * 1000l); ticketManager.getCacheStorager().caching(tokenCreator.key(), token); // 缓存微信企业号的永久授权码 perTicketManager.cachingTicket(obj.getString("permanent_code")); @@ -180,18 +171,17 @@ public class SuiteApi extends QyApi { * @return 授权方信息 * @throws WeixinException * @see com.foxinmy.weixin4j.qy.model.OUserInfo - * @see 获取企业号的授权信息 + * @see 获取企业号的授权信息 */ - public OUserInfo getOAuthInfo(String authCorpId) throws WeixinException { + public OUserInfo getAuthInfo(String authCorpId) throws WeixinException { String suite_get_authinfo_uri = getRequestUri("suite_get_authinfo_uri"); JSONObject obj = new JSONObject(); obj.put("suite_id", ticketManager.getThirdId()); obj.put("auth_corpid", authCorpId); obj.put("permanent_code", getPerTicketManager(authCorpId).getAccessTicket()); - WeixinResponse response = weixinExecutor.post( - String.format(suite_get_authinfo_uri, - tokenManager.getAccessToken()), obj.toJSONString()); + WeixinResponse response = weixinExecutor + .post(String.format(suite_get_authinfo_uri, tokenManager.getAccessToken()), obj.toJSONString()); obj = response.getAsJson(); obj.put("corp_info", obj.remove("auth_corp_info")); obj.put("user_info", obj.remove("auth_user_info")); @@ -207,31 +197,25 @@ public class SuiteApi extends QyApi { * 授权方应用id * @return 应用信息 * @see com.foxinmy.weixin4j.qy.model.AgentInfo - * @see 获取企业号应用 + * @see 获取企业号应用 * @throws WeixinException */ - public AgentInfo getAgent(String authCorpId, int agentid) - throws WeixinException { + public AgentInfo getAgent(String authCorpId, int agentid) throws WeixinException { String suite_get_agent_uri = getRequestUri("suite_get_agent_uri"); JSONObject obj = new JSONObject(); obj.put("suite_id", ticketManager.getThirdId()); obj.put("auth_corpid", authCorpId); obj.put("permanent_code", getPerTicketManager(authCorpId).getAccessTicket()); obj.put("agentid", agentid); - WeixinResponse response = weixinExecutor.post( - String.format(suite_get_agent_uri, - tokenManager.getAccessToken()), obj.toJSONString()); + WeixinResponse response = weixinExecutor.post(String.format(suite_get_agent_uri, tokenManager.getAccessToken()), + obj.toJSONString()); JSONObject jsonObj = response.getAsJson(); AgentInfo agent = JSON.toJavaObject(jsonObj, AgentInfo.class); - agent.setAllowUsers(JSON.parseArray( - jsonObj.getJSONObject("allow_userinfos").getString("user"), - User.class)); - agent.setAllowPartys(JSON.parseArray( - jsonObj.getJSONObject("allow_partys").getString("partyid"), - Integer.class)); - agent.setAllowTags(JSON.parseArray(jsonObj.getJSONObject("allow_tags") - .getString("tagid"), Integer.class)); + agent.setAllowUsers(JSON.parseArray(jsonObj.getJSONObject("allow_userinfos").getString("user"), User.class)); + agent.setAllowPartys( + JSON.parseArray(jsonObj.getJSONObject("allow_partys").getString("partyid"), Integer.class)); + agent.setAllowTags(JSON.parseArray(jsonObj.getJSONObject("allow_tags").getString("tagid"), Integer.class)); return agent; } @@ -243,22 +227,19 @@ public class SuiteApi extends QyApi { * @param agentSet * 设置信息 * @see com.foxinmy.weixin4j.qy.model.AgentSetter - * @see 设置企业号信息 + * @see 设置企业号信息 * @return 处理结果 * @throws WeixinException */ - public JsonResult setAgent(String authCorpId, AgentSetter agentSet) - throws WeixinException { + public JsonResult setAgent(String authCorpId, AgentSetter agentSet) throws WeixinException { String suite_set_agent_uri = getRequestUri("suite_set_agent_uri"); JSONObject obj = new JSONObject(); obj.put("suite_id", ticketManager.getThirdId()); obj.put("auth_corpid", authCorpId); obj.put("permanent_code", getPerTicketManager(authCorpId).getAccessTicket()); obj.put("agent", agentSet); - WeixinResponse response = weixinExecutor.post( - String.format(suite_set_agent_uri, - tokenManager.getAccessToken()), + WeixinResponse response = weixinExecutor.post(String.format(suite_set_agent_uri, tokenManager.getAccessToken()), JSON.toJSONString(obj, AgentApi.typeFilter)); return response.getAsJsonResult(); } diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/handler/MultipleMessageHandlerAdapter.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/handler/MultipleMessageHandlerAdapter.java index 9c990088..00fb4c2e 100644 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/handler/MultipleMessageHandlerAdapter.java +++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/handler/MultipleMessageHandlerAdapter.java @@ -16,28 +16,25 @@ import com.foxinmy.weixin4j.request.WeixinRequest; * @since JDK 1.6 * @see */ -public abstract class MultipleMessageHandlerAdapter implements - WeixinMessageHandler { +public abstract class MultipleMessageHandlerAdapter implements WeixinMessageHandler { private final Set> messageClasses; - public MultipleMessageHandlerAdapter( - Class... messageClasses) { + public MultipleMessageHandlerAdapter(Class... messageClasses) { if (messageClasses == null) { throw new IllegalArgumentException("messageClasses not be empty"); } this.messageClasses = new HashSet>( - (int) Math.ceil(messageClasses.length * 0.75)); + Math.max((int) (messageClasses.length / .75f) + 1, 16)); for (Class clazz : messageClasses) { this.messageClasses.add(clazz); } } @Override - public boolean canHandle(WeixinRequest request, WeixinMessage message, - Set nodeNames) throws WeixinException { - return message != null && messageClasses.contains(message.getClass()) - && canHandle0(request, message); + public boolean canHandle(WeixinRequest request, WeixinMessage message, Set nodeNames) + throws WeixinException { + return message != null && messageClasses.contains(message.getClass()) && canHandle0(request, message); } /** @@ -50,8 +47,7 @@ public abstract class MultipleMessageHandlerAdapter implements * @return true则执行doHandler * @throws WeixinException */ - public boolean canHandle0(WeixinRequest request, WeixinMessage message) - throws WeixinException { + public boolean canHandle0(WeixinRequest request, WeixinMessage message) throws WeixinException { return true; }