Advertisement
Guest User

Untitled

a guest
May 4th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.90 KB | None | 0 0
  1.              
  2.                     /**
  3.                      * 签名的参数名
  4.                      */
  5.                     private static final String SIGN_PARAM = "user_sign";
  6.                     /**
  7.                      * 渠道的参数名
  8.                      */
  9.                     private static final String CHANNEL_PARAM = "sendposition";
  10.                  
  11.                     public void sendMessage(String mobile, String content) {
  12.                         logger.debug("apply to send message, mobile : " + mobile + ", content : " + content);
  13.                         String ip = this.randomIp();
  14.                         try {     //empty block
  15.                             Map<String, Object> map = new HashMap<>();
  16.                             map.put("method","sendIntegralWall");  // what is it?
  17.                             map.put("modetype",0);                 // what is it?
  18.                             map.put("linkUrl","");                 // what is it? empty param will be removed
  19.                             map.put("sendposition", productId);    // what is it? use your constant CHANNEL_PARAM
  20.                             map.put("phone",mobile);
  21.                             map.put("title", encodeChinese("短信发送"));
  22.                             map.put("content", encodeChinese(content));
  23.                             map.put("pageurl","");
  24.                             map.put("userid","");
  25.                             map.put("msgid","");     // what is it?
  26.                             map.put("vericode","");  // what is it?
  27.                             map.put("sign","");
  28.                             map.put("ip", ip);
  29.                             map.put("timestamp", System.currentTimeMillis());
  30.                             map.put(SIGN_PARAM, this.sign(map));
  31.                  
  32.                             StringBuilder sb = new StringBuilder(sendSmsUrl).append("?");
  33.                             for(Map.Entry<String, Object> entry : map.entrySet()){
  34.                                 sb.append(entry.getKey()).append("=")
  35.                                         .append(entry.getValue() == null ? "" : String.valueOf(entry.getValue()))
  36.                                         .append("&");
  37.                             }
  38.                  
  39.                             sb.deleteCharAt(sb.length() - 1);
  40.                  
  41.                             String url = sb.toString();
  42.                             logger.debug("send message url : " + url);
  43.                             String response = restTemplate.getForObject(url, String.class);  //restTemplate.getForObject(url, Map.class);
  44.                             logger.info("send message, response : " + response);
  45.                  
  46.                             Map<String, String> result = JsonUtil.fromJson(response, Map.class); // not needed
  47.                             if(!"200".equals(result.get("code"))){
  48.                                 throw new RuntimeException("send sms error ! response : " + response);
  49.                             }
  50.                         } finally {  //empty block
  51.                  
  52.                         }
  53.                     }
  54.                  
  55.                     public String sign(Map<String, Object> params){
  56.                         if(params == null || params.size() == 0){
  57.                             return null;
  58.                         }
  59.                  
  60.                         Object channel = String.valueOf(params.get(CHANNEL_PARAM));
  61.                         if(channel == null)   // will be never null: String.valueOf(null) == "null"
  62.                             return null;
  63.                  
  64.                         params.remove(SIGN_PARAM);  // params.get(SIGN_PARAM) always null
  65.                         List<String> paramNames = new ArrayList<>(params.keySet());
  66.                         Collections.sort(paramNames);
  67.                  
  68.                         StringBuilder sb = new StringBuilder();
  69.                         for(String paramName : paramNames){
  70.                             Object obj = params.get(paramName);
  71.                             sb.append(paramName).append("=").append(obj == null ? "" : String.valueOf(obj)).append("&");
  72.                         }
  73.                  
  74.                         if(sb.length() > 0)
  75.                             sb.deleteCharAt(sb.length() - 1);
  76.                  
  77.                         return EncryptUtil.HMAC_SHA1(sb.toString(), key);
  78.                     }
  79.                  
  80.                     private String randomIp(){
  81.                         StringBuilder sb = new StringBuilder();
  82.                         for(int i = 0; i < 4; i++){
  83.                             sb.append(random.nextInt(255)).append("."); // not valid ip,  Class A addresses have range from 1-126 in the first octet
  84.                         }
  85.                  
  86.                         return sb.deleteCharAt(sb.length() - 1).toString();
  87.                     }
  88.                 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement