forked from jinzhenwang/Java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResource.java
More file actions
613 lines (466 loc) · 13.7 KB
/
Resource.java
File metadata and controls
613 lines (466 loc) · 13.7 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
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
package com.apicloud.sdk.api;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import com.alibaba.fastjson.JSONObject;
import com.apicloud.sdk.utils.HttpUtils;
/**
* 操作数据云api
* @author wangjinzhen
* @time 21/05/2015
* @version 0.0.1
*/
public class Resource{
//headers参数
private Map<String,String> headers = new HashMap<String,String>();
private String domain = "https://d.apicloud.com";
/**
* @param appId
* @param appKey
* @param domain 为空或者null为默认https
*/
public Resource(String appId,String appKey,String domain){
if(null!=domain&&!"".equals(domain)){
this.domain = domain;
}
headers.put("X-APICloud-AppId", appId);
headers.put("X-APICloud-AppKey", HttpUtils.encrypt(appId,appKey,"SHA-1"));
}
@SuppressWarnings("unused")
private Resource() {}
/** 对象 ------begin------ **/
/**
* 创建对象
* @param object 对象名称
* @param property 对象所具有的属性
* @return
*/
public JSONObject createObject(String object,JSONObject property){
//校验是否传递参数
if(property==null){
property = new JSONObject();
}
handleFile(property);
headers.put("Content-Type", "application/json");
String url = domain+"/mcm/api/"+object;
return HttpUtils.doPost(url, headers, null,property.toJSONString());
}
/**
* 根据id获取对象
* @param object 对象名称
* @param id 对象Id
* @return
*/
public JSONObject getObject(String object,String id){
String url = domain+"/mcm/api/"+object+"/"+id;
return HttpUtils.doGet(url, headers);
}
/**
* 获取所有对象
* @param object 对象名称
* @return
*/
public JSONObject getObjects(String object){
String url = domain+"/mcm/api/"+object;
return HttpUtils.doGet(url, headers);
}
/**
* 更新对象
* @param object
* @param id
* @param property
* @return
*/
public JSONObject updateObject(String object,String id,JSONObject property){
headers.put("Content-Type", "application/json");
if(property==null||property.size()==0){
return JSONObject.parseObject("{status:0,msg:\"请至少更新一个字段\"}");
}
String url = domain+"/mcm/api/"+object+"/"+id;
return HttpUtils.doPut(url,headers,property.toJSONString());
}
/**
* 删除对象
* @param object 对象名称
* @param id 对象Id
* @return
*/
public JSONObject deleteObject(String object,String id){
String url = domain+"/mcm/api/"+object+"/"+id;
return HttpUtils.doDelete(url, headers);
}
/**
* 统计对象数量
* @param object 对象名称
* @return
*/
public JSONObject getObjectCount(String object){
String url = domain+"/mcm/api/"+object+"/count";
return HttpUtils.doGet(url, headers);
}
/**
* 判断对象是否存在
* @param object 对象名称
* @param id 对象Id
* @return
*/
public JSONObject checkObjectExists(String object,String id){
String url = domain+"/mcm/api/"+object+"/"+id+"/exists";
return HttpUtils.doGet(url, headers);
}
/** 对象 ------end------ **/
/** Relation对象 ------begin------ **/
/**
* 获取关联对象
* @param object
* @param id
* @param relationObject
* @return
*/
public JSONObject getRelationObject(String object,String id,String relationObject){
String url = domain+"/mcm/api/"+object+"/"+id+"/"+relationObject;
return HttpUtils.doGet(url, headers);
}
/**
* 创建关联对象
* @param object
* @param id
* @param relationObject
* @return
*/
public JSONObject createRelationObject(String object,String id,String relationObject,JSONObject property){
//处理文件参数
handleFile(property);
String url = domain+"/mcm/api/"+object+"/"+id+"/"+relationObject;
Map<String,String> propertyMap = new HashMap<String,String>();
Set<String> propertySet = property.keySet();
Iterator<String> iterProperty = propertySet.iterator();
while(iterProperty.hasNext()){
String key = iterProperty.next();
propertyMap.put(key, property.getString(key));
}
headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
return HttpUtils.doPost(url, headers,propertyMap,"");
}
/**
* 统计关联对象数量
* @param object
* @param id
* @param relationObject
* @return
*/
public JSONObject getRelationObjectCount(String object,String id,String relationObject){
String url = domain+"/mcm/api/"+object+"/"+id+"/"+relationObject+"/count";
return HttpUtils.doGet(url, headers);
}
/**
* 删除所有关联对象
* @param object
* @param id
* @param relationObject
* @return
*/
public JSONObject deleteRelationObject(String object,String id,String relationObject){
String url = domain+"/mcm/api/"+object+"/"+id+"/"+relationObject;
return HttpUtils.doDelete(url, headers);
}
/** Relation对象 ------end------ **/
/** 用户 ------begin------ **/
/**
* 创建用户
* @param property
* @return
*/
public JSONObject createUser(JSONObject property){
//校验是否传递参数
if(property==null){
return JSONObject.parseObject("{status:0,msg:\"请传递参数\"}");
}
String userName = property.getString("username");
if(null==userName||"".equals(userName)){
return JSONObject.parseObject("{status:0,msg:\"姓名不能为空\"}");
}
String password = property.getString("password");
if(null==password||"".equals(password)){
return JSONObject.parseObject("{status:0,msg:\"密码不能为空\"}");
}
//处理文件参数
handleFile(property);
headers.put("Content-Type", "application/json");
String url = domain+"/mcm/api/user";
return HttpUtils.doPost(url, headers, null,property.toJSONString());
}
/**
* 用户登录
* @param userName
* @param password
* @return
*/
public JSONObject userLogin(String userName,String passWord){
headers.put("Content-Type", "application/json");
if(null==userName||"".equals(userName)){
return JSONObject.parseObject("{status:0,msg:\"姓名不能为空\"}");
}
if(null==passWord||"".equals(passWord)){
return JSONObject.parseObject("{status:0,msg:\"密码不能为空\"}");
}
String url = domain+"/mcm/api/user/login";
JSONObject property = new JSONObject();
property.put("username", userName);
property.put("password", passWord);
JSONObject returnJson = HttpUtils.doPost(url, headers, null,property.toJSONString());
handleAuthorization(returnJson);
return returnJson;
}
/**
* 请求验证Email
* @param property
* @return
*/
public JSONObject verifyEmail(JSONObject property){
headers.put("Content-Type", "application/json");
//校验是否传递参数
if(property==null){
return JSONObject.parseObject("{status:0,msg:\"请传递参数\"}");
}
String userName = property.getString("username");
if(null==userName||"".equals(userName)){
return JSONObject.parseObject("{status:0,msg:\"姓名不能为空\"}");
}
String email = property.getString("email");
if(null==email||"".equals(email)){
return JSONObject.parseObject("{status:0,msg:\"邮箱不能为空\"}");
}
String url = domain+"/mcm/api/user/verifyEmail";
return HttpUtils.doPost(url, headers, null,property.toJSONString());
}
/**
* 密码重置
* @param property
* @return
*/
public JSONObject resetRequest(JSONObject property){
headers.put("Content-Type", "application/json");
//校验是否传递参数
if(property==null){
return JSONObject.parseObject("{status:0,msg:\"请传递参数\"}");
}
String userName = property.getString("username");
if(null==userName||"".equals(userName)){
return JSONObject.parseObject("{status:0,msg:\"姓名不能为空\"}");
}
String email = property.getString("email");
if(null==email||"".equals(email)){
return JSONObject.parseObject("{status:0,msg:\"邮箱不能为空\"}");
}
String url = domain+"/mcm/api/user/resetRequest";
return HttpUtils.doPost(url, headers, null,property.toJSONString());
}
/**
* 获取用户
* @param authorization login 返回的id
* @param userId
* @return
*/
public JSONObject getUser(String userId){
headers.put("Content-Type", "application/json");
String url = domain+"/mcm/api/user/"+userId;
return HttpUtils.doGet(url, headers);
}
/**
* 更新用户
* @param authorization
* @param userId
* @param property 需要更新的属性
* @return
*/
public JSONObject updateUser(String userId,JSONObject property){
headers.put("Content-Type", "application/json");
if(null==property){
property = new JSONObject();
}
String url = domain+"/mcm/api/user/"+userId;
return HttpUtils.doPut(url, headers, property.toJSONString());
}
/**
* 删除用户
* @param authorization
* @param userId
* @return
*/
public JSONObject deleteUser(String userId){
headers.put("Content-Type", "application/json");
String url = domain+"/mcm/api/user/"+userId;
return HttpUtils.doDelete(url, headers);
}
/**
* 登出
* @param authorization
* @return
*/
public JSONObject loginOut(){
headers.put("Content-Type", "application/json");
String url = domain+"/mcm/api/user/logout";
return HttpUtils.doPost(url, headers, null, "");
}
/** 用户 ------end------ **/
/** 角色 ------end------ **/
/**
* 创建角色
* @param property
* @return
*/
public JSONObject createRole(JSONObject property){
//校验是否传递参数
if(property==null){
return JSONObject.parseObject("{status:0,msg:\"请传递参数\"}");
}
//处理文件参数
handleFile(property);
headers.put("Content-Type", "application/json");
String url = domain+"/mcm/api/role";
return HttpUtils.doPost(url, headers, null,property.toJSONString());
}
/**
* 根据Id获取角色
* @param id
* @return
*/
public JSONObject getRole(String id){
String url = domain+"/mcm/api/role/"+id;
return HttpUtils.doGet(url, headers);
}
/**
* 根据id更新角色
* @param id
* @param property
* @return
*/
public JSONObject updateRole(String id,JSONObject property){
//校验是否传递参数
if(property==null){
return JSONObject.parseObject("{status:0,msg:\"请传递参数\"}");
}
headers.put("Content-Type", "application/json");
String url = domain+"/mcm/api/role/"+id;
return HttpUtils.doPut(url, headers,property.toJSONString());
}
/**
* 根据Id删除角色
* @param id
* @return
*/
public JSONObject deleteRole(String id){
String url = domain+"/mcm/api/role/"+id;
return HttpUtils.doDelete(url, headers);
}
/** 角色 ------end------ **/
/** 批量操作------begin------ **/
/**
* @param prams
* @return
*/
public JSONObject batch(JSONObject params){
//校验是否传递参数
if(params==null){
return JSONObject.parseObject("{status:0,msg:\"请传递参数\"}");
}
String url = domain+"/mcm/api/batch";
headers.put("Content-Type", "application/json");
return HttpUtils.doPost(url, headers, null, params.toJSONString());
}
/** 批量操作------end------ **/
/** 文件(file)------begin------ **/
/**
* 文件上传
* @param fileName
* @return
*/
public JSONObject upload(String filePath){
if(null==filePath||"".equals(filePath)){
return JSONObject.parseObject("{status:0,msg:\"路径不能为空\"}");
}
String url = domain+"/mcm/api/file";
return HttpUtils.doUpload(url, filePath , headers, new HashMap<String,String>());
}
/** 文件(file)------end------ **/
/** 更新操作符------begin------ **/
/**
* @param id
* @param params
* @return
*/
public JSONObject updateModel(String object,String id,JSONObject params){
//校验是否传递参数
if(params==null){
return JSONObject.parseObject("{status:0,msg:\"请传递参数\"}");
}
String url = domain+"/mcm/api/"+object+"/"+id;
return HttpUtils.doPut(url, headers, params.toJSONString());
}
/** 更新操作符------end------ **/
/** 条件过滤------begin------ **/
/**
* 按照条件过滤
* @param object
* @param filter
* @return
*/
public JSONObject doFilterSearch(String object,String filter){
if(null==object||"".equals(object)){
return JSONObject.parseObject("{status:0,msg:\"请确定查询对象\"}");
}
String url = domain+"/mcm/api/"+object+"?filter=";
try {
url += URLEncoder.encode(filter, "utf-8");
} catch (UnsupportedEncodingException e) {
}
return HttpUtils.doGet(url, headers);
}
/** 条件过滤------end------ **/
/** 安全相关------begin------**/
/**
* 设置权限验证码
* @param authorization
*/
public void setAuthorization(String authorization){
if(null!=authorization&&!"".equals(authorization)){
headers.put("authorization", authorization);
}
}
/** 安全相关------end------**/
/**
* 查看参数中是否含有file对象,如果有的话,先上传,在将返回的信息替换掉原来的file对象
* @param property
*/
private void handleFile(JSONObject property) {
//查看values中是否有file对象
Set<String> keySet = property.keySet();
Iterator<String> keyIter = keySet.iterator();
while(keyIter.hasNext()){
String key = keyIter.next();
Object obj = property.get(key);
if(null!=obj&&obj instanceof File){
File file = (File)obj;
if(file.exists()&&file.isFile()){
JSONObject fileJson = upload(file.getPath());
property.put(key, fileJson.toJSONString());
}
}
}
}
/**
* @param returnJson
* 处理login返回来的authorization,登录以后缓存用户的校验码
*/
private void handleAuthorization(JSONObject returnJson) {
String key = returnJson.getString("id");
if(null!=key&&!"".equals(key)){
headers.put("authorization", key);
}
}
}