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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
package com.hmit.kernes.utils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import javax.net.ssl.*;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CaiyunUtil {
private static Logger logger = Logger.getLogger(CaiyunUtil.class);
private final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
public static JSONObject HttpRequest(String request , String RequestMethod , String output, String type, Map<String,Object> headParam ){
@SuppressWarnings("unused")
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
try {
//建立连接
trustAllHosts();//HTTPS请求需建立信任链接
URL url = new URL(request);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
if(RequestMethod.equals("POST"))
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestMethod(RequestMethod);
connection.setHostnameVerifier(DO_NOT_VERIFY);
//一定要设置 Content-Type 要不然服务端接收不到参数
setHeaderParam(connection,type,headParam);
connection.setRequestProperty("Content-Type", "application/Json; charset=UTF-8");
if(output != null){
OutputStream out = connection.getOutputStream();
out.write(output.getBytes("UTF-8"));
out.close();
}
//流处理
InputStream input = connection.getInputStream();
InputStreamReader inputReader = new InputStreamReader(input,"UTF-8");
BufferedReader reader = new BufferedReader(inputReader);
String line;
while((line=reader.readLine())!=null){
buffer.append(line);
}
//关闭连接、释放资源
reader.close();
inputReader.close();
input.close();
input = null;
connection.disconnect();
logger.info(buffer.toString());
jsonObject = JSONObject.fromObject(buffer.toString());
} catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}
public static JSONObject HttpRequestNoSSL(String request , String RequestMethod , String output, String type, Map<String,Object> headParam ){
@SuppressWarnings("unused")
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
try {
//建立连接
URL url = new URL(request);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
// HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
if(RequestMethod.equals("POST"))
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestMethod(RequestMethod);
// connection.setHostnameVerifier(DO_NOT_VERIFY);
//一定要设置 Content-Type 要不然服务端接收不到参数
setHeaderParam(connection,type,headParam);
connection.setRequestProperty("Content-Type", "application/Json; charset=UTF-8");
if(output != null){
OutputStream out = connection.getOutputStream();
out.write(output.getBytes("UTF-8"));
out.close();
}
//流处理
InputStream input = connection.getInputStream();
InputStreamReader inputReader = new InputStreamReader(input,"UTF-8");
BufferedReader reader = new BufferedReader(inputReader);
String line;
while((line=reader.readLine())!=null){
buffer.append(line);
}
//关闭连接、释放资源
reader.close();
inputReader.close();
input.close();
input = null;
connection.disconnect();
logger.info(buffer.toString());
jsonObject = JSONObject.fromObject(buffer.toString());
} catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}
public static Map<String, Object> getOrg(String accessToken){
Map<String,Object> headParam = new HashMap<String, Object>();
headParam.put("accessToken",accessToken);
JSONObject jsonObject = HttpRequest(CaiYun.ORG_URL, "GET",
null,"getOrg",headParam);
String orgSerect=null;
Map<String, Object> map = null;
if (null != jsonObject) {
try {
if (jsonObject.getString("status").equals("0")){ //获取成功
map = new HashMap<String,Object>();
JSONObject dataObject = jsonObject.getJSONObject("data");
JSONObject subscribes= dataObject.getJSONArray("subscribes").getJSONObject(0);
String orgId= subscribes.getString("orgId");
String orgName=subscribes.getString("orgName");
orgSerect=subscribes.getString("orgSecret");
map.put("orgId",orgId);
map.put("orgName",orgName);
map.put("orgSecret",orgSerect);
} else {
logger.info("获取org失败:错误代码--"+jsonObject.getString("status")+",错误原因--"+jsonObject.getString("message"));
}
logger.info("获取org成功:"+orgSerect);
} catch (Exception e) {
e.printStackTrace();
logger.error("获取org异常!!");
}
}
return map;
}
public static Map<String, Object> getAccessToken() {
JSONObject jsonObject = HttpRequest(CaiYun.TOKEN_URL, "GET", null,"AccessToken",null);
String accessToken = null;
String expireTime = null;
Map<String, Object> map = null;
// 如果请求成功
if (null != jsonObject) {
try {
if (jsonObject.getString("status").equals("0")){ //获取成功
map = new HashMap<String,Object>();
JSONObject tokenObject = jsonObject.getJSONObject("data");
accessToken = tokenObject.getString("accessToken");
expireTime = tokenObject.getString("expireTime");
map.put("accessToken",accessToken);
map.put("expireTime",expireTime);
} else {
logger.info("获取token失败:错误代码--"+jsonObject.getString("status")+",错误原因--"+jsonObject.getString("message"));
}
logger.info("获取token成功:"+accessToken+",过期时间:"+expireTime);
} catch (Exception e) {
e.printStackTrace();
logger.error("获取token异常!!");
}
}
return map;
}
public static HttpURLConnection setHeaderParam(HttpURLConnection connection, String type, Map<String,Object> headParam) {
if (type.equals("AccessToken")) {
connection.setRequestProperty("appId", CaiYun.APP_ID);
connection.setRequestProperty("appSecret", CaiYun.APP_SECRET);
} else if (type.equals("authlogin")) {
connection.setRequestProperty("accessToken", headParam.get("accessToken").toString());
connection.setRequestProperty("token", headParam.get("token").toString());
} else if (type.equals("pushMessage")) {
connection.setRequestProperty("accessToken", headParam.get("accessToken").toString());
connection.setRequestProperty("orgSecret", headParam.get("orgSecret").toString());
//connection.setRequestProperty("uid", headParam.get("uid").toString());
} else if (type.equals("getUserInfoByMobile")) {
connection.setRequestProperty("accessToken", headParam.get("accessToken").toString());
connection.setRequestProperty("orgSecret", headParam.get("orgSecret").toString());
}else if (type.equals("getOrg")) {
connection.setRequestProperty("accessToken", headParam.get("accessToken").toString());
}else if(type.equals("getDept")){
connection.setRequestProperty("accessToken", headParam.get("accessToken").toString());
connection.setRequestProperty("orgSecret", headParam.get("orgSecret").toString());
// connection.setRequestProperty("deptId", headParam.get("deptId").toString());
}
return connection;
}
private static void trustAllHosts() {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}
}};
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
}
}
public static int userlist(String accessToken,String orgSecret) {
Map<String,Object> headParam = new HashMap<String, Object>();
int i=0;
headParam.put("accessToken",accessToken);
headParam.put("orgSecret",orgSecret);
logger.info("getUserList start!!");
try{
JSONObject jsonObject = HttpRequest(CaiYun.Contact_export, "GET",null,"pushMessage",headParam);
if (jsonObject != null && jsonObject.getString("status").equals("0")) {
JSONObject data=jsonObject.getJSONObject("data");
JSONArray userList=data.getJSONArray("userList");
i=userList.size();
} else {
logger.info("消息推送失败");
}
} catch (Exception e) {
e.printStackTrace();
logger.error("消息推送失败!");
}
return i;
}
public static JSONArray allUserlist(String accessToken,String orgSecret) {
Map<String,Object> headParam = new HashMap<String, Object>();
headParam.put("accessToken",accessToken);
headParam.put("orgSecret",orgSecret);
JSONArray userList=new JSONArray();
logger.info("getUserList start!!");
try{
JSONObject jsonObject = HttpRequest(CaiYun.Contact_export, "GET",null,"pushMessage",headParam);
if (jsonObject != null && jsonObject.getString("status").equals("0")) {
JSONObject data=jsonObject.getJSONObject("data");
userList=data.getJSONArray("userList");
} else {
logger.info("消息推送失败");
}
} catch (Exception e) {
e.printStackTrace();
logger.error("消息推送失败!");
}
return userList;
}
public static JSONArray allDeptList(String accessToken,String orgSecret,String deptId){
Map<String,Object> headParam = new HashMap<String, Object>();
headParam.put("accessToken",accessToken);
headParam.put("orgSecret",orgSecret);
headParam.put("deptId",deptId);
JSONArray deptList=new JSONArray();
logger.info("getDeptList start!!----deptId:"+deptId);
try{
JSONObject jsonObject =HttpRequestGetDeptInfo(CaiYun.DEPT_LIST_URL,headParam,"getDept");
//JSONObject jsonObject = HttpRequest(CaiYun.DEPT_LIST_URL, "GET",null,"getDept",headParam);
if (jsonObject != null && jsonObject.getString("status").equals("0")) {
JSONObject data=jsonObject.getJSONObject("data");
deptList=data.getJSONArray("departments");
} else {
logger.info("getDeptList 失败");
}
} catch (Exception e) {
e.printStackTrace();
logger.error("getDeptList 失败!");
}
return deptList;
}
public static int wxWatcherList(){
int i=0;
logger.info("getWXList start!!");
try{
JSONObject jsonObject = HttpRequestNoSSL(CaiYun.WX_URL, "GET",null,"NOSSL",null);
if (jsonObject != null) {
JSONObject data=jsonObject.getJSONObject("data");
i=data.getInt("cumulateUser");
} else {
logger.info("WX失败");
}
} catch (Exception e) {
e.printStackTrace();
logger.error("WX失败!");
}
return i;
}
public static Map<String,Object> authLogin(String accessToken,String token) {
Map<String,Object> headParam = new HashMap<String, Object>();
Map<String,Object> result = null;
headParam.put("accessToken",accessToken);
headParam.put("token",token);
logger.info("auth login");
try{
JSONObject jsonObject = HttpRequest(CaiYun.AUTH_LOGIN_URL, "GET", null,"authlogin",headParam);
if (jsonObject != null && jsonObject.getString("status").equals("0")) {
logger.info("免登授权请求成功");
result = new HashMap<String,Object>();
JSONObject userInfo = jsonObject.getJSONObject("data");
result.put("uid",userInfo.getString("uid"));
result.put("mobile",userInfo.getString("mobile"));
result.put("name",userInfo.getString("name"));
result.put("orgSecret",userInfo.getString("orgSecret"));
} else {
logger.info("无返回值");
return null;
}
} catch (Exception e) {
e.printStackTrace();
logger.error("免登授权请求失败!");
}
return result;
}
public static Map<String,Object> pushMessage(String accessToken,String orgSecret,String uid,String queryParam) {
Map<String,Object> headParam = new HashMap<String, Object>();
Map<String,Object> result = null;
headParam.put("accessToken",accessToken);
headParam.put("orgSecret",orgSecret);
headParam.put("uid",uid);
logger.info("pushMessage start!!");
try{
JSONObject jsonObject = HttpRequest(CaiYun.PUSH_MESSAGE_SIMPLE, "POST", queryParam,"pushMessage",headParam);
if (jsonObject != null && jsonObject.getString("status").equals("0")) {
logger.info("消息推送成功");
result = new HashMap<String,Object>();
result.put("status",jsonObject.getString("status"));
result.put("success",jsonObject.getString("success"));
} else {
logger.info("消息推送失败");
}
} catch (Exception e) {
e.printStackTrace();
logger.error("消息推送失败!");
}
return result;
}
public static Map<String,Object> getUserInfoByMobile(String accessToken,String orgSecret,String mobile) {
Map<String,Object> headParam = new HashMap<String, Object>();
Map<String,Object> result = null;
headParam.put("accessToken",accessToken);
headParam.put("orgSecret",orgSecret);
headParam.put("mobile",mobile);
logger.info("getUserInfoByMobile start!!");
try{
JSONObject jsonObject = HttpRequestGetUserInfoByMobile(CaiYun.GET_USER_INFO_BY_MOBILE,headParam,"getUserInfoByMobile");
if (jsonObject != null && jsonObject.getString("status").equals("0")) {
logger.info("获取用户信息成功");
result = new HashMap<String,Object>();
JSONObject userInfo = jsonObject.getJSONObject("data");
result.put("uid",userInfo.getString("uid"));
result.put("name",userInfo.getString("name"));
} else {
logger.info("获取用户信息失败!");
}
} catch (Exception e) {
e.printStackTrace();
logger.error("获取用户信息失败!");
}
return result;
}
public static JSONObject HttpRequestGetUserInfoByMobile(String request ,Map<String,Object> headParam,String type ){
@SuppressWarnings("unused")
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
try {
//建立连接
trustAllHosts();//HTTPS请求需建立信任链接
URL url = new URL(request+"?"+"mobile="+headParam.get("mobile").toString());
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setUseCaches(false);
setHeaderParam(connection,type,headParam);
connection.setHostnameVerifier(DO_NOT_VERIFY);
//流处理
InputStream input = connection.getInputStream();
InputStreamReader inputReader = new InputStreamReader(input,"UTF-8");
BufferedReader reader = new BufferedReader(inputReader);
String line;
while((line=reader.readLine())!=null){
buffer.append(line);
}
//关闭连接、释放资源
reader.close();
inputReader.close();
input.close();
input = null;
connection.disconnect();
logger.info(buffer.toString());
jsonObject = JSONObject.fromObject(buffer.toString());
} catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}
public static JSONObject HttpRequestGetDeptInfo(String request ,Map<String,Object> headParam,String type ){
@SuppressWarnings("unused")
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
try {
//建立连接
trustAllHosts();//HTTPS请求需建立信任链接
URL url = new URL(request+"?"+"deptId="+headParam.get("deptId").toString());
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setUseCaches(false);
setHeaderParam(connection,type,headParam);
connection.setHostnameVerifier(DO_NOT_VERIFY);
//流处理
InputStream input = connection.getInputStream();
InputStreamReader inputReader = new InputStreamReader(input,"UTF-8");
BufferedReader reader = new BufferedReader(inputReader);
String line;
while((line=reader.readLine())!=null){
buffer.append(line);
}
//关闭连接、释放资源
reader.close();
inputReader.close();
input.close();
input = null;
connection.disconnect();
logger.info(buffer.toString());
jsonObject = JSONObject.fromObject(buffer.toString());
} catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}
/* public static void pushMessageToUser(String title, String content, String action, String departmentLabel) {
CaiYunUtil caiYunUtil = new CaiYunUtil();
caiYunUtil.pushMessageTo(title, content, action, departmentLabel);
}*/
/*public static void pushMessageTo(String title, String content, String action, String departmentLabel) {
String[] s1 = null;
List<String> stringList = yspQueryService.queryMobileByDepartmentLabel(departmentLabel);
for (int i = 0; i < stringList.size(); i++) {
Map map = CaiYunUtil.getUserInfoByMobile(CaiYunUtil.getAccessToken().get("accessToken").toString(), yspQueryService.getOrgSecret(), stringList.get(i));
s1[i] = map.get("uid").toString();
}
JSONObject jsonObject = new JSONObject();
jsonObject.put("title", title);
jsonObject.put("content", content);
jsonObject.put("action", action);
jsonObject.put("receivers", s1);
CaiYunUtil.pushMessage(yspQueryService.getAccessToken(), yspQueryService.getOrgSecret(), null, jsonObject.toString());
}*/
public static void pushMsgToUser(String title,String content,String action,List<String> receiveTel ){
Map <String,Object> token = CaiyunUtil.getAccessToken();
JSONObject object = new JSONObject();
object.put("title",title);
object.put("content",content);
object.put("action",action);
Map <String,Object> orgMap=CaiyunUtil.getOrg(token.get("accessToken").toString());
for(String str:receiveTel){
Map <String,Object> map = CaiyunUtil.getUserInfoByMobile(token.get("accessToken").toString(),
orgMap.get("orgSecret").toString(),str);
object.put("receivers", new String[]{map.get("uid").toString()});
CaiyunUtil.pushMessage(token.get("accessToken").toString(),
orgMap.get("orgSecret").toString(),map.get("uid").toString(),object.toString());
}
}
public static int getUserListSize(){
int i=0;
Map <String,Object> token = CaiyunUtil.getAccessToken();
Map <String,Object> orgMap=CaiyunUtil.getOrg(token.get("accessToken").toString());
i=userlist(token.get("accessToken").toString(),
orgMap.get("orgSecret").toString());
return i;
}
public static JSONArray getAllUserList(){
Map <String,Object> token = CaiyunUtil.getAccessToken();
Map <String,Object> orgMap=CaiyunUtil.getOrg(token.get("accessToken").toString());
JSONArray userlist=allUserlist(token.get("accessToken").toString(),
orgMap.get("orgSecret").toString());
return userlist;
}
public static JSONArray getAllDeptList(String deptId){
Map <String,Object> token = CaiyunUtil.getAccessToken();
Map <String,Object> orgMap=CaiyunUtil.getOrg(token.get("accessToken").toString());
JSONArray dept=allDeptList(token.get("accessToken").toString(),
orgMap.get("orgSecret").toString(),deptId);
return dept;
}
public static int getWX(){
int i=0;
i=wxWatcherList();
return i;
}
}