MessageTransferHandler替换CruxMessageHandler
This commit is contained in:
parent
55246ab9b4
commit
e454969421
@ -29,12 +29,12 @@ import com.foxinmy.weixin4j.request.WeixinMessage;
|
||||
import com.foxinmy.weixin4j.request.WeixinRequest;
|
||||
import com.foxinmy.weixin4j.response.BlankResponse;
|
||||
import com.foxinmy.weixin4j.response.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.socket.WeixinMessageTransfer;
|
||||
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;
|
||||
|
||||
/**
|
||||
* 微信消息分发器
|
||||
@ -108,22 +108,22 @@ public class WeixinMessageDispatcher {
|
||||
* 上下文环境
|
||||
* @param request
|
||||
* 微信请求
|
||||
* @param cruxMessage
|
||||
* 微信的关键消息
|
||||
* @param messageTransfer
|
||||
* 微信消息
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public void doDispatch(final ChannelHandlerContext context,
|
||||
final WeixinRequest request, final CruxMessageHandler cruxMessage)
|
||||
throws WeixinException {
|
||||
final WeixinRequest request,
|
||||
final WeixinMessageTransfer messageTransfer) throws WeixinException {
|
||||
WeixinMessageKey messageKey = defineMessageKey(
|
||||
cruxMessage.getMsgType(), cruxMessage.getEventType(),
|
||||
cruxMessage.getAccountType());
|
||||
messageTransfer.getMsgType(), messageTransfer.getEventType(),
|
||||
messageTransfer.getAccountType());
|
||||
Class<? extends WeixinMessage> targetClass = messageMatcher
|
||||
.match(messageKey);
|
||||
Object message = messageRead(request.getOriginalContent(), targetClass);
|
||||
logger.info("define '{}' matched '{}'", messageKey, targetClass);
|
||||
MessageHandlerExecutor handlerExecutor = getHandlerExecutor(context,
|
||||
request, messageKey, message, cruxMessage.getNodeNames());
|
||||
request, messageKey, message, messageTransfer.getNodeNames());
|
||||
if (handlerExecutor == null
|
||||
|| handlerExecutor.getMessageHandler() == null) {
|
||||
noHandlerFound(context, request, message);
|
||||
@ -136,7 +136,7 @@ public class WeixinMessageDispatcher {
|
||||
WeixinResponse response = null;
|
||||
try {
|
||||
response = handlerExecutor.getMessageHandler().doHandle(request,
|
||||
message, cruxMessage.getNodeNames());
|
||||
message, messageTransfer.getNodeNames());
|
||||
// fixed..
|
||||
if (response == null) {
|
||||
response = BlankResponse.global;
|
||||
|
||||
@ -1,7 +1,9 @@
|
||||
package com.foxinmy.weixin4j.socket;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Set;
|
||||
|
||||
import com.foxinmy.weixin4j.type.AccountType;
|
||||
import com.foxinmy.weixin4j.type.EncryptType;
|
||||
import com.foxinmy.weixin4j.util.AesToken;
|
||||
|
||||
@ -26,7 +28,6 @@ public class WeixinMessageTransfer implements Serializable {
|
||||
* 加密类型
|
||||
*/
|
||||
private EncryptType encryptType;
|
||||
|
||||
/**
|
||||
* 消息接收方
|
||||
*/
|
||||
@ -35,13 +36,34 @@ public class WeixinMessageTransfer implements Serializable {
|
||||
* 消息发送方
|
||||
*/
|
||||
private String fromUserName;
|
||||
/**
|
||||
* 账号
|
||||
*/
|
||||
private AccountType accountType;
|
||||
/**
|
||||
* 消息类型
|
||||
*/
|
||||
private String msgType;
|
||||
/**
|
||||
* 事件类型
|
||||
*/
|
||||
private String eventType;
|
||||
/**
|
||||
* 节点集合
|
||||
*/
|
||||
private Set<String> nodeNames;
|
||||
|
||||
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.encryptType = encryptType;
|
||||
this.toUserName = toUserName;
|
||||
this.fromUserName = fromUserName;
|
||||
this.accountType = accountType;
|
||||
this.msgType = msgType;
|
||||
this.eventType = eventType;
|
||||
this.nodeNames = nodeNames;
|
||||
}
|
||||
|
||||
public AesToken getAesToken() {
|
||||
@ -60,10 +82,28 @@ public class WeixinMessageTransfer implements Serializable {
|
||||
return fromUserName;
|
||||
}
|
||||
|
||||
public AccountType getAccountType() {
|
||||
return accountType;
|
||||
}
|
||||
|
||||
public String getMsgType() {
|
||||
return msgType;
|
||||
}
|
||||
|
||||
public String getEventType() {
|
||||
return eventType;
|
||||
}
|
||||
|
||||
public Set<String> getNodeNames() {
|
||||
return nodeNames;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "WeixinMessageTransfer [aesToken=" + aesToken + ", encryptType="
|
||||
+ encryptType + ", toUserName=" + toUserName
|
||||
+ ", fromUserName=" + fromUserName + "]";
|
||||
+ ", fromUserName=" + fromUserName + ", accountType="
|
||||
+ accountType + ", msgType=" + msgType + ", eventType="
|
||||
+ eventType + ", nodeNames=" + nodeNames + "]";
|
||||
}
|
||||
}
|
||||
|
||||
@ -20,7 +20,7 @@ import com.foxinmy.weixin4j.util.Consts;
|
||||
import com.foxinmy.weixin4j.util.HttpUtil;
|
||||
import com.foxinmy.weixin4j.util.MessageUtil;
|
||||
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);
|
||||
return;
|
||||
}
|
||||
CruxMessageHandler cruxMessage = CruxMessageHandler.parser(request
|
||||
.getOriginalContent());
|
||||
WeixinMessageTransfer messageTransfer = new WeixinMessageTransfer(
|
||||
aesToken, request.getEncryptType(),
|
||||
cruxMessage.getToUserName(), cruxMessage.getFromUserName());
|
||||
WeixinMessageTransfer messageTransfer = MessageTransferHandler.parser(
|
||||
request.getOriginalContent(), aesToken,
|
||||
request.getEncryptType());
|
||||
ctx.channel().attr(Consts.MESSAGE_TRANSFER_KEY).set(messageTransfer);
|
||||
messageDispatcher.doDispatch(ctx, request, cruxMessage);
|
||||
messageDispatcher.doDispatch(ctx, request, messageTransfer);
|
||||
}
|
||||
}
|
||||
|
||||
@ -11,20 +11,23 @@ import org.xml.sax.XMLReader;
|
||||
import org.xml.sax.helpers.DefaultHandler;
|
||||
import org.xml.sax.helpers.XMLReaderFactory;
|
||||
|
||||
import com.foxinmy.weixin4j.socket.WeixinMessageTransfer;
|
||||
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.StringUtil;
|
||||
|
||||
/**
|
||||
* 获取微信消息的关键信息
|
||||
* 微信消息
|
||||
*
|
||||
* @className CruxMessageHandler
|
||||
* @className MessageTransferHandler
|
||||
* @author jy
|
||||
* @date 2015年5月17日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class CruxMessageHandler extends DefaultHandler {
|
||||
public class MessageTransferHandler extends DefaultHandler {
|
||||
|
||||
private String fromUserName;
|
||||
private String toUserName;
|
||||
@ -68,7 +71,7 @@ public class CruxMessageHandler extends DefaultHandler {
|
||||
this.content = new String(ch, start, length);
|
||||
}
|
||||
|
||||
public AccountType getAccountType() {
|
||||
private AccountType getAccountType() {
|
||||
if (hasAgent) {
|
||||
return AccountType.QY;
|
||||
}
|
||||
@ -78,30 +81,10 @@ public class CruxMessageHandler extends DefaultHandler {
|
||||
return AccountType.MP;
|
||||
}
|
||||
|
||||
public String getMsgType() {
|
||||
return msgType;
|
||||
}
|
||||
private static MessageTransferHandler global = new MessageTransferHandler();
|
||||
|
||||
public String getEventType() {
|
||||
return eventType;
|
||||
}
|
||||
|
||||
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 {
|
||||
public static WeixinMessageTransfer parser(String xmlContent,
|
||||
AesToken aesToken, EncryptType encryptType) throws RuntimeException {
|
||||
try {
|
||||
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
|
||||
xmlReader.setContentHandler(global);
|
||||
@ -112,6 +95,9 @@ public class CruxMessageHandler extends DefaultHandler {
|
||||
} catch (SAXException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return global;
|
||||
return new WeixinMessageTransfer(aesToken, encryptType,
|
||||
global.toUserName, global.fromUserName,
|
||||
global.getAccountType(), global.msgType, global.eventType,
|
||||
global.nodeNames);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user