MessageTransferHandler替换CruxMessageHandler

This commit is contained in:
jinyu 2015-08-09 11:38:42 +08:00
parent 55246ab9b4
commit e454969421
4 changed files with 71 additions and 47 deletions

View File

@ -29,12 +29,12 @@ import com.foxinmy.weixin4j.request.WeixinMessage;
import com.foxinmy.weixin4j.request.WeixinRequest; import com.foxinmy.weixin4j.request.WeixinRequest;
import com.foxinmy.weixin4j.response.BlankResponse; import com.foxinmy.weixin4j.response.BlankResponse;
import com.foxinmy.weixin4j.response.WeixinResponse; import com.foxinmy.weixin4j.response.WeixinResponse;
import com.foxinmy.weixin4j.socket.WeixinMessageTransfer;
import com.foxinmy.weixin4j.type.AccountType; import com.foxinmy.weixin4j.type.AccountType;
import com.foxinmy.weixin4j.util.ClassUtil; import com.foxinmy.weixin4j.util.ClassUtil;
import com.foxinmy.weixin4j.util.Consts; import com.foxinmy.weixin4j.util.Consts;
import com.foxinmy.weixin4j.util.HttpUtil; import com.foxinmy.weixin4j.util.HttpUtil;
import com.foxinmy.weixin4j.util.ReflectionUtil; import com.foxinmy.weixin4j.util.ReflectionUtil;
import com.foxinmy.weixin4j.xml.CruxMessageHandler;
/** /**
* 微信消息分发器 * 微信消息分发器
@ -108,22 +108,22 @@ public class WeixinMessageDispatcher {
* 上下文环境 * 上下文环境
* @param request * @param request
* 微信请求 * 微信请求
* @param cruxMessage * @param messageTransfer
* 微信的关键消息 * 微信消息
* @throws WeixinException * @throws WeixinException
*/ */
public void doDispatch(final ChannelHandlerContext context, public void doDispatch(final ChannelHandlerContext context,
final WeixinRequest request, final CruxMessageHandler cruxMessage) final WeixinRequest request,
throws WeixinException { final WeixinMessageTransfer messageTransfer) throws WeixinException {
WeixinMessageKey messageKey = defineMessageKey( WeixinMessageKey messageKey = defineMessageKey(
cruxMessage.getMsgType(), cruxMessage.getEventType(), messageTransfer.getMsgType(), messageTransfer.getEventType(),
cruxMessage.getAccountType()); messageTransfer.getAccountType());
Class<? extends WeixinMessage> targetClass = messageMatcher Class<? extends WeixinMessage> targetClass = messageMatcher
.match(messageKey); .match(messageKey);
Object message = messageRead(request.getOriginalContent(), targetClass); Object message = messageRead(request.getOriginalContent(), targetClass);
logger.info("define '{}' matched '{}'", messageKey, targetClass); logger.info("define '{}' matched '{}'", messageKey, targetClass);
MessageHandlerExecutor handlerExecutor = getHandlerExecutor(context, MessageHandlerExecutor handlerExecutor = getHandlerExecutor(context,
request, messageKey, message, cruxMessage.getNodeNames()); request, messageKey, message, messageTransfer.getNodeNames());
if (handlerExecutor == null if (handlerExecutor == null
|| handlerExecutor.getMessageHandler() == null) { || handlerExecutor.getMessageHandler() == null) {
noHandlerFound(context, request, message); noHandlerFound(context, request, message);
@ -136,7 +136,7 @@ public class WeixinMessageDispatcher {
WeixinResponse response = null; WeixinResponse response = null;
try { try {
response = handlerExecutor.getMessageHandler().doHandle(request, response = handlerExecutor.getMessageHandler().doHandle(request,
message, cruxMessage.getNodeNames()); message, messageTransfer.getNodeNames());
// fixed.. // fixed..
if (response == null) { if (response == null) {
response = BlankResponse.global; response = BlankResponse.global;

View File

@ -1,7 +1,9 @@
package com.foxinmy.weixin4j.socket; package com.foxinmy.weixin4j.socket;
import java.io.Serializable; import java.io.Serializable;
import java.util.Set;
import com.foxinmy.weixin4j.type.AccountType;
import com.foxinmy.weixin4j.type.EncryptType; import com.foxinmy.weixin4j.type.EncryptType;
import com.foxinmy.weixin4j.util.AesToken; import com.foxinmy.weixin4j.util.AesToken;
@ -26,7 +28,6 @@ public class WeixinMessageTransfer implements Serializable {
* 加密类型 * 加密类型
*/ */
private EncryptType encryptType; private EncryptType encryptType;
/** /**
* 消息接收方 * 消息接收方
*/ */
@ -35,13 +36,34 @@ public class WeixinMessageTransfer implements Serializable {
* 消息发送方 * 消息发送方
*/ */
private String fromUserName; private String fromUserName;
/**
* 账号
*/
private AccountType accountType;
/**
* 消息类型
*/
private String msgType;
/**
* 事件类型
*/
private String eventType;
/**
* 节点集合
*/
private Set<String> nodeNames;
public WeixinMessageTransfer(AesToken aesToken, EncryptType encryptType, public WeixinMessageTransfer(AesToken aesToken, EncryptType encryptType,
String toUserName, String fromUserName) { String toUserName, String fromUserName, AccountType accountType,
String msgType, String eventType, Set<String> nodeNames) {
this.aesToken = aesToken; this.aesToken = aesToken;
this.encryptType = encryptType; this.encryptType = encryptType;
this.toUserName = toUserName; this.toUserName = toUserName;
this.fromUserName = fromUserName; this.fromUserName = fromUserName;
this.accountType = accountType;
this.msgType = msgType;
this.eventType = eventType;
this.nodeNames = nodeNames;
} }
public AesToken getAesToken() { public AesToken getAesToken() {
@ -60,10 +82,28 @@ public class WeixinMessageTransfer implements Serializable {
return fromUserName; return fromUserName;
} }
public AccountType getAccountType() {
return accountType;
}
public String getMsgType() {
return msgType;
}
public String getEventType() {
return eventType;
}
public Set<String> getNodeNames() {
return nodeNames;
}
@Override @Override
public String toString() { public String toString() {
return "WeixinMessageTransfer [aesToken=" + aesToken + ", encryptType=" return "WeixinMessageTransfer [aesToken=" + aesToken + ", encryptType="
+ encryptType + ", toUserName=" + toUserName + encryptType + ", toUserName=" + toUserName
+ ", fromUserName=" + fromUserName + "]"; + ", fromUserName=" + fromUserName + ", accountType="
+ accountType + ", msgType=" + msgType + ", eventType="
+ eventType + ", nodeNames=" + nodeNames + "]";
} }
} }

View File

@ -20,7 +20,7 @@ import com.foxinmy.weixin4j.util.Consts;
import com.foxinmy.weixin4j.util.HttpUtil; import com.foxinmy.weixin4j.util.HttpUtil;
import com.foxinmy.weixin4j.util.MessageUtil; import com.foxinmy.weixin4j.util.MessageUtil;
import com.foxinmy.weixin4j.util.StringUtil; import com.foxinmy.weixin4j.util.StringUtil;
import com.foxinmy.weixin4j.xml.CruxMessageHandler; import com.foxinmy.weixin4j.xml.MessageTransferHandler;
/** /**
* 微信请求处理类 * 微信请求处理类
@ -110,12 +110,10 @@ public class WeixinRequestHandler extends
.addListener(ChannelFutureListener.CLOSE); .addListener(ChannelFutureListener.CLOSE);
return; return;
} }
CruxMessageHandler cruxMessage = CruxMessageHandler.parser(request WeixinMessageTransfer messageTransfer = MessageTransferHandler.parser(
.getOriginalContent()); request.getOriginalContent(), aesToken,
WeixinMessageTransfer messageTransfer = new WeixinMessageTransfer( request.getEncryptType());
aesToken, request.getEncryptType(),
cruxMessage.getToUserName(), cruxMessage.getFromUserName());
ctx.channel().attr(Consts.MESSAGE_TRANSFER_KEY).set(messageTransfer); ctx.channel().attr(Consts.MESSAGE_TRANSFER_KEY).set(messageTransfer);
messageDispatcher.doDispatch(ctx, request, cruxMessage); messageDispatcher.doDispatch(ctx, request, messageTransfer);
} }
} }

View File

@ -11,20 +11,23 @@ import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory; import org.xml.sax.helpers.XMLReaderFactory;
import com.foxinmy.weixin4j.socket.WeixinMessageTransfer;
import com.foxinmy.weixin4j.type.AccountType; import com.foxinmy.weixin4j.type.AccountType;
import com.foxinmy.weixin4j.type.EncryptType;
import com.foxinmy.weixin4j.util.AesToken;
import com.foxinmy.weixin4j.util.Consts; import com.foxinmy.weixin4j.util.Consts;
import com.foxinmy.weixin4j.util.StringUtil; import com.foxinmy.weixin4j.util.StringUtil;
/** /**
* 获取微信消息的关键信 * 微信消息
* *
* @className CruxMessageHandler * @className MessageTransferHandler
* @author jy * @author jy
* @date 2015年5月17日 * @date 2015年5月17日
* @since JDK 1.7 * @since JDK 1.7
* @see * @see
*/ */
public class CruxMessageHandler extends DefaultHandler { public class MessageTransferHandler extends DefaultHandler {
private String fromUserName; private String fromUserName;
private String toUserName; private String toUserName;
@ -68,7 +71,7 @@ public class CruxMessageHandler extends DefaultHandler {
this.content = new String(ch, start, length); this.content = new String(ch, start, length);
} }
public AccountType getAccountType() { private AccountType getAccountType() {
if (hasAgent) { if (hasAgent) {
return AccountType.QY; return AccountType.QY;
} }
@ -78,30 +81,10 @@ public class CruxMessageHandler extends DefaultHandler {
return AccountType.MP; return AccountType.MP;
} }
public String getMsgType() { private static MessageTransferHandler global = new MessageTransferHandler();
return msgType;
}
public String getEventType() { public static WeixinMessageTransfer parser(String xmlContent,
return eventType; AesToken aesToken, EncryptType encryptType) throws RuntimeException {
}
public String getFromUserName() {
return fromUserName;
}
public String getToUserName() {
return toUserName;
}
public Set<String> getNodeNames() {
return nodeNames;
}
private static CruxMessageHandler global = new CruxMessageHandler();
public static CruxMessageHandler parser(String xmlContent)
throws RuntimeException {
try { try {
XMLReader xmlReader = XMLReaderFactory.createXMLReader(); XMLReader xmlReader = XMLReaderFactory.createXMLReader();
xmlReader.setContentHandler(global); xmlReader.setContentHandler(global);
@ -112,6 +95,9 @@ public class CruxMessageHandler extends DefaultHandler {
} catch (SAXException e) { } catch (SAXException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
return global; return new WeixinMessageTransfer(aesToken, encryptType,
global.toUserName, global.fromUserName,
global.getAccountType(), global.msgType, global.eventType,
global.nodeNames);
} }
} }