mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-04-05 17:38:05 +08:00
优化代码,增加日志
This commit is contained in:
parent
3bc2bbc208
commit
5f1290720e
@ -6,7 +6,7 @@ import java.util.Map;
|
||||
/**
|
||||
* 微信开发所使用到的常量类.
|
||||
*
|
||||
* @author chanjarster & binarywang
|
||||
* @author Daniel Qian & binarywang
|
||||
*/
|
||||
public class WxConsts {
|
||||
/**
|
||||
|
@ -4,6 +4,8 @@ import me.chanjar.weixin.common.error.WxErrorException;
|
||||
|
||||
/**
|
||||
* WxErrorException处理器.
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public interface WxErrorExceptionHandler {
|
||||
|
||||
|
@ -5,6 +5,8 @@ package me.chanjar.weixin.common.api;
|
||||
* 消息重复检查器.
|
||||
* 微信服务器在五秒内收不到响应会断掉连接,并且重新发起请求,总共重试三次
|
||||
* </pre>
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public interface WxMessageDuplicateChecker {
|
||||
|
||||
|
@ -9,6 +9,8 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
* 默认消息重复检查器.
|
||||
* 将每个消息id保存在内存里,每隔5秒清理已经过期的消息id,每个消息id的过期时间是15秒
|
||||
* </pre>
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public class WxMessageInMemoryDuplicateChecker implements WxMessageDuplicateChecker {
|
||||
|
||||
|
@ -1,10 +1,15 @@
|
||||
package me.chanjar.weixin.common.bean;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import lombok.Data;
|
||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* access token.
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
@Data
|
||||
public class WxAccessToken implements Serializable {
|
||||
private static final long serialVersionUID = 8709719312922168909L;
|
||||
|
@ -1,16 +1,16 @@
|
||||
package me.chanjar.weixin.common.bean;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* jspai signature.
|
||||
*
|
||||
* @author chanjarster
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
|
@ -10,6 +10,11 @@ import org.apache.commons.lang3.builder.ToStringStyle;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* menu button.
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
@Data
|
||||
public class WxMenuButton implements Serializable {
|
||||
private static final long serialVersionUID = -1070939403109776555L;
|
||||
|
@ -8,6 +8,11 @@ import org.apache.commons.lang3.builder.ToStringStyle;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* menu rule.
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
@Data
|
||||
public class WxMenuRule implements Serializable {
|
||||
private static final long serialVersionUID = -4587181819499286670L;
|
||||
|
@ -8,6 +8,10 @@ import org.apache.commons.lang3.builder.ToStringStyle;
|
||||
import lombok.Data;
|
||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
@Data
|
||||
public class WxMediaUploadResult implements Serializable {
|
||||
private static final long serialVersionUID = 330834334738622341L;
|
||||
|
@ -1,5 +1,9 @@
|
||||
package me.chanjar.weixin.common.session;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public interface InternalSession {
|
||||
|
||||
/**
|
||||
|
@ -1,5 +1,8 @@
|
||||
package me.chanjar.weixin.common.session;
|
||||
|
||||
/**
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public interface InternalSessionManager {
|
||||
|
||||
/**
|
||||
|
@ -1,11 +1,18 @@
|
||||
package me.chanjar.weixin.common.session;
|
||||
|
||||
import me.chanjar.weixin.common.util.res.StringManager;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import me.chanjar.weixin.common.util.res.StringManager;
|
||||
|
||||
/**
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public class StandardSession implements WxSession, InternalSession {
|
||||
|
||||
/**
|
||||
|
@ -2,6 +2,9 @@ package me.chanjar.weixin.common.session;
|
||||
|
||||
import java.util.Enumeration;
|
||||
|
||||
/**
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public class StandardSessionFacade implements WxSession {
|
||||
|
||||
/**
|
||||
|
@ -1,15 +1,18 @@
|
||||
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.concurrent.ConcurrentHashMap;
|
||||
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 {
|
||||
|
||||
|
@ -19,6 +19,8 @@ package me.chanjar.weixin.common.session;
|
||||
/**
|
||||
* An exception that indicates the maximum number of active sessions has been
|
||||
* reached and the server is refusing to create any new sessions.
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public class TooManyActiveSessionsException
|
||||
extends IllegalStateException {
|
||||
|
@ -2,6 +2,9 @@ package me.chanjar.weixin.common.session;
|
||||
|
||||
import java.util.Enumeration;
|
||||
|
||||
/**
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public interface WxSession {
|
||||
|
||||
Object getAttribute(String name);
|
||||
|
@ -1,5 +1,8 @@
|
||||
package me.chanjar.weixin.common.session;
|
||||
|
||||
/**
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public interface WxSessionManager {
|
||||
|
||||
/**
|
||||
|
@ -13,6 +13,7 @@ import org.apache.commons.lang3.builder.ToStringStyle;
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
import com.thoughtworks.xstream.annotations.XStreamConverter;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.common.util.xml.XStreamCDataConverter;
|
||||
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
|
||||
@ -29,8 +30,9 @@ import me.chanjar.weixin.cp.util.xml.XStreamTransformer;
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
@XStreamAlias("xml")
|
||||
@Data
|
||||
@Slf4j
|
||||
@XStreamAlias("xml")
|
||||
public class WxCpXmlMessage implements Serializable {
|
||||
private static final long serialVersionUID = -1042994982179476410L;
|
||||
|
||||
@ -52,6 +54,24 @@ public class WxCpXmlMessage implements Serializable {
|
||||
@XStreamAlias("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")
|
||||
@XStreamConverter(value = XStreamCDataConverter.class)
|
||||
private String msgType;
|
||||
@ -339,19 +359,16 @@ public class WxCpXmlMessage implements Serializable {
|
||||
/**
|
||||
* 从加密字符串转换.
|
||||
*/
|
||||
public static WxCpXmlMessage fromEncryptedXml(
|
||||
String encryptedXml,
|
||||
WxCpConfigStorage wxCpConfigStorage,
|
||||
String timestamp, String nonce, String msgSignature) {
|
||||
public static WxCpXmlMessage fromEncryptedXml(String encryptedXml, WxCpConfigStorage wxCpConfigStorage,
|
||||
String timestamp, String nonce, String msgSignature) {
|
||||
WxCpCryptUtil cryptUtil = new WxCpCryptUtil(wxCpConfigStorage);
|
||||
String plainText = cryptUtil.decrypt(msgSignature, timestamp, nonce, encryptedXml);
|
||||
log.debug("解密后的原始xml消息内容:{}", plainText);
|
||||
return fromXml(plainText);
|
||||
}
|
||||
|
||||
public static WxCpXmlMessage fromEncryptedXml(
|
||||
InputStream is,
|
||||
WxCpConfigStorage wxCpConfigStorage,
|
||||
String timestamp, String nonce, String msgSignature) {
|
||||
public static WxCpXmlMessage fromEncryptedXml(InputStream is, WxCpConfigStorage wxCpConfigStorage,
|
||||
String timestamp, String nonce, String msgSignature) {
|
||||
try {
|
||||
return fromEncryptedXml(IOUtils.toString(is, "UTF-8"), wxCpConfigStorage, timestamp, nonce, msgSignature);
|
||||
} 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
|
||||
public String toString() {
|
||||
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
|
||||
|
@ -1,6 +1,16 @@
|
||||
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.cp.WxCpConsts;
|
||||
import me.chanjar.weixin.cp.api.WxCpService;
|
||||
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
|
||||
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.message.WxCpMessageHandler;
|
||||
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 {
|
||||
|
||||
@ -78,9 +81,26 @@ public class WxCpDemoServer {
|
||||
};
|
||||
|
||||
wxCpMessageRouter = new WxCpMessageRouter(wxCpService);
|
||||
wxCpMessageRouter.rule().async(false).content("哈哈") // 拦截内容为“哈哈”的消息
|
||||
.handler(handler).end().rule().async(false).content("oauth")
|
||||
.handler(oauth2handler).end();
|
||||
wxCpMessageRouter.rule()
|
||||
.async(false)
|
||||
.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();
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ import org.apache.commons.lang3.builder.ToStringStyle;
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
import com.thoughtworks.xstream.annotations.XStreamConverter;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.common.util.xml.XStreamCDataConverter;
|
||||
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
|
||||
@ -27,8 +28,9 @@ import me.chanjar.weixin.mp.util.xml.XStreamTransformer;
|
||||
*
|
||||
* @author chanjarster
|
||||
*/
|
||||
@XStreamAlias("xml")
|
||||
@Data
|
||||
@Slf4j
|
||||
@XStreamAlias("xml")
|
||||
public class WxMpXmlMessage implements Serializable {
|
||||
private static final long serialVersionUID = -3586245291677274914L;
|
||||
|
||||
@ -482,10 +484,11 @@ public class WxMpXmlMessage implements Serializable {
|
||||
* @param nonce 随机串
|
||||
* @param msgSignature 签名串
|
||||
*/
|
||||
public static WxMpXmlMessage fromEncryptedXml(String encryptedXml, WxMpConfigStorage wxMpConfigStorage, String timestamp,
|
||||
String nonce, String msgSignature) {
|
||||
public static WxMpXmlMessage fromEncryptedXml(String encryptedXml, WxMpConfigStorage wxMpConfigStorage,
|
||||
String timestamp, String nonce, String msgSignature) {
|
||||
WxMpCryptUtil cryptUtil = new WxMpCryptUtil(wxMpConfigStorage);
|
||||
String plainText = cryptUtil.decrypt(msgSignature, timestamp, nonce, encryptedXml);
|
||||
log.debug("解密后的原始xml消息内容:{}", plainText);
|
||||
return fromXml(plainText);
|
||||
}
|
||||
|
||||
|
@ -1,25 +1,28 @@
|
||||
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.XStreamConverter;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.util.xml.XStreamCDataConverter;
|
||||
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
|
||||
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
|
||||
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
|
||||
import me.chanjar.weixin.open.util.WxOpenCryptUtil;
|
||||
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>
|
||||
*/
|
||||
@XStreamAlias("xml")
|
||||
@Data
|
||||
@Slf4j
|
||||
@XStreamAlias("xml")
|
||||
public class WxOpenXmlMessage implements Serializable {
|
||||
private static final long serialVersionUID = -5641769554709507771L;
|
||||
|
||||
@ -78,30 +81,26 @@ public class WxOpenXmlMessage implements Serializable {
|
||||
* @param nonce 随机串
|
||||
* @param msgSignature 签名串
|
||||
*/
|
||||
public static WxOpenXmlMessage fromEncryptedXml(String encryptedXml,
|
||||
WxOpenConfigStorage wxOpenConfigStorage, String timestamp, String nonce,
|
||||
String msgSignature) {
|
||||
public static WxOpenXmlMessage fromEncryptedXml(String encryptedXml, WxOpenConfigStorage wxOpenConfigStorage,
|
||||
String timestamp, String nonce, String msgSignature) {
|
||||
WxOpenCryptUtil cryptUtil = new WxOpenCryptUtil(wxOpenConfigStorage);
|
||||
String plainText = cryptUtil.decrypt(msgSignature, timestamp, nonce,
|
||||
encryptedXml);
|
||||
String plainText = cryptUtil.decrypt(msgSignature, timestamp, nonce, encryptedXml);
|
||||
log.debug("解密后的原始xml消息内容:{}", plainText);
|
||||
return fromXml(plainText);
|
||||
}
|
||||
|
||||
public static WxMpXmlMessage fromEncryptedMpXml(String encryptedXml,
|
||||
WxOpenConfigStorage wxOpenConfigStorage, String timestamp, String nonce,
|
||||
String msgSignature) {
|
||||
public static WxMpXmlMessage fromEncryptedMpXml(String encryptedXml, WxOpenConfigStorage wxOpenConfigStorage,
|
||||
String timestamp, String nonce, String msgSignature) {
|
||||
WxOpenCryptUtil cryptUtil = new WxOpenCryptUtil(wxOpenConfigStorage);
|
||||
String plainText = cryptUtil.decrypt(msgSignature, timestamp, nonce,
|
||||
encryptedXml);
|
||||
String plainText = cryptUtil.decrypt(msgSignature, timestamp, nonce, encryptedXml);
|
||||
return WxMpXmlMessage.fromXml(plainText);
|
||||
}
|
||||
|
||||
public static WxOpenXmlMessage fromEncryptedXml(InputStream is,
|
||||
WxOpenConfigStorage wxOpenConfigStorage, String timestamp, String nonce,
|
||||
String msgSignature) {
|
||||
public static WxOpenXmlMessage fromEncryptedXml(InputStream is, WxOpenConfigStorage wxOpenConfigStorage,
|
||||
String timestamp, String nonce, String msgSignature) {
|
||||
try {
|
||||
return fromEncryptedXml(IOUtils.toString(is, "UTF-8"), wxOpenConfigStorage,
|
||||
timestamp, nonce, msgSignature);
|
||||
return fromEncryptedXml(IOUtils.toString(is, "UTF-8"),
|
||||
wxOpenConfigStorage, timestamp, nonce, msgSignature);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user