PushClient.java
5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.glsx.modules.notify.push;
import com.alibaba.fastjson.JSONObject;
import com.glsx.common.utils.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Component
@Slf4j
public class PushClient {
// The user agent
//protected final String USER_AGENT = "Mozilla/5.0";
//
//// This object is used for sending the post request to Umeng
//protected HttpClient client = new DefaultHttpClient();
//
//// The host
@Value("${youmeng.host}")
protected String host;
//
//// The upload path
////protected static final String uploadPath = "/upload";
//
//// The post path
@Value("${youmeng.postPath}")
protected String postPath;
//
//public boolean send(UmengNotification msg) throws Exception {
// String timestamp = Integer.toString((int)(System.currentTimeMillis() / 1000));
// msg.setPredefinedKeyValue("timestamp", timestamp);
// String url = host + postPath;
// String postBody = msg.getPostBody();
// String sign = DigestUtils.md5Hex(("POST" + url + postBody + msg.getAppMasterSecret()).getBytes("utf8"));
// url = url + "?sign=" + sign;
// HttpPost post = new HttpPost(url);
// post.setHeader("User-Agent", USER_AGENT);
// StringEntity se = new StringEntity(postBody, "UTF-8");
// post.setEntity(se);
// // Send the post request and get the response
// HttpResponse response = client.execute(post);
// int status = response.getStatusLine().getStatusCode();
// System.out.println("Response Code : " + status);
// BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
// StringBuffer result = new StringBuffer();
// String line = "";
// while ((line = rd.readLine()) != null) {
// result.append(line);
// }
// System.out.println(result.toString());
// if (status == 200) {
// System.out.println("Notification sent successfully.");
// } else {
// System.out.println("Failed to send the notification!");
// }
// return true;
//}
// Upload file with device_tokens to Umeng
//public String uploadContents(String appkey,String appMasterSecret,String contents) throws Exception {
// // Construct the json string
// JSONObject uploadJson = new JSONObject();
// uploadJson.put("appkey", appkey);
// String timestamp = Integer.toString((int)(System.currentTimeMillis() / 1000));
// uploadJson.put("timestamp", timestamp);
// uploadJson.put("content", contents);
// // Construct the request
// String url = host + uploadPath;
// String postBody = uploadJson.toString();
// String sign = DigestUtils.md5Hex(("POST" + url + postBody + appMasterSecret).getBytes("utf8"));
// url = url + "?sign=" + sign;
// HttpPost post = new HttpPost(url);
// post.setHeader("User-Agent", USER_AGENT);
// StringEntity se = new StringEntity(postBody, "UTF-8");
// post.setEntity(se);
// // Send the post request and get the response
// HttpResponse response = client.execute(post);
// System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
// BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
// StringBuffer result = new StringBuffer();
// String line = "";
// while ((line = rd.readLine()) != null) {
// result.append(line);
// }
// System.out.println(result.toString());
// // Decode response string and get file_id from it
// JSONObject respJson = new JSONObject(result.toString());
// String ret = respJson.getString("ret");
// if (!ret.equals("SUCCESS")) {
// throw new Exception("Failed to upload file");
// }
// JSONObject data = respJson.getJSONObject("data");
// String fileId = data.getString("file_id");
// // Set file_id into rootJson using setPredefinedKeyValue
//
// return fileId;
//}
public boolean send(UmengNotification msg) throws Exception {
String timestamp = Integer.toString((int) (System.currentTimeMillis() / 1000));
msg.setPredefinedKeyValue("timestamp", timestamp);
String url = host + postPath;
String postBody = msg.getPostBody();
log.info("调用url======="+url);
log.info("调用body======="+postBody);
String sign = DigestUtils.md5Hex(("POST" + url + postBody + msg.getAppMasterSecret()).getBytes("utf8"));
url = url + "?sign=" + sign;
JSONObject respJson = null;
try {
respJson = HttpUtil.sendPost(url, postBody);
} catch (IOException e) {
e.printStackTrace();
log.info("调用请求失败:"+e.getMessage());
}
String ret = respJson.getString("ret");
JSONObject data = respJson.getJSONObject("data");
if (!ret.equals("SUCCESS")) {
String errorCode = data.getString("error_code");
String errorMsg = data.getString("error_msg");
log.error("友盟fail===errorCode:" + errorCode + "===errorMsg:" + errorMsg);
return false;
}
return true;
}
}