forked from Tencent/APIJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoFunction.java
More file actions
executable file
·545 lines (459 loc) · 16.4 KB
/
DemoFunction.java
File metadata and controls
executable file
·545 lines (459 loc) · 16.4 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
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
package apijson.demo.server;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import javax.servlet.http.HttpSession;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import apijson.demo.server.model.BaseModel;
import zuo.biao.apijson.JSON;
import zuo.biao.apijson.JSONResponse;
import zuo.biao.apijson.Log;
import zuo.biao.apijson.RequestMethod;
import zuo.biao.apijson.RequestRole;
import zuo.biao.apijson.StringUtil;
import zuo.biao.apijson.server.JSONRequest;
import zuo.biao.apijson.server.NotNull;
import zuo.biao.apijson.server.RemoteFunction;
/**可远程调用的函数类
* @author Lemon
*/
public class DemoFunction extends RemoteFunction {
private static final String TAG = "DemoFunction";
private final HttpSession session;
public DemoFunction(HttpSession session) {
this.session = session;
}
public static void test() throws Exception {
int i0 = 1, i1 = -2;
JSONObject request = new JSONObject();
request.put("id", 10);
request.put("i0", i0);
request.put("i1", i1);
JSONArray arr = new JSONArray();
arr.add(new JSONObject());
request.put("arr", arr);
JSONArray array = new JSONArray();
array.add(1);//new JSONObject());
array.add(2);//new JSONObject());
array.add(4);//new JSONObject());
array.add(10);//new JSONObject());
request.put("array", array);
request.put("position", 1);
request.put("@position", 0);
request.put("key", "key");
JSONObject object = new JSONObject();
object.put("key", true);
request.put("object", object);
Log.i(TAG, "plus(1,-2) = " + new DemoFunction(null).invoke(request, "plus(i0,i1)"));
Log.i(TAG, "count([1,2,4,10]) = " + new DemoFunction(null).invoke(request, "countArray(array)"));
Log.i(TAG, "isContain([1,2,4,10], 10) = " + new DemoFunction(null).invoke(request, "isContain(array,id)"));
Log.i(TAG, "getFromArray([1,2,4,10], 0) = " + new DemoFunction(null).invoke(request, "getFromArray(array,@position)"));
Log.i(TAG, "getFromObject({key:true}, key) = " + new DemoFunction(null).invoke(request, "getFromObject(object,key)"));
forceUseable();
}
/**测试可用性,不catch,不可用直接抛异常,强制在Function表修改为demo为可用的
*/
public static void forceUseable() { // throws UnsupportedOperationException {
//查出所有的 Function 并校验是否已在应用层代码实现
JSONObject request = new JSONObject();
//Function[]<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
JSONRequest functionItem = new JSONRequest();
//Function<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
JSONRequest function = new JSONRequest();
functionItem.put("Function", function);
//Function>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
request.putAll(functionItem.toArray(0, 0, "Function"));
//Function[]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
JSONObject response = new DemoParser(RequestMethod.GET, true).parseResponse(request);
JSONArray fl = response.getJSONArray("Function[]");
if (fl == null || fl.isEmpty()) {
Log.d(TAG, "没有可用的远程函数");
return;
}
JSONObject fi;
for (int i = 0; i < fl.size(); i++) {
fi = fl.getJSONObject(i);
if (fi == null) {
continue;
}
JSONObject demo = JSON.parseObject(fi.getString("demo"));
if (demo == null) {
exitWithError("字段 demo 的值必须为合法且非null的 JSONObejct 字符串!");
}
if (demo.containsKey("result()") == false) {
demo.put("result()", getFunctionCall(fi.getString("name"), fi.getString("arguments")));
}
demo.put(JSONRequest.KEY_COLUMN, "id,name,arguments,demo");
JSONObject r = new DemoParser(RequestMethod.GET, true).parseResponse(demo);
if (JSONResponse.isSuccess(r) == false) {
// throw new UnsupportedOperationException("远程函数测试未通过!请修改 Function 表里的 demo!原因:" + JSONResponse.getMsg(r));
exitWithError(JSONResponse.getMsg(r));
}
}
}
private static void exitWithError(String msg) {
Log.e(TAG, "\n远程函数文档测试未通过!\n请新增 demo 里的函数,或修改 Function 表里的 demo 为已有的函数示例!\n保证前端看到的远程函数文档是正确的!!!\n\n原因:\n" + msg);
System.exit(1);
}
/**反射调用
* @param request
* @param function 例如get(object,key),参数只允许引用,不能直接传值
* @return
*/
public Object invoke(JSONObject request, String function) throws Exception {
//TODO 不允许调用invoke,避免死循环
// if (function.startsWith("invoke(")) {
//
// }
return invoke(this, request, function);
}
/**
* @param request
* @return
* @throws Exception
*/
public Object verifyIdList(@NotNull JSONObject request, @NotNull String idList) throws Exception {
Object obj = request.get(idList);
if (obj instanceof Collection == false) {
throw new IllegalArgumentException(idList + " 不符合 Array 类型! 结构必须是 [] !");
}
JSONArray array = (JSONArray) obj;
if (array != null) {
for (int i = 0; i < array.size(); i++) {
if (array.get(i) instanceof Long == false && array.get(i) instanceof Integer == false) {
throw new IllegalArgumentException(idList + " 内字符 " + array.getString(i) + " 不符合 Long 类型!");
}
}
}
return null;
}
/**
* @param request
* @return
* @throws Exception
*/
public Object verifyURLList(@NotNull JSONObject request, @NotNull String urlList) throws Exception {
Object obj = request.get(urlList);
if (obj instanceof Collection == false) {
throw new IllegalArgumentException(urlList + " 不符合 Array 类型! 结构必须是 [] !");
}
JSONArray array = (JSONArray) obj;
if (array != null) {
for (int i = 0; i < array.size(); i++) {
if (StringUtil.isUrl(array.getString(i)) == false) {
throw new IllegalArgumentException(urlList + " 内字符 " + array.getString(i) + " 不符合 URL 格式!");
}
}
}
return null;
}
/**
* @param rq
* @param momentId
* @return
* @throws Exception
*/
public int deleteCommentOfMoment(@NotNull JSONObject rq, @NotNull String momentId) throws Exception {
long mid = rq.getLongValue(momentId);
if (mid <= 0 || rq.getIntValue(JSONResponse.KEY_COUNT) <= 0) {
return 0;
}
JSONRequest request = new JSONRequest();
//Comment<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
JSONRequest comment = new JSONRequest();
comment.put("momentId", mid);
request.put("Comment", comment);
//Comment>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
JSONObject rp = new DemoParser(RequestMethod.DELETE).setNoVerify(true).parseResponse(request);
JSONObject c = rp.getJSONObject("Comment");
return c == null ? 0 : c.getIntValue(JSONResponse.KEY_COUNT);
}
/**删除评论的子评论
* @param rq
* @param toId
* @return
*/
public int deleteChildComment(@NotNull JSONObject rq, @NotNull String toId) throws Exception {
long tid = rq.getLongValue(toId);
if (tid <= 0 || rq.getIntValue(JSONResponse.KEY_COUNT) <= 0) {
return 0;
}
//递归获取到全部子评论id
JSONRequest request = new JSONRequest();
//Comment<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
JSONRequest comment = new JSONRequest();
comment.put("id{}", getChildCommentIdList(tid));
request.put("Comment", comment);
//Comment>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
JSONObject rp = new DemoParser(RequestMethod.DELETE).setNoVerify(true).parseResponse(request);
JSONObject c = rp.getJSONObject("Comment");
return c == null ? 0 : c.getIntValue(JSONResponse.KEY_COUNT);
}
private JSONArray getChildCommentIdList(long tid) {
JSONArray arr = new JSONArray();
JSONRequest request = new JSONRequest();
//Comment-id[]<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
JSONRequest idItem = new JSONRequest();
//Comment<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
JSONRequest comment = new JSONRequest();
comment.put("toId", tid);
comment.setColumn("id");
idItem.put("Comment", comment);
//Comment>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
request.putAll(idItem.toArray(0, 0, "Comment-id"));
//Comment-id[]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
JSONObject rp = new DemoParser().setNoVerify(true).parseResponse(request);
JSONArray a = rp.getJSONArray("Comment-id[]");
if (a != null) {
arr.addAll(a);
JSONArray a2;
for (int i = 0; i < a.size(); i++) {
a2 = getChildCommentIdList(a.getLongValue(i));
if (a2 != null) {
arr.addAll(a2);
}
}
}
return arr;
}
/**获取远程函数的demo,如果没有就自动补全
* @param request
* @return
*/
public JSONObject getFunctionDemo(@NotNull JSONObject request) {
JSONObject demo = JSON.parseObject(request.getString("demo"));
if (demo == null) {
exitWithError("字段 demo 的值必须为合法且非null的 JSONObejct 字符串!");
}
if (demo.containsKey("result()") == false) {
demo.put("result()", getFunctionCall(request.getString("name"), request.getString("arguments")));
}
return demo;
}
/**获取远程函数的demo,如果没有就自动补全
* @param request
* @return
*/
public String getFunctionDetail(@NotNull JSONObject request) {
return getFunctionCall(request.getString("name"), request.getString("arguments"))
+ ": " + StringUtil.getTrimedString(request.getString("detail"));
}
/**获取函数调用代码
* @param name
* @param arguments
* @return
*/
private static String getFunctionCall(String name, String arguments) {
return name + "(" + arguments + ")";
}
/**TODO 仅用来测试 "key-()":"getIdList()" 和 "key()":"getIdList()"
* @param request
* @return JSONArray 只能用JSONArray,用long[]会在SQLConfig解析崩溃
* @throws Exception
*/
public JSONArray getIdList(@NotNull JSONObject request) throws Exception {
return new JSONArray(new ArrayList<Object>(Arrays.asList(12, 15, 301, 82001, 82002, 38710)));
}
/**TODO 仅用来测试 "key-()":"verifyAccess()"
* @param request
* @return
* @throws Exception
*/
public Object verifyAccess(@NotNull JSONObject request) throws Exception {
long userId = request.getLongValue(zuo.biao.apijson.JSONObject.KEY_USER_ID);
RequestRole role = RequestRole.get(request.getString(zuo.biao.apijson.JSONObject.KEY_ROLE));
if (role == RequestRole.OWNER && userId != DemoVerifier.getVisitorId(session)) {
throw new IllegalAccessException("登录用户与角色OWNER不匹配!");
}
return null;
}
public double plus(@NotNull JSONObject request, String i0, String i1) {
return request.getDoubleValue(i0) + request.getDoubleValue(i1);
}
public double minus(@NotNull JSONObject request, String i0, String i1) {
return request.getDoubleValue(i0) - request.getDoubleValue(i1);
}
public double multiply(@NotNull JSONObject request, String i0, String i1) {
return request.getDoubleValue(i0) * request.getDoubleValue(i1);
}
public double divide(@NotNull JSONObject request, String i0, String i1) {
return request.getDoubleValue(i0) / request.getDoubleValue(i1);
}
//判断是否为空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/**判断array是否为空
* @param request
* @param array
* @return
*/
public boolean isArrayEmpty(@NotNull JSONObject request, String array) {
return BaseModel.isEmpty(request.getJSONArray(array));
}
/**判断object是否为空
* @param request
* @param object
* @return
*/
public boolean isObjectEmpty(@NotNull JSONObject request, String object) {
return BaseModel.isEmpty(request.getJSONObject(object));
}
//判断是否为空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//判断是否为包含 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/**判断array是否包含value
* @param request
* @param array
* @param value
* @return
*/
public boolean isContain(@NotNull JSONObject request, String array, String value) {
//解决isContain((List<Long>) [82001,...], (Integer) 82001) == false及类似问题, list元素可能是从数据库查到的bigint类型的值
// return BaseModel.isContain(request.getJSONArray(array), request.get(value));
//不用准确的的 request.getString(value).getClass() ,因为Long值转Integer崩溃,而且转成一种类型本身就和字符串对比效果一样了。
List<String> list = com.alibaba.fastjson.JSON.parseArray(request.getString(array), String.class);
return list != null && list.contains(request.getString(value));
}
/**判断object是否包含key
* @param request
* @param object
* @param key
* @return
*/
public boolean isContainKey(@NotNull JSONObject request, String object, String key) {
return BaseModel.isContainKey(request.getJSONObject(object), request.getString(key));
}
/**判断object是否包含value
* @param request
* @param object
* @param value
* @return
*/
public boolean isContainValue(@NotNull JSONObject request, String object, String value) {
return BaseModel.isContainValue(request.getJSONObject(object), request.get(value));
}
//判断是否为包含 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//获取集合长度 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/**获取数量
* @param request
* @param array
* @return
*/
public int countArray(@NotNull JSONObject request, String array) {
return BaseModel.count(request.getJSONArray(array));
}
/**获取数量
* @param request
* @param object
* @return
*/
public int countObject(@NotNull JSONObject request, String object) {
return BaseModel.count(request.getJSONObject(object));
}
//获取集合长度 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//根据键获取值 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/**获取
** @param request
* @param array
* @param position 支持直接传数字,例如 getFromArray(array,0) ;或者引用当前对象的值,例如 "@position": 0, "result()": "getFromArray(array,@position)"
* @return
*/
public Object getFromArray(@NotNull JSONObject request, String array, String position) {
int p;
try {
p = Integer.parseInt(position);
} catch (Exception e) {
p = request.getIntValue(position);
}
return BaseModel.get(request.getJSONArray(array), p);
}
/**获取
* @param request
* @param object
* @param key
* @return
*/
public Object getFromObject(@NotNull JSONObject request, String object, String key) {
return BaseModel.get(request.getJSONObject(object), request.getString(key));
}
//根据键获取值 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//根据键移除值 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/**移除
** @param request
* @param array
* @param position 支持直接传数字,例如 getFromArray(array,0) ;或者引用当前对象的值,例如 "@position": 0, "result()": "getFromArray(array,@position)"
* @return
*/
public Object removeIndex(@NotNull JSONObject request, String position) {
int p;
try {
p = Integer.parseInt(position);
} catch (Exception e) {
p = request.getIntValue(position);
}
request.remove(p);
return null;
}
/**移除
* @param request
* @param object
* @param key
* @return
*/
public Object removeKey(@NotNull JSONObject request, String key) {
request.remove(key);
return null;
}
//根据键获取值 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//获取非基本类型对应基本类型的非空值 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/**获取非空值
* @param request
* @param value
* @return
*/
public boolean booleanValue(@NotNull JSONObject request, String value) {
return request.getBooleanValue(value);
}
/**获取非空值
* @param request
* @param value
* @return
*/
public int intValue(@NotNull JSONObject request, String value) {
return request.getIntValue(value);
}
/**获取非空值
* @param request
* @param value
* @return
*/
public long longValue(@NotNull JSONObject request, String value) {
return request.getLongValue(value);
}
/**获取非空值
* @param request
* @param value
* @return
*/
public float floatValue(@NotNull JSONObject request, String value) {
return request.getFloatValue(value);
}
/**获取非空值
* @param request
* @param value
* @return
*/
public double doubleValue(@NotNull JSONObject request, String value) {
return request.getDoubleValue(value);
}
//获取非基本类型对应基本类型的非空值 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
}