View Javadoc
1   package com.zone.weixin4j.controller;
2   
3   import com.zone.weixin4j.exception.HttpResponseException;
4   import com.zone.weixin4j.exception.MessageInterceptorException;
5   import com.zone.weixin4j.exception.WeixinException;
6   import com.zone.weixin4j.response.WeixinResponse;
7   import com.zone.weixin4j.service.WeiXin4jContextAware;
8   import com.zone.weixin4j.service.WxService;
9   import com.zone.weixin4j.util.AesToken;
10  import org.apache.commons.logging.Log;
11  import org.apache.commons.logging.LogFactory;
12  import org.springframework.beans.factory.annotation.Autowired;
13  import org.springframework.util.StringUtils;
14  import org.springframework.web.bind.annotation.RequestParam;
15  
16  import javax.servlet.http.HttpServletRequest;
17  import javax.servlet.http.HttpServletResponse;
18  import java.io.*;
19  
20  /**
21   * Created by Yz on 2017/3/14.
22   * WxController
23   * Spring 主入口需继承的类 / 模版
24   */
25  
26  public abstract class WxController {
27  
28      private final Log logger = LogFactory.getLog(getClass());
29  
30      private final String defaultCharset = "UTF-8";
31  
32      @Autowired
33      protected WxService wxService;
34  
35      @Autowired
36      protected WeiXin4jContextAware weiXin4jContextAware;
37  
38      protected abstract void doRequest(HttpServletRequest request, HttpServletResponse response, @RequestParam(required = false) String encrypt_type, @RequestParam(required = false) String echostr, @RequestParam(required = false) String timestamp, @RequestParam(required = false) String nonce,
39                                        @RequestParam(required = false) String signature, @RequestParam(required = false) String msg_signature, @RequestParam(required = false) String weixin_id);
40  
41      protected void processMessage(HttpServletRequest request, HttpServletResponse response, String encrypt_type, String echostr, String timestamp, String nonce,
42                                    String signature, String msg_signature, String weixin_id) {
43          String messageContent = getMessageContent(request);
44          logger.info("read original message {}" + messageContent);
45          AesToken aesToken = weiXin4jContextAware.getAesTokenMap().get(StringUtils.isEmpty(weixin_id) ? "" : weixin_id);
46          WeixinResponse weixinResponse = null;
47          try {
48              weixinResponse = wxService.processRequest(request.getRequestURI(), encrypt_type, echostr, timestamp, nonce, signature, msg_signature, messageContent, aesToken, request);
49              response.setCharacterEncoding(defaultCharset);
50              writeMessage(wxService.transferResponse(weixinResponse), response);
51          } catch (WeixinException e) {
52              logger.error("errorCode : " + e.getErrorCode() + " , errorMsg :  " + e.getErrorMsg(), e.getCause());
53              e.printStackTrace();
54              writeMessage("", response);
55          } catch (HttpResponseException e) {
56              logger.error(e.getMessage(), e.getCause());
57              response.setStatus(e.getHttpResponseStatus().getCode());
58              response.setContentType(e.getHttpResponseStatus().getReasonPhrase());
59              writeMessage("", response);
60          } catch (MessageInterceptorException e) {
61              logger.error("errorCode : " + e.getErrorCode() + " , errorMsg :  " + e.getErrorMsg(), e.getCause());
62              writeMessage("", response);
63          }
64      }
65  
66      protected String getMessageContent(HttpServletRequest request) {
67          try {
68              // 从request中取得输入流
69              InputStream inputStream = request.getInputStream();
70              BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, defaultCharset));
71              String line;
72              StringBuilder buf = new StringBuilder();
73              while ((line = reader.readLine()) != null) {
74                  buf.append(line);
75              }
76              reader.close();
77              inputStream.close();
78              return buf.toString();
79          } catch (IOException e) {
80              logger.error(e.getMessage(), e.getCause());
81          }
82          return "";
83      }
84  
85      protected void writeMessage(String result, HttpServletResponse response) {
86          response.setContentType("application/xml");
87          response.setCharacterEncoding("UTF-8");
88          try {
89              PrintWriter writer = response.getWriter();
90              writer.write(result);
91              writer.flush();
92              writer.close();
93          } catch (IOException e) {
94              logger.error(e.getMessage(), e.getCause());
95          }
96      }
97  
98      public void setWxService(WxService wxService) {
99          this.wxService = wxService;
100     }
101 
102     public void setWeiXin4jContextAware(WeiXin4jContextAware weiXin4jContextAware) {
103         this.weiXin4jContextAware = weiXin4jContextAware;
104     }
105 
106     public WeiXin4jContextAware getWeiXin4jContextAware() {
107         return weiXin4jContextAware;
108     }
109 }