weixin4j-server:添加了许多注释
This commit is contained in:
parent
3dbd115aeb
commit
2229364c87
@ -21,7 +21,8 @@ import com.foxinmy.weixin4j.response.WeixinResponse;
|
||||
*/
|
||||
public class MessageHandlerExecutor {
|
||||
|
||||
private final InternalLogger logger = InternalLoggerFactory.getInstance(getClass());
|
||||
private final InternalLogger logger = InternalLoggerFactory
|
||||
.getInstance(getClass());
|
||||
/**
|
||||
* 消息处理器
|
||||
*/
|
||||
@ -47,6 +48,16 @@ public class MessageHandlerExecutor {
|
||||
return messageHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行预拦截动作
|
||||
*
|
||||
* @param request
|
||||
* 微信请求信息
|
||||
* @param message
|
||||
* 微信消息
|
||||
* @return true则继续执行往下执行
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public boolean applyPreHandle(WeixinRequest request, Object message)
|
||||
throws WeixinException {
|
||||
if (messageInterceptors != null) {
|
||||
@ -63,6 +74,17 @@ public class MessageHandlerExecutor {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* MessageHandler处理玩请求后的动作
|
||||
*
|
||||
* @param request
|
||||
* 微信请求
|
||||
* @param response
|
||||
* 处理后的响应
|
||||
* @param message
|
||||
* 微信消息
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public void applyPostHandle(WeixinRequest request, WeixinResponse response,
|
||||
Object message) throws WeixinException {
|
||||
if (messageInterceptors == null) {
|
||||
@ -75,6 +97,17 @@ public class MessageHandlerExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 全部执行完毕后触发
|
||||
*
|
||||
* @param request
|
||||
* 微信请求
|
||||
* @param message
|
||||
* 微信消息
|
||||
* @param exception
|
||||
* 处理时可能的异常
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public void triggerAfterCompletion(WeixinRequest request, Object message,
|
||||
WeixinException exception) throws WeixinException {
|
||||
if (messageInterceptors == null) {
|
||||
|
||||
@ -42,7 +42,11 @@ import com.foxinmy.weixin4j.util.ReflectionUtil;
|
||||
* @author jy
|
||||
* @date 2015年5月7日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
* @see com.foxinmy.weixin4j.handler.WeixinMessageHandler
|
||||
* @see com.foxinmy.weixin4j.interceptor.WeixinMessageInterceptor
|
||||
* @see com.foxinmy.weixin4j.dispatcher.WeixinMessageMatcher
|
||||
* @see com.foxinmy.weixin4j.dispatcher.MessageHandlerExecutor
|
||||
* @see com.foxinmy.weixin4j.bean.BeanFactory
|
||||
*/
|
||||
public class WeixinMessageDispatcher {
|
||||
|
||||
@ -88,6 +92,17 @@ public class WeixinMessageDispatcher {
|
||||
messageUnmarshaller = new HashMap<Class<?>, Unmarshaller>();
|
||||
}
|
||||
|
||||
/**
|
||||
* 对消息进行一系列的处理,包括 拦截、匹配、分发等动作
|
||||
*
|
||||
* @param context
|
||||
* 上下文环境
|
||||
* @param request
|
||||
* 微信请求
|
||||
* @param messageKey
|
||||
* 消息的key
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public void doDispatch(final ChannelHandlerContext context,
|
||||
final WeixinRequest request, final String messageKey)
|
||||
throws WeixinException {
|
||||
@ -122,12 +137,38 @@ public class WeixinMessageDispatcher {
|
||||
dispatchException);
|
||||
}
|
||||
|
||||
protected void noHandlerFound(ChannelHandlerContext ctx,
|
||||
/**
|
||||
* 未匹配到handler时触发
|
||||
*
|
||||
* @param context
|
||||
* 上下文环境
|
||||
* @param request
|
||||
* 微信请求
|
||||
* @param message
|
||||
* 微信消息
|
||||
*/
|
||||
protected void noHandlerFound(ChannelHandlerContext context,
|
||||
WeixinRequest request, Object message) {
|
||||
ctx.writeAndFlush(HttpUtil.createHttpResponse(null, NOT_FOUND, null))
|
||||
context.writeAndFlush(
|
||||
HttpUtil.createHttpResponse(null, NOT_FOUND, null))
|
||||
.addListener(ChannelFutureListener.CLOSE);
|
||||
}
|
||||
|
||||
/**
|
||||
* MessageHandlerExecutor
|
||||
*
|
||||
* @param context
|
||||
* 上下文环境
|
||||
* @param request
|
||||
* 微信请求
|
||||
* @param messageKey
|
||||
* 消息的key
|
||||
* @param message
|
||||
* 微信消息
|
||||
* @return MessageHandlerExecutor
|
||||
* @see com.foxinmy.weixin4j.dispatcher.MessageHandlerExecutor
|
||||
* @throws WeixinException
|
||||
*/
|
||||
protected MessageHandlerExecutor getHandlerExecutor(
|
||||
ChannelHandlerContext context, WeixinRequest request,
|
||||
String messageKey, Object message) throws WeixinException {
|
||||
@ -164,6 +205,13 @@ public class WeixinMessageDispatcher {
|
||||
getMessageInterceptors());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有的handler
|
||||
*
|
||||
* @return handler集合
|
||||
* @see com.foxinmy.weixin4j.handler.WeixinMessageHandler
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public WeixinMessageHandler[] getMessageHandlers() throws WeixinException {
|
||||
if (this.messageHandlers == null) {
|
||||
if (messageHandlerPackages != null) {
|
||||
@ -208,6 +256,13 @@ public class WeixinMessageDispatcher {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有的interceptor
|
||||
*
|
||||
* @return interceptor集合
|
||||
* @throws WeixinException
|
||||
* @see com.foxinmy.weixin4j.interceptor.WeixinMessageInterceptor
|
||||
*/
|
||||
public WeixinMessageInterceptor[] getMessageInterceptors()
|
||||
throws WeixinException {
|
||||
if (this.messageInterceptors == null) {
|
||||
@ -253,6 +308,16 @@ public class WeixinMessageDispatcher {
|
||||
return this.messageInterceptors;
|
||||
}
|
||||
|
||||
/**
|
||||
* jaxb读取微信消息
|
||||
*
|
||||
* @param message
|
||||
* xml消息
|
||||
* @param clazz
|
||||
* 消息类型
|
||||
* @return 消息对象
|
||||
* @throws WeixinException
|
||||
*/
|
||||
protected Object messageRead(String message, Class<?> clazz)
|
||||
throws WeixinException {
|
||||
try {
|
||||
@ -266,6 +331,14 @@ public class WeixinMessageDispatcher {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* xml消息转换器
|
||||
*
|
||||
* @param clazz
|
||||
* 消息类型
|
||||
* @return 消息转换器
|
||||
* @throws WeixinException
|
||||
*/
|
||||
protected Unmarshaller getUnmarshaller(Class<?> clazz)
|
||||
throws WeixinException {
|
||||
Unmarshaller unmarshaller = messageUnmarshaller.get(clazz);
|
||||
@ -281,6 +354,12 @@ public class WeixinMessageDispatcher {
|
||||
return unmarshaller;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得泛型类型
|
||||
*
|
||||
* @param object
|
||||
* @return
|
||||
*/
|
||||
private Class<?> genericTypeRead(Object object) {
|
||||
Class<?> clazz = null;
|
||||
Type type = object.getClass().getGenericSuperclass();
|
||||
|
||||
@ -23,9 +23,19 @@ import com.foxinmy.weixin4j.qy.event.EnterAgentEventMessage;
|
||||
import com.foxinmy.weixin4j.type.EventType;
|
||||
import com.foxinmy.weixin4j.type.MessageType;
|
||||
|
||||
/**
|
||||
* 微信消息匹配
|
||||
*
|
||||
* @className WeixinMessageMatcher
|
||||
* @author jy
|
||||
* @date 2015年5月17日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class WeixinMessageMatcher {
|
||||
|
||||
private final InternalLogger logger = InternalLoggerFactory.getInstance(getClass());
|
||||
private final InternalLogger logger = InternalLoggerFactory
|
||||
.getInstance(getClass());
|
||||
|
||||
public static final String MESSAGEKEY_MP_TAG = "mp";
|
||||
public static final String MESSAGEKEY_SEPARATOR = ":";
|
||||
@ -138,28 +148,62 @@ public class WeixinMessageMatcher {
|
||||
EnterAgentEventMessage.class);
|
||||
}
|
||||
|
||||
private String mpEventMessagKey(EventType eventType) {
|
||||
/**
|
||||
* 公众平台事件消息的唯一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());
|
||||
}
|
||||
|
||||
private String qyEventMessagKey(EventType eventType) {
|
||||
/**
|
||||
* 企业号事件消息的唯一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());
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册一个消息类型
|
||||
*
|
||||
* @param messageKey
|
||||
* 消息的key
|
||||
* @param clazz
|
||||
* 消息类型
|
||||
*/
|
||||
public void regist(String messageKey, Class<?> clazz) {
|
||||
key2ClassMap.put(messageKey, clazz);
|
||||
class2KeyMap.put(clazz, messageKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 匹配到消息
|
||||
*
|
||||
* @param keyOrClass
|
||||
* 消息key或者消息类型
|
||||
* @return 匹配结果
|
||||
*/
|
||||
public boolean match(Object keyOrClass) {
|
||||
return key2ClassMap.containsKey(keyOrClass)
|
||||
|| class2KeyMap.containsKey(keyOrClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* 消息key找到消息类型
|
||||
*
|
||||
* @param messageKey
|
||||
* 消息key
|
||||
* @return 消息类型
|
||||
*/
|
||||
public Class<?> find(String messageKey) {
|
||||
return key2ClassMap.get(messageKey);
|
||||
}
|
||||
|
||||
@ -5,6 +5,15 @@ import com.foxinmy.weixin4j.request.WeixinRequest;
|
||||
import com.foxinmy.weixin4j.response.BlankResponse;
|
||||
import com.foxinmy.weixin4j.response.WeixinResponse;
|
||||
|
||||
/**
|
||||
* 空白响应消息处理器
|
||||
*
|
||||
* @className BlankMessageHandler
|
||||
* @author jy
|
||||
* @date 2015年5月17日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class BlankMessageHandler implements WeixinMessageHandler {
|
||||
|
||||
public final static BlankMessageHandler global = new BlankMessageHandler();
|
||||
|
||||
@ -5,6 +5,15 @@ import com.foxinmy.weixin4j.request.WeixinRequest;
|
||||
import com.foxinmy.weixin4j.response.TextResponse;
|
||||
import com.foxinmy.weixin4j.response.WeixinResponse;
|
||||
|
||||
/**
|
||||
* 调试消息处理器
|
||||
*
|
||||
* @className DebugMessageHandler
|
||||
* @author jy
|
||||
* @date 2015年5月17日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class DebugMessageHandler implements WeixinMessageHandler {
|
||||
|
||||
public static final DebugMessageHandler global = new DebugMessageHandler();
|
||||
@ -16,7 +25,6 @@ public class DebugMessageHandler implements WeixinMessageHandler {
|
||||
@Override
|
||||
public boolean canHandle(WeixinRequest request, Object message)
|
||||
throws WeixinException {
|
||||
System.err.println(message.getClass());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@ -4,6 +4,15 @@ import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.request.WeixinRequest;
|
||||
import com.foxinmy.weixin4j.response.WeixinResponse;
|
||||
|
||||
/**
|
||||
* 消息处理的适配,主要对微信消息进行泛型转换
|
||||
*
|
||||
* @className MessageHandlerAdapter
|
||||
* @author jy
|
||||
* @date 2015年5月17日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public abstract class MessageHandlerAdapter<M> implements WeixinMessageHandler {
|
||||
|
||||
@ -13,6 +22,16 @@ public abstract class MessageHandlerAdapter<M> implements WeixinMessageHandler {
|
||||
return canHandle0(request, (M) message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 能否处理请求
|
||||
*
|
||||
* @param request
|
||||
* 微信请求
|
||||
* @param message
|
||||
* 微信消息
|
||||
* @return true则执行doHandler
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public boolean canHandle0(WeixinRequest request, M message)
|
||||
throws WeixinException {
|
||||
return true;
|
||||
@ -24,6 +43,15 @@ public abstract class MessageHandlerAdapter<M> implements WeixinMessageHandler {
|
||||
return doHandle0(request, (M) message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理请求
|
||||
*
|
||||
* @param request
|
||||
* 微信请求
|
||||
* @param message
|
||||
* 微信消息
|
||||
* @return
|
||||
*/
|
||||
public abstract WeixinResponse doHandle0(WeixinRequest request, M message)
|
||||
throws WeixinException;
|
||||
|
||||
|
||||
@ -11,7 +11,7 @@ import com.foxinmy.weixin4j.response.WeixinResponse;
|
||||
* @author jy
|
||||
* @date 2015年5月7日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
* @see MessageHandlerAdapter
|
||||
*/
|
||||
public interface WeixinMessageHandler {
|
||||
|
||||
@ -22,7 +22,7 @@ public interface WeixinMessageHandler {
|
||||
* 微信请求
|
||||
* @param message
|
||||
* 微信消息
|
||||
* @return
|
||||
* @return true则执行doHandle
|
||||
*/
|
||||
public boolean canHandle(WeixinRequest request, Object message)
|
||||
throws WeixinException;
|
||||
|
||||
@ -14,7 +14,7 @@ import com.foxinmy.weixin4j.response.WeixinResponse;
|
||||
* @author jy
|
||||
* @date 2015年5月7日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
* @see MessageInterceptorAdapter
|
||||
*/
|
||||
public interface WeixinMessageInterceptor {
|
||||
|
||||
|
||||
@ -28,6 +28,7 @@ import com.foxinmy.weixin4j.xml.EncryptMessageHandler;
|
||||
* @since JDK 1.7
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/0/61c3a8b9d50ac74f18bdf2e54ddfc4e0.html">加密接入指引</a>
|
||||
* @see com.foxinmy.weixin4j.request.WeixinRequest
|
||||
*/
|
||||
public class WeixinMessageDecoder extends
|
||||
MessageToMessageDecoder<FullHttpRequest> {
|
||||
|
||||
@ -28,7 +28,7 @@ import com.foxinmy.weixin4j.xml.CruxMessageHandler;
|
||||
* @author jy
|
||||
* @date 2014年11月16日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
* @see com.foxinmy.weixin4j.dispatcher.WeixinMessageDispatcher
|
||||
*/
|
||||
public class WeixinRequestHandler extends
|
||||
SimpleChannelInboundHandler<WeixinRequest> {
|
||||
|
||||
@ -28,6 +28,7 @@ import com.foxinmy.weixin4j.util.StringUtil;
|
||||
* @since JDK 1.7
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/0/61c3a8b9d50ac74f18bdf2e54ddfc4e0.html">加密接入指引</a>
|
||||
* @see com.foxinmy.weixin4j.response.WeixinResponse
|
||||
*/
|
||||
public class WeixinResponseEncoder extends
|
||||
MessageToMessageEncoder<WeixinResponse> {
|
||||
|
||||
@ -10,6 +10,15 @@ import com.foxinmy.weixin4j.bean.AesToken;
|
||||
import com.foxinmy.weixin4j.dispatcher.WeixinMessageDispatcher;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
|
||||
/**
|
||||
* 微信消息服务器初始化
|
||||
*
|
||||
* @className WeixinServerInitializer
|
||||
* @author jy
|
||||
* @date 2015年5月17日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class WeixinServerInitializer extends ChannelInitializer<SocketChannel> {
|
||||
|
||||
private final AesToken aesToken;
|
||||
|
||||
@ -27,7 +27,10 @@ import com.foxinmy.weixin4j.socket.WeixinServerInitializer;
|
||||
* @author jy
|
||||
* @date 2014年10月12日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
* @see com.foxinmy.weixin4j.handler.WeixinMessageHandler
|
||||
* @see com.foxinmy.weixin4j.interceptor.WeixinMessageInterceptor
|
||||
* @see com.foxinmy.weixin4j.dispatcher.WeixinMessageDispatcher
|
||||
* @see com.foxinmy.weixin4j.bean.BeanFactory
|
||||
*/
|
||||
public final class WeixinServerBootstrap {
|
||||
|
||||
|
||||
@ -14,6 +14,15 @@ import com.foxinmy.weixin4j.type.MessageType;
|
||||
import com.foxinmy.weixin4j.util.Consts;
|
||||
import com.foxinmy.weixin4j.util.StringUtil;
|
||||
|
||||
/**
|
||||
* 获取微信消息的关键信息
|
||||
*
|
||||
* @className CruxMessageHandler
|
||||
* @author jy
|
||||
* @date 2015年5月17日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class CruxMessageHandler extends DefaultHandler {
|
||||
|
||||
private String fromUserName;
|
||||
|
||||
@ -12,6 +12,15 @@ import org.xml.sax.helpers.XMLReaderFactory;
|
||||
|
||||
import com.foxinmy.weixin4j.util.Consts;
|
||||
|
||||
/**
|
||||
* 获取加密的密文内容
|
||||
*
|
||||
* @className EncryptMessageHandler
|
||||
* @author jy
|
||||
* @date 2015年5月17日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class EncryptMessageHandler extends DefaultHandler {
|
||||
|
||||
private String encryptContent;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user