diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/dispatcher/MessageHandlerExecutor.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/dispatcher/MessageHandlerExecutor.java index 24ae7f67..34425f3b 100644 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/dispatcher/MessageHandlerExecutor.java +++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/dispatcher/MessageHandlerExecutor.java @@ -4,8 +4,6 @@ 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; @@ -38,19 +36,16 @@ public class MessageHandlerExecutor { /** * 节点名称集合 */ - private final Set nodeNames; - private final ChannelHandlerContext context; + private int interceptorIndex = -1; public MessageHandlerExecutor(ChannelHandlerContext context, WeixinMessageHandler messageHandler, - WeixinMessageInterceptor[] messageInterceptors, - Set nodeNames) { + WeixinMessageInterceptor[] messageInterceptors) { this.context = context; this.messageHandler = messageHandler; this.messageInterceptors = messageInterceptors; - this.nodeNames = nodeNames; } public WeixinMessageHandler getMessageHandler() { @@ -73,8 +68,8 @@ public class MessageHandlerExecutor { for (int i = 0; i < messageInterceptors.length; i++) { WeixinMessageInterceptor interceptor = messageInterceptors[i]; if (!interceptor.preHandle(context, request, message, - nodeNames, messageHandler)) { - triggerAfterCompletion(request, message, null); + messageHandler)) { + triggerAfterCompletion(request, null, message, null); return false; } this.interceptorIndex = i; @@ -102,7 +97,7 @@ public class MessageHandlerExecutor { for (int i = messageInterceptors.length - 1; i >= 0; i--) { WeixinMessageInterceptor interceptor = messageInterceptors[i]; interceptor.postHandle(context, request, response, message, - nodeNames, messageHandler); + messageHandler); } } @@ -111,22 +106,25 @@ public class MessageHandlerExecutor { * * @param request * 微信请求 + * @param response + * 微信响应 可能为空 * @param message * 微信消息 * @param exception * 处理时可能的异常 * @throws WeixinException */ - public void triggerAfterCompletion(WeixinRequest request, Object message, - WeixinException exception) throws WeixinException { + public void triggerAfterCompletion(WeixinRequest request, + WeixinResponse response, Object message, Exception exception) + throws WeixinException { if (messageInterceptors == null) { return; } for (int i = this.interceptorIndex; i >= 0; i--) { WeixinMessageInterceptor interceptor = messageInterceptors[i]; try { - interceptor.afterCompletion(context, request, message, - nodeNames, messageHandler, exception); + interceptor.afterCompletion(context, request, response, + message, messageHandler, exception); } catch (WeixinException e) { logger.error( "MessageInterceptor.afterCompletion threw exception", e); diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/dispatcher/WeixinMessageDispatcher.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/dispatcher/WeixinMessageDispatcher.java index eb7a19c0..9c530628 100644 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/dispatcher/WeixinMessageDispatcher.java +++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/dispatcher/WeixinMessageDispatcher.java @@ -31,7 +31,6 @@ import com.foxinmy.weixin4j.interceptor.WeixinMessageInterceptor; import com.foxinmy.weixin4j.request.WeixinMessage; import com.foxinmy.weixin4j.request.WeixinRequest; import com.foxinmy.weixin4j.response.BlankResponse; -import com.foxinmy.weixin4j.response.SingleResponse; import com.foxinmy.weixin4j.response.WeixinResponse; import com.foxinmy.weixin4j.type.AccountType; import com.foxinmy.weixin4j.util.ClassUtil; @@ -136,20 +135,18 @@ public class WeixinMessageDispatcher { if (!handlerExecutor.applyPreHandle(request, message)) { return; } - WeixinException dispatchException = null; + Exception exception = null; + WeixinResponse response = null; try { - SingleResponse response = handlerExecutor.getMessageHandler() - .doHandle(request, message, cruxMessage.getNodeNames()); - if (response instanceof WeixinResponse) { - handlerExecutor.applyPostHandle(request, - (WeixinResponse) response, message); - } + response = handlerExecutor.getMessageHandler().doHandle(request, + message, cruxMessage.getNodeNames()); + handlerExecutor.applyPostHandle(request, response, message); context.write(response); - } catch (WeixinException e) { - dispatchException = e; + } catch (Exception e) { + exception = e; } - handlerExecutor.triggerAfterCompletion(request, message, - dispatchException); + handlerExecutor.triggerAfterCompletion(request, response, message, + exception); } /** @@ -202,7 +199,7 @@ public class WeixinMessageDispatcher { * @param nodeNames * 节点名称集合 * @return MessageHandlerExecutor - * @see com.foxinmy.weixin4j.dispatcher.MessageHandlerExecutor + * @see MessageHandlerExecutor * @throws WeixinException */ protected MessageHandlerExecutor getHandlerExecutor( @@ -234,7 +231,7 @@ public class WeixinMessageDispatcher { } } return new MessageHandlerExecutor(context, messageHandler, - getMessageInterceptors(), nodeNames); + getMessageInterceptors()); } /** @@ -350,16 +347,16 @@ public class WeixinMessageDispatcher { * @return 消息对象 * @throws WeixinException */ - protected Object messageRead(String message, - Class clazz) throws WeixinException { + protected M messageRead(String message, + Class clazz) throws WeixinException { if (clazz == null) { return null; } try { Source source = new StreamSource(new ByteArrayInputStream( message.getBytes(Consts.UTF_8))); - JAXBElement jaxbElement = getUnmarshaller( - clazz).unmarshal(source, clazz); + JAXBElement jaxbElement = getUnmarshaller(clazz).unmarshal( + source, clazz); return jaxbElement.getValue(); } catch (JAXBException e) { throw new WeixinException(e); diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/handler/WeixinMessageHandler.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/handler/WeixinMessageHandler.java index 32359c4d..dac049f1 100644 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/handler/WeixinMessageHandler.java +++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/handler/WeixinMessageHandler.java @@ -4,7 +4,7 @@ import java.util.Set; import com.foxinmy.weixin4j.exception.WeixinException; import com.foxinmy.weixin4j.request.WeixinRequest; -import com.foxinmy.weixin4j.response.SingleResponse; +import com.foxinmy.weixin4j.response.WeixinResponse; /** * 微信消息处理器 @@ -42,6 +42,6 @@ public interface WeixinMessageHandler { * 节点名称集合 * @return 回复内容 */ - public SingleResponse doHandle(WeixinRequest request, Object message, + public WeixinResponse doHandle(WeixinRequest request, Object message, Set nodeNames) throws WeixinException; } diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/interceptor/MessageInterceptorAdapter.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/interceptor/MessageInterceptorAdapter.java index 89f98df2..a263a13e 100644 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/interceptor/MessageInterceptorAdapter.java +++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/interceptor/MessageInterceptorAdapter.java @@ -2,8 +2,6 @@ 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; @@ -23,22 +21,21 @@ public abstract class MessageInterceptorAdapter implements @Override public boolean preHandle(ChannelHandlerContext context, - WeixinRequest request, Object message, Set nodeNames, - WeixinMessageHandler handler) throws WeixinException { + WeixinRequest request, Object message, WeixinMessageHandler handler) + throws WeixinException { return true; } @Override public void postHandle(ChannelHandlerContext context, WeixinRequest request, WeixinResponse response, Object message, - Set nodeNames, WeixinMessageHandler handler) - throws WeixinException { + WeixinMessageHandler handler) throws WeixinException { } @Override public void afterCompletion(ChannelHandlerContext context, - WeixinRequest request, Object message, Set nodeNames, - WeixinMessageHandler handler, WeixinException exception) + WeixinRequest request, WeixinResponse response, Object message, + WeixinMessageHandler handler, Exception exception) throws WeixinException { } } diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/interceptor/WeixinMessageInterceptor.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/interceptor/WeixinMessageInterceptor.java index 7e59a41c..b00cb1ce 100644 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/interceptor/WeixinMessageInterceptor.java +++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/interceptor/WeixinMessageInterceptor.java @@ -2,8 +2,6 @@ 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; @@ -29,15 +27,13 @@ public interface WeixinMessageInterceptor { * 微信请求 * @param message * 微信消息 - * @param nodeNames - * 节点名称集合 * @param handler * 消息处理器 * @return 返回true执行下一个拦截器 * @throws WeixinException */ boolean preHandle(ChannelHandlerContext context, WeixinRequest request, - Object message, Set nodeNames, WeixinMessageHandler handler) + Object message, WeixinMessageHandler handler) throws WeixinException; /** @@ -51,14 +47,12 @@ public interface WeixinMessageInterceptor { * 微信响应 * @param message * 微信消息 - * @param nodeNames - * 节点名称集合 * @param handler * 消息处理器 * @throws WeixinException */ void postHandle(ChannelHandlerContext context, WeixinRequest request, - WeixinResponse response, Object message, Set nodeNames, + WeixinResponse response, Object message, WeixinMessageHandler handler) throws WeixinException; /** @@ -70,8 +64,6 @@ public interface WeixinMessageInterceptor { * 微信请求 * @param message * 微信消息 - * @param nodeNames - * 节点名称集合 * @param handler * 消息处理器 * @param exception @@ -79,7 +71,7 @@ public interface WeixinMessageInterceptor { * @throws WeixinException */ void afterCompletion(ChannelHandlerContext context, WeixinRequest request, - Object message, Set nodeNames, - WeixinMessageHandler handler, WeixinException exception) + WeixinResponse response, Object message, + WeixinMessageHandler handler, Exception exception) throws WeixinException; } diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/response/BlankResponse.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/response/BlankResponse.java index 10ab5ef9..a3342a87 100644 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/response/BlankResponse.java +++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/response/BlankResponse.java @@ -9,7 +9,7 @@ package com.foxinmy.weixin4j.response; * @since JDK 1.7 * @see */ -public class BlankResponse extends SingleContentResponse { +public class BlankResponse extends SingleResponse { public static final BlankResponse global = new BlankResponse(); diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/response/SingleContentResponse.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/response/SingleContentResponse.java deleted file mode 100644 index 0cf408ad..00000000 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/response/SingleContentResponse.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.foxinmy.weixin4j.response; - -/** - * 单一内容回复 - * - * @className SingleContentResponse - * @author jy - * @date 2015年8月3日 - * @since JDK 1.7 - * @see - */ -public class SingleContentResponse implements SingleResponse { - - private final String content; - - public SingleContentResponse(String content) { - this.content = content; - } - - @Override - public String toContent() { - return content; - } -} diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/response/SingleResponse.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/response/SingleResponse.java index a58ff098..1bd50d55 100644 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/response/SingleResponse.java +++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/response/SingleResponse.java @@ -1,19 +1,34 @@ package com.foxinmy.weixin4j.response; /** - * 单一的字符串回复,如回复SUCCESS + * 单一内容回复 * * @className SingleResponse * @author jy - * @date 2015年6月23日 + * @date 2015年8月3日 * @since JDK 1.7 * @see */ -public interface SingleResponse { - /** - * 回复内容 - * - * @return - */ - public String toContent(); +public class SingleResponse implements WeixinResponse { + + private final String content; + + public SingleResponse(String content) { + this.content = content; + } + + @Override + public String toContent() { + return content; + } + + @Override + public String getMsgType() { + return "single"; + } + + @Override + public String toString() { + return "SingleResponse [content=" + content + "]"; + } } diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/response/WeixinResponse.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/response/WeixinResponse.java index c4a1ed21..1092dff7 100644 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/response/WeixinResponse.java +++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/response/WeixinResponse.java @@ -1,6 +1,5 @@ package com.foxinmy.weixin4j.response; - /** * 微信被动消息回复 * @@ -15,17 +14,25 @@ package com.foxinmy.weixin4j.response; * @see VideoResponse * @see NewsResponse * @see TransferCustomerResponse + * @see SingleResponse * @see BlankResponse * @see 订阅号、服务号的被动响应消息 * @see 企业号的被动响应消息 */ -public interface WeixinResponse extends SingleResponse { +public interface WeixinResponse { /** * 回复的消息类型 * * @return */ public String getMsgType(); + + /** + * 回复的消息内容 + * + * @return + */ + public String toContent(); } diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/socket/WeixinRequestHandler.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/socket/WeixinRequestHandler.java index c974c7b0..4c6485fe 100644 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/socket/WeixinRequestHandler.java +++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/socket/WeixinRequestHandler.java @@ -12,7 +12,7 @@ import io.netty.util.internal.logging.InternalLoggerFactory; import com.foxinmy.weixin4j.dispatcher.WeixinMessageDispatcher; import com.foxinmy.weixin4j.exception.WeixinException; import com.foxinmy.weixin4j.request.WeixinRequest; -import com.foxinmy.weixin4j.response.SingleContentResponse; +import com.foxinmy.weixin4j.response.SingleResponse; import com.foxinmy.weixin4j.type.EncryptType; import com.foxinmy.weixin4j.util.AesToken; import com.foxinmy.weixin4j.util.Consts; @@ -59,7 +59,7 @@ public class WeixinRequestHandler extends if (MessageUtil.signature(aesToken.getToken(), request.getTimeStamp(), request.getNonce()).equals( request.getSignature())) { - ctx.write(new SingleContentResponse(request.getEchoStr())); + ctx.write(new SingleResponse(request.getEchoStr())); return; } ctx.writeAndFlush( diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/socket/WeixinServerInitializer.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/socket/WeixinServerInitializer.java index 5b02a530..6bfd8eb0 100644 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/socket/WeixinServerInitializer.java +++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/socket/WeixinServerInitializer.java @@ -41,8 +41,8 @@ public class WeixinServerInitializer extends ChannelInitializer { pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new HttpObjectAggregator(65536)); pipeline.addLast(new WeixinMessageDecoder(aesTokenMap)); - pipeline.addLast(new SingleResponseEncoder()); pipeline.addLast(new WeixinResponseEncoder()); + pipeline.addLast(new SingleResponseEncoder()); pipeline.addLast(new WeixinRequestHandler(messageDispatcher)); } } diff --git a/weixin4j-server/src/test/java/com/foxinmy/weixin4j/server/test/MessageServerStartup.java b/weixin4j-server/src/test/java/com/foxinmy/weixin4j/server/test/MessageServerStartup.java index a054333a..96337f82 100644 --- a/weixin4j-server/src/test/java/com/foxinmy/weixin4j/server/test/MessageServerStartup.java +++ b/weixin4j-server/src/test/java/com/foxinmy/weixin4j/server/test/MessageServerStartup.java @@ -2,8 +2,6 @@ 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.DebugMessageHandler; import com.foxinmy.weixin4j.handler.MessageHandlerAdapter; @@ -89,8 +87,7 @@ public class MessageServerStartup { @Override public boolean preHandle(ChannelHandlerContext context, WeixinRequest request, Object message, - Set nodeNames, WeixinMessageHandler handler) - throws WeixinException { + WeixinMessageHandler handler) throws WeixinException { context.writeAndFlush(new TextResponse("所有消息被拦截了!")); return false; } @@ -98,16 +95,16 @@ public class MessageServerStartup { @Override public void postHandle(ChannelHandlerContext context, WeixinRequest request, WeixinResponse response, - Object message, Set nodeNames, - WeixinMessageHandler handler) throws WeixinException { + Object message, WeixinMessageHandler handler) + throws WeixinException { System.err.println("preHandle返回为true,执行handler后"); } @Override public void afterCompletion(ChannelHandlerContext context, - WeixinRequest request, Object message, - Set nodeNames, WeixinMessageHandler handler, - WeixinException exception) throws WeixinException { + WeixinRequest request, WeixinResponse response, + Object message, WeixinMessageHandler handler, + Exception exception) throws WeixinException { System.err.println("请求处理完毕"); } };