weixin4j-qy: 新增userid与openid互换接口

This commit is contained in:
jinyu
2015-06-24 20:57:24 +08:00
parent bb79042d4e
commit f6b1f76210
23 changed files with 255 additions and 195 deletions
@@ -4,6 +4,8 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.util.Set;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.handler.WeixinMessageHandler;
import com.foxinmy.weixin4j.interceptor.WeixinMessageInterceptor;
@@ -33,16 +35,22 @@ public class MessageHandlerExecutor {
* 消息拦截器
*/
private final WeixinMessageInterceptor[] messageInterceptors;
/**
* 节点名称集合
*/
private final Set<String> nodeNames;
private final ChannelHandlerContext context;
private int interceptorIndex = -1;
public MessageHandlerExecutor(ChannelHandlerContext context,
WeixinMessageHandler messageHandler,
WeixinMessageInterceptor[] messageInterceptors) {
WeixinMessageInterceptor[] messageInterceptors,
Set<String> nodeNames) {
this.context = context;
this.messageHandler = messageHandler;
this.messageInterceptors = messageInterceptors;
this.nodeNames = nodeNames;
}
public WeixinMessageHandler getMessageHandler() {
@@ -65,7 +73,7 @@ public class MessageHandlerExecutor {
for (int i = 0; i < messageInterceptors.length; i++) {
WeixinMessageInterceptor interceptor = messageInterceptors[i];
if (!interceptor.preHandle(context, request, message,
messageHandler)) {
nodeNames, messageHandler)) {
triggerAfterCompletion(request, message, null);
return false;
}
@@ -94,7 +102,7 @@ public class MessageHandlerExecutor {
for (int i = messageInterceptors.length - 1; i >= 0; i--) {
WeixinMessageInterceptor interceptor = messageInterceptors[i];
interceptor.postHandle(context, request, response, message,
messageHandler);
nodeNames, messageHandler);
}
}
@@ -118,7 +126,7 @@ public class MessageHandlerExecutor {
WeixinMessageInterceptor interceptor = messageInterceptors[i];
try {
interceptor.afterCompletion(context, request, message,
messageHandler, exception);
nodeNames, messageHandler, exception);
} catch (WeixinException e) {
logger.error(
"MessageInterceptor.afterCompletion threw exception", e);
@@ -15,6 +15,7 @@ import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
@@ -29,6 +30,7 @@ import com.foxinmy.weixin4j.handler.WeixinMessageHandler;
import com.foxinmy.weixin4j.interceptor.WeixinMessageInterceptor;
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;
@@ -110,17 +112,16 @@ public class WeixinMessageDispatcher {
public void doDispatch(final ChannelHandlerContext context,
final WeixinRequest request, final CruxMessageHandler cruxMessage)
throws WeixinException {
MessageKey messageKey = new MessageKey(cruxMessage.getMsgType(),
MessageKey messageKey = defineMessageKey(cruxMessage.getMsgType(),
cruxMessage.getEventType(), cruxMessage.getAccountType());
Class<?> targetClass = messageMatcher.match(messageKey);
Object message = request.getOriginalContent();
if (targetClass != null) {
message = messageRead(request.getOriginalContent(), targetClass);
}
logger.info("define '{}' matched '{}'", messageKey,
targetClass);
logger.info("define '{}' matched '{}'", messageKey, targetClass);
MessageHandlerExecutor handlerExecutor = getHandlerExecutor(context,
request, messageKey, message);
request, messageKey, message, cruxMessage.getNodeNames());
if (handlerExecutor == null
|| handlerExecutor.getMessageHandler() == null) {
noHandlerFound(context, request, message);
@@ -132,7 +133,7 @@ public class WeixinMessageDispatcher {
WeixinException dispatchException = null;
try {
WeixinResponse response = handlerExecutor.getMessageHandler()
.doHandle(request, message);
.doHandle(request, message, cruxMessage.getNodeNames());
handlerExecutor.applyPostHandle(request, response, message);
context.write(response);
} catch (WeixinException e) {
@@ -142,6 +143,22 @@ public class WeixinMessageDispatcher {
dispatchException);
}
/**
* 声明messagekey
*
* @param messageType
* 消息类型
* @param eventType
* 事件类型
* @param accountType
* 账号类型
* @return
*/
protected MessageKey defineMessageKey(String messageType, String eventType,
AccountType accountType) {
return new MessageKey(messageType, eventType, accountType);
}
/**
* 未匹配到handler时触发
*
@@ -170,13 +187,16 @@ public class WeixinMessageDispatcher {
* 消息的key
* @param message
* 微信消息
* @param nodeNames
* 节点名称集合
* @return MessageHandlerExecutor
* @see com.foxinmy.weixin4j.dispatcher.MessageHandlerExecutor
* @throws WeixinException
*/
protected MessageHandlerExecutor getHandlerExecutor(
ChannelHandlerContext context, WeixinRequest request,
MessageKey messageKey, Object message) throws WeixinException {
MessageKey messageKey, Object message, Set<String> nodeNames)
throws WeixinException {
WeixinMessageHandler messageHandler = null;
WeixinMessageHandler[] messageHandlers = getMessageHandlers();
if (messageHandlers == null) {
@@ -186,7 +206,7 @@ public class WeixinMessageDispatcher {
if (handler instanceof MessageHandlerAdapter) {
Class<?> genericType = genericTypeRead(handler);
if (genericType == message.getClass()
&& handler.canHandle(request, message)) {
&& handler.canHandle(request, message, nodeNames)) {
messageHandler = handler;
break;
}
@@ -195,14 +215,14 @@ public class WeixinMessageDispatcher {
if (messageHandler == null) {
for (WeixinMessageHandler handler : messageHandlers) {
if (!(handler instanceof MessageHandlerAdapter)
&& handler.canHandle(request, message)) {
&& handler.canHandle(request, message, nodeNames)) {
messageHandler = handler;
break;
}
}
}
return new MessageHandlerExecutor(context, messageHandler,
getMessageInterceptors());
getMessageInterceptors(), nodeNames);
}
/**
@@ -1,5 +1,7 @@
package com.foxinmy.weixin4j.handler;
import java.util.Set;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.request.WeixinRequest;
import com.foxinmy.weixin4j.response.BlankResponse;
@@ -23,14 +25,14 @@ public class BlankMessageHandler implements WeixinMessageHandler {
}
@Override
public boolean canHandle(WeixinRequest request, Object message)
throws WeixinException {
public boolean canHandle(WeixinRequest request, Object message,
Set<String> nodeNames) throws WeixinException {
return true;
}
@Override
public WeixinResponse doHandle(WeixinRequest request, Object message)
throws WeixinException {
public WeixinResponse doHandle(WeixinRequest request, Object message,
Set<String> nodeNames) throws WeixinException {
return BlankResponse.global;
}
}
@@ -1,5 +1,7 @@
package com.foxinmy.weixin4j.handler;
import java.util.Set;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.request.WeixinRequest;
import com.foxinmy.weixin4j.response.TextResponse;
@@ -23,14 +25,14 @@ public class DebugMessageHandler implements WeixinMessageHandler {
}
@Override
public boolean canHandle(WeixinRequest request, Object message)
throws WeixinException {
public boolean canHandle(WeixinRequest request, Object message,
Set<String> nodeNames) throws WeixinException {
return true;
}
@Override
public WeixinResponse doHandle(WeixinRequest request, Object message)
throws WeixinException {
public WeixinResponse doHandle(WeixinRequest request, Object message,
Set<String> nodeNames) throws WeixinException {
return new TextResponse(message.toString());
}
}
@@ -1,5 +1,7 @@
package com.foxinmy.weixin4j.handler;
import java.util.Set;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.request.WeixinRequest;
import com.foxinmy.weixin4j.response.WeixinResponse;
@@ -17,8 +19,8 @@ import com.foxinmy.weixin4j.response.WeixinResponse;
public abstract class MessageHandlerAdapter<M> implements WeixinMessageHandler {
@Override
public boolean canHandle(WeixinRequest request, Object message)
throws WeixinException {
public boolean canHandle(WeixinRequest request, Object message,
Set<String> nodeNames) throws WeixinException {
return canHandle0(request, (M) message);
}
@@ -38,8 +40,8 @@ public abstract class MessageHandlerAdapter<M> implements WeixinMessageHandler {
}
@Override
public WeixinResponse doHandle(WeixinRequest request, Object message)
throws WeixinException {
public WeixinResponse doHandle(WeixinRequest request, Object message,
Set<String> nodeNames) throws WeixinException {
return doHandle0(request, (M) message);
}
@@ -1,5 +1,7 @@
package com.foxinmy.weixin4j.handler;
import java.util.Set;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.request.WeixinRequest;
import com.foxinmy.weixin4j.response.WeixinResponse;
@@ -22,10 +24,12 @@ public interface WeixinMessageHandler {
* 微信请求
* @param message
* 微信消息
* @param nodeNames
* 节点名称集合
* @return true则执行doHandle
*/
public boolean canHandle(WeixinRequest request, Object message)
throws WeixinException;
public boolean canHandle(WeixinRequest request, Object message,
Set<String> nodeNames) throws WeixinException;
/**
* 处理请求
@@ -34,8 +38,10 @@ public interface WeixinMessageHandler {
* 微信请求
* @param message
* 微信消息
* @return
* @param nodeNames
* 节点名称集合
* @return 回复内容
*/
public WeixinResponse doHandle(WeixinRequest request, Object message)
throws WeixinException;
public WeixinResponse doHandle(WeixinRequest request, Object message,
Set<String> nodeNames) throws WeixinException;
}
@@ -2,6 +2,8 @@ package com.foxinmy.weixin4j.interceptor;
import io.netty.channel.ChannelHandlerContext;
import java.util.Set;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.handler.WeixinMessageHandler;
import com.foxinmy.weixin4j.request.WeixinRequest;
@@ -21,20 +23,21 @@ public abstract class MessageInterceptorAdapter implements
@Override
public boolean preHandle(ChannelHandlerContext context,
WeixinRequest request, Object message, WeixinMessageHandler handler)
throws WeixinException {
WeixinRequest request, Object message, Set<String> nodeNames,
WeixinMessageHandler handler) throws WeixinException {
return true;
}
@Override
public void postHandle(ChannelHandlerContext context,
WeixinRequest request, WeixinResponse response, Object message,
WeixinMessageHandler handler) throws WeixinException {
Set<String> nodeNames, WeixinMessageHandler handler)
throws WeixinException {
}
@Override
public void afterCompletion(ChannelHandlerContext context,
WeixinRequest request, Object message,
WeixinRequest request, Object message, Set<String> nodeNames,
WeixinMessageHandler handler, WeixinException exception)
throws WeixinException {
}
@@ -2,6 +2,8 @@ package com.foxinmy.weixin4j.interceptor;
import io.netty.channel.ChannelHandlerContext;
import java.util.Set;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.handler.WeixinMessageHandler;
import com.foxinmy.weixin4j.request.WeixinRequest;
@@ -27,13 +29,15 @@ public interface WeixinMessageInterceptor {
* 微信请求
* @param message
* 微信消息
* @param nodeNames
* 节点名称集合
* @param handler
* 消息处理器
* @return 返回true执行下一个拦截器
* @throws WeixinException
*/
boolean preHandle(ChannelHandlerContext context, WeixinRequest request,
Object message, WeixinMessageHandler handler)
Object message, Set<String> nodeNames, WeixinMessageHandler handler)
throws WeixinException;
/**
@@ -47,12 +51,14 @@ public interface WeixinMessageInterceptor {
* 微信响应
* @param message
* 微信消息
* @param nodeNames
* 节点名称集合
* @param handler
* 消息处理器
* @throws WeixinException
*/
void postHandle(ChannelHandlerContext context, WeixinRequest request,
WeixinResponse response, Object message,
WeixinResponse response, Object message, Set<String> nodeNames,
WeixinMessageHandler handler) throws WeixinException;
/**
@@ -64,6 +70,8 @@ public interface WeixinMessageInterceptor {
* 微信请求
* @param message
* 微信消息
* @param nodeNames
* 节点名称集合
* @param handler
* 消息处理器
* @param exception
@@ -71,6 +79,7 @@ public interface WeixinMessageInterceptor {
* @throws WeixinException
*/
void afterCompletion(ChannelHandlerContext context, WeixinRequest request,
Object message, WeixinMessageHandler handler,
WeixinException exception) throws WeixinException;
Object message, Set<String> nodeNames,
WeixinMessageHandler handler, WeixinException exception)
throws WeixinException;
}
@@ -18,6 +18,7 @@ import com.foxinmy.weixin4j.util.AesToken;
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;
/**
@@ -36,8 +37,7 @@ public class WeixinRequestHandler extends
private final WeixinMessageDispatcher messageDispatcher;
public WeixinRequestHandler(WeixinMessageDispatcher messageDispatcher)
throws WeixinException {
public WeixinRequestHandler(WeixinMessageDispatcher messageDispatcher) {
this.messageDispatcher = messageDispatcher;
}
@@ -70,9 +70,10 @@ public class WeixinRequestHandler extends
.addListener(ChannelFutureListener.CLOSE);
return;
} else if (request.getMethod().equals(HttpMethod.POST.name())) {
if (!MessageUtil.signature(aesToken.getToken(),
request.getTimeStamp(), request.getNonce()).equals(
request.getSignature())) {
if (!StringUtil.isBlank(request.getSignature())
&& !MessageUtil.signature(aesToken.getToken(),
request.getTimeStamp(), request.getNonce()).equals(
request.getSignature())) {
ctx.writeAndFlush(
HttpUtil.createHttpResponse(null, FORBIDDEN, null))
.addListener(ChannelFutureListener.CLOSE);
@@ -103,4 +104,6 @@ public class WeixinRequestHandler extends
ctx.channel().attr(Consts.MESSAGE_TRANSFER_KEY).set(messageTransfer);
messageDispatcher.doDispatch(ctx, request, cruxMessage);
}
}
@@ -36,7 +36,7 @@ public class WeixinServerInitializer extends ChannelInitializer<SocketChannel> {
}
@Override
protected void initChannel(SocketChannel channel) throws WeixinException {
protected void initChannel(SocketChannel channel) {
ChannelPipeline pipeline = channel.pipeline();
pipeline.addLast(new HttpServerCodec());
pipeline.addLast(new HttpObjectAggregator(65536));
@@ -1,26 +0,0 @@
package com.foxinmy.weixin4j.suite;
/**
* 应用套件回调事件
*
* @className SuiteEventType
* @author jy
* @date 2015年6月21日
* @since JDK 1.7
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AC%AC%E4%B8%89%E6%96%B9%E5%9B%9E%E8%B0%83%E5%8D%8F%E8%AE%AE">第三方回调协议</a>
*/
public enum SuiteEventType {
/**
* 推送ticket
*/
suite_ticket,
/**
* 变更授权
*/
change_auth,
/**
* 取消授权
*/
cancel_auth;
}
@@ -1,23 +0,0 @@
package com.foxinmy.weixin4j.suite;
import com.foxinmy.weixin4j.response.SingleResponse;
/**
* 处理第三方应用套件请求
*
* @className SuiteMessageHandler
* @author jy
* @date 2015年6月23日
* @since JDK 1.7
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AC%AC%E4%B8%89%E6%96%B9%E5%9B%9E%E8%B0%83%E5%8D%8F%E8%AE%AE">套件回调协议</a>
*/
public interface SuiteMessageHandler {
/**
* 处理套件消息
*
* @param suiteMessage
* @return
*/
public SingleResponse handle(WeixinSuiteMessage suiteMessage);
}
@@ -1,76 +0,0 @@
package com.foxinmy.weixin4j.suite;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* 套件消息
*
* @className WeixinSuiteMessage
* @author jy
* @date 2015年6月23日
* @since JDK 1.7
* @see
*/
@XmlRootElement(name = "xml")
@XmlAccessorType(XmlAccessType.FIELD)
public class WeixinSuiteMessage implements Serializable {
private static final long serialVersionUID = 6457919241019021514L;
/**
* 应用套件的SuiteId
*/
@XmlElement(name = "SuiteId")
private String suiteId;
/**
* 事件类型
*/
@XmlElement(name = "InfoType")
private SuiteEventType eventType;
/**
* 时间戳
*/
@XmlElement(name = "TimeStamp")
private long timeStamp;
/**
* Ticket内容
*/
@XmlElement(name = "SuiteTicket")
private String SuiteTicket;
/**
* 授权方企业号的corpid
*/
@XmlElement(name = "AuthCorpId")
private String authCorpId;
public String getSuiteId() {
return suiteId;
}
public SuiteEventType getEventType() {
return eventType;
}
public long getTimeStamp() {
return timeStamp;
}
public String getSuiteTicket() {
return SuiteTicket;
}
public String getAuthCorpId() {
return authCorpId;
}
@Override
public String toString() {
return "WeixinSuiteMessage [suiteId=" + suiteId + ", eventType="
+ eventType + ", timeStamp=" + timeStamp + ", SuiteTicket="
+ SuiteTicket + ", authCorpId=" + authCorpId + "]";
}
}
@@ -2,6 +2,8 @@ package com.foxinmy.weixin4j.xml;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
@@ -28,7 +30,8 @@ public class CruxMessageHandler extends DefaultHandler {
private String toUserName;
private String msgType;
private String eventType;
private String agentId;
private boolean agentId;
private Set<String> nodeNames;
private String content;
@@ -38,12 +41,14 @@ public class CruxMessageHandler extends DefaultHandler {
toUserName = null;
msgType = null;
eventType = null;
agentId = null;
agentId = false;
nodeNames = new HashSet<String>();
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
nodeNames.add(localName.toLowerCase());
if (localName.equalsIgnoreCase("fromUserName")) {
fromUserName = content;
} else if (localName.equalsIgnoreCase("toUserName")) {
@@ -53,7 +58,7 @@ public class CruxMessageHandler extends DefaultHandler {
} else if (localName.equalsIgnoreCase("event")) {
eventType = content.toLowerCase();
} else if (localName.equalsIgnoreCase("agentId")) {
agentId = content;
agentId = true;
}
}
@@ -64,7 +69,13 @@ public class CruxMessageHandler extends DefaultHandler {
}
public AccountType getAccountType() {
return StringUtil.isBlank(agentId) ? AccountType.MP : AccountType.QY;
if (agentId) {
return AccountType.QY;
}
if (StringUtil.isBlank(msgType) && StringUtil.isBlank(eventType)) {
return null;
}
return AccountType.MP;
}
public String getMsgType() {
@@ -83,6 +94,10 @@ public class CruxMessageHandler extends DefaultHandler {
return toUserName;
}
public Set<String> getNodeNames() {
return nodeNames;
}
private static CruxMessageHandler global = new CruxMessageHandler();
public static CruxMessageHandler parser(String xmlContent)
@@ -2,6 +2,8 @@ package com.foxinmy.weixin4j.server.test;
import io.netty.channel.ChannelHandlerContext;
import java.util.Set;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.handler.BlankMessageHandler;
import com.foxinmy.weixin4j.handler.DebugMessageHandler;
@@ -88,7 +90,8 @@ public class MessageServerStartup {
@Override
public boolean preHandle(ChannelHandlerContext context,
WeixinRequest request, Object message,
WeixinMessageHandler handler) throws WeixinException {
Set<String> nodeNames, WeixinMessageHandler handler)
throws WeixinException {
context.writeAndFlush(new TextResponse("所有消息被拦截了!"));
return false;
}
@@ -96,16 +99,16 @@ public class MessageServerStartup {
@Override
public void postHandle(ChannelHandlerContext context,
WeixinRequest request, WeixinResponse response,
Object message, WeixinMessageHandler handler)
throws WeixinException {
Object message, Set<String> nodeNames,
WeixinMessageHandler handler) throws WeixinException {
System.err.println("preHandle返回为true,执行handler后");
}
@Override
public void afterCompletion(ChannelHandlerContext context,
WeixinRequest request, Object message,
WeixinMessageHandler handler, WeixinException exception)
throws WeixinException {
Set<String> nodeNames, WeixinMessageHandler handler,
WeixinException exception) throws WeixinException {
System.err.println("请求处理完毕");
}
};