-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathUser.java
More file actions
361 lines (319 loc) · 15 KB
/
Copy pathUser.java
File metadata and controls
361 lines (319 loc) · 15 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
package io.rong.methods.user;
import com.google.gson.JsonParseException;
import io.rong.RongCloud;
import io.rong.methods.BaseMethod;
import io.rong.methods.user.blacklist.Blacklist;
import io.rong.methods.user.block.Block;
import io.rong.methods.user.blockpushperiod.BlockPushPeriod;
import io.rong.methods.user.mute.MuteChatrooms;
import io.rong.methods.user.mute.MuteGroups;
import io.rong.methods.user.onlinestatus.OnlineStatus;
import io.rong.methods.user.remark.Remark;
import io.rong.methods.user.whitelist.Whitelist;
import io.rong.methods.user.chat.Ban;
import io.rong.models.*;
import io.rong.models.response.*;
import io.rong.models.user.ExpireModel;
import io.rong.models.user.UserModel;
import io.rong.methods.user.tag.Tag;
import io.rong.util.*;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
import com.alibaba.fastjson.JSONException;
import org.apache.commons.lang3.StringUtils;
/**
* User Service
**/
public class User extends BaseMethod {
private static final String UTF8 = "UTF-8";
private static final String PATH = "user";
public Block block;
public Blacklist blackList;
public Whitelist whiteList;
public OnlineStatus onlineStatus;
public Tag tag;
public Remark remark;
public MuteChatrooms muteChatrooms;
public MuteGroups muteGroups;
public Ban ban;
public BlockPushPeriod blockPushPeriod;
@Override
protected void initPath() {
super.path = PATH;
}
public User(String appKey, String appSecret, RongCloud rongCloud) {
this.appKey = appKey;
this.appSecret = appSecret;
this.rongCloud = rongCloud;
this.block = new Block(appKey, appSecret, rongCloud);
this.blackList = new Blacklist(appKey, appSecret, rongCloud);
this.whiteList = new Whitelist(appKey, appSecret, rongCloud);
this.onlineStatus = new OnlineStatus(appKey, appSecret, rongCloud);
this.muteChatrooms = new MuteChatrooms(appKey, appSecret, rongCloud);
this.muteGroups = new MuteGroups(appKey, appSecret, rongCloud);
this.tag = new Tag(appKey, appSecret, rongCloud);
this.ban = new Ban(appKey, appSecret, rongCloud);
this.remark = new Remark(appKey, appSecret, rongCloud);
this.blockPushPeriod = new BlockPushPeriod(appKey, appSecret, rongCloud);
initPath();
}
/**
* Get Token method
* url "/user/getToken"
*
* @param user User information including id, name, portrait (required)
* @return TokenResult
**/
public TokenResult register(UserModel user) throws Exception {
// Fields to be validated
String message = CommonUtil.checkFiled(user, PATH, CheckMethod.REGISTER);
if (null != message) {
return (TokenResult) GsonUtil.fromJson(message, TokenResult.class);
}
StringBuilder sb = new StringBuilder();
sb.append("&userId=").append(URLEncoder.encode(user.id, UTF8));
sb.append("&name=").append(URLEncoder.encode(user.name, UTF8));
if (user.getPortrait() != null) {
sb.append("&portraitUri=").append(URLEncoder.encode(user.portrait, UTF8));
}
String body = sb.toString();
if (body.indexOf("&") == 0) {
body = body.substring(1, body.length());
}
HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(rongCloud.getConfig(), appKey, appSecret, "/user/getToken.json", "application/x-www-form-urlencoded");
HttpUtil.setBodyParameter(body, conn, rongCloud.getConfig());
TokenResult result = null;
String response = "";
try {
response = HttpUtil.returnResult(conn, rongCloud.getConfig());
result = (TokenResult) GsonUtil.fromJson(CommonUtil.getResponseByCode(PATH, CheckMethod.REGISTER, response), TokenResult.class);
} catch (JSONException | JsonParseException | IllegalStateException e) {
rongCloud.getConfig().errorCounter.incrementAndGet();
result = new TokenResult(500, "", user.id, "request:" + conn.getURL() +
" ,response:" + response + " ,JSONException:" + e.getMessage());
}
result.setReqBody(body);
return result;
}
/**
* Refresh user information
* url "/user/refresh"
*
* @param user User information including id, name, and portrait (required)
* @return ResponseResult
**/
public Result update(UserModel user) throws Exception {
// Fields to be validated
String message = CommonUtil.checkFiled(user, PATH, CheckMethod.UPDATE);
if (null != message) {
return (ResponseResult) GsonUtil.fromJson(message, ResponseResult.class);
}
StringBuilder sb = new StringBuilder();
sb.append("&userId=").append(URLEncoder.encode(user.id.toString(), UTF8));
if (user.name != null) {
sb.append("&name=").append(URLEncoder.encode(user.name.toString(), UTF8));
}
if (user.portrait != null) {
sb.append("&portraitUri=").append(URLEncoder.encode(user.portrait.toString(), UTF8));
}
String body = sb.toString();
if (body.indexOf("&") == 0) {
body = body.substring(1, body.length());
}
HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(rongCloud.getConfig(), appKey, appSecret,
"/user/refresh.json", "application/x-www-form-urlencoded");
HttpUtil.setBodyParameter(body, conn, rongCloud.getConfig());
return (ResponseResult) GsonUtil.fromJson(CommonUtil.getResponseByCode(PATH, CheckMethod.UPDATE, HttpUtil.returnResult(conn, rongCloud.getConfig())), ResponseResult.class);
}
/**
* Query user information method
* url "/user/info"
*
* @param user User information id (required)
* @return UserResult
* @throws Exception
*/
public UserResult get(UserModel user) throws Exception {
// Fields to be validated
String message = CommonUtil.checkFiled(user, PATH, CheckMethod.GET);
if (null != message) {
return (UserResult) GsonUtil.fromJson(message, UserResult.class);
}
StringBuilder sb = new StringBuilder();
sb.append("userId=").append(URLEncoder.encode(user.id, UTF8));
String body = sb.toString();
HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(rongCloud.getConfig(), appKey, appSecret,
"/user/info.json", "application/x-www-form-urlencoded");
HttpUtil.setBodyParameter(body, conn, rongCloud.getConfig());
return (UserResult) GsonUtil.fromJson(CommonUtil.getResponseByCode(PATH, CheckMethod.GET, HttpUtil.returnResult(conn, rongCloud.getConfig())), UserResult.class);
}
/**
* Deactivate user
* @param user
* @return
* @throws Exception
*/
public ResponseResult deactivate(UserModel user) throws Exception {
// Fields to be validated
String message = CommonUtil.checkFiled(user, PATH, CheckMethod.GET);
if (null != message) {
return (ResponseResult) GsonUtil.fromJson(message, ResponseResult.class);
}
StringBuilder sb = new StringBuilder();
sb.append("userId=").append(URLEncoder.encode(user.id, UTF8));
String body = sb.toString();
HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(rongCloud.getConfig(), appKey, appSecret,
"/user/deactivate.json", "application/x-www-form-urlencoded");
HttpUtil.setBodyParameter(body, conn, rongCloud.getConfig());
return (ResponseResult) GsonUtil.fromJson(CommonUtil.getResponseByCode(PATH, CheckMethod.GET, HttpUtil.returnResult(conn, rongCloud.getConfig())), ResponseResult.class);
}
public UserDeactivateResult deactivateList() throws Exception {
return deactivateList(1, 50);
}
/**
* Query deactivated users
* @param page The page number
* @param pageSize The number of users per page
* @return UserDeactivateResult
* @throws Exception
*/
public UserDeactivateResult deactivateList(int page, int pageSize) throws Exception {
StringBuilder sb = new StringBuilder();
sb.append("pageNo=").append(page);
sb.append("&pageSize=").append(pageSize);
String body = sb.toString();
HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(rongCloud.getConfig(), appKey, appSecret,
"/user/deactivate/query.json", "application/x-www-form-urlencoded");
HttpUtil.setBodyParameter(body, conn, rongCloud.getConfig());
return (UserDeactivateResult) GsonUtil.fromJson(CommonUtil.getResponseByCode(PATH, CheckMethod.GET, HttpUtil.returnResult(conn, rongCloud.getConfig())), UserDeactivateResult.class);
}
/**
* Activate a user
* @param user The user model containing the user ID
* @return ResponseResult
* @throws Exception
*/
public ResponseResult activate(UserModel user) throws Exception {
// Fields to be validated
String message = CommonUtil.checkFiled(user, PATH, CheckMethod.GET);
if (null != message) {
return (ResponseResult) GsonUtil.fromJson(message, ResponseResult.class);
}
StringBuilder sb = new StringBuilder();
sb.append("userId=").append(URLEncoder.encode(user.id, UTF8));
String body = sb.toString();
HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(rongCloud.getConfig(), appKey, appSecret,
"/user/activate.json", "application/x-www-form-urlencoded");
HttpUtil.setBodyParameter(body, conn, rongCloud.getConfig());
return (ResponseResult) GsonUtil.fromJson(CommonUtil.getResponseByCode(PATH, CheckMethod.GET, HttpUtil.returnResult(conn, rongCloud.getConfig())), ResponseResult.class);
}
/**
* Reactivate a user ID
* @param user The user model containing the user ID
* @return ResponseResult
* @throws Exception
*/
public ResponseResult reactivate(UserModel user) throws Exception {
// Fields to be validated
String message = CommonUtil.checkFiled(user, PATH, CheckMethod.REACTIVATE);
if (null != message) {
return (ResponseResult) GsonUtil.fromJson(message, ResponseResult.class);
}
StringBuilder sb = new StringBuilder();
for (String userId : user.getIds()) {
sb.append(",").append(URLEncoder.encode(userId, UTF8));
}
String body = sb.toString();
if (body.indexOf(",") == 0) {
body = "userId=" + body.substring(1);
}
HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(rongCloud.getConfig(), appKey, appSecret,
"/user/reactivate.json", "application/x-www-form-urlencoded");
HttpUtil.setBodyParameter(body, conn, rongCloud.getConfig());
return (ResponseResult) GsonUtil.fromJson(CommonUtil.getResponseByCode(PATH, CheckMethod.GET, HttpUtil.returnResult(conn, rongCloud.getConfig())), ResponseResult.class);
}
/**
* Query the groups a user belongs to
* url "/user/group/query"
*
* @param user User information, id (required)
* @return UserGroupQueryResult
* @throws Exception
*/
public UserGroupQueryResult getGroups(UserModel user) throws Exception {
// Fields to be validated
String message = CommonUtil.checkFiled(user, PATH, CheckMethod.GET_GROUPS);
if (null != message) {
return (UserGroupQueryResult) GsonUtil.fromJson(message, UserGroupQueryResult.class);
}
StringBuilder sb = new StringBuilder();
sb.append("userId=").append(URLEncoder.encode(user.id, UTF8));
String body = sb.toString();
HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(rongCloud.getConfig(), appKey, appSecret,
"/user/group/query.json", "application/x-www-form-urlencoded");
HttpUtil.setBodyParameter(body, conn, rongCloud.getConfig());
UserGroupQueryResult result = null;
String response = "";
try {
response = HttpUtil.returnResult(conn, rongCloud.getConfig());
result = (UserGroupQueryResult) GsonUtil.fromJson(CommonUtil.getResponseByCode(PATH, CheckMethod.GET_GROUPS, response), UserGroupQueryResult.class);
} catch (JSONException | JsonParseException | IllegalStateException e) {
rongCloud.getConfig().errorCounter.incrementAndGet();
result = new UserGroupQueryResult(500, "request:" + conn.getURL() +
" ,response:" + response + " ,JSONException:" + e.getMessage());
}
result.setReqBody(body);
return result;
}
/**
* Token expiration
* url "/user/refresh"
*
* @param user userId (required) time (required)
* @return ResponseResult
**/
public Result expire(ExpireModel user) throws Exception {
// Fields to validate
String message = CommonUtil.checkFiled(user, PATH, CheckMethod.EXPIRE);
if (null != message) {
return (ResponseResult) GsonUtil.fromJson(message, ResponseResult.class);
}
StringBuilder sb = new StringBuilder();
for (String userId : user.getUserId()) {
sb.append("&userId=").append(URLEncoder.encode(userId, UTF8));
}
sb.append("&time=").append(URLEncoder.encode(user.getTime().toString(), UTF8));
String body = sb.toString();
if (body.indexOf("&") == 0) {
body = body.substring(1);
}
HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(rongCloud.getConfig(), appKey, appSecret,
"/user/token/expire.json", "application/x-www-form-urlencoded");
HttpUtil.setBodyParameter(body, conn, rongCloud.getConfig());
String response = "";
ResponseResult result;
try {
response = HttpUtil.returnResult(conn, rongCloud.getConfig());
result = (ResponseResult) GsonUtil.fromJson(CommonUtil.getResponseByCode(PATH, CheckMethod.EXPIRE, response), ResponseResult.class);
} catch (JSONException | JsonParseException | IllegalStateException e) {
rongCloud.getConfig().errorCounter.incrementAndGet();
result = new ResponseResult(500, "request:" + conn.getURL() + " ,response:" + response + " ,JSONException:" + e.getMessage());
}
result.setReqBody(body);
return result;
}
public QueryUsersResult querySandBoxUsers(Integer page, Integer pageSize, Integer order) throws Exception {
StringBuilder sb = new StringBuilder();
addFormParam(sb, "page=", page);
addFormParam(sb, "&pageSize=", pageSize);
addFormParam(sb, "&order=", order);
String body = sb.toString();
return doRequest("/user/query.json", body, CheckMethod.GET, QueryUsersResult.class);
}
public ResponseResult deleteSandBoxUsers(String... userIds) throws Exception {
StringBuilder sb = new StringBuilder();
addFormParam(sb, "&userId=",StringUtils.join(removeDuplicates(userIds), ","));
String body = sb.toString();
return doRequest("/user/delusers.json", body, CheckMethod.GET, ResponseResult.class);
}
}