View Javadoc
1   package com.zone.weixin4j.dispatcher;
2   
3   import com.zone.weixin4j.exception.WeixinException;
4   import com.zone.weixin4j.handler.WeixinMessageHandler;
5   import com.zone.weixin4j.interceptor.WeixinMessageInterceptor;
6   import com.zone.weixin4j.request.WeixinMessage;
7   import com.zone.weixin4j.request.WeixinRequest;
8   import com.zone.weixin4j.response.WeixinResponse;
9   import org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  
12  /**
13   * 微信消息的处理执行
14   *
15   * @author jinyu(foxinmy@gmail.com)
16   * @className MessageHandlerExecutor
17   * @date 2015年5月7日
18   * @see com.zone.weixin4j.handler.WeixinMessageHandler
19   * @see com.zone.weixin4j.interceptor.WeixinMessageInterceptor
20   * @since JDK 1.6
21   */
22  public class MessageHandlerExecutor {
23  
24      private final Log logger = LogFactory.getLog(getClass());
25      /**
26       * 消息处理器
27       */
28      private final WeixinMessageHandler messageHandler;
29  
30      /**
31       * 消息拦截器
32       */
33      private final WeixinMessageInterceptor[] messageInterceptors;
34  
35      private int interceptorIndex = -1;
36  
37      public MessageHandlerExecutor(WeixinMessageHandler messageHandler, WeixinMessageInterceptor[] messageInterceptors) {
38          this.messageHandler = messageHandler;
39          this.messageInterceptors = messageInterceptors;
40      }
41  
42      public WeixinMessageHandler getMessageHandler() {
43          return messageHandler;
44      }
45  
46      /**
47       * 执行预拦截动作
48       *
49       * @param request 微信请求信息
50       * @param message 微信消息
51       * @return true则继续执行往下执行
52       * @throws WeixinException
53       */
54      public boolean applyPreHandle(WeixinRequest request, WeixinMessage message)
55              throws WeixinException {
56          if (messageInterceptors != null) {
57              for (int i = 0; i < messageInterceptors.length; i++) {
58                  WeixinMessageInterceptor interceptor = messageInterceptors[i];
59                  if (!interceptor.preHandle(request, message, messageHandler)) {
60                      triggerAfterCompletion(request, null, message, null);
61                      return false;
62                  }
63                  this.interceptorIndex = i;
64              }
65          }
66          return true;
67      }
68  
69      /**
70       * MessageHandler处理玩请求后的动作
71       *
72       * @param request  微信请求
73       * @param response 处理后的响应
74       * @param message  微信消息
75       * @throws WeixinException
76       */
77      public void applyPostHandle(WeixinRequest request, WeixinResponse response,
78                                  WeixinMessage message) throws WeixinException {
79          if (messageInterceptors == null) {
80              return;
81          }
82          for (int i = messageInterceptors.length - 1; i >= 0; i--) {
83              WeixinMessageInterceptor interceptor = messageInterceptors[i];
84              interceptor.postHandle(request, response, message, messageHandler);
85          }
86      }
87  
88      /**
89       * 全部执行完毕后触发
90       *
91       * @param request   微信请求
92       * @param response  微信响应 可能为空
93       * @param message   微信消息
94       * @param exception 处理时可能的异常
95       * @throws WeixinException
96       */
97      public void triggerAfterCompletion(WeixinRequest request,
98                                         WeixinResponse response, WeixinMessage message, Exception exception)
99              throws WeixinException {
100         if (messageInterceptors == null) {
101             return;
102         }
103         for (int i = this.interceptorIndex; i >= 0; i--) {
104             WeixinMessageInterceptor interceptor = messageInterceptors[i];
105             try {
106                 interceptor.afterCompletion(request, response, message, messageHandler, exception);
107             } catch (WeixinException e) {
108                 logger.error("MessageInterceptor.afterCompletion threw exception", e);
109             }
110         }
111     }
112 }