21
21
22
22
import javax .servlet .http .HttpSession ;
23
23
24
- import com .alibaba .fastjson .JSONArray ;
25
- import com .alibaba .fastjson .JSONObject ;
26
-
27
24
import apijson .JSONResponse ;
28
25
import apijson .NotNull ;
29
26
import apijson .RequestMethod ;
30
27
import apijson .StringUtil ;
28
+ import com .alibaba .fastjson .JSONArray ;
29
+ import com .alibaba .fastjson .JSONObject ;
30
+
31
31
import apijson .framework .javax .APIJSONFunctionParser ;
32
32
import apijson .orm .AbstractVerifier ;
33
- import apijson .orm .JSONRequest ;
34
33
import apijson .orm .Visitor ;
35
34
35
+ import static apijson .JSONObject .KEY_COLUMN ;
36
+ import static apijson .JSONRequest .KEY_COUNT ;
37
+
36
38
37
39
/**可远程调用的函数类,用于自定义业务逻辑处理
38
40
* 具体见 https://github.com/Tencent/APIJSON/issues/101
39
41
* @author Lemon
40
42
*/
41
- public class DemoFunctionParser extends APIJSONFunctionParser {
43
+ public class DemoFunctionParser extends APIJSONFunctionParser < Long , JSONObject , JSONArray > {
42
44
public static final String TAG = "DemoFunctionParser" ;
43
45
44
46
public DemoFunctionParser () {
@@ -48,34 +50,34 @@ public DemoFunctionParser(RequestMethod method, String tag, int version, JSONObj
48
50
super (method , tag , version , request , session );
49
51
}
50
52
51
- public Visitor <Long > getCurrentUser (@ NotNull JSONObject current ) {
53
+ public Visitor <Long > getCurrentUser (@ NotNull JSONObject curObj ) {
52
54
return DemoVerifier .getVisitor (getSession ());
53
55
}
54
56
55
- public Long getCurrentUserId (@ NotNull JSONObject current ) {
57
+ public Long getCurrentUserId (@ NotNull JSONObject curObj ) {
56
58
return DemoVerifier .getVisitorId (getSession ());
57
59
}
58
60
59
- public List <Long > getCurrentUserIdAsList (@ NotNull JSONObject current ) {
61
+ public List <Long > getCurrentUserIdAsList (@ NotNull JSONObject curObj ) {
60
62
List <Long > list = new ArrayList <>(1 );
61
63
list .add (DemoVerifier .getVisitorId (getSession ()));
62
64
return list ;
63
65
}
64
66
65
- public List <Long > getCurrentContactIdList (@ NotNull JSONObject current ) {
66
- Visitor <Long > user = getCurrentUser (current );
67
+ public List <Long > getCurrentContactIdList (@ NotNull JSONObject curObj ) {
68
+ Visitor <Long > user = getCurrentUser (curObj );
67
69
return user == null ? null : user .getContactIdList ();
68
70
}
69
71
70
72
71
73
/**
72
- * @param current
74
+ * @param curObj
73
75
* @param idList
74
76
* @return
75
77
* @throws Exception
76
78
*/
77
- public void verifyIdList (@ NotNull JSONObject current , @ NotNull String idList ) throws Exception {
78
- Object obj = current .get (idList );
79
+ public void verifyIdList (@ NotNull JSONObject curObj , @ NotNull String idList ) throws Exception {
80
+ Object obj = curObj .get (idList );
79
81
if (obj == null ) {
80
82
return ;
81
83
}
@@ -98,13 +100,13 @@ public void verifyIdList(@NotNull JSONObject current, @NotNull String idList) th
98
100
99
101
100
102
/**
101
- * @param current
103
+ * @param curObj
102
104
* @param urlList
103
105
* @return
104
106
* @throws Exception
105
107
*/
106
- public void verifyURLList (@ NotNull JSONObject current , @ NotNull String urlList ) throws Exception {
107
- Object obj = current .get (urlList );
108
+ public void verifyURLList (@ NotNull JSONObject curObj , @ NotNull String urlList ) throws Exception {
109
+ Object obj = curObj .get (urlList );
108
110
if (obj == null ) {
109
111
return ;
110
112
}
@@ -127,21 +129,21 @@ public void verifyURLList(@NotNull JSONObject current, @NotNull String urlList)
127
129
128
130
129
131
/**
130
- * @param current
132
+ * @param curObj
131
133
* @param momentId
132
134
* @return
133
135
* @throws Exception
134
136
*/
135
- public int deleteCommentOfMoment (@ NotNull JSONObject current , @ NotNull String momentId ) throws Exception {
136
- long mid = current .getLongValue (momentId );
137
- if (mid <= 0 || current .getIntValue (JSONResponse .KEY_COUNT ) <= 0 ) {
137
+ public int deleteCommentOfMoment (@ NotNull JSONObject curObj , @ NotNull String momentId ) throws Exception {
138
+ long mid = curObj .getLongValue (momentId );
139
+ if (mid <= 0 || curObj .getIntValue (JSONResponse .KEY_COUNT ) <= 0 ) {
138
140
return 0 ;
139
141
}
140
142
141
- JSONRequest request = new JSONRequest ();
143
+ JSONObject request = JSON . createJSONObject ();
142
144
143
145
//Comment<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
144
- JSONRequest comment = new JSONRequest ();
146
+ JSONObject comment = JSON . createJSONObject ();
145
147
comment .put ("momentId" , mid );
146
148
147
149
request .put ("Comment" , comment );
@@ -155,22 +157,22 @@ public int deleteCommentOfMoment(@NotNull JSONObject current, @NotNull String mo
155
157
156
158
157
159
/**删除评论的子评论
158
- * @param current
160
+ * @param curObj
159
161
* @param toId
160
162
* @return
161
163
*/
162
- public int deleteChildComment (@ NotNull JSONObject current , @ NotNull String toId ) throws Exception {
163
- long tid = current .getLongValue (toId );
164
- if (tid <= 0 || current .getIntValue (JSONResponse .KEY_COUNT ) <= 0 ) {
164
+ public int deleteChildComment (@ NotNull JSONObject curObj , @ NotNull String toId ) throws Exception {
165
+ long tid = curObj .getLongValue (toId );
166
+ if (tid <= 0 || curObj .getIntValue (JSONResponse .KEY_COUNT ) <= 0 ) {
165
167
return 0 ;
166
168
}
167
169
168
170
//递归获取到全部子评论id
169
171
170
- JSONRequest request = new JSONRequest ();
172
+ JSONObject request = JSON . createJSONObject ();
171
173
172
174
//Comment<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
173
- JSONRequest comment = new JSONRequest ();
175
+ JSONObject comment = JSON . createJSONObject ();
174
176
comment .put ("id{}" , getChildCommentIdList (tid ));
175
177
176
178
request .put ("Comment" , comment );
@@ -187,19 +189,20 @@ private JSONArray getChildCommentIdList(long tid) {
187
189
188
190
JSONArray arr = new JSONArray ();
189
191
190
- JSONRequest request = new JSONRequest ();
192
+ JSONObject request = JSON . createJSONObject ();
191
193
192
194
//Comment-id[]<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
193
- JSONRequest idItem = new JSONRequest ();
195
+ JSONObject idItem = JSON . createJSONObject ();
194
196
195
197
//Comment<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
196
- JSONRequest comment = new JSONRequest ();
198
+ JSONObject comment = JSON . createJSONObject ();
197
199
comment .put ("toId" , tid );
198
- comment .setColumn ( "id" );
200
+ comment .put ( KEY_COLUMN , "id" );
199
201
idItem .put ("Comment" , comment );
202
+ idItem .put (KEY_COUNT , 0 );
200
203
//Comment>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
201
204
202
- request .putAll ( idItem . toArray ( 0 , 0 , "Comment-id" ) );
205
+ request .put ( "Comment-id[]" , idItem );
203
206
//Comment-id[]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
204
207
205
208
JSONObject rp = new DemoParser ().setNeedVerify (false ).parseResponse (request );
@@ -223,23 +226,23 @@ private JSONArray getChildCommentIdList(long tid) {
223
226
224
227
225
228
/**TODO 仅用来测试 "key-()":"getIdList()" 和 "key()":"getIdList()"
226
- * @param current
229
+ * @param curObj
227
230
* @return JSONArray 只能用JSONArray,用long[]会在SQLConfig解析崩溃
228
231
* @throws Exception
229
232
*/
230
- public JSONArray getIdList (@ NotNull JSONObject current ) {
233
+ public JSONArray getIdList (@ NotNull JSONObject curObj ) {
231
234
return new JSONArray (new ArrayList <Object >(Arrays .asList (12 , 15 , 301 , 82001 , 82002 , 38710 )));
232
235
}
233
236
234
237
235
238
/**TODO 仅用来测试 "key-()":"verifyAccess()"
236
- * @param current
239
+ * @param curObj
237
240
* @return
238
241
* @throws Exception
239
242
*/
240
- public Object verifyAccess (@ NotNull JSONObject current ) throws Exception {
241
- long userId = current .getLongValue (JSONRequest .KEY_USER_ID );
242
- String role = current .getString (JSONRequest .KEY_ROLE );
243
+ public Object verifyAccess (@ NotNull JSONObject curObj ) throws Exception {
244
+ long userId = curObj .getLongValue (apijson . JSONObject .KEY_USER_ID );
245
+ String role = curObj .getString (apijson . JSONObject .KEY_ROLE );
243
246
if (AbstractVerifier .OWNER .equals (role ) && userId != (Long ) DemoVerifier .getVisitorId (getSession ())) {
244
247
throw new IllegalAccessException ("登录用户与角色OWNER不匹配!" );
245
248
}
0 commit comments