优化代码,增加日志

This commit is contained in:
Binary Wang 2018-08-27 21:03:32 +08:00
parent 3bc2bbc208
commit 5f1290720e
21 changed files with 149 additions and 87 deletions

View File

@ -6,7 +6,7 @@ import java.util.Map;
/** /**
* 微信开发所使用到的常量类. * 微信开发所使用到的常量类.
* *
* @author chanjarster & binarywang * @author Daniel Qian & binarywang
*/ */
public class WxConsts { public class WxConsts {
/** /**

View File

@ -4,6 +4,8 @@ import me.chanjar.weixin.common.error.WxErrorException;
/** /**
* WxErrorException处理器. * WxErrorException处理器.
*
* @author Daniel Qian
*/ */
public interface WxErrorExceptionHandler { public interface WxErrorExceptionHandler {

View File

@ -5,6 +5,8 @@ package me.chanjar.weixin.common.api;
* 消息重复检查器. * 消息重复检查器.
* 微信服务器在五秒内收不到响应会断掉连接并且重新发起请求总共重试三次 * 微信服务器在五秒内收不到响应会断掉连接并且重新发起请求总共重试三次
* </pre> * </pre>
*
* @author Daniel Qian
*/ */
public interface WxMessageDuplicateChecker { public interface WxMessageDuplicateChecker {

View File

@ -9,6 +9,8 @@ import java.util.concurrent.atomic.AtomicBoolean;
* 默认消息重复检查器. * 默认消息重复检查器.
* 将每个消息id保存在内存里每隔5秒清理已经过期的消息id每个消息id的过期时间是15秒 * 将每个消息id保存在内存里每隔5秒清理已经过期的消息id每个消息id的过期时间是15秒
* </pre> * </pre>
*
* @author Daniel Qian
*/ */
public class WxMessageInMemoryDuplicateChecker implements WxMessageDuplicateChecker { public class WxMessageInMemoryDuplicateChecker implements WxMessageDuplicateChecker {

View File

@ -1,10 +1,15 @@
package me.chanjar.weixin.common.bean; package me.chanjar.weixin.common.bean;
import java.io.Serializable;
import lombok.Data; import lombok.Data;
import me.chanjar.weixin.common.util.json.WxGsonBuilder; import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import java.io.Serializable; /**
* access token.
*
* @author Daniel Qian
*/
@Data @Data
public class WxAccessToken implements Serializable { public class WxAccessToken implements Serializable {
private static final long serialVersionUID = 8709719312922168909L; private static final long serialVersionUID = 8709719312922168909L;

View File

@ -1,16 +1,16 @@
package me.chanjar.weixin.common.bean; package me.chanjar.weixin.common.bean;
import java.io.Serializable;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import java.io.Serializable;
/** /**
* jspai signature. * jspai signature.
* *
* @author chanjarster * @author Daniel Qian
*/ */
@Data @Data
@Builder @Builder

View File

@ -10,6 +10,11 @@ import org.apache.commons.lang3.builder.ToStringStyle;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import lombok.Data; import lombok.Data;
/**
* menu button.
*
* @author Daniel Qian
*/
@Data @Data
public class WxMenuButton implements Serializable { public class WxMenuButton implements Serializable {
private static final long serialVersionUID = -1070939403109776555L; private static final long serialVersionUID = -1070939403109776555L;

View File

@ -8,6 +8,11 @@ import org.apache.commons.lang3.builder.ToStringStyle;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import lombok.Data; import lombok.Data;
/**
* menu rule.
*
* @author Daniel Qian
*/
@Data @Data
public class WxMenuRule implements Serializable { public class WxMenuRule implements Serializable {
private static final long serialVersionUID = -4587181819499286670L; private static final long serialVersionUID = -4587181819499286670L;

View File

@ -8,6 +8,10 @@ import org.apache.commons.lang3.builder.ToStringStyle;
import lombok.Data; import lombok.Data;
import me.chanjar.weixin.common.util.json.WxGsonBuilder; import me.chanjar.weixin.common.util.json.WxGsonBuilder;
/**
*
* @author Daniel Qian
*/
@Data @Data
public class WxMediaUploadResult implements Serializable { public class WxMediaUploadResult implements Serializable {
private static final long serialVersionUID = 330834334738622341L; private static final long serialVersionUID = 330834334738622341L;

View File

@ -1,5 +1,9 @@
package me.chanjar.weixin.common.session; package me.chanjar.weixin.common.session;
/**
*
* @author Daniel Qian
*/
public interface InternalSession { public interface InternalSession {
/** /**

View File

@ -1,5 +1,8 @@
package me.chanjar.weixin.common.session; package me.chanjar.weixin.common.session;
/**
* @author Daniel Qian
*/
public interface InternalSessionManager { public interface InternalSessionManager {
/** /**

View File

@ -1,11 +1,18 @@
package me.chanjar.weixin.common.session; package me.chanjar.weixin.common.session;
import me.chanjar.weixin.common.util.res.StringManager; import java.util.Collections;
import java.util.Enumeration;
import java.util.*; import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import me.chanjar.weixin.common.util.res.StringManager;
/**
* @author Daniel Qian
*/
public class StandardSession implements WxSession, InternalSession { public class StandardSession implements WxSession, InternalSession {
/** /**

View File

@ -2,6 +2,9 @@ package me.chanjar.weixin.common.session;
import java.util.Enumeration; import java.util.Enumeration;
/**
* @author Daniel Qian
*/
public class StandardSessionFacade implements WxSession { public class StandardSessionFacade implements WxSession {
/** /**

View File

@ -1,15 +1,18 @@
package me.chanjar.weixin.common.session; package me.chanjar.weixin.common.session;
import me.chanjar.weixin.common.util.res.StringManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import me.chanjar.weixin.common.util.res.StringManager;
/** /**
* 基于内存的session manager * 基于内存的session manager.
*
* @author Daniel Qian
*/ */
public class StandardSessionManager implements WxSessionManager, InternalSessionManager { public class StandardSessionManager implements WxSessionManager, InternalSessionManager {

View File

@ -19,6 +19,8 @@ package me.chanjar.weixin.common.session;
/** /**
* An exception that indicates the maximum number of active sessions has been * An exception that indicates the maximum number of active sessions has been
* reached and the server is refusing to create any new sessions. * reached and the server is refusing to create any new sessions.
*
* @author Daniel Qian
*/ */
public class TooManyActiveSessionsException public class TooManyActiveSessionsException
extends IllegalStateException { extends IllegalStateException {

View File

@ -2,6 +2,9 @@ package me.chanjar.weixin.common.session;
import java.util.Enumeration; import java.util.Enumeration;
/**
* @author Daniel Qian
*/
public interface WxSession { public interface WxSession {
Object getAttribute(String name); Object getAttribute(String name);

View File

@ -1,5 +1,8 @@
package me.chanjar.weixin.common.session; package me.chanjar.weixin.common.session;
/**
* @author Daniel Qian
*/
public interface WxSessionManager { public interface WxSessionManager {
/** /**

View File

@ -13,6 +13,7 @@ import org.apache.commons.lang3.builder.ToStringStyle;
import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter; import com.thoughtworks.xstream.annotations.XStreamConverter;
import lombok.Data; import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts; import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.util.xml.XStreamCDataConverter; import me.chanjar.weixin.common.util.xml.XStreamCDataConverter;
import me.chanjar.weixin.cp.config.WxCpConfigStorage; import me.chanjar.weixin.cp.config.WxCpConfigStorage;
@ -29,8 +30,9 @@ import me.chanjar.weixin.cp.util.xml.XStreamTransformer;
* *
* @author Daniel Qian * @author Daniel Qian
*/ */
@XStreamAlias("xml")
@Data @Data
@Slf4j
@XStreamAlias("xml")
public class WxCpXmlMessage implements Serializable { public class WxCpXmlMessage implements Serializable {
private static final long serialVersionUID = -1042994982179476410L; private static final long serialVersionUID = -1042994982179476410L;
@ -52,6 +54,24 @@ public class WxCpXmlMessage implements Serializable {
@XStreamAlias("CreateTime") @XStreamAlias("CreateTime")
private Long createTime; private Long createTime;
/**
* <pre>
* 当接受用户消息时可能会获得以下值
* {@link WxConsts.XmlMsgType#TEXT}
* {@link WxConsts.XmlMsgType#IMAGE}
* {@link WxConsts.XmlMsgType#VOICE}
* {@link WxConsts.XmlMsgType#VIDEO}
* {@link WxConsts.XmlMsgType#LOCATION}
* {@link WxConsts.XmlMsgType#LINK}
* {@link WxConsts.XmlMsgType#EVENT}
* 当发送消息的时候使用
* {@link WxConsts.XmlMsgType#TEXT}
* {@link WxConsts.XmlMsgType#IMAGE}
* {@link WxConsts.XmlMsgType#VOICE}
* {@link WxConsts.XmlMsgType#VIDEO}
* {@link WxConsts.XmlMsgType#NEWS}
* </pre>
*/
@XStreamAlias("MsgType") @XStreamAlias("MsgType")
@XStreamConverter(value = XStreamCDataConverter.class) @XStreamConverter(value = XStreamCDataConverter.class)
private String msgType; private String msgType;
@ -339,19 +359,16 @@ public class WxCpXmlMessage implements Serializable {
/** /**
* 从加密字符串转换. * 从加密字符串转换.
*/ */
public static WxCpXmlMessage fromEncryptedXml( public static WxCpXmlMessage fromEncryptedXml(String encryptedXml, WxCpConfigStorage wxCpConfigStorage,
String encryptedXml, String timestamp, String nonce, String msgSignature) {
WxCpConfigStorage wxCpConfigStorage,
String timestamp, String nonce, String msgSignature) {
WxCpCryptUtil cryptUtil = new WxCpCryptUtil(wxCpConfigStorage); WxCpCryptUtil cryptUtil = new WxCpCryptUtil(wxCpConfigStorage);
String plainText = cryptUtil.decrypt(msgSignature, timestamp, nonce, encryptedXml); String plainText = cryptUtil.decrypt(msgSignature, timestamp, nonce, encryptedXml);
log.debug("解密后的原始xml消息内容{}", plainText);
return fromXml(plainText); return fromXml(plainText);
} }
public static WxCpXmlMessage fromEncryptedXml( public static WxCpXmlMessage fromEncryptedXml(InputStream is, WxCpConfigStorage wxCpConfigStorage,
InputStream is, String timestamp, String nonce, String msgSignature) {
WxCpConfigStorage wxCpConfigStorage,
String timestamp, String nonce, String msgSignature) {
try { try {
return fromEncryptedXml(IOUtils.toString(is, "UTF-8"), wxCpConfigStorage, timestamp, nonce, msgSignature); return fromEncryptedXml(IOUtils.toString(is, "UTF-8"), wxCpConfigStorage, timestamp, nonce, msgSignature);
} catch (IOException e) { } catch (IOException e) {
@ -359,36 +376,6 @@ public class WxCpXmlMessage implements Serializable {
} }
} }
/**
* <pre>
* 当接受用户消息时可能会获得以下值
* {@link WxConsts.XmlMsgType#TEXT}
* {@link WxConsts.XmlMsgType#IMAGE}
* {@link WxConsts.XmlMsgType#VOICE}
* {@link WxConsts.XmlMsgType#VIDEO}
* {@link WxConsts.XmlMsgType#LOCATION}
* {@link WxConsts.XmlMsgType#LINK}
* {@link WxConsts.XmlMsgType#EVENT}
* </pre>
*/
public String getMsgType() {
return this.msgType;
}
/**
* <pre>
* 当发送消息的时候使用
* {@link WxConsts.XmlMsgType#TEXT}
* {@link WxConsts.XmlMsgType#IMAGE}
* {@link WxConsts.XmlMsgType#VOICE}
* {@link WxConsts.XmlMsgType#VIDEO}
* {@link WxConsts.XmlMsgType#NEWS}
* </pre>
*/
public void setMsgType(String msgType) {
this.msgType = msgType;
}
@Override @Override
public String toString() { public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);

View File

@ -1,6 +1,16 @@
package me.chanjar.weixin.cp.demo; package me.chanjar.weixin.cp.demo;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager; import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.cp.WxCpConsts;
import me.chanjar.weixin.cp.api.WxCpService; import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl; import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.WxCpXmlMessage; import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
@ -9,13 +19,6 @@ import me.chanjar.weixin.cp.bean.WxCpXmlOutTextMessage;
import me.chanjar.weixin.cp.config.WxCpConfigStorage; import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import me.chanjar.weixin.cp.message.WxCpMessageHandler; import me.chanjar.weixin.cp.message.WxCpMessageHandler;
import me.chanjar.weixin.cp.message.WxCpMessageRouter; import me.chanjar.weixin.cp.message.WxCpMessageRouter;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
public class WxCpDemoServer { public class WxCpDemoServer {
@ -78,9 +81,26 @@ public class WxCpDemoServer {
}; };
wxCpMessageRouter = new WxCpMessageRouter(wxCpService); wxCpMessageRouter = new WxCpMessageRouter(wxCpService);
wxCpMessageRouter.rule().async(false).content("哈哈") // 拦截内容为哈哈的消息 wxCpMessageRouter.rule()
.handler(handler).end().rule().async(false).content("oauth") .async(false)
.handler(oauth2handler).end(); .content("哈哈") // 拦截内容为哈哈的消息
.handler(handler)
.end()
.rule()
.async(false)
.content("oauth")
.handler(oauth2handler)
.end()
.rule()
.event(WxCpConsts.EventType.CHANGE_CONTACT)
.handler(new WxCpMessageHandler() {
@Override
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService wxCpService, WxSessionManager sessionManager) throws WxErrorException {
System.out.println("通讯录发生变更");
return null;
}
})
.end();
} }
} }

View File

@ -11,6 +11,7 @@ import org.apache.commons.lang3.builder.ToStringStyle;
import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter; import com.thoughtworks.xstream.annotations.XStreamConverter;
import lombok.Data; import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts; import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.util.xml.XStreamCDataConverter; import me.chanjar.weixin.common.util.xml.XStreamCDataConverter;
import me.chanjar.weixin.mp.api.WxMpConfigStorage; import me.chanjar.weixin.mp.api.WxMpConfigStorage;
@ -27,8 +28,9 @@ import me.chanjar.weixin.mp.util.xml.XStreamTransformer;
* *
* @author chanjarster * @author chanjarster
*/ */
@XStreamAlias("xml")
@Data @Data
@Slf4j
@XStreamAlias("xml")
public class WxMpXmlMessage implements Serializable { public class WxMpXmlMessage implements Serializable {
private static final long serialVersionUID = -3586245291677274914L; private static final long serialVersionUID = -3586245291677274914L;
@ -482,10 +484,11 @@ public class WxMpXmlMessage implements Serializable {
* @param nonce 随机串 * @param nonce 随机串
* @param msgSignature 签名串 * @param msgSignature 签名串
*/ */
public static WxMpXmlMessage fromEncryptedXml(String encryptedXml, WxMpConfigStorage wxMpConfigStorage, String timestamp, public static WxMpXmlMessage fromEncryptedXml(String encryptedXml, WxMpConfigStorage wxMpConfigStorage,
String nonce, String msgSignature) { String timestamp, String nonce, String msgSignature) {
WxMpCryptUtil cryptUtil = new WxMpCryptUtil(wxMpConfigStorage); WxMpCryptUtil cryptUtil = new WxMpCryptUtil(wxMpConfigStorage);
String plainText = cryptUtil.decrypt(msgSignature, timestamp, nonce, encryptedXml); String plainText = cryptUtil.decrypt(msgSignature, timestamp, nonce, encryptedXml);
log.debug("解密后的原始xml消息内容{}", plainText);
return fromXml(plainText); return fromXml(plainText);
} }

View File

@ -1,25 +1,28 @@
package me.chanjar.weixin.open.bean.message; package me.chanjar.weixin.open.bean.message;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import org.apache.commons.io.IOUtils;
import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter; import com.thoughtworks.xstream.annotations.XStreamConverter;
import lombok.Data; import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.util.xml.XStreamCDataConverter; import me.chanjar.weixin.common.util.xml.XStreamCDataConverter;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.open.api.WxOpenConfigStorage; import me.chanjar.weixin.open.api.WxOpenConfigStorage;
import me.chanjar.weixin.open.util.WxOpenCryptUtil; import me.chanjar.weixin.open.util.WxOpenCryptUtil;
import me.chanjar.weixin.open.util.xml.XStreamTransformer; import me.chanjar.weixin.open.util.xml.XStreamTransformer;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
/** /**
* @author <a href="https://github.com/007gzs">007</a> * @author <a href="https://github.com/007gzs">007</a>
*/ */
@XStreamAlias("xml")
@Data @Data
@Slf4j
@XStreamAlias("xml")
public class WxOpenXmlMessage implements Serializable { public class WxOpenXmlMessage implements Serializable {
private static final long serialVersionUID = -5641769554709507771L; private static final long serialVersionUID = -5641769554709507771L;
@ -78,30 +81,26 @@ public class WxOpenXmlMessage implements Serializable {
* @param nonce 随机串 * @param nonce 随机串
* @param msgSignature 签名串 * @param msgSignature 签名串
*/ */
public static WxOpenXmlMessage fromEncryptedXml(String encryptedXml, public static WxOpenXmlMessage fromEncryptedXml(String encryptedXml, WxOpenConfigStorage wxOpenConfigStorage,
WxOpenConfigStorage wxOpenConfigStorage, String timestamp, String nonce, String timestamp, String nonce, String msgSignature) {
String msgSignature) {
WxOpenCryptUtil cryptUtil = new WxOpenCryptUtil(wxOpenConfigStorage); WxOpenCryptUtil cryptUtil = new WxOpenCryptUtil(wxOpenConfigStorage);
String plainText = cryptUtil.decrypt(msgSignature, timestamp, nonce, String plainText = cryptUtil.decrypt(msgSignature, timestamp, nonce, encryptedXml);
encryptedXml); log.debug("解密后的原始xml消息内容{}", plainText);
return fromXml(plainText); return fromXml(plainText);
} }
public static WxMpXmlMessage fromEncryptedMpXml(String encryptedXml, public static WxMpXmlMessage fromEncryptedMpXml(String encryptedXml, WxOpenConfigStorage wxOpenConfigStorage,
WxOpenConfigStorage wxOpenConfigStorage, String timestamp, String nonce, String timestamp, String nonce, String msgSignature) {
String msgSignature) {
WxOpenCryptUtil cryptUtil = new WxOpenCryptUtil(wxOpenConfigStorage); WxOpenCryptUtil cryptUtil = new WxOpenCryptUtil(wxOpenConfigStorage);
String plainText = cryptUtil.decrypt(msgSignature, timestamp, nonce, String plainText = cryptUtil.decrypt(msgSignature, timestamp, nonce, encryptedXml);
encryptedXml);
return WxMpXmlMessage.fromXml(plainText); return WxMpXmlMessage.fromXml(plainText);
} }
public static WxOpenXmlMessage fromEncryptedXml(InputStream is, public static WxOpenXmlMessage fromEncryptedXml(InputStream is, WxOpenConfigStorage wxOpenConfigStorage,
WxOpenConfigStorage wxOpenConfigStorage, String timestamp, String nonce, String timestamp, String nonce, String msgSignature) {
String msgSignature) {
try { try {
return fromEncryptedXml(IOUtils.toString(is, "UTF-8"), wxOpenConfigStorage, return fromEncryptedXml(IOUtils.toString(is, "UTF-8"),
timestamp, nonce, msgSignature); wxOpenConfigStorage, timestamp, nonce, msgSignature);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }