weixin4j-server:添加了许多注释

This commit is contained in:
jinyu 2015-05-17 23:04:14 +08:00
parent 3dbd115aeb
commit 2229364c87
15 changed files with 246 additions and 13 deletions

View File

@ -21,7 +21,8 @@ import com.foxinmy.weixin4j.response.WeixinResponse;
*/ */
public class MessageHandlerExecutor { 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; return messageHandler;
} }
/**
* 执行预拦截动作
*
* @param request
* 微信请求信息
* @param message
* 微信消息
* @return true则继续执行往下执行
* @throws WeixinException
*/
public boolean applyPreHandle(WeixinRequest request, Object message) public boolean applyPreHandle(WeixinRequest request, Object message)
throws WeixinException { throws WeixinException {
if (messageInterceptors != null) { if (messageInterceptors != null) {
@ -63,6 +74,17 @@ public class MessageHandlerExecutor {
return true; return true;
} }
/**
* MessageHandler处理玩请求后的动作
*
* @param request
* 微信请求
* @param response
* 处理后的响应
* @param message
* 微信消息
* @throws WeixinException
*/
public void applyPostHandle(WeixinRequest request, WeixinResponse response, public void applyPostHandle(WeixinRequest request, WeixinResponse response,
Object message) throws WeixinException { Object message) throws WeixinException {
if (messageInterceptors == null) { 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, public void triggerAfterCompletion(WeixinRequest request, Object message,
WeixinException exception) throws WeixinException { WeixinException exception) throws WeixinException {
if (messageInterceptors == null) { if (messageInterceptors == null) {

View File

@ -42,7 +42,11 @@ import com.foxinmy.weixin4j.util.ReflectionUtil;
* @author jy * @author jy
* @date 2015年5月7日 * @date 2015年5月7日
* @since JDK 1.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 { public class WeixinMessageDispatcher {
@ -88,6 +92,17 @@ public class WeixinMessageDispatcher {
messageUnmarshaller = new HashMap<Class<?>, Unmarshaller>(); messageUnmarshaller = new HashMap<Class<?>, Unmarshaller>();
} }
/**
* 对消息进行一系列的处理,包括 拦截匹配分发等动作
*
* @param context
* 上下文环境
* @param request
* 微信请求
* @param messageKey
* 消息的key
* @throws WeixinException
*/
public void doDispatch(final ChannelHandlerContext context, public void doDispatch(final ChannelHandlerContext context,
final WeixinRequest request, final String messageKey) final WeixinRequest request, final String messageKey)
throws WeixinException { throws WeixinException {
@ -122,12 +137,38 @@ public class WeixinMessageDispatcher {
dispatchException); dispatchException);
} }
protected void noHandlerFound(ChannelHandlerContext ctx, /**
* 未匹配到handler时触发
*
* @param context
* 上下文环境
* @param request
* 微信请求
* @param message
* 微信消息
*/
protected void noHandlerFound(ChannelHandlerContext context,
WeixinRequest request, Object message) { WeixinRequest request, Object message) {
ctx.writeAndFlush(HttpUtil.createHttpResponse(null, NOT_FOUND, null)) context.writeAndFlush(
HttpUtil.createHttpResponse(null, NOT_FOUND, null))
.addListener(ChannelFutureListener.CLOSE); .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( protected MessageHandlerExecutor getHandlerExecutor(
ChannelHandlerContext context, WeixinRequest request, ChannelHandlerContext context, WeixinRequest request,
String messageKey, Object message) throws WeixinException { String messageKey, Object message) throws WeixinException {
@ -164,6 +205,13 @@ public class WeixinMessageDispatcher {
getMessageInterceptors()); getMessageInterceptors());
} }
/**
* 获取所有的handler
*
* @return handler集合
* @see com.foxinmy.weixin4j.handler.WeixinMessageHandler
* @throws WeixinException
*/
public WeixinMessageHandler[] getMessageHandlers() throws WeixinException { public WeixinMessageHandler[] getMessageHandlers() throws WeixinException {
if (this.messageHandlers == null) { if (this.messageHandlers == null) {
if (messageHandlerPackages != 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() public WeixinMessageInterceptor[] getMessageInterceptors()
throws WeixinException { throws WeixinException {
if (this.messageInterceptors == null) { if (this.messageInterceptors == null) {
@ -253,6 +308,16 @@ public class WeixinMessageDispatcher {
return this.messageInterceptors; return this.messageInterceptors;
} }
/**
* jaxb读取微信消息
*
* @param message
* xml消息
* @param clazz
* 消息类型
* @return 消息对象
* @throws WeixinException
*/
protected Object messageRead(String message, Class<?> clazz) protected Object messageRead(String message, Class<?> clazz)
throws WeixinException { throws WeixinException {
try { try {
@ -266,6 +331,14 @@ public class WeixinMessageDispatcher {
} }
} }
/**
* xml消息转换器
*
* @param clazz
* 消息类型
* @return 消息转换器
* @throws WeixinException
*/
protected Unmarshaller getUnmarshaller(Class<?> clazz) protected Unmarshaller getUnmarshaller(Class<?> clazz)
throws WeixinException { throws WeixinException {
Unmarshaller unmarshaller = messageUnmarshaller.get(clazz); Unmarshaller unmarshaller = messageUnmarshaller.get(clazz);
@ -281,6 +354,12 @@ public class WeixinMessageDispatcher {
return unmarshaller; return unmarshaller;
} }
/**
* 获得泛型类型
*
* @param object
* @return
*/
private Class<?> genericTypeRead(Object object) { private Class<?> genericTypeRead(Object object) {
Class<?> clazz = null; Class<?> clazz = null;
Type type = object.getClass().getGenericSuperclass(); Type type = object.getClass().getGenericSuperclass();

View File

@ -23,9 +23,19 @@ import com.foxinmy.weixin4j.qy.event.EnterAgentEventMessage;
import com.foxinmy.weixin4j.type.EventType; import com.foxinmy.weixin4j.type.EventType;
import com.foxinmy.weixin4j.type.MessageType; import com.foxinmy.weixin4j.type.MessageType;
/**
* 微信消息匹配
*
* @className WeixinMessageMatcher
* @author jy
* @date 2015年5月17日
* @since JDK 1.7
* @see
*/
public class WeixinMessageMatcher { 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_MP_TAG = "mp";
public static final String MESSAGEKEY_SEPARATOR = ":"; public static final String MESSAGEKEY_SEPARATOR = ":";
@ -138,28 +148,62 @@ public class WeixinMessageMatcher {
EnterAgentEventMessage.class); 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, return String.format("%s%s%s%s", MESSAGEKEY_MP_SEPARATOR,
MessageType.event.name(), MESSAGEKEY_SEPARATOR, MessageType.event.name(), MESSAGEKEY_SEPARATOR,
eventType.name()); 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, return String.format("%s%s%s%s", MESSAGEKEY_QY_SEPARATOR,
MessageType.event.name(), MESSAGEKEY_SEPARATOR, MessageType.event.name(), MESSAGEKEY_SEPARATOR,
eventType.name()); eventType.name());
} }
/**
* 注册一个消息类型
*
* @param messageKey
* 消息的key
* @param clazz
* 消息类型
*/
public void regist(String messageKey, Class<?> clazz) { public void regist(String messageKey, Class<?> clazz) {
key2ClassMap.put(messageKey, clazz); key2ClassMap.put(messageKey, clazz);
class2KeyMap.put(clazz, messageKey); class2KeyMap.put(clazz, messageKey);
} }
/**
* 匹配到消息
*
* @param keyOrClass
* 消息key或者消息类型
* @return 匹配结果
*/
public boolean match(Object keyOrClass) { public boolean match(Object keyOrClass) {
return key2ClassMap.containsKey(keyOrClass) return key2ClassMap.containsKey(keyOrClass)
|| class2KeyMap.containsKey(keyOrClass); || class2KeyMap.containsKey(keyOrClass);
} }
/**
* 消息key找到消息类型
*
* @param messageKey
* 消息key
* @return 消息类型
*/
public Class<?> find(String messageKey) { public Class<?> find(String messageKey) {
return key2ClassMap.get(messageKey); return key2ClassMap.get(messageKey);
} }

View File

@ -5,6 +5,15 @@ 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;
/**
* 空白响应消息处理器
*
* @className BlankMessageHandler
* @author jy
* @date 2015年5月17日
* @since JDK 1.7
* @see
*/
public class BlankMessageHandler implements WeixinMessageHandler { public class BlankMessageHandler implements WeixinMessageHandler {
public final static BlankMessageHandler global = new BlankMessageHandler(); public final static BlankMessageHandler global = new BlankMessageHandler();

View File

@ -5,6 +5,15 @@ import com.foxinmy.weixin4j.request.WeixinRequest;
import com.foxinmy.weixin4j.response.TextResponse; import com.foxinmy.weixin4j.response.TextResponse;
import com.foxinmy.weixin4j.response.WeixinResponse; 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 class DebugMessageHandler implements WeixinMessageHandler {
public static final DebugMessageHandler global = new DebugMessageHandler(); public static final DebugMessageHandler global = new DebugMessageHandler();
@ -16,7 +25,6 @@ public class DebugMessageHandler implements WeixinMessageHandler {
@Override @Override
public boolean canHandle(WeixinRequest request, Object message) public boolean canHandle(WeixinRequest request, Object message)
throws WeixinException { throws WeixinException {
System.err.println(message.getClass());
return true; return true;
} }

View File

@ -4,6 +4,15 @@ import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.request.WeixinRequest; import com.foxinmy.weixin4j.request.WeixinRequest;
import com.foxinmy.weixin4j.response.WeixinResponse; import com.foxinmy.weixin4j.response.WeixinResponse;
/**
* 消息处理的适配,主要对微信消息进行泛型转换
*
* @className MessageHandlerAdapter
* @author jy
* @date 2015年5月17日
* @since JDK 1.7
* @see
*/
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public abstract class MessageHandlerAdapter<M> implements WeixinMessageHandler { public abstract class MessageHandlerAdapter<M> implements WeixinMessageHandler {
@ -13,6 +22,16 @@ public abstract class MessageHandlerAdapter<M> implements WeixinMessageHandler {
return canHandle0(request, (M) message); return canHandle0(request, (M) message);
} }
/**
* 能否处理请求
*
* @param request
* 微信请求
* @param message
* 微信消息
* @return true则执行doHandler
* @throws WeixinException
*/
public boolean canHandle0(WeixinRequest request, M message) public boolean canHandle0(WeixinRequest request, M message)
throws WeixinException { throws WeixinException {
return true; return true;
@ -24,6 +43,15 @@ public abstract class MessageHandlerAdapter<M> implements WeixinMessageHandler {
return doHandle0(request, (M) message); return doHandle0(request, (M) message);
} }
/**
* 处理请求
*
* @param request
* 微信请求
* @param message
* 微信消息
* @return
*/
public abstract WeixinResponse doHandle0(WeixinRequest request, M message) public abstract WeixinResponse doHandle0(WeixinRequest request, M message)
throws WeixinException; throws WeixinException;

View File

@ -11,7 +11,7 @@ import com.foxinmy.weixin4j.response.WeixinResponse;
* @author jy * @author jy
* @date 2015年5月7日 * @date 2015年5月7日
* @since JDK 1.7 * @since JDK 1.7
* @see * @see MessageHandlerAdapter
*/ */
public interface WeixinMessageHandler { public interface WeixinMessageHandler {
@ -22,7 +22,7 @@ public interface WeixinMessageHandler {
* 微信请求 * 微信请求
* @param message * @param message
* 微信消息 * 微信消息
* @return * @return true则执行doHandle
*/ */
public boolean canHandle(WeixinRequest request, Object message) public boolean canHandle(WeixinRequest request, Object message)
throws WeixinException; throws WeixinException;

View File

@ -14,7 +14,7 @@ import com.foxinmy.weixin4j.response.WeixinResponse;
* @author jy * @author jy
* @date 2015年5月7日 * @date 2015年5月7日
* @since JDK 1.7 * @since JDK 1.7
* @see * @see MessageInterceptorAdapter
*/ */
public interface WeixinMessageInterceptor { public interface WeixinMessageInterceptor {

View File

@ -28,6 +28,7 @@ import com.foxinmy.weixin4j.xml.EncryptMessageHandler;
* @since JDK 1.7 * @since JDK 1.7
* @see <a * @see <a
* href="http://mp.weixin.qq.com/wiki/0/61c3a8b9d50ac74f18bdf2e54ddfc4e0.html">加密接入指引</a> * href="http://mp.weixin.qq.com/wiki/0/61c3a8b9d50ac74f18bdf2e54ddfc4e0.html">加密接入指引</a>
* @see com.foxinmy.weixin4j.request.WeixinRequest
*/ */
public class WeixinMessageDecoder extends public class WeixinMessageDecoder extends
MessageToMessageDecoder<FullHttpRequest> { MessageToMessageDecoder<FullHttpRequest> {

View File

@ -28,7 +28,7 @@ import com.foxinmy.weixin4j.xml.CruxMessageHandler;
* @author jy * @author jy
* @date 2014年11月16日 * @date 2014年11月16日
* @since JDK 1.7 * @since JDK 1.7
* @see * @see com.foxinmy.weixin4j.dispatcher.WeixinMessageDispatcher
*/ */
public class WeixinRequestHandler extends public class WeixinRequestHandler extends
SimpleChannelInboundHandler<WeixinRequest> { SimpleChannelInboundHandler<WeixinRequest> {

View File

@ -28,6 +28,7 @@ import com.foxinmy.weixin4j.util.StringUtil;
* @since JDK 1.7 * @since JDK 1.7
* @see <a * @see <a
* href="http://mp.weixin.qq.com/wiki/0/61c3a8b9d50ac74f18bdf2e54ddfc4e0.html">加密接入指引</a> * href="http://mp.weixin.qq.com/wiki/0/61c3a8b9d50ac74f18bdf2e54ddfc4e0.html">加密接入指引</a>
* @see com.foxinmy.weixin4j.response.WeixinResponse
*/ */
public class WeixinResponseEncoder extends public class WeixinResponseEncoder extends
MessageToMessageEncoder<WeixinResponse> { MessageToMessageEncoder<WeixinResponse> {

View File

@ -10,6 +10,15 @@ import com.foxinmy.weixin4j.bean.AesToken;
import com.foxinmy.weixin4j.dispatcher.WeixinMessageDispatcher; import com.foxinmy.weixin4j.dispatcher.WeixinMessageDispatcher;
import com.foxinmy.weixin4j.exception.WeixinException; 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> { public class WeixinServerInitializer extends ChannelInitializer<SocketChannel> {
private final AesToken aesToken; private final AesToken aesToken;

View File

@ -27,7 +27,10 @@ import com.foxinmy.weixin4j.socket.WeixinServerInitializer;
* @author jy * @author jy
* @date 2014年10月12日 * @date 2014年10月12日
* @since JDK 1.7 * @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 { public final class WeixinServerBootstrap {

View File

@ -14,6 +14,15 @@ import com.foxinmy.weixin4j.type.MessageType;
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
* @author jy
* @date 2015年5月17日
* @since JDK 1.7
* @see
*/
public class CruxMessageHandler extends DefaultHandler { public class CruxMessageHandler extends DefaultHandler {
private String fromUserName; private String fromUserName;

View File

@ -12,6 +12,15 @@ import org.xml.sax.helpers.XMLReaderFactory;
import com.foxinmy.weixin4j.util.Consts; import com.foxinmy.weixin4j.util.Consts;
/**
* 获取加密的密文内容
*
* @className EncryptMessageHandler
* @author jy
* @date 2015年5月17日
* @since JDK 1.7
* @see
*/
public class EncryptMessageHandler extends DefaultHandler { public class EncryptMessageHandler extends DefaultHandler {
private String encryptContent; private String encryptContent;