From 6643498a66dc0f565fee5264ae2ccb4a5479aa53 Mon Sep 17 00:00:00 2001 From: BinaryWang Date: Tue, 2 Aug 2016 19:40:44 +0800 Subject: [PATCH] code clean up for common module --- .../chanjar/weixin/common/api/WxConsts.java | 92 +++-- .../common/api/WxMessageDuplicateChecker.java | 9 +- .../WxMessageInMemoryDuplicateChecker.java | 3 +- .../weixin/common/bean/WxAccessToken.java | 10 +- .../weixin/common/bean/WxJsapiSignature.java | 6 +- .../me/chanjar/weixin/common/bean/WxMenu.java | 108 +++--- .../weixin/common/bean/result/WxError.java | 38 +- .../bean/result/WxMediaUploadResult.java | 12 +- .../common/exception/WxErrorException.java | 4 +- .../weixin/common/session/Constants.java | 2 +- .../common/session/InternalSession.java | 10 +- .../session/InternalSessionManager.java | 25 +- .../common/session/LocalStrings.properties | 1 - .../common/session/StandardSession.java | 152 ++++---- .../session/StandardSessionManager.java | 140 ++++--- .../TooManyActiveSessionsException.java | 59 ++- .../weixin/common/util/StringUtils.java | 22 +- .../weixin/common/util/crypto/ByteGroup.java | 32 +- .../common/util/crypto/PKCS7Encoder.java | 92 ++--- .../weixin/common/util/crypto/SHA1.java | 6 +- .../common/util/crypto/WxCryptUtil.java | 102 ++--- .../weixin/common/util/fs/FileUtils.java | 16 +- .../util/http/ApacheHttpClientBuilder.java | 6 + .../DefaultApacheHttpHttpClientBuilder.java | 40 +- .../util/http/InputStreamResponseHandler.java | 8 +- .../util/http/JoddGetRequestExecutor.java | 66 ++-- .../util/http/JoddPostRequestExecutor.java | 54 +-- .../http/MediaDownloadRequestExecutor.java | 13 +- .../util/http/MediaUploadRequestExecutor.java | 13 +- .../common/util/http/RequestExecutor.java | 12 +- .../util/http/SimpleGetRequestExecutor.java | 5 +- .../util/http/SimplePostRequestExecutor.java | 5 +- .../common/util/http/Utf8ResponseHandler.java | 8 +- .../weixin/common/util/json/GsonHelper.java | 194 +++++----- .../util/json/WxAccessTokenAdapter.java | 4 +- .../common/util/json/WxErrorAdapter.java | 4 +- .../common/util/json/WxGsonBuilder.java | 2 +- .../util/json/WxMediaUploadResultAdapter.java | 4 +- .../common/util/json/WxMenuGsonAdapter.java | 33 +- .../weixin/common/util/res/StringManager.java | 364 +++++++++--------- .../common/util/xml/XStreamInitializer.java | 5 +- .../weixin/common/bean/WxAccessTokenTest.java | 2 +- .../weixin/common/bean/WxErrorTest.java | 6 +- .../weixin/common/bean/WxMenuTest.java | 150 ++++---- .../weixin/common/session/SessionTest.java | 4 +- ...WxMessageInMemoryDuplicateCheckerTest.java | 2 +- .../common/util/crypto/WxCryptUtilTest.java | 2 +- 47 files changed, 963 insertions(+), 984 deletions(-) diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/api/WxConsts.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/api/WxConsts.java index 0c9661d2..e45643b7 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/api/WxConsts.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/api/WxConsts.java @@ -19,7 +19,7 @@ public class WxConsts { public static final String XML_MSG_LINK = "link"; public static final String XML_MSG_EVENT = "event"; public static final String XML_TRANSFER_CUSTOMER_SERVICE = "transfer_customer_service"; - + /////////////////////// // 主动发送消息(即客服消息)的消息类型 /////////////////////// @@ -33,7 +33,7 @@ public class WxConsts { public static final String CUSTOM_MSG_TRANSFER_CUSTOMER_SERVICE = "transfer_customer_service"; public static final String CUSTOM_MSG_SAFE_NO = "0"; public static final String CUSTOM_MSG_SAFE_YES = "1"; - + /////////////////////// // 群发消息的消息类型 /////////////////////// @@ -42,7 +42,7 @@ public class WxConsts { public static final String MASS_MSG_VOICE = "voice"; public static final String MASS_MSG_IMAGE = "image"; public static final String MASS_MSG_VIDEO = "mpvideo"; - + /////////////////////// // 群发消息后微信端推送给服务器的反馈消息 /////////////////////// @@ -57,25 +57,11 @@ public class WxConsts { public static final String MASS_ST_涉嫌版权 = "err(20013)"; public static final String MASS_ST_涉嫌互推_互相宣传 = "err(22000)"; public static final String MASS_ST_涉嫌其他 = "err(21000)"; - + /** * 群发反馈消息代码所对应的文字描述 */ public static final Map MASS_ST_2_DESC = new HashMap(); - static { - MASS_ST_2_DESC.put(MASS_ST_SUCCESS, "发送成功"); - MASS_ST_2_DESC.put(MASS_ST_FAIL, "发送失败"); - MASS_ST_2_DESC.put(MASS_ST_涉嫌广告, "涉嫌广告"); - MASS_ST_2_DESC.put(MASS_ST_涉嫌政治, "涉嫌政治"); - MASS_ST_2_DESC.put(MASS_ST_涉嫌社会, "涉嫌社会"); - MASS_ST_2_DESC.put(MASS_ST_涉嫌色情, "涉嫌色情"); - MASS_ST_2_DESC.put(MASS_ST_涉嫌违法犯罪, "涉嫌违法犯罪"); - MASS_ST_2_DESC.put(MASS_ST_涉嫌欺诈, "涉嫌欺诈"); - MASS_ST_2_DESC.put(MASS_ST_涉嫌版权, "涉嫌版权"); - MASS_ST_2_DESC.put(MASS_ST_涉嫌互推_互相宣传, "涉嫌互推_互相宣传"); - MASS_ST_2_DESC.put(MASS_ST_涉嫌其他, "涉嫌其他"); - } - /////////////////////// // 微信端推送过来的事件类型 /////////////////////// @@ -103,11 +89,9 @@ public class WxConsts { public static final String EVT_USER_VIEW_CARD = "user_view_card"; public static final String EVT_USER_ENTER_SESSION_FROM_CARD = "user_enter_session_from_card"; public static final String EVT_CARD_SKU_REMIND = "card_sku_remind"; // 库存报警 - public static final String EVT_KF_CREATE_SESSION = "kf_create_session"; // 客服接入会话 public static final String EVT_KF_CLOSE_SESSION = "kf_close_session"; // 客服关闭会话 public static final String EVT_KF_SWITCH_SESSION = "kf_switch_session"; // 客服转接会话 - /////////////////////// // 上传多媒体文件的类型 /////////////////////// @@ -116,7 +100,6 @@ public class WxConsts { public static final String MEDIA_VIDEO = "video"; public static final String MEDIA_THUMB = "thumb"; public static final String MEDIA_FILE = "file"; - /////////////////////// // 文件类型 /////////////////////// @@ -124,40 +107,63 @@ public class WxConsts { public static final String FILE_MP3 = "mp3"; public static final String FILE_AMR = "amr"; public static final String FILE_MP4 = "mp4"; + /** + * 点击推事件 + */ + public static final String BUTTON_CLICK = "click"; /////////////////////// // 自定义菜单的按钮类型 /////////////////////// - /** 点击推事件 */ - public static final String BUTTON_CLICK = "click"; - /** 跳转URL */ + /** + * 跳转URL + */ public static final String BUTTON_VIEW = "view"; - /** 扫码推事件 */ + /** + * 扫码推事件 + */ public static final String BUTTON_SCANCODE_PUSH = "scancode_push"; - /** 扫码推事件且弹出“消息接收中”提示框 */ + /** + * 扫码推事件且弹出“消息接收中”提示框 + */ public static final String BUTTON_SCANCODE_WAITMSG = "scancode_waitmsg"; - /** 弹出系统拍照发图 */ + /** + * 弹出系统拍照发图 + */ public static final String BUTTON_PIC_SYSPHOTO = "pic_sysphoto"; - /** 弹出拍照或者相册发图 */ + /** + * 弹出拍照或者相册发图 + */ public static final String BUTTON_PIC_PHOTO_OR_ALBUM = "pic_photo_or_album"; - /** 弹出微信相册发图器 */ + /** + * 弹出微信相册发图器 + */ public static final String BUTTON_PIC_WEIXIN = "pic_weixin"; - /** 弹出地理位置选择器 */ + /** + * 弹出地理位置选择器 + */ public static final String BUTTON_LOCATION_SELECT = "location_select"; - /** 下发消息(除文本消息) */ + /** + * 下发消息(除文本消息) + */ public static final String BUTTON_MEDIA_ID = "media_id"; - /** 跳转图文消息URL */ + /** + * 跳转图文消息URL + */ public static final String BUTTON_VIEW_LIMITED = "view_limited"; + /** + * 不弹出授权页面,直接跳转,只能获取用户openid + */ + public static final String OAUTH2_SCOPE_BASE = "snsapi_base"; /////////////////////// // oauth2网页授权的scope /////////////////////// - /** 不弹出授权页面,直接跳转,只能获取用户openid */ - public static final String OAUTH2_SCOPE_BASE = "snsapi_base"; - /** 弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息 */ + /** + * 弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息 + */ public static final String OAUTH2_SCOPE_USER_INFO = "snsapi_userinfo"; - /////////////////////// // 永久素材类型 /////////////////////// @@ -165,4 +171,18 @@ public class WxConsts { public static final String MATERIAL_VOICE = "voice"; public static final String MATERIAL_IMAGE = "image"; public static final String MATERIAL_VIDEO = "video"; + + static { + MASS_ST_2_DESC.put(MASS_ST_SUCCESS, "发送成功"); + MASS_ST_2_DESC.put(MASS_ST_FAIL, "发送失败"); + MASS_ST_2_DESC.put(MASS_ST_涉嫌广告, "涉嫌广告"); + MASS_ST_2_DESC.put(MASS_ST_涉嫌政治, "涉嫌政治"); + MASS_ST_2_DESC.put(MASS_ST_涉嫌社会, "涉嫌社会"); + MASS_ST_2_DESC.put(MASS_ST_涉嫌色情, "涉嫌色情"); + MASS_ST_2_DESC.put(MASS_ST_涉嫌违法犯罪, "涉嫌违法犯罪"); + MASS_ST_2_DESC.put(MASS_ST_涉嫌欺诈, "涉嫌欺诈"); + MASS_ST_2_DESC.put(MASS_ST_涉嫌版权, "涉嫌版权"); + MASS_ST_2_DESC.put(MASS_ST_涉嫌互推_互相宣传, "涉嫌互推_互相宣传"); + MASS_ST_2_DESC.put(MASS_ST_涉嫌其他, "涉嫌其他"); + } } diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/api/WxMessageDuplicateChecker.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/api/WxMessageDuplicateChecker.java index 2bcc98f8..831458a4 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/api/WxMessageDuplicateChecker.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/api/WxMessageDuplicateChecker.java @@ -10,15 +10,16 @@ public interface WxMessageDuplicateChecker { /** *

公众号的排重方式

- * + *

*

普通消息:关于重试的消息排重,推荐使用msgid排重。文档参考

*

事件消息:关于重试的消息排重,推荐使用FromUserName + CreateTime 排重。文档参考

- * + *

*

企业号的排重方式

- * + *

* 官方文档完全没有写,参照公众号的方式排重。 - * + *

*

或者可以采取更简单的方式,如果有MsgId就用MsgId排重,如果没有就用FromUserName+CreateTime排重

+ * * @param messageId messageId需要根据上面讲的方式构造 * @return 如果是重复消息,返回true,否则返回false */ diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/api/WxMessageInMemoryDuplicateChecker.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/api/WxMessageInMemoryDuplicateChecker.java index 5b0f4e4e..e0d4d08c 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/api/WxMessageInMemoryDuplicateChecker.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/api/WxMessageInMemoryDuplicateChecker.java @@ -46,7 +46,8 @@ public class WxMessageInMemoryDuplicateChecker implements WxMessageDuplicateChec /** * WxMsgIdInMemoryDuplicateChecker构造函数 - * @param timeToLive 一个消息ID在内存的过期时间:毫秒 + * + * @param timeToLive 一个消息ID在内存的过期时间:毫秒 * @param clearPeriod 每隔多少周期检查消息ID是否过期:毫秒 */ public WxMessageInMemoryDuplicateChecker(Long timeToLive, Long clearPeriod) { diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/WxAccessToken.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/WxAccessToken.java index ce69a104..eedecb84 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/WxAccessToken.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/WxAccessToken.java @@ -8,9 +8,13 @@ public class WxAccessToken implements Serializable { private static final long serialVersionUID = 8709719312922168909L; private String accessToken; - + private int expiresIn = -1; + public static WxAccessToken fromJson(String json) { + return WxGsonBuilder.create().fromJson(json, WxAccessToken.class); + } + public String getAccessToken() { return accessToken; } @@ -27,8 +31,4 @@ public class WxAccessToken implements Serializable { this.expiresIn = expiresIn; } - public static WxAccessToken fromJson(String json) { - return WxGsonBuilder.create().fromJson(json, WxAccessToken.class); - } - } diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/WxJsapiSignature.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/WxJsapiSignature.java index 74f0b1b1..312860df 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/WxJsapiSignature.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/WxJsapiSignature.java @@ -9,7 +9,7 @@ public class WxJsapiSignature implements Serializable { private static final long serialVersionUID = -1116808193154384804L; private String appid; - + private String noncestr; private long timestamp; @@ -51,11 +51,11 @@ public class WxJsapiSignature implements Serializable { } public String getAppid() { - return appid; + return appid; } public void setAppid(String appid) { - this.appid = appid; + this.appid = appid; } } diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/WxMenu.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/WxMenu.java index d236f62b..3c530e85 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/WxMenu.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/WxMenu.java @@ -1,5 +1,7 @@ package me.chanjar.weixin.common.bean; +import me.chanjar.weixin.common.util.json.WxGsonBuilder; + import java.io.InputStream; import java.io.InputStreamReader; import java.io.Serializable; @@ -7,12 +9,10 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; -import me.chanjar.weixin.common.util.json.WxGsonBuilder; - /** * 企业号菜单 - * @author Daniel Qian * + * @author Daniel Qian */ public class WxMenu implements Serializable { @@ -21,26 +21,6 @@ public class WxMenu implements Serializable { private List buttons = new ArrayList(); private WxMenuRule matchRule; - - public List getButtons() { - return buttons; - } - - public void setButtons(List buttons) { - this.buttons = buttons; - } - - public WxMenuRule getMatchRule() { - return matchRule; - } - - public void setMatchRule(WxMenuRule matchRule) { - this.matchRule = matchRule; - } - - public String toJson() { - return WxGsonBuilder.create().toJson(this); - } /** * 要用 http://mp.weixin.qq.com/wiki/16/ff9b7b85220e1396ffa16794a9d95adc.html 格式来反序列化 @@ -58,11 +38,31 @@ public class WxMenu implements Serializable { return WxGsonBuilder.create().fromJson(new InputStreamReader(is, StandardCharsets.UTF_8), WxMenu.class); } + public List getButtons() { + return buttons; + } + + public void setButtons(List buttons) { + this.buttons = buttons; + } + + public WxMenuRule getMatchRule() { + return matchRule; + } + + public void setMatchRule(WxMenuRule matchRule) { + this.matchRule = matchRule; + } + + public String toJson() { + return WxGsonBuilder.create().toJson(this); + } + @Override public String toString() { return "WxMenu{" + - "buttons=" + buttons + - '}'; + "buttons=" + buttons + + '}'; } public static class WxMenuButton { @@ -71,7 +71,7 @@ public class WxMenu implements Serializable { private String name; private String key; private String url; - + private List subButtons = new ArrayList(); public String getType() { @@ -117,15 +117,15 @@ public class WxMenu implements Serializable { @Override public String toString() { return "WxMenuButton{" + - "type='" + type + '\'' + - ", name='" + name + '\'' + - ", key='" + key + '\'' + - ", url='" + url + '\'' + - ", subButtons=" + subButtons + - '}'; + "type='" + type + '\'' + + ", name='" + name + '\'' + + ", key='" + key + '\'' + + ", url='" + url + '\'' + + ", subButtons=" + subButtons + + '}'; } } - + public static class WxMenuRule { private String tagId; private String sex; @@ -134,51 +134,51 @@ public class WxMenu implements Serializable { private String city; private String clientPlatformType; private String language; - + public String getTagId() { return tagId; } - + public void setTagId(String tagId) { this.tagId = tagId; } - + public String getSex() { return sex; } - + public void setSex(String sex) { this.sex = sex; } - + public String getCountry() { return country; } - + public void setCountry(String country) { this.country = country; } - + public String getProvince() { return province; } - + public void setProvince(String province) { this.province = province; } - + public String getCity() { return city; } - + public void setCity(String city) { this.city = city; } - + public String getClientPlatformType() { return clientPlatformType; } - + public void setClientPlatformType(String clientPlatformType) { this.clientPlatformType = clientPlatformType; } @@ -194,15 +194,15 @@ public class WxMenu implements Serializable { @Override public String toString() { return "matchrule:{" + - "tag_id='" + tagId + '\'' + - ", sex='" + sex + '\'' + - ", country" + country + '\'' + - ", province" + province + '\'' + - ", city" + city + '\'' + - ", client_platform_type" + clientPlatformType + '\'' + - ", language" + language + '\'' + - "}"; + "tag_id='" + tagId + '\'' + + ", sex='" + sex + '\'' + + ", country" + country + '\'' + + ", province" + province + '\'' + + ", city" + city + '\'' + + ", client_platform_type" + clientPlatformType + '\'' + + ", language" + language + '\'' + + "}"; } } - + } diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/result/WxError.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/result/WxError.java index 12d4a3e6..070ec720 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/result/WxError.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/result/WxError.java @@ -6,19 +6,28 @@ import java.io.Serializable; /** * 微信错误码说明,请阅读: 全局返回码说明 - * @author Daniel Qian * + * @author Daniel Qian */ public class WxError implements Serializable { private static final long serialVersionUID = 7869786563361406291L; private int errorCode; - + private String errorMsg; private String json; - + + public static WxError fromJson(String json) { + WxError error = WxGsonBuilder.create().fromJson(json, WxError.class); + return error; + } + + public static Builder newBuilder() { + return new Builder(); + } + public int getErrorCode() { return errorCode; } @@ -43,43 +52,34 @@ public class WxError implements Serializable { this.json = json; } - public static WxError fromJson(String json) { - WxError error = WxGsonBuilder.create().fromJson(json, WxError.class); - return error; - } - @Override public String toString() { - if(json != null){ + if (json != null) { return json; } return "错误: Code=" + errorCode + ", Msg=" + errorMsg; } - public static Builder newBuilder(){ - return new Builder(); - } - - public static class Builder{ + public static class Builder { private int errorCode; private String errorMsg; - + public Builder setErrorCode(int errorCode) { this.errorCode = errorCode; return this; } - + public Builder setErrorMsg(String errorMsg) { this.errorMsg = errorMsg; return this; } - - public WxError build(){ + + public WxError build() { WxError wxError = new WxError(); wxError.setErrorCode(this.errorCode); wxError.setErrorMsg(this.errorMsg); return wxError; } - + } } diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/result/WxMediaUploadResult.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/result/WxMediaUploadResult.java index 500dc000..2e4b5fcd 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/result/WxMediaUploadResult.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/result/WxMediaUploadResult.java @@ -6,12 +6,16 @@ import java.io.Serializable; public class WxMediaUploadResult implements Serializable { private static final long serialVersionUID = 330834334738622341L; - + private String type; private String mediaId; private String thumbMediaId; private long createdAt; + public static WxMediaUploadResult fromJson(String json) { + return WxGsonBuilder.create().fromJson(json, WxMediaUploadResult.class); + } + public String getType() { return type; } @@ -44,14 +48,10 @@ public class WxMediaUploadResult implements Serializable { this.thumbMediaId = thumbMediaId; } - public static WxMediaUploadResult fromJson(String json) { - return WxGsonBuilder.create().fromJson(json, WxMediaUploadResult.class); - } - @Override public String toString() { return "WxUploadResult [type=" + type + ", media_id=" + mediaId + ", thumb_media_id=" + thumbMediaId - + ", created_at=" + createdAt + "]"; + + ", created_at=" + createdAt + "]"; } } diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/exception/WxErrorException.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/exception/WxErrorException.java index d1dc569b..7e2e1941 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/exception/WxErrorException.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/exception/WxErrorException.java @@ -5,7 +5,7 @@ import me.chanjar.weixin.common.bean.result.WxError; public class WxErrorException extends Exception { private static final long serialVersionUID = -6357149550353160810L; - + private WxError error; public WxErrorException(WxError error) { @@ -17,5 +17,5 @@ public class WxErrorException extends Exception { return error; } - + } diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/Constants.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/Constants.java index 33518f17..f30a0ae0 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/Constants.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/Constants.java @@ -26,6 +26,6 @@ package me.chanjar.weixin.common.session; public class Constants { - public static final String Package = "me.chanjar.weixin.common.session"; + public static final String Package = "me.chanjar.weixin.common.session"; } diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/InternalSession.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/InternalSession.java index 283672af..77d4d282 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/InternalSession.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/InternalSession.java @@ -8,6 +8,11 @@ public interface InternalSession { */ WxSession getSession(); + /** + * Return the isValid flag for this session. + */ + boolean isValid(); + /** * Set the isValid flag for this session. * @@ -15,11 +20,6 @@ public interface InternalSession { */ void setValid(boolean isValid); - /** - * Return the isValid flag for this session. - */ - boolean isValid(); - /** * Return the session identifier for this session. */ diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/InternalSessionManager.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/InternalSessionManager.java index bb9a8dca..a92e1071 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/InternalSessionManager.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/InternalSessionManager.java @@ -7,11 +7,10 @@ public interface InternalSessionManager { * specified session id (if any); otherwise return null. * * @param id The session id for the session to be returned - * - * @exception IllegalStateException if a new session cannot be - * instantiated for any reason - * @exception java.io.IOException if an input/output error occurs while - * processing this request + * @throws IllegalStateException if a new session cannot be + * instantiated for any reason + * @throws java.io.IOException if an input/output error occurs while + * processing this request */ InternalSession findSession(String id); @@ -23,10 +22,10 @@ public interface InternalSessionManager { * null. * * @param sessionId The session id which should be used to create the - * new session; if null, a new session id will be - * generated - * @exception IllegalStateException if a new session cannot be - * instantiated for any reason + * new session; if null, a new session id will be + * generated + * @throws IllegalStateException if a new session cannot be + * instantiated for any reason */ InternalSession createSession(String sessionId); @@ -40,8 +39,8 @@ public interface InternalSessionManager { /** * Remove this Session from the active Sessions for this Manager. * - * @param session Session to be removed - * @param update Should the expiration statistics be updated + * @param session Session to be removed + * @param update Should the expiration statistics be updated */ void remove(InternalSession session, boolean update); @@ -59,6 +58,7 @@ public interface InternalSessionManager { * @return number of sessions active */ int getActiveSessions(); + /** * Get a session from the recycled ones or create a new empty one. * The PersistentManager manager does not need to create session data @@ -88,6 +88,7 @@ public interface InternalSessionManager { * 要和{@link #setBackgroundProcessorDelay(int)}联合起来看 * 如果把这个数字设置为6(默认),那么就是说manager要等待 6 * backgroundProcessorDay的时间才会清理过期session * + * * @param processExpiresFrequency the new manager checks frequency */ void setProcessExpiresFrequency(int processExpiresFrequency); @@ -97,6 +98,7 @@ public interface InternalSessionManager { * Set the manager background processor delay * 设置manager sleep几秒,尝试执行一次background操作(清理过期session) * + * * @param backgroundProcessorDelay */ void setBackgroundProcessorDelay(int backgroundProcessorDelay); @@ -106,6 +108,7 @@ public interface InternalSessionManager { * Set the maximum number of active Sessions allowed, or -1 for * no limit. * 设置最大活跃session数,默认无限 + * * @param max The new maximum number of sessions */ void setMaxActiveSessions(int max); diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/LocalStrings.properties b/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/LocalStrings.properties index ede528f7..d3ded84b 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/LocalStrings.properties +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/LocalStrings.properties @@ -12,7 +12,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - applicationSession.session.ise=invalid session state applicationSession.value.iae=null value fileStore.saving=Saving Session {0} to file {1} diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/StandardSession.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/StandardSession.java index b482c2b7..9212c619 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/StandardSession.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/StandardSession.java @@ -12,17 +12,68 @@ public class StandardSession implements WxSession, InternalSession { * The string manager for this package. */ protected static final StringManager sm = - StringManager.getManager(Constants.Package); - + StringManager.getManager(Constants.Package); + /** + * Type array. + */ + protected static final String EMPTY_ARRAY[] = new String[0]; // ------------------------------ WxSession protected Map attributes = new ConcurrentHashMap(); + /** + * The session identifier of this Session. + */ + protected String id = null; + /** + * Flag indicating whether this session is valid or not. + */ + protected volatile boolean isValid = false; + /** + * We are currently processing a session expiration, so bypass + * certain IllegalStateException tests. NOTE: This value is not + * included in the serialized version of this object. + */ + protected transient volatile boolean expiring = false; + /** + * The Manager with which this Session is associated. + */ + protected transient InternalSessionManager manager = null; + + // ------------------------------ InternalSession + /** + * The time this session was created, in milliseconds since midnight, + * January 1, 1970 GMT. + */ + protected long creationTime = 0L; + /** + * The current accessed time for this session. + */ + protected volatile long thisAccessedTime = creationTime; + /** + * The default maximum inactive interval for Sessions created by + * this Manager. + */ + protected int maxInactiveInterval = 30 * 60; + /** + * The facade associated with this session. NOTE: This value is not + * included in the serialized version of this object. + */ + protected transient StandardSessionFacade facade = null; + /** + * The access count for this session. + */ + protected transient AtomicInteger accessCount = null; + + public StandardSession(InternalSessionManager manager) { + this.manager = manager; + this.accessCount = new AtomicInteger(); + } @Override public Object getAttribute(String name) { if (!isValidInternal()) throw new IllegalStateException - (sm.getString("sessionImpl.getAttribute.ise")); + (sm.getString("sessionImpl.getAttribute.ise")); if (name == null) return null; @@ -33,7 +84,7 @@ public class StandardSession implements WxSession, InternalSession { public Enumeration getAttributeNames() { if (!isValidInternal()) throw new IllegalStateException - (sm.getString("sessionImpl.getAttributeNames.ise")); + (sm.getString("sessionImpl.getAttributeNames.ise")); Set names = new HashSet(); names.addAll(attributes.keySet()); @@ -45,7 +96,7 @@ public class StandardSession implements WxSession, InternalSession { // Name cannot be null if (name == null) throw new IllegalArgumentException - (sm.getString("sessionImpl.setAttribute.namenull")); + (sm.getString("sessionImpl.setAttribute.namenull")); // Null value is the same as removeAttribute() if (value == null) { @@ -56,97 +107,32 @@ public class StandardSession implements WxSession, InternalSession { // Validate our current state if (!isValidInternal()) throw new IllegalStateException(sm.getString( - "sessionImpl.setAttribute.ise", getIdInternal())); + "sessionImpl.setAttribute.ise", getIdInternal())); attributes.put(name, value); } - @Override public void removeAttribute(String name) { removeAttributeInternal(name); } - @Override public void invalidate() { if (!isValidInternal()) throw new IllegalStateException - (sm.getString("sessionImpl.invalidate.ise")); + (sm.getString("sessionImpl.invalidate.ise")); // Cause this session to expire expire(); } - // ------------------------------ InternalSession - /** - * The session identifier of this Session. - */ - protected String id = null; - - /** - * Flag indicating whether this session is valid or not. - */ - protected volatile boolean isValid = false; - - /** - * We are currently processing a session expiration, so bypass - * certain IllegalStateException tests. NOTE: This value is not - * included in the serialized version of this object. - */ - protected transient volatile boolean expiring = false; - - /** - * The Manager with which this Session is associated. - */ - protected transient InternalSessionManager manager = null; - - /** - * Type array. - */ - protected static final String EMPTY_ARRAY[] = new String[0]; - - /** - * The time this session was created, in milliseconds since midnight, - * January 1, 1970 GMT. - */ - protected long creationTime = 0L; - - /** - * The current accessed time for this session. - */ - protected volatile long thisAccessedTime = creationTime; - - /** - * The default maximum inactive interval for Sessions created by - * this Manager. - */ - protected int maxInactiveInterval = 30 * 60; - - /** - * The facade associated with this session. NOTE: This value is not - * included in the serialized version of this object. - */ - protected transient StandardSessionFacade facade = null; - - /** - * The access count for this session. - */ - protected transient AtomicInteger accessCount = null; - - - public StandardSession(InternalSessionManager manager) { - this.manager = manager; - this.accessCount = new AtomicInteger(); - } - - @Override public WxSession getSession() { - if (facade == null){ + if (facade == null) { facade = new StandardSessionFacade(this); } return (facade); @@ -161,16 +147,6 @@ public class StandardSession implements WxSession, InternalSession { return this.isValid; } - /** - * Set the isValid flag for this session. - * - * @param isValid The new value for the isValid flag - */ - @Override - public void setValid(boolean isValid) { - this.isValid = isValid; - } - @Override public boolean isValid() { if (!this.isValid) { @@ -197,6 +173,16 @@ public class StandardSession implements WxSession, InternalSession { return this.isValid; } + /** + * Set the isValid flag for this session. + * + * @param isValid The new value for the isValid flag + */ + @Override + public void setValid(boolean isValid) { + this.isValid = isValid; + } + @Override public String getIdInternal() { return (this.id); diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/StandardSessionManager.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/StandardSessionManager.java index f3a02695..cd14a1fd 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/StandardSessionManager.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/StandardSessionManager.java @@ -13,60 +13,26 @@ import java.util.concurrent.atomic.AtomicBoolean; */ public class StandardSessionManager implements WxSessionManager, InternalSessionManager { - protected final Logger log = LoggerFactory.getLogger(StandardSessionManager.class); - protected static final StringManager sm = - StringManager.getManager(Constants.Package); - + StringManager.getManager(Constants.Package); /** - * The set of currently active Sessions for this Manager, keyed by - * session identifier. + * The descriptive name of this Manager implementation (for logging). */ - protected Map sessions = new ConcurrentHashMap(); - - @Override - public WxSession getSession(String sessionId) { - return getSession(sessionId, true); - } - - @Override - public WxSession getSession(String sessionId, boolean create) { - if (sessionId == null) { - throw new IllegalStateException - (sm.getString("sessionManagerImpl.getSession.ise")); - } - - InternalSession session = findSession(sessionId); - if ((session != null) && !session.isValid()) { - session = null; - } - if (session != null) { - session.access(); - return session.getSession(); - } - - // Create a new session if requested and the response is not committed - if (!create) { - return (null); - } - - session = createSession(sessionId); - - if (session == null) { - return null; - } - - session.access(); - return session.getSession(); - } + private static final String name = "SessionManagerImpl"; + protected final Logger log = LoggerFactory.getLogger(StandardSessionManager.class); + private final Object maxActiveUpdateLock = new Object(); + /** + * 后台清理线程是否已经开启 + */ + private final AtomicBoolean backgroundProcessStarted = new AtomicBoolean(false); // -------------------------------------- InternalSessionManager /** - * The descriptive name of this Manager implementation (for logging). + * The set of currently active Sessions for this Manager, keyed by + * session identifier. */ - private static final String name = "SessionManagerImpl"; - + protected Map sessions = new ConcurrentHashMap(); /** * The maximum number of active Sessions allowed, or -1 for no limit. */ @@ -84,22 +50,13 @@ public class StandardSessionManager implements WxSessionManager, InternalSession protected int maxInactiveInterval = 30 * 60; // Number of sessions created by this manager - protected long sessionCounter=0; - - protected volatile int maxActive=0; - - private final Object maxActiveUpdateLock = new Object(); + protected long sessionCounter = 0; + protected volatile int maxActive = 0; /** * Processing time during session expiration. */ protected long processingTime = 0; - - /** - * Iteration count for background processing. - */ - private int count = 0; - /** * Frequency of the session expiration, and related manager operations. * Manager operations will be done once for the specified amount of @@ -107,16 +64,50 @@ public class StandardSessionManager implements WxSessionManager, InternalSession * checks will occur). */ protected int processExpiresFrequency = 6; - /** * background processor delay in seconds */ protected int backgroundProcessorDelay = 10; - /** - * 后台清理线程是否已经开启 + * Iteration count for background processing. */ - private final AtomicBoolean backgroundProcessStarted = new AtomicBoolean(false); + private int count = 0; + + @Override + public WxSession getSession(String sessionId) { + return getSession(sessionId, true); + } + + @Override + public WxSession getSession(String sessionId, boolean create) { + if (sessionId == null) { + throw new IllegalStateException + (sm.getString("sessionManagerImpl.getSession.ise")); + } + + InternalSession session = findSession(sessionId); + if ((session != null) && !session.isValid()) { + session = null; + } + if (session != null) { + session.access(); + return session.getSession(); + } + + // Create a new session if requested and the response is not committed + if (!create) { + return (null); + } + + session = createSession(sessionId); + + if (session == null) { + return null; + } + + session.access(); + return session.getSession(); + } @Override public void remove(InternalSession session) { @@ -131,7 +122,6 @@ public class StandardSessionManager implements WxSessionManager, InternalSession } - @Override public InternalSession findSession(String id) { @@ -145,15 +135,15 @@ public class StandardSessionManager implements WxSessionManager, InternalSession public InternalSession createSession(String sessionId) { if (sessionId == null) { throw new IllegalStateException - (sm.getString("sessionManagerImpl.createSession.ise")); + (sm.getString("sessionManagerImpl.createSession.ise")); } if ((maxActiveSessions >= 0) && - (getActiveSessions() >= maxActiveSessions)) { + (getActiveSessions() >= maxActiveSessions)) { rejectedSessions++; throw new TooManyActiveSessionsException( - sm.getString("sessionManagerImpl.createSession.tmase"), - maxActiveSessions); + sm.getString("sessionManagerImpl.createSession.tmase"), + maxActiveSessions); } // Recycle or create a Session instance @@ -216,14 +206,14 @@ public class StandardSessionManager implements WxSessionManager, InternalSession sessions.put(session.getIdInternal(), session); int size = getActiveSessions(); - if( size > maxActive ) { - synchronized(maxActiveUpdateLock) { - if( size > maxActive ) { + if (size > maxActive) { + synchronized (maxActiveUpdateLock) { + if (size > maxActive) { maxActive = size; } } } - + } /** @@ -251,19 +241,19 @@ public class StandardSessionManager implements WxSessionManager, InternalSession long timeNow = System.currentTimeMillis(); InternalSession sessions[] = findSessions(); - int expireHere = 0 ; + int expireHere = 0; - if(log.isDebugEnabled()) + if (log.isDebugEnabled()) log.debug("Start expire sessions {} at {} sessioncount {}", getName(), timeNow, sessions.length); for (int i = 0; i < sessions.length; i++) { - if (sessions[i]!=null && !sessions[i].isValid()) { + if (sessions[i] != null && !sessions[i].isValid()) { expireHere++; } } long timeEnd = System.currentTimeMillis(); - if(log.isDebugEnabled()) + if (log.isDebugEnabled()) log.debug("End expire sessions {} processingTime {} expired sessions: {}", getName(), timeEnd - timeNow, expireHere); - processingTime += ( timeEnd - timeNow ); + processingTime += (timeEnd - timeNow); } diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/TooManyActiveSessionsException.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/TooManyActiveSessionsException.java index bd0c0f2b..69fd89f8 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/TooManyActiveSessionsException.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/TooManyActiveSessionsException.java @@ -21,37 +21,34 @@ package me.chanjar.weixin.common.session; * reached and the server is refusing to create any new sessions. */ public class TooManyActiveSessionsException - extends IllegalStateException -{ - private static final long serialVersionUID = 1L; + extends IllegalStateException { + private static final long serialVersionUID = 1L; - /** - * The maximum number of active sessions the server will tolerate. - */ - private final int maxActiveSessions; + /** + * The maximum number of active sessions the server will tolerate. + */ + private final int maxActiveSessions; - /** - * Creates a new TooManyActiveSessionsException. - * - * @param message A description for the exception. - * @param maxActive The maximum number of active sessions allowed by the - * session manager. - */ - public TooManyActiveSessionsException(String message, - int maxActive) - { - super(message); - - maxActiveSessions = maxActive; - } - - /** - * Gets the maximum number of sessions allowed by the session manager. - * - * @return The maximum number of sessions allowed by the session manager. - */ - public int getMaxActiveSessions() - { - return maxActiveSessions; - } + /** + * Creates a new TooManyActiveSessionsException. + * + * @param message A description for the exception. + * @param maxActive The maximum number of active sessions allowed by the + * session manager. + */ + public TooManyActiveSessionsException(String message, + int maxActive) { + super(message); + + maxActiveSessions = maxActive; + } + + /** + * Gets the maximum number of sessions allowed by the session manager. + * + * @return The maximum number of sessions allowed by the session manager. + */ + public int getMaxActiveSessions() { + return maxActiveSessions; + } } diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/StringUtils.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/StringUtils.java index 4234a8c8..49acaeea 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/StringUtils.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/StringUtils.java @@ -7,7 +7,7 @@ public class StringUtils { /** *

Checks if a CharSequence is whitespace, empty ("") or null.

- * + *

*

    * StringUtils.isBlank(null)      = true
    * StringUtils.isBlank("")        = true
@@ -16,9 +16,8 @@ public class StringUtils {
    * StringUtils.isBlank("  bob  ") = false
    * 
* - * @param cs the CharSequence to check, may be null + * @param cs the CharSequence to check, may be null * @return {@code true} if the CharSequence is null, empty or whitespace - * @since 2.0 * @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence) */ public static boolean isBlank(CharSequence cs) { @@ -36,7 +35,7 @@ public class StringUtils { /** *

Checks if a CharSequence is not empty (""), not null and not whitespace only.

- * + *

*

    * StringUtils.isNotBlank(null)      = false
    * StringUtils.isNotBlank("")        = false
@@ -45,10 +44,9 @@ public class StringUtils {
    * StringUtils.isNotBlank("  bob  ") = true
    * 
* - * @param cs the CharSequence to check, may be null + * @param cs the CharSequence to check, may be null * @return {@code true} if the CharSequence is - * not empty and not null and not whitespace - * @since 2.0 + * not empty and not null and not whitespace * @since 3.0 Changed signature from isNotBlank(String) to isNotBlank(CharSequence) */ public static boolean isNotBlank(CharSequence cs) { @@ -57,7 +55,7 @@ public class StringUtils { /** *

Checks if a CharSequence is empty ("") or null.

- * + *

*

    * StringUtils.isEmpty(null)      = true
    * StringUtils.isEmpty("")        = true
@@ -65,12 +63,12 @@ public class StringUtils {
    * StringUtils.isEmpty("bob")     = false
    * StringUtils.isEmpty("  bob  ") = false
    * 
- * + *

*

NOTE: This method changed in Lang version 2.0. * It no longer trims the CharSequence. * That functionality is available in isBlank().

* - * @param cs the CharSequence to check, may be null + * @param cs the CharSequence to check, may be null * @return {@code true} if the CharSequence is empty or null * @since 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence) */ @@ -80,7 +78,7 @@ public class StringUtils { /** *

Checks if a CharSequence is not empty ("") and not null.

- * + *

*

    * StringUtils.isNotEmpty(null)      = false
    * StringUtils.isNotEmpty("")        = false
@@ -89,7 +87,7 @@ public class StringUtils {
    * StringUtils.isNotEmpty("  bob  ") = true
    * 
* - * @param cs the CharSequence to check, may be null + * @param cs the CharSequence to check, may be null * @return {@code true} if the CharSequence is not empty and not null * @since 3.0 Changed signature from isNotEmpty(String) to isNotEmpty(CharSequence) */ diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/ByteGroup.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/ByteGroup.java index de777dc9..6cd4a5ef 100755 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/ByteGroup.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/ByteGroup.java @@ -3,24 +3,24 @@ package me.chanjar.weixin.common.util.crypto; import java.util.ArrayList; public class ByteGroup { - ArrayList byteContainer = new ArrayList(); + ArrayList byteContainer = new ArrayList(); public byte[] toBytes() { - byte[] bytes = new byte[byteContainer.size()]; - for (int i = 0; i < byteContainer.size(); i++) { - bytes[i] = byteContainer.get(i); - } - return bytes; - } + byte[] bytes = new byte[byteContainer.size()]; + for (int i = 0; i < byteContainer.size(); i++) { + bytes[i] = byteContainer.get(i); + } + return bytes; + } - public ByteGroup addBytes(byte[] bytes) { - for (byte b : bytes) { - byteContainer.add(b); - } - return this; - } + public ByteGroup addBytes(byte[] bytes) { + for (byte b : bytes) { + byteContainer.add(b); + } + return this; + } - public int size() { - return byteContainer.size(); - } + public int size() { + return byteContainer.size(); + } } diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/PKCS7Encoder.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/PKCS7Encoder.java index 35771c47..2a01aae0 100755 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/PKCS7Encoder.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/PKCS7Encoder.java @@ -1,6 +1,6 @@ /** * 对公众平台发送给公众账号的消息加解密示例代码. - * + * * @copyright Copyright (c) 1998-2014 Tencent Inc. */ @@ -16,53 +16,53 @@ import java.util.Arrays; */ public class PKCS7Encoder { - private static final Charset CHARSET = Charset.forName("utf-8"); - private static final int BLOCK_SIZE = 32; + private static final Charset CHARSET = Charset.forName("utf-8"); + private static final int BLOCK_SIZE = 32; - /** - * 获得对明文进行补位填充的字节. - * - * @param count 需要进行填充补位操作的明文字节个数 - * @return 补齐用的字节数组 - */ - public static byte[] encode(int count) { - // 计算需要填充的位数 - int amountToPad = BLOCK_SIZE - (count % BLOCK_SIZE); - if (amountToPad == 0) { - amountToPad = BLOCK_SIZE; - } - // 获得补位所用的字符 - char padChr = chr(amountToPad); - String tmp = new String(); - for (int index = 0; index < amountToPad; index++) { - tmp += padChr; - } - return tmp.getBytes(CHARSET); - } + /** + * 获得对明文进行补位填充的字节. + * + * @param count 需要进行填充补位操作的明文字节个数 + * @return 补齐用的字节数组 + */ + public static byte[] encode(int count) { + // 计算需要填充的位数 + int amountToPad = BLOCK_SIZE - (count % BLOCK_SIZE); + if (amountToPad == 0) { + amountToPad = BLOCK_SIZE; + } + // 获得补位所用的字符 + char padChr = chr(amountToPad); + String tmp = new String(); + for (int index = 0; index < amountToPad; index++) { + tmp += padChr; + } + return tmp.getBytes(CHARSET); + } - /** - * 删除解密后明文的补位字符 - * - * @param decrypted 解密后的明文 - * @return 删除补位字符后的明文 - */ - public static byte[] decode(byte[] decrypted) { - int pad = (int) decrypted[decrypted.length - 1]; - if (pad < 1 || pad > 32) { - pad = 0; - } - return Arrays.copyOfRange(decrypted, 0, decrypted.length - pad); - } + /** + * 删除解密后明文的补位字符 + * + * @param decrypted 解密后的明文 + * @return 删除补位字符后的明文 + */ + public static byte[] decode(byte[] decrypted) { + int pad = (int) decrypted[decrypted.length - 1]; + if (pad < 1 || pad > 32) { + pad = 0; + } + return Arrays.copyOfRange(decrypted, 0, decrypted.length - pad); + } - /** - * 将数字转化成ASCII码对应的字符,用于对明文进行补码 - * - * @param a 需要转化的数字 - * @return 转化得到的字符 - */ - public static char chr(int a) { - byte target = (byte) (a & 0xFF); - return (char) target; - } + /** + * 将数字转化成ASCII码对应的字符,用于对明文进行补码 + * + * @param a 需要转化的数字 + * @return 转化得到的字符 + */ + public static char chr(int a) { + byte target = (byte) (a & 0xFF); + return (char) target; + } } diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/SHA1.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/SHA1.java index 38a6c28d..4f7503c6 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/SHA1.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/SHA1.java @@ -1,10 +1,10 @@ package me.chanjar.weixin.common.util.crypto; +import org.apache.commons.codec.digest.DigestUtils; + import java.security.NoSuchAlgorithmException; import java.util.Arrays; -import org.apache.commons.codec.digest.DigestUtils; - /** * Created by Daniel Qian on 14/10/19. */ @@ -37,4 +37,4 @@ public class SHA1 { } return DigestUtils.sha1Hex(sb.toString()); } - } +} diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/WxCryptUtil.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/WxCryptUtil.java index 87413334..c1341e7b 100755 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/WxCryptUtil.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/WxCryptUtil.java @@ -2,6 +2,10 @@ * 对公众平台发送给公众账号的消息加解密示例代码. * * @copyright Copyright (c) 1998-2014 Tencent Inc. + *

+ * 针对org.apache.commons.codec.binary.Base64, + * 需要导入架包commons-codec-1.9(或commons-codec-1.8等其他版本) + * 官方下载地址:http://commons.apache.org/proper/commons-codec/download_codec.cgi */ // ------------------------------------------------------------------------ @@ -62,12 +66,51 @@ public class WxCryptUtil { * @param appidOrCorpid 公众平台appid/corpid */ public WxCryptUtil(String token, String encodingAesKey, - String appidOrCorpid) { + String appidOrCorpid) { this.token = token; this.appidOrCorpid = appidOrCorpid; this.aesKey = Base64.decodeBase64(encodingAesKey + "="); } + /** + * 微信公众号支付签名算法(详见:http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=4_3) + * @param packageParams 原始参数 + * @param signKey 加密Key(即 商户Key) + * @return 签名字符串 + */ + public static String createSign(Map packageParams, + String signKey) { + SortedMap sortedMap = new TreeMap(); + sortedMap.putAll(packageParams); + + List keys = new ArrayList(packageParams.keySet()); + Collections.sort(keys); + + StringBuffer toSign = new StringBuffer(); + for (String key : keys) { + String value = packageParams.get(key); + if (null != value && !"".equals(value) && !"sign".equals(key) + && !"key".equals(key)) { + toSign.append(key + "=" + value + "&"); + } + } + toSign.append("key=" + signKey); + String sign = DigestUtils.md5Hex(toSign.toString()).toUpperCase(); + return sign; + } + + static String extractEncryptPart(String xml) { + try { + DocumentBuilder db = builderLocal.get(); + Document document = db.parse(new InputSource(new StringReader(xml))); + + Element root = document.getDocumentElement(); + return root.getElementsByTagName("Encrypt").item(0).getTextContent(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + /** * 将公众平台回复用户的消息加密打包. *

    @@ -107,7 +150,7 @@ public class WxCryptUtil { byte[] randomStringBytes = randomStr.getBytes(CHARSET); byte[] plainTextBytes = plainText.getBytes(CHARSET); byte[] bytesOfSizeInNetworkOrder = number2BytesInNetworkOrder( - plainTextBytes.length); + plainTextBytes.length); byte[] appIdBytes = appidOrCorpid.getBytes(CHARSET); // randomStr + networkBytesOrder + text + appid @@ -157,7 +200,7 @@ public class WxCryptUtil { * @return 解密后的原文 */ public String decrypt(String msgSignature, String timeStamp, String nonce, - String encryptedXml) { + String encryptedXml) { // 密钥,公众账号的app corpSecret // 提取密文 String cipherText = extractEncryptPart(encryptedXml); @@ -190,7 +233,7 @@ public class WxCryptUtil { Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); SecretKeySpec key_spec = new SecretKeySpec(aesKey, "AES"); IvParameterSpec iv = new IvParameterSpec( - Arrays.copyOfRange(aesKey, 0, 16)); + Arrays.copyOfRange(aesKey, 0, 16)); cipher.init(Cipher.DECRYPT_MODE, key_spec, iv); // 使用BASE64对密文进行解码 @@ -213,9 +256,9 @@ public class WxCryptUtil { int xmlLength = bytesNetworkOrder2Number(networkOrder); xmlContent = new String(Arrays.copyOfRange(bytes, 20, 20 + xmlLength), - CHARSET); + CHARSET); from_appid = new String( - Arrays.copyOfRange(bytes, 20 + xmlLength, bytes.length), CHARSET); + Arrays.copyOfRange(bytes, 20 + xmlLength, bytes.length), CHARSET); } catch (Exception e) { throw new RuntimeException(e); } @@ -229,33 +272,6 @@ public class WxCryptUtil { } - /** - * 微信公众号支付签名算法(详见:http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=4_3) - * @param packageParams 原始参数 - * @param signKey 加密Key(即 商户Key) - * @return 签名字符串 - */ - public static String createSign(Map packageParams, - String signKey) { - SortedMap sortedMap = new TreeMap(); - sortedMap.putAll(packageParams); - - List keys = new ArrayList(packageParams.keySet()); - Collections.sort(keys); - - StringBuffer toSign = new StringBuffer(); - for (String key : keys) { - String value = packageParams.get(key); - if (null != value && !"".equals(value) && !"sign".equals(key) - && !"key".equals(key)) { - toSign.append(key + "=" + value + "&"); - } - } - toSign.append("key=" + signKey); - String sign = DigestUtils.md5Hex(toSign.toString()).toUpperCase(); - return sign; - } - /** * 将一个数字转换成生成4个字节的网络字节序bytes数组 * @@ -308,24 +324,12 @@ public class WxCryptUtil { * @return 生成的xml字符串 */ private String generateXml(String encrypt, String signature, String timestamp, - String nonce) { + String nonce) { String format = "\n" + "\n" - + "\n" - + "%3$s\n" + "\n" - + ""; + + "\n" + + "%3$s\n" + "\n" + + ""; return String.format(format, encrypt, signature, timestamp, nonce); } - static String extractEncryptPart(String xml) { - try { - DocumentBuilder db = builderLocal.get(); - Document document = db.parse(new InputSource(new StringReader(xml))); - - Element root = document.getDocumentElement(); - return root.getElementsByTagName("Encrypt").item(0).getTextContent(); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - } diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/fs/FileUtils.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/fs/FileUtils.java index 35c50e3f..706ced39 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/fs/FileUtils.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/fs/FileUtils.java @@ -10,17 +10,18 @@ public class FileUtils { /** * 创建临时文件 + * * @param inputStream - * @param name 文件名 - * @param ext 扩展名 - * @param tmpDirFile 临时文件夹目录 + * @param name 文件名 + * @param ext 扩展名 + * @param tmpDirFile 临时文件夹目录 */ public static File createTmpFile(InputStream inputStream, String name, String ext, File tmpDirFile) throws IOException { FileOutputStream fos = null; try { File tmpFile; if (tmpDirFile == null) { - tmpFile = File.createTempFile(name, '.' + ext); + tmpFile = File.createTempFile(name, '.' + ext); } else { tmpFile = File.createTempFile(name, '.' + ext, tmpDirFile); } @@ -51,12 +52,13 @@ public class FileUtils { /** * 创建临时文件 + * * @param inputStream - * @param name 文件名 - * @param ext 扩展名 + * @param name 文件名 + * @param ext 扩展名 */ public static File createTmpFile(InputStream inputStream, String name, String ext) throws IOException { return createTmpFile(inputStream, name, ext, null); } - + } diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/ApacheHttpClientBuilder.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/ApacheHttpClientBuilder.java index c821d86c..8bd900ca 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/ApacheHttpClientBuilder.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/ApacheHttpClientBuilder.java @@ -10,36 +10,42 @@ public interface ApacheHttpClientBuilder { /** * 构建httpclient实例 + * * @return new instance of CloseableHttpClient */ CloseableHttpClient build(); /** * 代理服务器地址 + * * @param httpProxyHost */ ApacheHttpClientBuilder httpProxyHost(String httpProxyHost); /** * 代理服务器端口 + * * @param httpProxyPort */ ApacheHttpClientBuilder httpProxyPort(int httpProxyPort); /** * 代理服务器用户名 + * * @param httpProxyUsername */ ApacheHttpClientBuilder httpProxyUsername(String httpProxyUsername); /** * 代理服务器密码 + * * @param httpProxyPassword */ ApacheHttpClientBuilder httpProxyPassword(String httpProxyPassword); /** * ssl连接socket工厂 + * * @param sslConnectionSocketFactory */ ApacheHttpClientBuilder sslConnectionSocketFactory(SSLConnectionSocketFactory sslConnectionSocketFactory); diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/DefaultApacheHttpHttpClientBuilder.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/DefaultApacheHttpHttpClientBuilder.java index aa20fa0b..d4701fca 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/DefaultApacheHttpHttpClientBuilder.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/DefaultApacheHttpHttpClientBuilder.java @@ -94,7 +94,7 @@ public class DefaultApacheHttpHttpClientBuilder implements ApacheHttpClientBuild return this; } - public ApacheHttpClientBuilder sslConnectionSocketFactory(SSLConnectionSocketFactory sslConnectionSocketFactory){ + public ApacheHttpClientBuilder sslConnectionSocketFactory(SSLConnectionSocketFactory sslConnectionSocketFactory) { this.sslConnectionSocketFactory = sslConnectionSocketFactory; return this; } @@ -103,18 +103,18 @@ public class DefaultApacheHttpHttpClientBuilder implements ApacheHttpClientBuild return idleConnectionMonitorThread; } - private void prepare(){ + private void prepare() { Registry registry = RegistryBuilder.create() - .register("http", plainConnectionSocketFactory) - .register("https", sslConnectionSocketFactory) - .build(); + .register("http", plainConnectionSocketFactory) + .register("https", sslConnectionSocketFactory) + .build(); connectionManager = new PoolingHttpClientConnectionManager(registry); connectionManager.setMaxTotal(maxTotalConn); connectionManager.setDefaultMaxPerRoute(maxConnPerHost); connectionManager.setDefaultSocketConfig( - SocketConfig.copy(SocketConfig.DEFAULT) - .setSoTimeout(soTimeout) - .build() + SocketConfig.copy(SocketConfig.DEFAULT) + .setSoTimeout(soTimeout) + .build() ); idleConnectionMonitorThread = new IdleConnectionMonitorThread(connectionManager, idleConnTimeout, checkWaitTime); @@ -122,22 +122,22 @@ public class DefaultApacheHttpHttpClientBuilder implements ApacheHttpClientBuild idleConnectionMonitorThread.start(); httpClientBuilder = HttpClients.custom() - .setConnectionManager(connectionManager) - .setDefaultRequestConfig( - RequestConfig.custom() - .setSocketTimeout(soTimeout) - .setConnectTimeout(connectionTimeout) - .setConnectionRequestTimeout(connectionRequestTimeout) - .build() - ) - .setRetryHandler(httpRequestRetryHandler); + .setConnectionManager(connectionManager) + .setDefaultRequestConfig( + RequestConfig.custom() + .setSocketTimeout(soTimeout) + .setConnectTimeout(connectionTimeout) + .setConnectionRequestTimeout(connectionRequestTimeout) + .build() + ) + .setRetryHandler(httpRequestRetryHandler); if (StringUtils.isNotBlank(httpProxyHost) && StringUtils.isNotBlank(httpProxyUsername)) { // 使用代理服务器 需要用户认证的代理服务器 CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( - new AuthScope(httpProxyHost, httpProxyPort), - new UsernamePasswordCredentials(httpProxyUsername, httpProxyPassword)); + new AuthScope(httpProxyHost, httpProxyPort), + new UsernamePasswordCredentials(httpProxyUsername, httpProxyPassword)); httpClientBuilder.setDefaultCredentialsProvider(credsProvider); } @@ -148,7 +148,7 @@ public class DefaultApacheHttpHttpClientBuilder implements ApacheHttpClientBuild } public CloseableHttpClient build() { - if(!prepared){ + if (!prepared) { prepare(); prepared = true; } diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/InputStreamResponseHandler.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/InputStreamResponseHandler.java index 1bf13b9c..6433c472 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/InputStreamResponseHandler.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/InputStreamResponseHandler.java @@ -1,8 +1,5 @@ package me.chanjar.weixin.common.util.http; -import java.io.IOException; -import java.io.InputStream; - import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.StatusLine; @@ -10,10 +7,13 @@ import org.apache.http.client.HttpResponseException; import org.apache.http.client.ResponseHandler; import org.apache.http.util.EntityUtils; +import java.io.IOException; +import java.io.InputStream; + public class InputStreamResponseHandler implements ResponseHandler { public static final ResponseHandler INSTANCE = new InputStreamResponseHandler(); - + public InputStream handleResponse(final HttpResponse response) throws IOException { final StatusLine statusLine = response.getStatusLine(); final HttpEntity entity = response.getEntity(); diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/JoddGetRequestExecutor.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/JoddGetRequestExecutor.java index 10011304..a2ff7a56 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/JoddGetRequestExecutor.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/JoddGetRequestExecutor.java @@ -18,39 +18,39 @@ import java.io.IOException; */ public class JoddGetRequestExecutor implements RequestExecutor { - @Override - public String execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, - String queryParam) throws WxErrorException, IOException { - if (queryParam != null) { - if (uri.indexOf('?') == -1) { - uri += '?'; - } - uri += uri.endsWith("?") ? queryParam : '&' + queryParam; - } - - SocketHttpConnectionProvider provider = new SocketHttpConnectionProvider(); - - if (httpProxy != null) { - ProxyInfo proxyInfoObj = new ProxyInfo( - ProxyInfo.ProxyType.HTTP, - httpProxy.getHostName(), - httpProxy.getPort(), "", ""); - provider.useProxy(proxyInfoObj); - } - - HttpRequest request = HttpRequest.get(uri); - request.method("GET"); - request.charset("UTF-8"); - - HttpResponse response = request.open(provider).send(); - response.charset("UTF-8"); - String result = response.bodyText(); - - WxError error = WxError.fromJson(result); - if (error.getErrorCode() != 0) { - throw new WxErrorException(error); - } - return result; + @Override + public String execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, + String queryParam) throws WxErrorException, IOException { + if (queryParam != null) { + if (uri.indexOf('?') == -1) { + uri += '?'; + } + uri += uri.endsWith("?") ? queryParam : '&' + queryParam; } + SocketHttpConnectionProvider provider = new SocketHttpConnectionProvider(); + + if (httpProxy != null) { + ProxyInfo proxyInfoObj = new ProxyInfo( + ProxyInfo.ProxyType.HTTP, + httpProxy.getHostName(), + httpProxy.getPort(), "", ""); + provider.useProxy(proxyInfoObj); + } + + HttpRequest request = HttpRequest.get(uri); + request.method("GET"); + request.charset("UTF-8"); + + HttpResponse response = request.open(provider).send(); + response.charset("UTF-8"); + String result = response.bodyText(); + + WxError error = WxError.fromJson(result); + if (error.getErrorCode() != 0) { + throw new WxErrorException(error); + } + return result; + } + } diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/JoddPostRequestExecutor.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/JoddPostRequestExecutor.java index d054835b..771fb46e 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/JoddPostRequestExecutor.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/JoddPostRequestExecutor.java @@ -18,33 +18,33 @@ import java.io.IOException; */ public class JoddPostRequestExecutor implements RequestExecutor { - @Override - public String execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, - String postEntity) throws WxErrorException, IOException { - SocketHttpConnectionProvider provider = new SocketHttpConnectionProvider(); - - if (httpProxy != null) { - ProxyInfo proxyInfoObj = new ProxyInfo( - ProxyInfo.ProxyType.HTTP, - httpProxy.getAddress().getHostAddress(), - httpProxy.getPort(), "", ""); - provider.useProxy(proxyInfoObj); - } - - HttpRequest request = HttpRequest.get(uri); - request.method("POST"); - request.charset("UTF-8"); - request.bodyText(postEntity); - - HttpResponse response = request.open(provider).send(); - response.charset("UTF-8"); - String result = response.bodyText(); - - WxError error = WxError.fromJson(result); - if (error.getErrorCode() != 0) { - throw new WxErrorException(error); - } - return result; + @Override + public String execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, + String postEntity) throws WxErrorException, IOException { + SocketHttpConnectionProvider provider = new SocketHttpConnectionProvider(); + + if (httpProxy != null) { + ProxyInfo proxyInfoObj = new ProxyInfo( + ProxyInfo.ProxyType.HTTP, + httpProxy.getAddress().getHostAddress(), + httpProxy.getPort(), "", ""); + provider.useProxy(proxyInfoObj); } + HttpRequest request = HttpRequest.get(uri); + request.method("POST"); + request.charset("UTF-8"); + request.bodyText(postEntity); + + HttpResponse response = request.open(provider).send(); + response.charset("UTF-8"); + String result = response.bodyText(); + + WxError error = WxError.fromJson(result); + if (error.getErrorCode() != 0) { + throw new WxErrorException(error); + } + return result; + } + } diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaDownloadRequestExecutor.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaDownloadRequestExecutor.java index 306ac258..5ffac4bf 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaDownloadRequestExecutor.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaDownloadRequestExecutor.java @@ -6,7 +6,6 @@ import me.chanjar.weixin.common.util.StringUtils; import me.chanjar.weixin.common.util.fs.FileUtils; import org.apache.http.Header; import org.apache.http.HttpHost; -import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; @@ -21,11 +20,11 @@ import java.util.regex.Pattern; /** * 下载媒体文件请求执行器,请求的参数是String, 返回的结果是File - * @author Daniel Qian * + * @author Daniel Qian */ public class MediaDownloadRequestExecutor implements RequestExecutor { - + private File tmpDirFile; public MediaDownloadRequestExecutor() { @@ -36,7 +35,7 @@ public class MediaDownloadRequestExecutor implements RequestExecutor { @@ -33,10 +32,10 @@ public class MediaUploadRequestExecutor implements RequestExecutor { /** - * * @param httpclient 传入的httpClient - * @param httpProxy http代理对象,如果没有配置代理则为空 - * @param uri uri - * @param data 数据 + * @param httpProxy http代理对象,如果没有配置代理则为空 + * @param uri uri + * @param data 数据 * @throws WxErrorException * @throws ClientProtocolException * @throws IOException diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/SimpleGetRequestExecutor.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/SimpleGetRequestExecutor.java index 36852377..ec221e28 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/SimpleGetRequestExecutor.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/SimpleGetRequestExecutor.java @@ -3,7 +3,6 @@ package me.chanjar.weixin.common.util.http; import me.chanjar.weixin.common.bean.result.WxError; import me.chanjar.weixin.common.exception.WxErrorException; import org.apache.http.HttpHost; -import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; @@ -13,8 +12,8 @@ import java.io.IOException; /** * 简单的GET请求执行器,请求的参数是String, 返回的结果也是String - * @author Daniel Qian * + * @author Daniel Qian */ public class SimpleGetRequestExecutor implements RequestExecutor { @@ -39,7 +38,7 @@ public class SimpleGetRequestExecutor implements RequestExecutor throw new WxErrorException(error); } return responseContent; - }finally { + } finally { httpGet.releaseConnection(); } } diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/SimplePostRequestExecutor.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/SimplePostRequestExecutor.java index e60ee06f..ad67bd51 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/SimplePostRequestExecutor.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/SimplePostRequestExecutor.java @@ -4,7 +4,6 @@ import me.chanjar.weixin.common.bean.result.WxError; import me.chanjar.weixin.common.exception.WxErrorException; import org.apache.http.Consts; import org.apache.http.HttpHost; -import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; @@ -15,8 +14,8 @@ import java.io.IOException; /** * 简单的POST请求执行器,请求的参数是String, 返回的结果也是String - * @author Daniel Qian * + * @author Daniel Qian */ public class SimplePostRequestExecutor implements RequestExecutor { @@ -40,7 +39,7 @@ public class SimplePostRequestExecutor implements RequestExecutor { public static final ResponseHandler INSTANCE = new Utf8ResponseHandler(); - + public String handleResponse(final HttpResponse response) throws IOException { final StatusLine statusLine = response.getStatusLine(); final HttpEntity entity = response.getEntity(); diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/GsonHelper.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/GsonHelper.java index 9c274569..3c106fbf 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/GsonHelper.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/GsonHelper.java @@ -15,101 +15,101 @@ import com.google.gson.JsonObject; public class GsonHelper { - public static boolean isNull(JsonElement element) { - return element == null || element.isJsonNull(); - } - - public static boolean isNotNull(JsonElement element) { - return !isNull(element); - } - - public static Long getLong(JsonObject json, String property) { - return getAsLong(json.get(property)); - } - - public static long getPrimitiveLong(JsonObject json, String property) { - return getAsPrimitiveLong(json.get(property)); - } - - public static Integer getInteger(JsonObject json, String property) { - return getAsInteger(json.get(property)); - } - - public static int getPrimitiveInteger(JsonObject json, String property) { - return getAsPrimitiveInt(json.get(property)); - } - - public static Double getDouble(JsonObject json, String property) { - return getAsDouble(json.get(property)); - } - - public static double getPrimitiveDouble(JsonObject json, String property) { - return getAsPrimitiveDouble(json.get(property)); - } - - public static Float getFloat(JsonObject json, String property) { - return getAsFloat(json.get(property)); - } - - public static float getPrimitiveFloat(JsonObject json, String property) { - return getAsPrimitiveFloat(json.get(property)); - } - - public static Boolean getBoolean(JsonObject json, String property) { - return getAsBoolean(json.get(property)); - } - - public static String getString(JsonObject json, String property) { - return getAsString(json.get(property)); - } - - public static String getAsString(JsonElement element) { - return isNull(element) ? null : element.getAsString(); - } - - public static Long getAsLong(JsonElement element) { - return isNull(element) ? null : element.getAsLong(); - } - - public static long getAsPrimitiveLong(JsonElement element) { - Long r = getAsLong(element); - return r == null ? 0l : r; - } - - public static Integer getAsInteger(JsonElement element) { - return isNull(element) ? null : element.getAsInt(); - } - - public static int getAsPrimitiveInt(JsonElement element) { - Integer r = getAsInteger(element); - return r == null ? 0 : r; - } - - public static Boolean getAsBoolean(JsonElement element) { - return isNull(element) ? null : element.getAsBoolean(); - } - - public static boolean getAsPrimitiveBool(JsonElement element) { - Boolean r = getAsBoolean(element); - return r != null && r.booleanValue(); - } - - public static Double getAsDouble(JsonElement element) { - return isNull(element) ? null : element.getAsDouble(); - } - - public static double getAsPrimitiveDouble(JsonElement element) { - Double r = getAsDouble(element); - return r == null ? 0d : r; - } - - public static Float getAsFloat(JsonElement element) { - return isNull(element) ? null : element.getAsFloat(); - } - - public static float getAsPrimitiveFloat(JsonElement element) { - Float r = getAsFloat(element); - return r == null ? 0f : r; - } - + public static boolean isNull(JsonElement element) { + return element == null || element.isJsonNull(); + } + + public static boolean isNotNull(JsonElement element) { + return !isNull(element); + } + + public static Long getLong(JsonObject json, String property) { + return getAsLong(json.get(property)); + } + + public static long getPrimitiveLong(JsonObject json, String property) { + return getAsPrimitiveLong(json.get(property)); + } + + public static Integer getInteger(JsonObject json, String property) { + return getAsInteger(json.get(property)); + } + + public static int getPrimitiveInteger(JsonObject json, String property) { + return getAsPrimitiveInt(json.get(property)); + } + + public static Double getDouble(JsonObject json, String property) { + return getAsDouble(json.get(property)); + } + + public static double getPrimitiveDouble(JsonObject json, String property) { + return getAsPrimitiveDouble(json.get(property)); + } + + public static Float getFloat(JsonObject json, String property) { + return getAsFloat(json.get(property)); + } + + public static float getPrimitiveFloat(JsonObject json, String property) { + return getAsPrimitiveFloat(json.get(property)); + } + + public static Boolean getBoolean(JsonObject json, String property) { + return getAsBoolean(json.get(property)); + } + + public static String getString(JsonObject json, String property) { + return getAsString(json.get(property)); + } + + public static String getAsString(JsonElement element) { + return isNull(element) ? null : element.getAsString(); + } + + public static Long getAsLong(JsonElement element) { + return isNull(element) ? null : element.getAsLong(); + } + + public static long getAsPrimitiveLong(JsonElement element) { + Long r = getAsLong(element); + return r == null ? 0l : r; + } + + public static Integer getAsInteger(JsonElement element) { + return isNull(element) ? null : element.getAsInt(); + } + + public static int getAsPrimitiveInt(JsonElement element) { + Integer r = getAsInteger(element); + return r == null ? 0 : r; + } + + public static Boolean getAsBoolean(JsonElement element) { + return isNull(element) ? null : element.getAsBoolean(); + } + + public static boolean getAsPrimitiveBool(JsonElement element) { + Boolean r = getAsBoolean(element); + return r != null && r.booleanValue(); + } + + public static Double getAsDouble(JsonElement element) { + return isNull(element) ? null : element.getAsDouble(); + } + + public static double getAsPrimitiveDouble(JsonElement element) { + Double r = getAsDouble(element); + return r == null ? 0d : r; + } + + public static Float getAsFloat(JsonElement element) { + return isNull(element) ? null : element.getAsFloat(); + } + + public static float getAsPrimitiveFloat(JsonElement element) { + Float r = getAsFloat(element); + return r == null ? 0f : r; + } + } diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/WxAccessTokenAdapter.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/WxAccessTokenAdapter.java index cfe4edb2..58be83ac 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/WxAccessTokenAdapter.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/WxAccessTokenAdapter.java @@ -14,9 +14,7 @@ import me.chanjar.weixin.common.bean.WxAccessToken; import java.lang.reflect.Type; /** - * * @author Daniel Qian - * */ public class WxAccessTokenAdapter implements JsonDeserializer { @@ -32,5 +30,5 @@ public class WxAccessTokenAdapter implements JsonDeserializer { } return accessToken; } - + } diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/WxErrorAdapter.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/WxErrorAdapter.java index 74b9ded8..30c3708d 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/WxErrorAdapter.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/WxErrorAdapter.java @@ -14,9 +14,7 @@ import me.chanjar.weixin.common.bean.result.WxError; import java.lang.reflect.Type; /** - * * @author Daniel Qian - * */ public class WxErrorAdapter implements JsonDeserializer { @@ -33,5 +31,5 @@ public class WxErrorAdapter implements JsonDeserializer { wxError.setJson(json.toString()); return wxError; } - + } diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/WxGsonBuilder.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/WxGsonBuilder.java index 7aa56965..072ee38b 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/WxGsonBuilder.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/WxGsonBuilder.java @@ -2,9 +2,9 @@ package me.chanjar.weixin.common.util.json; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import me.chanjar.weixin.common.bean.WxAccessToken; import me.chanjar.weixin.common.bean.WxMenu; import me.chanjar.weixin.common.bean.result.WxError; -import me.chanjar.weixin.common.bean.WxAccessToken; import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; public class WxGsonBuilder { diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/WxMediaUploadResultAdapter.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/WxMediaUploadResultAdapter.java index 35eb18b4..16317a40 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/WxMediaUploadResultAdapter.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/WxMediaUploadResultAdapter.java @@ -14,9 +14,7 @@ import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; import java.lang.reflect.Type; /** - * * @author Daniel Qian - * */ public class WxMediaUploadResultAdapter implements JsonDeserializer { @@ -38,5 +36,5 @@ public class WxMediaUploadResultAdapter implements JsonDeserializer, JsonDeserializer { @@ -36,11 +27,11 @@ public class WxMenuGsonAdapter implements JsonSerializer, JsonDeserializ buttonArray.add(buttonJson); } json.add("button", buttonArray); - + if (menu.getMatchRule() != null) { json.add("matchrule", convertToJson(menu.getMatchRule())); } - + return json; } @@ -60,15 +51,15 @@ public class WxMenuGsonAdapter implements JsonSerializer, JsonDeserializ return buttonJson; } - protected JsonObject convertToJson(WxMenu.WxMenuRule menuRule){ + protected JsonObject convertToJson(WxMenu.WxMenuRule menuRule) { JsonObject matchRule = new JsonObject(); - matchRule.addProperty("tag_id",menuRule.getTagId()); - matchRule.addProperty("sex",menuRule.getSex()); - matchRule.addProperty("country",menuRule.getCountry()); - matchRule.addProperty("province",menuRule.getProvince()); - matchRule.addProperty("city",menuRule.getCity()); - matchRule.addProperty("client_platform_type",menuRule.getClientPlatformType()); - matchRule.addProperty("language",menuRule.getLanguage()); + matchRule.addProperty("tag_id", menuRule.getTagId()); + matchRule.addProperty("sex", menuRule.getSex()); + matchRule.addProperty("country", menuRule.getCountry()); + matchRule.addProperty("province", menuRule.getProvince()); + matchRule.addProperty("city", menuRule.getCity()); + matchRule.addProperty("client_platform_type", menuRule.getClientPlatformType()); + matchRule.addProperty("language", menuRule.getLanguage()); return matchRule; } @@ -96,7 +87,7 @@ public class WxMenuGsonAdapter implements JsonSerializer, JsonDeserializ } return menu; } - + protected WxMenu.WxMenuButton convertFromJson(JsonObject json) { WxMenu.WxMenuButton button = new WxMenu.WxMenuButton(); button.setName(GsonHelper.getString(json, "name")); diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/res/StringManager.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/res/StringManager.java index 0abe4d89..94303fec 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/res/StringManager.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/res/StringManager.java @@ -18,30 +18,24 @@ package me.chanjar.weixin.common.util.res; import java.text.MessageFormat; -import java.util.Enumeration; -import java.util.Hashtable; -import java.util.LinkedHashMap; -import java.util.Locale; -import java.util.Map; -import java.util.MissingResourceException; -import java.util.ResourceBundle; +import java.util.*; /** * An internationalization / localization helper class which reduces * the bother of handling ResourceBundles and takes care of the * common cases of message formating which otherwise require the * creation of Object arrays and such. - * + *

    *

    The StringManager operates on a package basis. One StringManager * per package can be created and accessed via the getManager method * call. - * + *

    *

    The StringManager will look for a ResourceBundle named by * the package name given plus the suffix of "LocalStrings". In * practice, this means that the localized information will be contained * in a LocalStrings.properties file located in the package * directory of the classpath. - * + *

    *

    Please see the documentation for java.util.ResourceBundle for * more information. * @@ -52,152 +46,80 @@ import java.util.ResourceBundle; */ public class StringManager { - private static int LOCALE_CACHE_SIZE = 10; - - /** - * The ResourceBundle for this StringManager. - */ - private final ResourceBundle bundle; - private final Locale locale; - - /** - * Creates a new StringManager for a given package. This is a - * private method and all access to it is arbitrated by the - * static getManager method call so that only one StringManager - * per package will be created. - * - * @param packageName Name of package to create StringManager for. - */ - private StringManager(String packageName, Locale locale) { - String bundleName = packageName + ".LocalStrings"; - ResourceBundle bnd = null; + private static final Map> managers = + new Hashtable>(); + private static int LOCALE_CACHE_SIZE = 10; + /** + * The ResourceBundle for this StringManager. + */ + private final ResourceBundle bundle; + private final Locale locale; + + /** + * Creates a new StringManager for a given package. This is a + * private method and all access to it is arbitrated by the + * static getManager method call so that only one StringManager + * per package will be created. + * + * @param packageName Name of package to create StringManager for. + */ + private StringManager(String packageName, Locale locale) { + String bundleName = packageName + ".LocalStrings"; + ResourceBundle bnd = null; + try { + bnd = ResourceBundle.getBundle(bundleName, locale); + } catch (MissingResourceException ex) { + // Try from the current loader (that's the case for trusted apps) + // Should only be required if using a TC5 style classloader structure + // where common != shared != server + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + if (cl != null) { try { - bnd = ResourceBundle.getBundle(bundleName, locale); - } catch( MissingResourceException ex ) { - // Try from the current loader (that's the case for trusted apps) - // Should only be required if using a TC5 style classloader structure - // where common != shared != server - ClassLoader cl = Thread.currentThread().getContextClassLoader(); - if( cl != null ) { - try { - bnd = ResourceBundle.getBundle(bundleName, locale, cl); - } catch(MissingResourceException ex2) { - // Ignore - } - } - } - bundle = bnd; - // Get the actual locale, which may be different from the requested one - if (bundle != null) { - Locale bundleLocale = bundle.getLocale(); - if (bundleLocale.equals(Locale.ROOT)) { - this.locale = Locale.ENGLISH; - } else { - this.locale = bundleLocale; - } - } else { - this.locale = null; + bnd = ResourceBundle.getBundle(bundleName, locale, cl); + } catch (MissingResourceException ex2) { + // Ignore } + } } - - /** - Get a string from the underlying resource bundle or return - null if the String is not found. - - @param key to desired resource String - @return resource String matching key from underlying - bundle or null if not found. - @throws IllegalArgumentException if key is null. - */ - public String getString(String key) { - if(key == null){ - String msg = "key may not have a null value"; - - throw new IllegalArgumentException(msg); - } - - String str = null; - - try { - // Avoid NPE if bundle is null and treat it like an MRE - if (bundle != null) { - str = bundle.getString(key); - } - } catch(MissingResourceException mre) { - //bad: shouldn't mask an exception the following way: - // str = "[cannot find message associated with key '" + key + - // "' due to " + mre + "]"; - // because it hides the fact that the String was missing - // from the calling code. - //good: could just throw the exception (or wrap it in another) - // but that would probably cause much havoc on existing - // code. - //better: consistent with container pattern to - // simply return null. Calling code can then do - // a null check. - str = null; - } - - return str; + bundle = bnd; + // Get the actual locale, which may be different from the requested one + if (bundle != null) { + Locale bundleLocale = bundle.getLocale(); + if (bundleLocale.equals(Locale.ROOT)) { + this.locale = Locale.ENGLISH; + } else { + this.locale = bundleLocale; + } + } else { + this.locale = null; } - - /** - * Get a string from the underlying resource bundle and format - * it with the given set of arguments. - * - * @param key - * @param args - */ - public String getString(final String key, final Object... args) { - String value = getString(key); - if (value == null) { - value = key; - } - - MessageFormat mf = new MessageFormat(value); - mf.setLocale(locale); - return mf.format(args, new StringBuffer(), null).toString(); - } - - /** - * Identify the Locale this StringManager is associated with - */ - public Locale getLocale() { - return locale; - } - - // -------------------------------------------------------------- - // STATIC SUPPORT METHODS - // -------------------------------------------------------------- - - private static final Map> managers = - new Hashtable>(); - - /** - * Get the StringManager for a particular package. If a manager for - * a package already exists, it will be reused, else a new - * StringManager will be created and returned. - * - * @param packageName The package name - */ - public static final synchronized StringManager getManager( - String packageName) { - return getManager(packageName, Locale.getDefault()); - } - - /** - * Get the StringManager for a particular package and Locale. If a manager - * for a package/Locale combination already exists, it will be reused, else - * a new StringManager will be created and returned. - * - * @param packageName The package name - * @param locale The Locale - */ - public static final synchronized StringManager getManager( - String packageName, Locale locale) { - - Map map = managers.get(packageName); - if (map == null) { + } + + /** + * Get the StringManager for a particular package. If a manager for + * a package already exists, it will be reused, else a new + * StringManager will be created and returned. + * + * @param packageName The package name + */ + public static final synchronized StringManager getManager( + String packageName) { + return getManager(packageName, Locale.getDefault()); + } + + /** + * Get the StringManager for a particular package and Locale. If a manager + * for a package/Locale combination already exists, it will be reused, else + * a new StringManager will be created and returned. + * + * @param packageName The package name + * @param locale The Locale + */ + public static final synchronized StringManager getManager( + String packageName, Locale locale) { + + Map map = managers.get(packageName); + if (map == null) { /* * Don't want the HashMap to be expanded beyond LOCALE_CACHE_SIZE. * Expansion occurs when size() exceeds capacity. Therefore keep @@ -206,43 +128,113 @@ public class StringManager { * for removal needs to use one less than the maximum desired size * */ - map = new LinkedHashMap(LOCALE_CACHE_SIZE, 1, true) { - private static final long serialVersionUID = 1L; - @Override - protected boolean removeEldestEntry( - Map.Entry eldest) { - return size() > (LOCALE_CACHE_SIZE - 1); - } - }; - managers.put(packageName, map); - } + map = new LinkedHashMap(LOCALE_CACHE_SIZE, 1, true) { + private static final long serialVersionUID = 1L; - StringManager mgr = map.get(locale); - if (mgr == null) { - mgr = new StringManager(packageName, locale); - map.put(locale, mgr); + @Override + protected boolean removeEldestEntry( + Map.Entry eldest) { + return size() > (LOCALE_CACHE_SIZE - 1); } - return mgr; + }; + managers.put(packageName, map); } - /** - * Retrieve the StringManager for a list of Locales. The first StringManager - * found will be returned. - * - * @param requestedLocales the list of Locales - * - * @return the found StringManager or the default StringManager - */ - public static StringManager getManager(String packageName, - Enumeration requestedLocales) { - while (requestedLocales.hasMoreElements()) { - Locale locale = requestedLocales.nextElement(); - StringManager result = getManager(packageName, locale); - if (result.getLocale().equals(locale)) { - return result; - } - } - // Return the default - return getManager(packageName); + StringManager mgr = map.get(locale); + if (mgr == null) { + mgr = new StringManager(packageName, locale); + map.put(locale, mgr); } + return mgr; + } + + // -------------------------------------------------------------- + // STATIC SUPPORT METHODS + // -------------------------------------------------------------- + + /** + * Retrieve the StringManager for a list of Locales. The first StringManager + * found will be returned. + * + * @param requestedLocales the list of Locales + * @return the found StringManager or the default StringManager + */ + public static StringManager getManager(String packageName, + Enumeration requestedLocales) { + while (requestedLocales.hasMoreElements()) { + Locale locale = requestedLocales.nextElement(); + StringManager result = getManager(packageName, locale); + if (result.getLocale().equals(locale)) { + return result; + } + } + // Return the default + return getManager(packageName); + } + + /** + * Get a string from the underlying resource bundle or return + * null if the String is not found. + * + * @param key to desired resource String + * @return resource String matching key from underlying + * bundle or null if not found. + * @throws IllegalArgumentException if key is null. + */ + public String getString(String key) { + if (key == null) { + String msg = "key may not have a null value"; + + throw new IllegalArgumentException(msg); + } + + String str = null; + + try { + // Avoid NPE if bundle is null and treat it like an MRE + if (bundle != null) { + str = bundle.getString(key); + } + } catch (MissingResourceException mre) { + //bad: shouldn't mask an exception the following way: + // str = "[cannot find message associated with key '" + key + + // "' due to " + mre + "]"; + // because it hides the fact that the String was missing + // from the calling code. + //good: could just throw the exception (or wrap it in another) + // but that would probably cause much havoc on existing + // code. + //better: consistent with container pattern to + // simply return null. Calling code can then do + // a null check. + str = null; + } + + return str; + } + + /** + * Get a string from the underlying resource bundle and format + * it with the given set of arguments. + * + * @param key + * @param args + */ + public String getString(final String key, final Object... args) { + String value = getString(key); + if (value == null) { + value = key; + } + + MessageFormat mf = new MessageFormat(value); + mf.setLocale(locale); + return mf.format(args, new StringBuffer(), null).toString(); + } + + /** + * Identify the Locale this StringManager is associated with + */ + public Locale getLocale() { + return locale; + } } diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/xml/XStreamInitializer.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/xml/XStreamInitializer.java index 1caa0a06..109acfc0 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/xml/XStreamInitializer.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/xml/XStreamInitializer.java @@ -1,7 +1,5 @@ package me.chanjar.weixin.common.util.xml; -import java.io.Writer; - import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.core.util.QuickWriter; import com.thoughtworks.xstream.io.HierarchicalStreamWriter; @@ -10,6 +8,8 @@ import com.thoughtworks.xstream.io.xml.XppDriver; import com.thoughtworks.xstream.security.NullPermission; import com.thoughtworks.xstream.security.PrimitiveTypePermission; +import java.io.Writer; + public class XStreamInitializer { public static XStream getInstance() { @@ -22,6 +22,7 @@ public class XStreamInitializer { protected String SUFFIX_CDATA = "]]>"; protected String PREFIX_MEDIA_ID = ""; protected String SUFFIX_MEDIA_ID = ""; + @Override protected void writeText(QuickWriter writer, String text) { if (text.startsWith(PREFIX_CDATA) && text.endsWith(SUFFIX_CDATA)) { diff --git a/weixin-java-common/src/test/java/me/chanjar/weixin/common/bean/WxAccessTokenTest.java b/weixin-java-common/src/test/java/me/chanjar/weixin/common/bean/WxAccessTokenTest.java index d236c631..b2bc7fe7 100644 --- a/weixin-java-common/src/test/java/me/chanjar/weixin/common/bean/WxAccessTokenTest.java +++ b/weixin-java-common/src/test/java/me/chanjar/weixin/common/bean/WxAccessTokenTest.java @@ -14,5 +14,5 @@ public class WxAccessTokenTest { Assert.assertTrue(wxError.getExpiresIn() == 7200); } - + } diff --git a/weixin-java-common/src/test/java/me/chanjar/weixin/common/bean/WxErrorTest.java b/weixin-java-common/src/test/java/me/chanjar/weixin/common/bean/WxErrorTest.java index 51e9dab3..3fe40795 100644 --- a/weixin-java-common/src/test/java/me/chanjar/weixin/common/bean/WxErrorTest.java +++ b/weixin-java-common/src/test/java/me/chanjar/weixin/common/bean/WxErrorTest.java @@ -15,7 +15,7 @@ public class WxErrorTest { Assert.assertEquals(wxError.getErrorMsg(), "invalid openid"); } - + public void testFromBadJson1() { String json = "{ \"errcode\": 40003, \"errmsg\": \"invalid openid\", \"media_id\": \"12323423dsfafsf232f\" }"; @@ -24,7 +24,7 @@ public class WxErrorTest { Assert.assertEquals(wxError.getErrorMsg(), "invalid openid"); } - + public void testFromBadJson2() { String json = "{\"access_token\":\"ACCESS_TOKEN\",\"expires_in\":7200}"; @@ -33,5 +33,5 @@ public class WxErrorTest { Assert.assertEquals(wxError.getErrorMsg(), null); } - + } diff --git a/weixin-java-common/src/test/java/me/chanjar/weixin/common/bean/WxMenuTest.java b/weixin-java-common/src/test/java/me/chanjar/weixin/common/bean/WxMenuTest.java index 2ae12c2c..b7492b37 100644 --- a/weixin-java-common/src/test/java/me/chanjar/weixin/common/bean/WxMenuTest.java +++ b/weixin-java-common/src/test/java/me/chanjar/weixin/common/bean/WxMenuTest.java @@ -8,51 +8,51 @@ import org.testng.annotations.Test; @Test public class WxMenuTest { - @Test(dataProvider="wxReturnMenu") + @Test(dataProvider = "wxReturnMenu") public void testFromJson(String json) { WxMenu menu = WxMenu.fromJson(json); Assert.assertEquals(menu.getButtons().size(), 3); } - - @Test(dataProvider="wxPushMenu") + + @Test(dataProvider = "wxPushMenu") public void testToJson(String json) { WxMenu menu = new WxMenu(); WxMenuButton button1 = new WxMenuButton(); button1.setType("click"); button1.setName("今日歌曲"); button1.setKey("V1001_TODAY_MUSIC"); - + WxMenuButton button2 = new WxMenuButton(); button2.setType("click"); button2.setName("歌手简介"); button2.setKey("V1001_TODAY_SINGER"); - + WxMenuButton button3 = new WxMenuButton(); button3.setName("菜单"); - + menu.getButtons().add(button1); menu.getButtons().add(button2); menu.getButtons().add(button3); - + WxMenuButton button31 = new WxMenuButton(); button31.setType("view"); button31.setName("搜索"); button31.setUrl("http://www.soso.com/"); - + WxMenuButton button32 = new WxMenuButton(); button32.setType("view"); button32.setName("视频"); button32.setUrl("http://v.qq.com/"); - + WxMenuButton button33 = new WxMenuButton(); button33.setType("click"); button33.setName("赞一下我们"); button33.setKey("V1001_GOOD"); - + button3.getSubButtons().add(button31); button3.getSubButtons().add(button32); button3.getSubButtons().add(button33); - + Assert.assertEquals(menu.toJson(), json); } @@ -78,82 +78,82 @@ public class WxMenuTest { Assert.assertEquals(menu.toJson(), json); } - + @DataProvider public Object[][] wxReturnMenu() { - Object[][] res = menuJson(); + Object[][] res = menuJson(); String json = "{ \"menu\" : " + res[0][0] + " }"; - return new Object[][] { - new Object[] { json } + return new Object[][]{ + new Object[]{json} }; } - - @DataProvider(name="wxPushMenu") + + @DataProvider(name = "wxPushMenu") public Object[][] menuJson() { - String json = - "{" - +"\"button\":[" - +"{" - +"\"type\":\"click\"," - +"\"name\":\"今日歌曲\"," - +"\"key\":\"V1001_TODAY_MUSIC\"" - +"}," - +"{" - +"\"type\":\"click\"," - +"\"name\":\"歌手简介\"," - +"\"key\":\"V1001_TODAY_SINGER\"" - +"}," - +"{" - +"\"name\":\"菜单\"," - +"\"sub_button\":[" - +"{" - +"\"type\":\"view\"," - +"\"name\":\"搜索\"," - +"\"url\":\"http://www.soso.com/\"" - +"}," - +"{" - +"\"type\":\"view\"," - +"\"name\":\"视频\"," - +"\"url\":\"http://v.qq.com/\"" - +"}," - +"{" - +"\"type\":\"click\"," - +"\"name\":\"赞一下我们\"," - +"\"key\":\"V1001_GOOD\"" - +"}" - +"]" - +"}" - +"]" - +"}"; - return new Object[][] { - new Object[] { json } + String json = + "{" + + "\"button\":[" + + "{" + + "\"type\":\"click\"," + + "\"name\":\"今日歌曲\"," + + "\"key\":\"V1001_TODAY_MUSIC\"" + + "}," + + "{" + + "\"type\":\"click\"," + + "\"name\":\"歌手简介\"," + + "\"key\":\"V1001_TODAY_SINGER\"" + + "}," + + "{" + + "\"name\":\"菜单\"," + + "\"sub_button\":[" + + "{" + + "\"type\":\"view\"," + + "\"name\":\"搜索\"," + + "\"url\":\"http://www.soso.com/\"" + + "}," + + "{" + + "\"type\":\"view\"," + + "\"name\":\"视频\"," + + "\"url\":\"http://v.qq.com/\"" + + "}," + + "{" + + "\"type\":\"click\"," + + "\"name\":\"赞一下我们\"," + + "\"key\":\"V1001_GOOD\"" + + "}" + + "]" + + "}" + + "]" + + "}"; + return new Object[][]{ + new Object[]{json} }; } @DataProvider(name = "wxAddConditionalMenu") - public Object[][] addConditionalMenuJson(){ + public Object[][] addConditionalMenuJson() { String json = - "{" - +"\"button\":[" - +"{" - +"\"type\":\"click\"," - +"\"name\":\"今日歌曲\"," - +"\"key\":\"V1001_TODAY_MUSIC\"" - +"}" - +"]," - +"\"matchrule\":{" - +"\"group_id\":\"2\"," - +"\"sex\":\"1\"," - +"\"country\":\"中国\"," - +"\"province\":\"广东\"," - +"\"city\":\"广州\"," - +"\"client_platform_type\":\"2\"," - +"\"language\":\"zh_CN\"" - +"}" - +"}"; + "{" + + "\"button\":[" + + "{" + + "\"type\":\"click\"," + + "\"name\":\"今日歌曲\"," + + "\"key\":\"V1001_TODAY_MUSIC\"" + + "}" + + "]," + + "\"matchrule\":{" + + "\"group_id\":\"2\"," + + "\"sex\":\"1\"," + + "\"country\":\"中国\"," + + "\"province\":\"广东\"," + + "\"city\":\"广州\"," + + "\"client_platform_type\":\"2\"," + + "\"language\":\"zh_CN\"" + + "}" + + "}"; return new Object[][]{ - new Object[]{json} + new Object[]{json} }; } - + } diff --git a/weixin-java-common/src/test/java/me/chanjar/weixin/common/session/SessionTest.java b/weixin-java-common/src/test/java/me/chanjar/weixin/common/session/SessionTest.java index bcb029b1..ce246636 100644 --- a/weixin-java-common/src/test/java/me/chanjar/weixin/common/session/SessionTest.java +++ b/weixin-java-common/src/test/java/me/chanjar/weixin/common/session/SessionTest.java @@ -10,8 +10,8 @@ public class SessionTest { @DataProvider public Object[][] getSessionManager() { - return new Object[][] { - new Object[] { new StandardSessionManager() } + return new Object[][]{ + new Object[]{new StandardSessionManager()} }; } diff --git a/weixin-java-common/src/test/java/me/chanjar/weixin/common/util/WxMessageInMemoryDuplicateCheckerTest.java b/weixin-java-common/src/test/java/me/chanjar/weixin/common/util/WxMessageInMemoryDuplicateCheckerTest.java index 7369d637..a3a243a4 100644 --- a/weixin-java-common/src/test/java/me/chanjar/weixin/common/util/WxMessageInMemoryDuplicateCheckerTest.java +++ b/weixin-java-common/src/test/java/me/chanjar/weixin/common/util/WxMessageInMemoryDuplicateCheckerTest.java @@ -8,7 +8,7 @@ import org.testng.annotations.Test; public class WxMessageInMemoryDuplicateCheckerTest { public void test() throws InterruptedException { - Long[] msgIds = new Long[] { 1l, 2l, 3l, 4l, 5l, 6l, 7l, 8l }; + Long[] msgIds = new Long[]{1l, 2l, 3l, 4l, 5l, 6l, 7l, 8l}; WxMessageInMemoryDuplicateChecker checker = new WxMessageInMemoryDuplicateChecker(2000l, 1000l); // 第一次检查 diff --git a/weixin-java-common/src/test/java/me/chanjar/weixin/common/util/crypto/WxCryptUtilTest.java b/weixin-java-common/src/test/java/me/chanjar/weixin/common/util/crypto/WxCryptUtilTest.java index 15b08c26..ab6c8db9 100755 --- a/weixin-java-common/src/test/java/me/chanjar/weixin/common/util/crypto/WxCryptUtilTest.java +++ b/weixin-java-common/src/test/java/me/chanjar/weixin/common/util/crypto/WxCryptUtilTest.java @@ -69,7 +69,7 @@ public class WxCryptUtilTest { } public void testValidateSignatureError() throws ParserConfigurationException, SAXException, - IOException { + IOException { try { WxCryptUtil pc = new WxCryptUtil(token, encodingAesKey, appId); String afterEncrpt = pc.encrypt(replyMsg);