weixin4j-server:新增WeixinMessageKeyDefiner类

This commit is contained in:
jinyu 2015-05-18 21:26:49 +08:00
parent 1a212f3cbf
commit 5a7b729873
15 changed files with 215 additions and 105 deletions

View File

@ -292,3 +292,7 @@
+ **weixin4j-server**: 去掉SLF4J-API依赖
+ **weixin4j-server**: released 1.0.0!
* 2015-05-18
+ **weixin4j-server**: 新增WeixinMessageKeyDefiner类

View File

@ -41,3 +41,7 @@
+ 去掉SLF4J-API依赖
+ released 1.0.0!
* 2015-05-18
+ 新增WeixinMessageKeyDefiner类

View File

@ -1,4 +1,4 @@
package com.foxinmy.weixin4j.bean;
package com.foxinmy.weixin4j.dispatcher;
import com.foxinmy.weixin4j.exception.WeixinException;

View File

@ -23,17 +23,20 @@ import javax.xml.bind.Unmarshaller;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import com.foxinmy.weixin4j.bean.BeanFactory;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.handler.MessageHandlerAdapter;
import com.foxinmy.weixin4j.handler.WeixinMessageHandler;
import com.foxinmy.weixin4j.interceptor.WeixinMessageInterceptor;
import com.foxinmy.weixin4j.messagekey.DefaultMessageKeyDefiner;
import com.foxinmy.weixin4j.messagekey.WeixinMessageKeyDefiner;
import com.foxinmy.weixin4j.request.WeixinRequest;
import com.foxinmy.weixin4j.response.WeixinResponse;
import com.foxinmy.weixin4j.type.AccountType;
import com.foxinmy.weixin4j.util.ClassUtil;
import com.foxinmy.weixin4j.util.Consts;
import com.foxinmy.weixin4j.util.HttpUtil;
import com.foxinmy.weixin4j.util.ReflectionUtil;
import com.foxinmy.weixin4j.xml.CruxMessageHandler;
/**
* 微信消息分发器
@ -45,8 +48,9 @@ import com.foxinmy.weixin4j.util.ReflectionUtil;
* @see com.foxinmy.weixin4j.handler.WeixinMessageHandler
* @see com.foxinmy.weixin4j.interceptor.WeixinMessageInterceptor
* @see com.foxinmy.weixin4j.dispatcher.WeixinMessageMatcher
* @see com.foxinmy.weixin4j.messagekey.WeixinMessageKeyDefiner
* @see com.foxinmy.weixin4j.dispatcher.MessageHandlerExecutor
* @see com.foxinmy.weixin4j.bean.BeanFactory
* @see com.foxinmy.weixin4j.dispatcher.BeanFactory
*/
public class WeixinMessageDispatcher {
@ -82,14 +86,23 @@ public class WeixinMessageDispatcher {
* 消息匹配
*/
private WeixinMessageMatcher messageMatcher;
/**
* 消息key
*/
private WeixinMessageKeyDefiner messageKeyDefiner;
/**
* 消息转换
*/
private Map<Class<?>, Unmarshaller> messageUnmarshaller;
public WeixinMessageDispatcher() {
messageMatcher = new WeixinMessageMatcher();
this(new DefaultMessageKeyDefiner());
}
public WeixinMessageDispatcher(WeixinMessageKeyDefiner messageKeyDefiner) {
messageMatcher = new WeixinMessageMatcher(messageKeyDefiner);
messageUnmarshaller = new HashMap<Class<?>, Unmarshaller>();
this.messageKeyDefiner = messageKeyDefiner;
}
/**
@ -104,8 +117,11 @@ public class WeixinMessageDispatcher {
* @throws WeixinException
*/
public void doDispatch(final ChannelHandlerContext context,
final WeixinRequest request, final String messageKey)
final WeixinRequest request, final CruxMessageHandler messageHandler)
throws WeixinException {
String messageKey = messageKeyDefiner.defineMessageKey(
messageHandler.getMsgType(), messageHandler.getEventType(),
messageHandler.getAccountType());
Class<?> targetClass = messageMatcher.find(messageKey);
Object message = request.getOriginalContent();
if (targetClass != null) {
@ -405,4 +421,22 @@ public class WeixinMessageDispatcher {
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
public void registMessageMatch(String messageType, String eventType,
AccountType accountType, Class<?> messageClass) {
registMessageMatch(messageKeyDefiner.defineMessageKey(messageType,
eventType, accountType), messageClass);
}
public void registMessageMatch(String messageKey, Class<?> messageClass) {
messageMatcher.regist(messageKey, messageClass);
}
public WeixinMessageMatcher getMessageMatcher() {
return this.messageMatcher;
}
public WeixinMessageKeyDefiner getMessageKeyDefiner() {
return this.messageKeyDefiner;
}
}

View File

@ -12,6 +12,7 @@ import com.foxinmy.weixin4j.message.TextMessage;
import com.foxinmy.weixin4j.message.VideoMessage;
import com.foxinmy.weixin4j.message.VoiceMessage;
import com.foxinmy.weixin4j.message.event.LocationEventMessage;
import com.foxinmy.weixin4j.messagekey.WeixinMessageKeyDefiner;
import com.foxinmy.weixin4j.mp.event.KfCloseEventMessage;
import com.foxinmy.weixin4j.mp.event.KfCreateEventMessage;
import com.foxinmy.weixin4j.mp.event.KfSwitchEventMessage;
@ -20,6 +21,7 @@ import com.foxinmy.weixin4j.mp.event.ScanEventMessage;
import com.foxinmy.weixin4j.mp.event.TemplatesendjobfinishMessage;
import com.foxinmy.weixin4j.qy.event.BatchjobresultMessage;
import com.foxinmy.weixin4j.qy.event.EnterAgentEventMessage;
import com.foxinmy.weixin4j.type.AccountType;
import com.foxinmy.weixin4j.type.EventType;
import com.foxinmy.weixin4j.type.MessageType;
@ -31,24 +33,20 @@ import com.foxinmy.weixin4j.type.MessageType;
* @date 2015年5月17日
* @since JDK 1.7
* @see com.foxinmy.weixin4j.request.WeixinMessage
* @see com.foxinmy.weixin4j.messagekey.WeixinMessageKeyDefiner
*/
public class WeixinMessageMatcher {
private final InternalLogger logger = InternalLoggerFactory
.getInstance(getClass());
public static final String MESSAGEKEY_MP_TAG = "mp";
public static final String MESSAGEKEY_SEPARATOR = ":";
public static final String MESSAGEKEY_MP_SEPARATOR = MESSAGEKEY_MP_TAG
+ MESSAGEKEY_SEPARATOR;
public static final String MESSAGEKEY_QY_TAG = "qy";
public static final String MESSAGEKEY_QY_SEPARATOR = MESSAGEKEY_QY_TAG
+ MESSAGEKEY_SEPARATOR;
private final Map<String, Class<?>> key2ClassMap;
private final Map<Class<?>, String> class2KeyMap;
public WeixinMessageMatcher() {
private final WeixinMessageKeyDefiner messageKeyDefiner;
public WeixinMessageMatcher(WeixinMessageKeyDefiner messageKeyDefiner) {
this.messageKeyDefiner = messageKeyDefiner;
key2ClassMap = new HashMap<String, Class<?>>();
class2KeyMap = new HashMap<Class<?>, String>();
init0();
@ -58,27 +56,35 @@ public class WeixinMessageMatcher {
logger.info("detected message for events: {}", key2ClassMap.keySet());
}
public WeixinMessageKeyDefiner getMessageKeyDefiner() {
return messageKeyDefiner;
}
private String messageKey(MessageType messageType) {
return messageKeyDefiner.defineMessageKey(messageType.name(), null,
null);
}
private String mpEventMessageKey(EventType eventType) {
return messageKeyDefiner.defineMessageKey(MessageType.event.name(),
eventType.name(), AccountType.MP);
}
private String qyEventMessageKey(EventType eventType) {
return messageKeyDefiner.defineMessageKey(MessageType.event.name(),
eventType.name(), AccountType.QY);
}
private void init0() {
// /////////////////////////////////////////////////
/******************** 普通消息 ********************/
// /////////////////////////////////////////////////
String messageKey = MessageType.text.name();
Class<?> clazz = TextMessage.class;
regist(messageKey, clazz);
messageKey = MessageType.image.name();
clazz = ImageMessage.class;
regist(messageKey, clazz);
messageKey = MessageType.voice.name();
clazz = VoiceMessage.class;
regist(messageKey, clazz);
messageKey = MessageType.video.name();
clazz = VideoMessage.class;
regist(messageKey, clazz);
messageKey = MessageType.shortvideo.name();
regist(messageKey, clazz);
messageKey = MessageType.location.name();
clazz = LocationMessage.class;
regist(messageKey, clazz);
regist(messageKey(MessageType.text), TextMessage.class);
regist(messageKey(MessageType.image), ImageMessage.class);
regist(messageKey(MessageType.voice), VoiceMessage.class);
regist(messageKey(MessageType.video), VideoMessage.class);
regist(messageKey(MessageType.shortvideo), VideoMessage.class);
regist(messageKey(MessageType.location), LocationMessage.class);
}
private void init1() {
@ -87,54 +93,54 @@ public class WeixinMessageMatcher {
// /////////////////////////////////////////////////
for (EventType eventType : new EventType[] { EventType.subscribe,
EventType.unsubscribe }) {
regist(mpEventMessagKey(eventType),
regist(mpEventMessageKey(eventType),
com.foxinmy.weixin4j.mp.event.ScribeEventMessage.class);
}
for (EventType eventType : new EventType[] { EventType.subscribe,
EventType.unsubscribe }) {
regist(qyEventMessagKey(eventType),
regist(qyEventMessageKey(eventType),
com.foxinmy.weixin4j.qy.event.ScribeEventMessage.class);
}
Class<?> clazz = LocationEventMessage.class;
regist(mpEventMessagKey(EventType.location), clazz);
regist(qyEventMessagKey(EventType.location), clazz);
regist(mpEventMessageKey(EventType.location), clazz);
regist(qyEventMessageKey(EventType.location), clazz);
for (EventType eventType : new EventType[] { EventType.click,
EventType.view }) {
clazz = com.foxinmy.weixin4j.message.event.MenuEventMessage.class;
regist(mpEventMessagKey(eventType), clazz);
regist(qyEventMessagKey(eventType), clazz);
regist(mpEventMessageKey(eventType), clazz);
regist(qyEventMessageKey(eventType), clazz);
}
for (EventType eventType : new EventType[] { EventType.scancode_push,
EventType.scancode_waitmsg }) {
clazz = com.foxinmy.weixin4j.message.event.MenuScanEventMessage.class;
regist(mpEventMessagKey(eventType), clazz);
regist(qyEventMessagKey(eventType), clazz);
regist(mpEventMessageKey(eventType), clazz);
regist(qyEventMessageKey(eventType), clazz);
}
for (EventType eventType : new EventType[] { EventType.pic_sysphoto,
EventType.pic_photo_or_album, EventType.pic_weixin }) {
clazz = com.foxinmy.weixin4j.message.event.MenuPhotoEventMessage.class;
regist(mpEventMessagKey(eventType), clazz);
regist(qyEventMessagKey(eventType), clazz);
regist(mpEventMessageKey(eventType), clazz);
regist(qyEventMessageKey(eventType), clazz);
}
clazz = com.foxinmy.weixin4j.message.event.MenuLocationEventMessage.class;
regist(mpEventMessagKey(EventType.location_select), clazz);
regist(qyEventMessagKey(EventType.location_select), clazz);
regist(mpEventMessageKey(EventType.location_select), clazz);
regist(qyEventMessageKey(EventType.location_select), clazz);
}
private void init2() {
// /////////////////////////////////////////////////
/******************** 公众平台事件消息 ********************/
// /////////////////////////////////////////////////
regist(mpEventMessagKey(EventType.scan), ScanEventMessage.class);
regist(mpEventMessagKey(EventType.masssendjobfinish),
regist(mpEventMessageKey(EventType.scan), ScanEventMessage.class);
regist(mpEventMessageKey(EventType.masssendjobfinish),
MassEventMessage.class);
regist(mpEventMessagKey(EventType.templatesendjobfinish),
regist(mpEventMessageKey(EventType.templatesendjobfinish),
TemplatesendjobfinishMessage.class);
regist(mpEventMessagKey(EventType.kf_create_session),
regist(mpEventMessageKey(EventType.kf_create_session),
KfCreateEventMessage.class);
regist(mpEventMessagKey(EventType.kf_close_session),
regist(mpEventMessageKey(EventType.kf_close_session),
KfCloseEventMessage.class);
regist(mpEventMessagKey(EventType.kf_switch_session),
regist(mpEventMessageKey(EventType.kf_switch_session),
KfSwitchEventMessage.class);
}
@ -142,36 +148,12 @@ public class WeixinMessageMatcher {
// /////////////////////////////////////////////////
/******************** 企业号事件消息 ********************/
// /////////////////////////////////////////////////
regist(qyEventMessagKey(EventType.batch_job_result),
regist(qyEventMessageKey(EventType.batch_job_result),
BatchjobresultMessage.class);
regist(qyEventMessagKey(EventType.enter_agent),
regist(qyEventMessageKey(EventType.enter_agent),
EnterAgentEventMessage.class);
}
/**
* 公众平台事件消息的唯一messageKey
*
* @param eventType
* @return
*/
protected String mpEventMessagKey(EventType eventType) {
return String.format("%s%s%s%s", MESSAGEKEY_MP_SEPARATOR,
MessageType.event.name(), MESSAGEKEY_SEPARATOR,
eventType.name());
}
/**
* 企业号事件消息的唯一messageKey
*
* @param eventType
* @return
*/
protected String qyEventMessagKey(EventType eventType) {
return String.format("%s%s%s%s", MESSAGEKEY_QY_SEPARATOR,
MessageType.event.name(), MESSAGEKEY_SEPARATOR,
eventType.name());
}
/**
* 注册一个消息类型
*

View File

@ -0,0 +1,36 @@
package com.foxinmy.weixin4j.messagekey;
import com.foxinmy.weixin4j.type.AccountType;
import com.foxinmy.weixin4j.util.StringUtil;
/**
* 默认的messageKey实现
*
* @className DefaultMessageKeyDefiner
* @author jy
* @date 2015年5月18日
* @since JDK 1.7
* @see
*/
public class DefaultMessageKeyDefiner implements WeixinMessageKeyDefiner {
private static final String MESSAGEKEY_SEPARATOR = ":";
@Override
public String defineMessageKey(String messageType, String eventType,
AccountType accountType) {
StringBuilder messageKey = new StringBuilder();
if (!StringUtil.isBlank(messageType)) {
messageKey.append(messageType.toLowerCase());
}
if (accountType != null) {
messageKey.insert(0, String.format("%s%s", accountType.name()
.toLowerCase(), MESSAGEKEY_SEPARATOR));
}
if (!StringUtil.isBlank(eventType)) {
messageKey.append(MESSAGEKEY_SEPARATOR).append(
eventType.toLowerCase());
}
return messageKey.toString();
}
}

View File

@ -0,0 +1,29 @@
package com.foxinmy.weixin4j.messagekey;
import com.foxinmy.weixin4j.type.AccountType;
/**
* 微信消息key的定义
*
* @className WeixinMessageKey
* @author jy
* @date 2015年5月18日
* @since JDK 1.7
* @see
*/
public interface WeixinMessageKeyDefiner {
/**
* 声明messageKey
*
* @param messageType
* 消息类型
* @param eventType
* 事件类型
* @param accountType
* 账号类型
* @return messageKey
*/
public String defineMessageKey(String messageType, String eventType,
AccountType accountType);
}

View File

@ -10,10 +10,10 @@ import io.netty.util.internal.logging.InternalLoggerFactory;
import java.util.List;
import java.util.Map;
import com.foxinmy.weixin4j.bean.AesToken;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.request.WeixinRequest;
import com.foxinmy.weixin4j.type.EncryptType;
import com.foxinmy.weixin4j.util.AesToken;
import com.foxinmy.weixin4j.util.Consts;
import com.foxinmy.weixin4j.util.MessageUtil;
import com.foxinmy.weixin4j.util.StringUtil;

View File

@ -10,11 +10,11 @@ import io.netty.handler.codec.http.HttpMethod;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import com.foxinmy.weixin4j.bean.AesToken;
import com.foxinmy.weixin4j.dispatcher.WeixinMessageDispatcher;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.request.WeixinRequest;
import com.foxinmy.weixin4j.type.EncryptType;
import com.foxinmy.weixin4j.util.AesToken;
import com.foxinmy.weixin4j.util.Consts;
import com.foxinmy.weixin4j.util.HttpUtil;
import com.foxinmy.weixin4j.util.MessageUtil;
@ -108,7 +108,6 @@ public class WeixinRequestHandler extends
ctx.channel().attr(Consts.ACCOUNTOPENID_KEY)
.set(messageHandler.getToUserName());
}
messageDispatcher.doDispatch(ctx, request,
messageHandler.getMessageKey());
messageDispatcher.doDispatch(ctx, request, messageHandler);
}
}

View File

@ -8,11 +8,11 @@ import io.netty.util.internal.logging.InternalLoggerFactory;
import java.util.List;
import com.foxinmy.weixin4j.bean.AesToken;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.response.BlankResponse;
import com.foxinmy.weixin4j.response.WeixinResponse;
import com.foxinmy.weixin4j.type.EncryptType;
import com.foxinmy.weixin4j.util.AesToken;
import com.foxinmy.weixin4j.util.Consts;
import com.foxinmy.weixin4j.util.HttpUtil;
import com.foxinmy.weixin4j.util.MessageUtil;

View File

@ -6,9 +6,9 @@ import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import com.foxinmy.weixin4j.bean.AesToken;
import com.foxinmy.weixin4j.dispatcher.WeixinMessageDispatcher;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.util.AesToken;
/**
* 微信消息服务器初始化

View File

@ -12,13 +12,15 @@ import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import com.foxinmy.weixin4j.bean.AesToken;
import com.foxinmy.weixin4j.bean.BeanFactory;
import com.foxinmy.weixin4j.dispatcher.BeanFactory;
import com.foxinmy.weixin4j.dispatcher.WeixinMessageDispatcher;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.handler.WeixinMessageHandler;
import com.foxinmy.weixin4j.interceptor.WeixinMessageInterceptor;
import com.foxinmy.weixin4j.messagekey.DefaultMessageKeyDefiner;
import com.foxinmy.weixin4j.messagekey.WeixinMessageKeyDefiner;
import com.foxinmy.weixin4j.socket.WeixinServerInitializer;
import com.foxinmy.weixin4j.util.AesToken;
/**
* 微信netty服务启动程序
@ -30,7 +32,7 @@ import com.foxinmy.weixin4j.socket.WeixinServerInitializer;
* @see com.foxinmy.weixin4j.handler.WeixinMessageHandler
* @see com.foxinmy.weixin4j.interceptor.WeixinMessageInterceptor
* @see com.foxinmy.weixin4j.dispatcher.WeixinMessageDispatcher
* @see com.foxinmy.weixin4j.bean.BeanFactory
* @see com.foxinmy.weixin4j.dispatcher.BeanFactory
*/
public final class WeixinServerBootstrap {
@ -91,10 +93,15 @@ public final class WeixinServerBootstrap {
}
public WeixinServerBootstrap(AesToken aesToken) {
this(aesToken, new DefaultMessageKeyDefiner());
}
public WeixinServerBootstrap(AesToken aesToken,
WeixinMessageKeyDefiner messageKeyDefiner) {
this.aesToken = aesToken;
this.messageHandlerList = new LinkedList<WeixinMessageHandler>();
this.messageInterceptorList = new LinkedList<WeixinMessageInterceptor>();
this.messageDispatcher = new WeixinMessageDispatcher();
this.messageDispatcher = new WeixinMessageDispatcher(messageKeyDefiner);
}
/**

View File

@ -0,0 +1,21 @@
package com.foxinmy.weixin4j.type;
/**
* 账号类型
*
* @className AccountType
* @author jy
* @date 2015年5月18日
* @since JDK 1.7
* @see
*/
public enum AccountType {
/**
* 公众号
*/
MP,
/**
* 企业号
*/
QY
}

View File

@ -1,4 +1,4 @@
package com.foxinmy.weixin4j.bean;
package com.foxinmy.weixin4j.util;
import java.io.Serializable;

View File

@ -9,8 +9,7 @@ import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
import com.foxinmy.weixin4j.dispatcher.WeixinMessageMatcher;
import com.foxinmy.weixin4j.type.MessageType;
import com.foxinmy.weixin4j.type.AccountType;
import com.foxinmy.weixin4j.util.Consts;
import com.foxinmy.weixin4j.util.StringUtil;
@ -64,21 +63,16 @@ public class CruxMessageHandler extends DefaultHandler {
this.content = new String(ch, start, length);
}
public String getMessageKey() {
StringBuilder uniqueKey = new StringBuilder();
uniqueKey.append(msgType);
if (msgType.equals(MessageType.event.name())) {
if (StringUtil.isBlank(agentId)) {
uniqueKey.insert(0,
WeixinMessageMatcher.MESSAGEKEY_MP_SEPARATOR);
} else {
uniqueKey.insert(0,
WeixinMessageMatcher.MESSAGEKEY_QY_SEPARATOR);
public AccountType getAccountType() {
return StringUtil.isBlank(agentId) ? AccountType.MP : AccountType.QY;
}
uniqueKey.append(WeixinMessageMatcher.MESSAGEKEY_SEPARATOR).append(
eventType);
public String getMsgType() {
return msgType;
}
return uniqueKey.toString().toLowerCase();
public String getEventType() {
return eventType;
}
public String getFromUserName() {