首页 > 技术文章 > 网易云信消息抄送服务的第三方接口示例(Java)

jun1019 2017-04-13 14:05 原文

 

 1 package com.netease.nim.route;
 2 
 3 import com.alibaba.fastjson.JSONObject;
 4 import com.netease.nim.route.CheckSumBuilder;
 5 import org.apache.commons.io.IOUtils;
 6 import org.slf4j.Logger;
 7 import org.slf4j.LoggerFactory;
 8 import org.springframework.stereotype.Controller;
 9 import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.bind.annotation.RequestMethod;
11 import org.springframework.web.bind.annotation.ResponseBody;
12 import javax.servlet.http.HttpServletRequest;
13 import java.io.IOException;
14 
15 @Controller
16 @RequestMapping(value = {"/route"})
17 public class RouteController {
18     public static final Logger logger = LoggerFactory
19             .getLogger(RouteController.class);
20 
21     // 需要改成自身应用的appSecret
22     private final String appSecret = "7bb79g40f44j";
23 
24     @RequestMapping(value = {"/mockClient.action"}, method = {RequestMethod.POST})
25     @ResponseBody
26     public JSONObject mockClient(HttpServletRequest request)
27             throws Exception {
28         JSONObject result = new JSONObject();
29         try {
30             // 获取请求体
31             byte[] body = readBody(request);
32             if (body == null) {
33                 logger.warn("request wrong, empty body!");
34                 result.put("code", 414);
35                 return result;
36             }
37 
38             // 获取部分request header,并打印
39             String ContentType = request.getContentType();
40             String AppKey = request.getHeader("AppKey");
41             String CurTime = request.getHeader("CurTime");
42             String MD5 = request.getHeader("MD5");
43             String CheckSum = request.getHeader("CheckSum");
44             logger.info("request headers: ContentType = {}, AppKey = {}, CurTime = {}, " +
45                     "MD5 = {}, CheckSum = {}", ContentType, AppKey, CurTime, MD5, CheckSum);
46 
47             // 将请求体转成String格式,并打印
48             String requestBody = new String(body, "utf-8");
49             logger.info("request body = {}", requestBody);
50 
51             // 获取计算过的md5及checkSum
52             String verifyMD5 = CheckSumBuilder.getMD5(requestBody);
53             String verifyChecksum = CheckSumBuilder.getCheckSum(appSecret, verifyMD5, CurTime);
54             logger.debug("verifyMD5 = {}, verifyChecksum = {}", verifyMD5, verifyChecksum);
55 
56             // TODO: 比较md5、checkSum是否一致,以及后续业务处理
57 
58             result.put("code", 200);
59             return result;
60         } catch (Exception ex) {
61             logger.error(ex.getMessage(), ex);
62             result.put("code", 414);
63             return result;
64         }
65     }
66 
67     private byte[] readBody(HttpServletRequest request) throws IOException {
68         if (request.getContentLength() > 0) {
69             byte[] body = new byte[request.getContentLength()];
70             IOUtils.readFully(request.getInputStream(), body);
71             return body;
72         } else
73             return null;
74     }
75 
76 }

 

 1 package com.netease.nim.route;
 2 
 3 import java.security.MessageDigest;
 4 
 5 
 6 public class CheckSumBuilder {
 7     // 计算并获取CheckSum
 8     public static String getCheckSum(String appSecret, String nonce, String curTime) {
 9         return encode("sha1", appSecret + nonce + curTime);
10     }
11 
12     // 计算并获取md5值
13     public static String getMD5(String requestBody) {
14         return encode("md5", requestBody);
15     }
16 
17     private static String encode(String algorithm, String value) {
18         if (value == null) {
19             return null;
20         }
21         try {
22             MessageDigest messageDigest
23                     = MessageDigest.getInstance(algorithm);
24             messageDigest.update(value.getBytes());
25             return getFormattedText(messageDigest.digest());
26         } catch (Exception e) {
27             throw new RuntimeException(e);
28         }
29     }
30     private static String getFormattedText(byte[] bytes) {
31         int len = bytes.length;
32         StringBuilder buf = new StringBuilder(len * 2);
33         for (int j = 0; j < len; j++) {
34             buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
35             buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
36         }
37         return buf.toString();
38     }
39     private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5',
40             '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
41 }

 

推荐阅读