-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathChatroomEntry.java
More file actions
214 lines (178 loc) · 7.24 KB
/
Copy pathChatroomEntry.java
File metadata and controls
214 lines (178 loc) · 7.24 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
package io.rong.methods.chatroom.entry;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URLEncoder;
import org.apache.commons.lang3.StringUtils;
import io.rong.RongCloud;
import io.rong.models.CheckMethod;
import io.rong.models.chatroom.ChatroomEntryModel;
import io.rong.models.response.ChatroomEntryListResult;
import io.rong.models.response.ResponseResult;
import io.rong.util.CommonUtil;
import io.rong.util.GsonUtil;
import io.rong.util.HttpUtil;
/**
* Chatroom custom attributes
*
* 1. Set chatroom custom attributes
*
* 2. Delete chatroom custom attributes
*
* 3. Query chatroom custom attributes
*
* @author RongCloud
*
*/
public class ChatroomEntry {
private static final String UTF8 = "UTF-8";
private static final String PATH = "chatroom/entry";
private String appKey;
private String appSecret;
private RongCloud rongCloud;
public RongCloud getRongCloud() {
return rongCloud;
}
public void setRongCloud(RongCloud rongCloud) {
this.rongCloud = rongCloud;
}
public ChatroomEntry(String appKey, String appSecret) {
this.appKey = appKey;
this.appSecret = appSecret;
}
public ChatroomEntry(String appKey, String appSecret, RongCloud rongCloud) {
this.appKey = appKey;
this.appSecret = appSecret;
this.rongCloud = rongCloud;
}
/**
* Set chatroom custom attributes
*
* @param model Required fields: chatroomId, userId, key, value
* @return
* @throws IOException
* @throws ProtocolException
* @throws MalformedURLException
*/
public ResponseResult set(ChatroomEntryModel model) throws Exception {
String message = CommonUtil.checkFiled(model, PATH, CheckMethod.SET);
if (null != message) {
return (ResponseResult) GsonUtil.fromJson(message, ResponseResult.class);
}
StringBuilder sb = new StringBuilder();
sb.append("&chatroomId=").append(URLEncoder.encode(model.chatroomId, UTF8));
sb.append("&userId=").append(URLEncoder.encode(model.userId.toString(), UTF8));
sb.append("&key=").append(URLEncoder.encode(model.key, UTF8));
sb.append("&value=").append(URLEncoder.encode(model.value, UTF8));
sb.append("&autoDelete=").append(model.autoDelete);
if (StringUtils.isNotBlank(model.objectName)) {
sb.append("&objectName=").append(URLEncoder.encode(model.objectName, UTF8));
}
if (StringUtils.isNotBlank(model.content)) {
sb.append("&content=").append(URLEncoder.encode(model.content, UTF8));
}
String body = sb.toString();
if (body.indexOf("&") == 0) {
body = body.substring(1, body.length());
}
HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(rongCloud.getConfig(), appKey, appSecret,
"/chatroom/entry/set.json", "application/x-www-form-urlencoded");
HttpUtil.setBodyParameter(body, conn, rongCloud.getConfig());
return (ResponseResult) GsonUtil.fromJson(
CommonUtil.getResponseByCode(PATH, CheckMethod.SET, HttpUtil.returnResult(conn, rongCloud.getConfig())),
ResponseResult.class);
}
/**
* Batch set chatroom attributes (KV)
*
* @param model Required fields: chatroomId, entryOwnerId, entryInfo
* @return
* @throws IOException
* @throws ProtocolException
* @throws MalformedURLException
*/
public ResponseResult batchSet(ChatroomEntryModel model) throws Exception {
String message = CommonUtil.checkFiled(model, PATH, CheckMethod.BATCHSET);
if (null != message) {
return (ResponseResult) GsonUtil.fromJson(message, ResponseResult.class);
}
StringBuilder sb = new StringBuilder();
sb.append("&chatroomId=").append(URLEncoder.encode(model.getChatroomId(), UTF8));
sb.append("&autoDelete=").append(URLEncoder.encode(model.isAutoDelete().toString(), UTF8));
sb.append("&entryOwnerId=").append(URLEncoder.encode(model.getEntryOwnerId(), UTF8));
sb.append("&entryInfo=").append(URLEncoder.encode(GsonUtil.toJson(model.getEntryInfo()), UTF8));
String body = sb.toString();
if (body.indexOf("&") == 0) {
body = body.substring(1, body.length());
}
HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(rongCloud.getConfig(), appKey, appSecret,
"/chatroom/entry/batch/set.json", "application/x-www-form-urlencoded");
HttpUtil.setBodyParameter(body, conn, rongCloud.getConfig());
ResponseResult result = (ResponseResult) GsonUtil.fromJson(
CommonUtil.getResponseByCode(PATH, CheckMethod.BATCHSET, HttpUtil.returnResult(conn, rongCloud.getConfig())),
ResponseResult.class);
result.setReqBody(body);
return result;
}
/**
* Remove chatroom attributes
*
* @param model Required fields: chatroomId, userId, key
* @return
* @throws Exception
*/
public ResponseResult remove(ChatroomEntryModel model) throws Exception {
String message = CommonUtil.checkFiled(model, PATH, CheckMethod.REMOVE);
if (null != message) {
return (ResponseResult) GsonUtil.fromJson(message, ResponseResult.class);
}
StringBuilder sb = new StringBuilder();
sb.append("&chatroomId=").append(URLEncoder.encode(model.chatroomId, UTF8));
sb.append("&userId=").append(URLEncoder.encode(model.userId, UTF8));
sb.append("&key=").append(URLEncoder.encode(model.key, UTF8));
if (!StringUtils.isBlank(model.objectName)) {
sb.append("&objectName=").append(URLEncoder.encode(model.objectName, UTF8));
}
if (!StringUtils.isBlank(model.content)) {
sb.append("&content=").append(URLEncoder.encode(model.content, UTF8));
}
String body = sb.toString();
if (body.indexOf("&") == 0) {
body = body.substring(1, body.length());
}
HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(rongCloud.getConfig(), appKey, appSecret,
"/chatroom/entry/remove.json", "application/x-www-form-urlencoded");
HttpUtil.setBodyParameter(body, conn, rongCloud.getConfig());
return (ResponseResult) GsonUtil.fromJson(CommonUtil.getResponseByCode(PATH, CheckMethod.REMOVE,
HttpUtil.returnResult(conn, rongCloud.getConfig())), ResponseResult.class);
}
/**
* Query chatroom attributes
*
* @param model Required fields: chatroomId
* @return
*/
public ChatroomEntryListResult query(ChatroomEntryModel model) throws Exception {
String message = CommonUtil.checkFiled(model, PATH, CheckMethod.QUERY);
if (null != message) {
return (ChatroomEntryListResult) GsonUtil.fromJson(message, ChatroomEntryListResult.class);
}
StringBuilder sb = new StringBuilder();
sb.append("&chatroomId=").append(URLEncoder.encode(model.chatroomId, UTF8));
if (null != model.keys && model.keys.length > 0) {
for (String key : model.keys) {
sb.append("&keys=").append(URLEncoder.encode(key, UTF8));
}
}
String body = sb.toString();
if (body.indexOf("&") == 0) {
body = body.substring(1, body.length());
}
HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(rongCloud.getConfig(), appKey, appSecret,
"/chatroom/entry/query.json", "application/x-www-form-urlencoded");
HttpUtil.setBodyParameter(body, conn, rongCloud.getConfig());
return (ChatroomEntryListResult) GsonUtil.fromJson(CommonUtil.getResponseByCode(PATH, CheckMethod.QUERY,
HttpUtil.returnResult(conn, rongCloud.getConfig())), ChatroomEntryListResult.class);
}
}