-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
AbstractParser.java
executable file
·2484 lines (2137 loc) · 86.7 KB
/
AbstractParser.java
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
This source code is licensed under the Apache License Version 2.0.*/
package apijson.orm;
import apijson.orm.exception.ConflictException;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.UnsupportedEncodingException;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.util.*;
import java.util.Map.Entry;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.Query;
import apijson.JSON;
import apijson.JSONRequest;
import apijson.JSONResponse;
import apijson.Log;
import apijson.NotNull;
import apijson.RequestMethod;
import apijson.StringUtil;
import apijson.orm.exception.CommonException;
import apijson.orm.exception.UnsupportedDataTypeException;
import static apijson.JSONObject.KEY_COMBINE;
import static apijson.JSONObject.KEY_EXPLAIN;
import static apijson.RequestMethod.CRUD;
import static apijson.RequestMethod.GET;
/**Parser<T> for parsing request to JSONObject
* @author Lemon
*/
public abstract class AbstractParser<T extends Object> implements Parser<T>, ParserCreator<T>, VerifierCreator<T>, SQLCreator {
protected static final String TAG = "AbstractParser";
/**
* JSON 对象、数组对应的数据源、版本、角色、method等
*/
protected Map<Object, Map<String, Object>> keyObjectAttributesMap = new HashMap<>();
/**
* 可以通过切换该变量来控制是否打印关键的接口请求内容。保守起见,该值默认为false。
* 与 {@link Log#DEBUG} 任何一个为 true 都会打印关键的接口请求内容。
*/
public static boolean IS_PRINT_REQUEST_STRING_LOG = false;
/**
* 打印大数据量日志的标识。线上环境比较敏感,可以通过切换该变量来控制异常栈抛出、错误日志打印。保守起见,该值默认为false。
* 与 {@link Log#DEBUG} 任何一个为 true 都会打印关键的接口请求及响应信息。
*/
public static boolean IS_PRINT_BIG_LOG = false;
/**
* 可以通过切换该变量来控制是否打印关键的接口请求结束时间。保守起见,该值默认为false。
* 与 {@link Log#DEBUG} 任何一个为 true 都会打印关键的接口请求结束时间。
*/
public static boolean IS_PRINT_REQUEST_ENDTIME_LOG = false;
public static int DEFAULT_QUERY_COUNT = 10;
public static int MAX_QUERY_PAGE = 100;
public static int MAX_QUERY_COUNT = 100;
public static int MAX_UPDATE_COUNT = 10;
public static int MAX_SQL_COUNT = 200;
public static int MAX_OBJECT_COUNT = 5;
public static int MAX_ARRAY_COUNT = 5;
public static int MAX_QUERY_DEPTH = 5;
@Override
public int getDefaultQueryCount() {
return DEFAULT_QUERY_COUNT;
}
@Override
public int getMaxQueryPage() {
return MAX_QUERY_PAGE;
}
@Override
public int getMaxQueryCount() {
return MAX_QUERY_COUNT;
}
@Override
public int getMaxUpdateCount() {
return MAX_UPDATE_COUNT;
}
@Override
public int getMaxSQLCount() {
return MAX_SQL_COUNT;
}
@Override
public int getMaxObjectCount() {
return MAX_OBJECT_COUNT;
}
@Override
public int getMaxArrayCount() {
return MAX_ARRAY_COUNT;
}
@Override
public int getMaxQueryDepth() {
return MAX_QUERY_DEPTH;
}
/**
* method = null
*/
public AbstractParser() {
this(null);
}
/**needVerify = true
* @param method null ? requestMethod = GET
*/
public AbstractParser(RequestMethod method) {
super();
setMethod(method);
setNeedVerifyRole(AbstractVerifier.ENABLE_VERIFY_ROLE);
setNeedVerifyContent(AbstractVerifier.ENABLE_VERIFY_CONTENT);
}
/**
* @param method null ? requestMethod = GET
* @param needVerify 仅限于为服务端提供方法免验证特权,普通请求不要设置为 false ! 如果对应Table有权限也建议用默认值 true,保持和客户端权限一致
*/
public AbstractParser(RequestMethod method, boolean needVerify) {
super();
setMethod(method);
setNeedVerify(needVerify);
}
protected boolean isRoot = true;
public boolean isRoot() {
return isRoot;
}
public AbstractParser<T> setRoot(boolean isRoot) {
this.isRoot = isRoot;
return this;
}
public static final String KEY_REF = "Reference";
/**警告信息
* Map<"Reference", "引用赋值获取路径 /Comment/userId 对应的值为 null!">
*/
protected Map<String, String> warnMap = new LinkedHashMap<>();
public String getWarn(String type) {
return warnMap == null ? null : warnMap.get(type);
}
public AbstractParser<T> putWarnIfNeed(String type, String warn) {
if (Log.DEBUG) {
String w = getWarn(type);
if (StringUtil.isEmpty(w, true)) {
putWarn(type, warn);
}
}
return this;
}
public AbstractParser<T> putWarn(String type, String warn) {
if (warnMap == null) {
warnMap = new LinkedHashMap<>();
}
warnMap.put(type, warn);
return this;
}
/**获取警告信息
* @return
*/
public String getWarnString() {
Set<Entry<String, String>> set = warnMap == null ? null : warnMap.entrySet();
if (set == null || set.isEmpty()) {
return null;
}
StringBuilder sb = new StringBuilder();
for (Entry<String, String> e : set) {
String k = e == null ? null : e.getKey();
String v = k == null ? null : e.getValue();
if (StringUtil.isEmpty(v, true)) {
continue;
}
if (StringUtil.isNotEmpty(k, true)) {
sb.append("[" + k + "]: ");
}
sb.append(v + "; ");
}
return sb.toString();
}
@NotNull
protected Visitor<T> visitor;
@NotNull
@Override
public Visitor<T> getVisitor() {
if (visitor == null) {
visitor = new Visitor<T>() {
@Override
public T getId() {
return null;
}
@Override
public List<T> getContactIdList() {
return null;
}
};
}
return visitor;
}
@Override
public AbstractParser<T> setVisitor(@NotNull Visitor<T> visitor) {
this.visitor = visitor;
return this;
}
protected RequestMethod requestMethod;
@NotNull
@Override
public RequestMethod getMethod() {
return requestMethod;
}
@NotNull
@Override
public AbstractParser<T> setMethod(RequestMethod method) {
this.requestMethod = method == null ? GET : method;
this.transactionIsolation = RequestMethod.isQueryMethod(method) ? Connection.TRANSACTION_NONE : Connection.TRANSACTION_REPEATABLE_READ;
return this;
}
protected int version;
@Override
public int getVersion() {
return version;
}
@Override
public AbstractParser<T> setVersion(int version) {
this.version = version;
return this;
}
protected String tag;
@Override
public String getTag() {
return tag;
}
@Override
public AbstractParser<T> setTag(String tag) {
this.tag = tag;
return this;
}
protected String requestURL;
public String getRequestURL() {
return requestURL;
}
public AbstractParser<T> setRequestURL(String requestURL) {
this.requestURL = requestURL;
return this;
}
protected JSONObject requestObject;
@Override
public JSONObject getRequest() {
return requestObject;
}
@Override
public AbstractParser<T> setRequest(JSONObject request) {
this.requestObject = request;
return this;
}
protected Boolean globalFormat;
public AbstractParser<T> setGlobalFormat(Boolean globalFormat) {
this.globalFormat = globalFormat;
return this;
}
@Override
public Boolean getGlobalFormat() {
return globalFormat;
}
protected String globalRole;
public AbstractParser<T> setGlobalRole(String globalRole) {
this.globalRole = globalRole;
return this;
}
@Override
public String getGlobalRole() {
return globalRole;
}
protected String globalDatabase;
public AbstractParser<T> setGlobalDatabase(String globalDatabase) {
this.globalDatabase = globalDatabase;
return this;
}
@Override
public String getGlobalDatabase() {
return globalDatabase;
}
protected String globalSchema;
public AbstractParser<T> setGlobalSchema(String globalSchema) {
this.globalSchema = globalSchema;
return this;
}
@Override
public String getGlobalSchema() {
return globalSchema;
}
protected String globalDatasource;
@Override
public String getGlobalDatasource() {
return globalDatasource;
}
public AbstractParser<T> setGlobalDatasource(String globalDatasource) {
this.globalDatasource = globalDatasource;
return this;
}
protected Boolean globalExplain;
public AbstractParser<T> setGlobalExplain(Boolean globalExplain) {
this.globalExplain = globalExplain;
return this;
}
@Override
public Boolean getGlobalExplain() {
return globalExplain;
}
protected String globalCache;
public AbstractParser<T> setGlobalCache(String globalCache) {
this.globalCache = globalCache;
return this;
}
@Override
public String getGlobalCache() {
return globalCache;
}
@Override
public AbstractParser<T> setNeedVerify(boolean needVerify) {
setNeedVerifyLogin(needVerify);
setNeedVerifyRole(needVerify);
setNeedVerifyContent(needVerify);
return this;
}
protected boolean needVerifyLogin;
@Override
public boolean isNeedVerifyLogin() {
return needVerifyLogin;
}
@Override
public AbstractParser<T> setNeedVerifyLogin(boolean needVerifyLogin) {
this.needVerifyLogin = needVerifyLogin;
return this;
}
protected boolean needVerifyRole;
@Override
public boolean isNeedVerifyRole() {
return needVerifyRole;
}
@Override
public AbstractParser<T> setNeedVerifyRole(boolean needVerifyRole) {
this.needVerifyRole = needVerifyRole;
return this;
}
protected boolean needVerifyContent;
@Override
public boolean isNeedVerifyContent() {
return needVerifyContent;
}
@Override
public AbstractParser<T> setNeedVerifyContent(boolean needVerifyContent) {
this.needVerifyContent = needVerifyContent;
return this;
}
protected SQLExecutor sqlExecutor;
protected Verifier<T> verifier;
protected Map<String, Object> queryResultMap;//path-result
@Override
public SQLExecutor getSQLExecutor() {
if (sqlExecutor == null) {
sqlExecutor = createSQLExecutor();
sqlExecutor.setParser(this);
}
return sqlExecutor;
}
@Override
public Verifier<T> getVerifier() {
if (verifier == null) {
verifier = createVerifier().setVisitor(getVisitor());
}
return verifier;
}
/**解析请求json并获取对应结果
* @param request
* @return
*/
@Override
public String parse(String request) {
return JSON.toJSONString(parseResponse(request));
}
/**解析请求json并获取对应结果
* @param request
* @return
*/
@NotNull
@Override
public String parse(JSONObject request) {
return JSON.toJSONString(parseResponse(request));
}
/**解析请求json并获取对应结果
* @param request 先parseRequest中URLDecoder.decode(request, UTF_8);再parseResponse(getCorrectRequest(...))
* @return parseResponse(requestObject);
*/
@NotNull
@Override
public JSONObject parseResponse(String request) {
Log.d(TAG, "\n\n\n\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"
+ requestMethod + "/parseResponse request = \n" + request + "\n\n");
try {
requestObject = parseRequest(request);
} catch (Exception e) {
return newErrorResult(e, isRoot);
}
return parseResponse(requestObject);
}
private int queryDepth;
private long executedSQLDuration;
/**解析请求json并获取对应结果
* @param request
* @return requestObject
*/
@NotNull
@Override
public JSONObject parseResponse(JSONObject request) {
long startTime = System.currentTimeMillis();
Log.d(TAG, "parseResponse startTime = " + startTime
+ "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n\n\n ");
requestObject = request;
try {
setVersion(requestObject.getIntValue(JSONRequest.KEY_VERSION));
requestObject.remove(JSONRequest.KEY_VERSION);
if (getMethod() != RequestMethod.CRUD) {
setTag(requestObject.getString(JSONRequest.KEY_TAG));
requestObject.remove(JSONRequest.KEY_TAG);
}
} catch (Exception e) {
return extendErrorResult(requestObject, e, requestMethod, getRequestURL(), isRoot);
}
verifier = createVerifier().setVisitor(getVisitor());
if (RequestMethod.isPublicMethod(requestMethod) == false) {
try {
if (isNeedVerifyLogin()) {
onVerifyLogin();
}
if (isNeedVerifyContent()) {
onVerifyContent();
}
} catch (Exception e) {
return extendErrorResult(requestObject, e, requestMethod, getRequestURL(), isRoot);
}
}
//必须在parseCorrectRequest后面,因为parseCorrectRequest可能会添加 @role
if (isNeedVerifyRole() && globalRole == null) {
try {
setGlobalRole(requestObject.getString(JSONRequest.KEY_ROLE));
requestObject.remove(JSONRequest.KEY_ROLE);
} catch (Exception e) {
return extendErrorResult(requestObject, e, requestMethod, getRequestURL(), isRoot);
}
}
try {
setGlobalFormat(requestObject.getBoolean(JSONRequest.KEY_FORMAT));
setGlobalDatabase(requestObject.getString(JSONRequest.KEY_DATABASE));
setGlobalSchema(requestObject.getString(JSONRequest.KEY_SCHEMA));
setGlobalDatasource(requestObject.getString(JSONRequest.KEY_DATASOURCE));
setGlobalExplain(requestObject.getBoolean(JSONRequest.KEY_EXPLAIN));
setGlobalCache(requestObject.getString(JSONRequest.KEY_CACHE));
requestObject.remove(JSONRequest.KEY_FORMAT);
requestObject.remove(JSONRequest.KEY_DATABASE);
requestObject.remove(JSONRequest.KEY_SCHEMA);
requestObject.remove(JSONRequest.KEY_DATASOURCE);
requestObject.remove(JSONRequest.KEY_EXPLAIN);
requestObject.remove(JSONRequest.KEY_CACHE);
} catch (Exception e) {
return extendErrorResult(requestObject, e, requestMethod, getRequestURL(), isRoot);
}
final String requestString = JSON.toJSONString(request);//request传进去解析后已经变了
queryResultMap = new HashMap<String, Object>();
Exception error = null;
sqlExecutor = getSQLExecutor();
onBegin();
try {
queryDepth = 0;
executedSQLDuration = 0;
requestObject = onObjectParse(request, null, null, null, false);
onCommit();
}
catch (Exception e) {
e.printStackTrace();
error = e;
onRollback();
}
String warn = Log.DEBUG == false || error != null ? null : getWarnString();
requestObject = error == null ? extendSuccessResult(requestObject, warn, isRoot) : extendErrorResult(requestObject, error, requestMethod, getRequestURL(), isRoot);
JSONObject res = (globalFormat != null && globalFormat) && JSONResponse.isSuccess(requestObject) ? new JSONResponse(requestObject) : requestObject;
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
res.putIfAbsent("time", endTime);
if (Log.DEBUG) {
res.put("sql:generate|cache|execute|maxExecute", getSQLExecutor().getGeneratedSQLCount() + "|" + getSQLExecutor().getCachedSQLCount() + "|" + getSQLExecutor().getExecutedSQLCount() + "|" + getMaxSQLCount());
res.put("depth:count|max", queryDepth + "|" + getMaxQueryDepth());
executedSQLDuration += sqlExecutor.getExecutedSQLDuration() + sqlExecutor.getSqlResultDuration();
long parseDuration = duration - executedSQLDuration;
res.put("time:start|duration|end|parse|sql", startTime + "|" + duration + "|" + endTime + "|" + parseDuration + "|" + executedSQLDuration);
if (error != null) {
// String msg = error.getMessage();
// if (msg != null && msg.contains(Log.KEY_SYSTEM_INFO_DIVIDER)) {
// }
Throwable t = error instanceof CommonException && error.getCause() != null ? error.getCause() : error;
res.put("trace:throw", t.getClass().getName());
res.put("trace:stack", t.getStackTrace());
}
}
onClose();
// CS304 Issue link: https://github.com/Tencent/APIJSON/issues/232
if (IS_PRINT_REQUEST_STRING_LOG || Log.DEBUG || error != null) {
Log.sl("\n\n\n", '<', "");
Log.fd(TAG, requestMethod + "/parseResponse request = \n" + requestString + "\n\n");
}
if (IS_PRINT_BIG_LOG || Log.DEBUG || error != null) { // 日志仅存服务器,所以不太敏感,而且这些日志虽然量大但非常重要,对排查 bug 很关键
Log.fd(TAG, requestMethod + "/parseResponse return response = \n" + JSON.toJSONString(requestObject) + "\n\n");
}
if (IS_PRINT_REQUEST_ENDTIME_LOG || Log.DEBUG || error != null) {
Log.fd(TAG, requestMethod + "/parseResponse endTime = " + endTime + "; duration = " + duration);
Log.sl("", '>', "\n\n\n");
}
return res;
}
@Override
public void onVerifyLogin() throws Exception {
getVerifier().verifyLogin();
}
@Override
public void onVerifyContent() throws Exception {
requestObject = parseCorrectRequest();
}
/**校验角色及对应操作的权限
* @param config
* @return
* @throws Exception
*/
@Override
public void onVerifyRole(@NotNull SQLConfig<T> config) throws Exception {
if (Log.DEBUG) {
Log.i(TAG, "onVerifyRole config = " + JSON.toJSONString(config));
}
if (isNeedVerifyRole()) {
if (config.getRole() == null) {
if (globalRole != null) {
config.setRole(globalRole);
} else {
config.setRole(getVisitor().getId() == null ? AbstractVerifier.UNKNOWN : AbstractVerifier.LOGIN);
}
}
getVerifier().verifyAccess(config);
}
}
/**解析请求JSONObject
* @param request => URLDecoder.decode(request, UTF_8);
* @return
* @throws Exception
*/
@NotNull
public static JSONObject parseRequest(String request) throws Exception {
JSONObject obj = JSON.parseObject(request);
if (obj == null) {
throw new UnsupportedEncodingException("JSON格式不合法!");
}
return obj;
}
@Override
public JSONObject parseCorrectRequest(RequestMethod method, String tag, int version, String name, @NotNull JSONObject request
, int maxUpdateCount, SQLCreator creator) throws Exception {
if (RequestMethod.isPublicMethod(method)) {
return request;//需要指定JSON结构的get请求可以改为post请求。一般只有对安全性要求高的才会指定,而这种情况用明文的GET方式几乎肯定不安全
}
return batchVerify(method, tag, version, name, request, maxUpdateCount, creator);
}
/**自动根据 tag 是否为 TableKey 及是否被包含在 object 内来决定是否包装一层,改为 { tag: object, "tag": tag }
* @param object
* @param tag
* @return
*/
public static JSONObject wrapRequest(RequestMethod method, String tag, JSONObject object, boolean isStructure) {
boolean putTag = ! isStructure;
if (object == null || object.containsKey(tag)) { //tag 是 Table 名或 Table[]
if (putTag) {
if (object == null) {
object = new JSONObject(true);
}
object.put(JSONRequest.KEY_TAG, tag);
}
return object;
}
boolean isDiffArrayKey = tag.endsWith(":[]");
boolean isArrayKey = isDiffArrayKey || JSONRequest.isArrayKey(tag);
String key = isArrayKey ? tag.substring(0, tag.length() - (isDiffArrayKey ? 3 : 2)) : tag;
JSONObject target = object;
if (apijson.JSONObject.isTableKey(key)) {
if (isDiffArrayKey) { //自动为 tag = Comment:[] 的 { ... } 新增键值对为 { "Comment[]":[], "TYPE": { "Comment[]": "OBJECT[]" } ... }
if (isStructure && (method == RequestMethod.POST || method == RequestMethod.PUT)) {
String arrKey = key + "[]";
if (target.containsKey(arrKey) == false) {
target.put(arrKey, new JSONArray());
}
try {
JSONObject type = target.getJSONObject(Operation.TYPE.name());
if (type == null || (type.containsKey(arrKey) == false)) {
if (type == null) {
type = new JSONObject(true);
}
type.put(arrKey, "OBJECT[]");
target.put(Operation.TYPE.name(), type);
}
}
catch (Throwable e) {
Log.w(TAG, "wrapRequest try { JSONObject type = target.getJSONObject(Operation.TYPE.name()); } catch (Exception e) = " + e.getMessage());
}
}
}
else { //自动为 tag = Comment 的 { ... } 包一层为 { "Comment": { ... } }
if (isArrayKey == false || RequestMethod.isGetMethod(method, true)) {
target = new JSONObject(true);
target.put(tag, object);
}
else if (target.containsKey(key) == false) {
target = new JSONObject(true);
target.put(key, object);
}
}
}
if (putTag) {
target.put(JSONRequest.KEY_TAG, tag);
}
return target;
}
/**新建带状态内容的JSONObject
* @param code
* @param msg
* @return
*/
public static JSONObject newResult(int code, String msg) {
return newResult(code, msg, null);
}
/**
* 添加JSONObject的状态内容,一般用于错误提示结果
*
* @param code
* @param msg
* @param warn
* @return
*/
public static JSONObject newResult(int code, String msg, String warn) {
return newResult(code, msg, warn, false);
}
/**
* 新建带状态内容的JSONObject
*
* @param code
* @param msg
* @param warn
* @param isRoot
* @return
*/
public static JSONObject newResult(int code, String msg, String warn, boolean isRoot) {
return extendResult(null, code, msg, warn, isRoot);
}
/**
* 添加JSONObject的状态内容,一般用于错误提示结果
*
* @param object
* @param code
* @param msg
* @return
*/
public static JSONObject extendResult(JSONObject object, int code, String msg, String warn, boolean isRoot) {
int index = Log.DEBUG == false || isRoot == false || msg == null ? -1 : msg.lastIndexOf(Log.KEY_SYSTEM_INFO_DIVIDER);
String debug = Log.DEBUG == false || isRoot == false ? null : (index >= 0 ? msg.substring(index + Log.KEY_SYSTEM_INFO_DIVIDER.length()).trim()
: " \n提 bug 请发请求和响应的【完整截屏】,没图的自行解决!"
+ " \n开发者有限的时间和精力主要放在【维护项目源码和文档】上!"
+ " \n【描述不详细】 或 【文档/常见问题 已有答案】 的问题可能会被忽略!!"
+ " \n【态度 不文明/不友善】的可能会被踢出群,问题也可能不予解答!!!"
+ " \n\n **环境信息** "
+ " \n系统: " + Log.OS_NAME + " " + Log.OS_VERSION
+ " \n数据库: DEFAULT_DATABASE = " + AbstractSQLConfig.DEFAULT_DATABASE
+ " \nJDK: " + Log.JAVA_VERSION + " " + Log.OS_ARCH
+ " \nAPIJSON: " + Log.VERSION
+ " \n \n【常见问题】:https://github.com/Tencent/APIJSON/issues/36"
+ " \n【通用文档】:https://github.com/Tencent/APIJSON/blob/master/Document.md"
+ " \n【视频教程】:https://search.bilibili.com/all?keyword=APIJSON");
msg = index >= 0 ? msg.substring(0, index) : msg;
if (object == null) {
object = new JSONObject(true);
}
if (object.get(JSONResponse.KEY_OK) == null) {
object.put(JSONResponse.KEY_OK, JSONResponse.isSuccess(code));
}
if (object.get(JSONResponse.KEY_CODE) == null) {
object.put(JSONResponse.KEY_CODE, code);
}
String m = StringUtil.getString(object.getString(JSONResponse.KEY_MSG));
if (m.isEmpty() == false) {
msg = m + " ;\n " + StringUtil.getString(msg);
}
object.put(JSONResponse.KEY_MSG, msg);
if (debug != null) {
if (StringUtil.isNotEmpty(warn, true)) {
debug += "\n 【警告】:" + warn;
}
object.put("debug:info|help", debug);
}
return object;
}
/**
* 添加请求成功的状态内容
*
* @param object
* @return
*/
public static JSONObject extendSuccessResult(JSONObject object) {
return extendSuccessResult(object, false);
}
public static JSONObject extendSuccessResult(JSONObject object, boolean isRoot) {
return extendSuccessResult(object, null, isRoot);
}
/**添加请求成功的状态内容
* @param object
* @param isRoot
* @return
*/
public static JSONObject extendSuccessResult(JSONObject object, String warn, boolean isRoot) {
return extendResult(object, JSONResponse.CODE_SUCCESS, JSONResponse.MSG_SUCCEED, warn, isRoot);
}
/**获取请求成功的状态内容
* @return
*/
public static JSONObject newSuccessResult() {
return newSuccessResult(null);
}
/**获取请求成功的状态内容
* @param warn
* @return
*/
public static JSONObject newSuccessResult(String warn) {
return newSuccessResult(warn, false);
}
/**获取请求成功的状态内容
* @param warn
* @param isRoot
* @return
*/
public static JSONObject newSuccessResult(String warn, boolean isRoot) {
return newResult(JSONResponse.CODE_SUCCESS, JSONResponse.MSG_SUCCEED, warn, isRoot);
}
/**添加请求成功的状态内容
* @param object
* @param e
* @return
*/
public static JSONObject extendErrorResult(JSONObject object, Throwable e) {
return extendErrorResult(object, e, false);
}
/**添加请求成功的状态内容
* @param object
* @param e
* @param isRoot
* @return
*/
public static JSONObject extendErrorResult(JSONObject object, Throwable e, boolean isRoot) {
return extendErrorResult(object, e, null, null, isRoot);
}
/**添加请求成功的状态内容
* @param object
* @return
*/
public static JSONObject extendErrorResult(JSONObject object, Throwable e, RequestMethod requestMethod, String url, boolean isRoot) {
String msg = CommonException.getMsg(e);
if (Log.DEBUG && isRoot) {
try {
boolean isCommon = e instanceof CommonException;
String env = isCommon ? ((CommonException) e).getEnvironment() : null;
if (StringUtil.isEmpty(env)) {
//int index = msg.lastIndexOf(Log.KEY_SYSTEM_INFO_DIVIDER);
//env = index >= 0 ? msg.substring(index + Log.KEY_SYSTEM_INFO_DIVIDER.length()).trim()
env = " \n **环境信息** "
+ " \n 系统: " + Log.OS_NAME + " " + Log.OS_VERSION
+ " \n 数据库: <!-- 请填写,例如 MySQL 5.7。默认数据库为 " + AbstractSQLConfig.DEFAULT_DATABASE + " -->"
+ " \n JDK: " + Log.JAVA_VERSION + " " + Log.OS_ARCH
+ " \n APIJSON: " + Log.VERSION;
//msg = index < 0 ? msg : msg.substring(0, index).trim();
}
String encodedMsg = URLEncoder.encode(msg, "UTF-8");
if (StringUtil.isEmpty(url, true)) {
String host = "localhost";
try {
host = InetAddress.getLocalHost().getHostAddress();
} catch (Throwable e2) {}
String port = "8080";
try {
MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
Set<ObjectName> objectNames = beanServer.queryNames(
new ObjectName("*:type=Connector,*"),
Query.match(Query.attr("protocol"), Query.value("HTTP/1.1"))
);
String p = objectNames.iterator().next().getKeyProperty("port");
port = StringUtil.isEmpty(p, true) ? port : p;
} catch (Throwable e2) {}
url = "http://" + host + ":" + port + "/" + (requestMethod == null ? RequestMethod.GET : requestMethod).name().toLowerCase();
}
String req = JSON.toJSONString(object);
try {
req = URLEncoder.encode(req, "UTF-8");
} catch (Throwable e2) {}
Throwable t = isCommon ? e.getCause() : e;
boolean isSQLException = t instanceof SQLException; // SQL 报错一般都是通用问题,优先搜索引擎
String apiatuoAndGitHubLink = "\n\n【APIAuto】: \n http://apijson.cn/api?type=JSON&url=" + URLEncoder.encode(url, "UTF-8") + "&json=" + req
+ " \n\n【GitHub】: \n https://www.google.com/search?q=site%3Agithub.com%2FTencent%2FAPIJSON+++" + encodedMsg;
msg += Log.KEY_SYSTEM_INFO_DIVIDER + " 浏览器打开以下链接查看解答"
+ (isSQLException ? "" : apiatuoAndGitHubLink)
// GitHub Issue 搜索貌似是精准包含,不易找到答案 + " \n\nGitHub: \n https://github.com/Tencent/APIJSON/issues?q=is%3Aissue+" + encodedMsg
+ " \n\n【Google】:\n https://www.google.com/search?q=" + encodedMsg
+ " \n\n【百度】:\n https://www.baidu.com/s?ie=UTF-8&wd=" + encodedMsg
+ (isSQLException ? apiatuoAndGitHubLink : "")
+ " \n\n都没找到答案?打开这个链接 \n https://github.com/Tencent/APIJSON/issues/new?assignees=&labels=&template=--bug.md "
+ " \n然后提交问题,推荐用以下模板修改,注意要换行保持清晰可读。"
+ " \n【标题】:" + msg
+ " \n【内容】:" + env + "\n\n**问题描述**\n" + msg
+ " \n\n<!-- 尽量完整截屏(至少包含请求和回包结果,还可以加上控制台报错日志),然后复制粘贴到这里 -->"
+ " \n\nPOST " + url
+ " \n发送请求 Request JSON:\n ```js"
+ " \n 请填写,例如 { \"Users\":{} }"
+ " \n```"
+ " \n\n返回结果 Response JSON:\n ```js"
+ " \n 请填写,例如 { \"Users\": {}, \"code\": 401, \"msg\": \"Users 不允许 UNKNOWN 用户的 GET 请求!\" }"
+ " \n```";
} catch (Throwable e2) {}
}
int code = CommonException.getCode(e);
return extendResult(object, code, msg, null, isRoot);
}
/**新建错误状态内容
* @param e
* @return
*/
public static JSONObject newErrorResult(Exception e) {
return newErrorResult(e, false);
}
/**新建错误状态内容
* @param e
* @param isRoot
* @return
*/
public static JSONObject newErrorResult(Exception e, boolean isRoot) {
if (e != null) {
// if (Log.DEBUG) {
e.printStackTrace();
// }
String msg = CommonException.getMsg(e);
Integer code = CommonException.getCode(e);
return newResult(code, msg, null, isRoot);
}
return newResult(JSONResponse.CODE_SERVER_ERROR, JSONResponse.MSG_SERVER_ERROR, null, isRoot);
}
/**获取正确的请求,非GET请求必须是服务器指定的
* @return
* @throws Exception
*/
@Override
public JSONObject parseCorrectRequest() throws Exception {
return parseCorrectRequest(requestMethod, tag, version, "", requestObject, getMaxUpdateCount(), this);
}
/**获取Request或Response内指定JSON结构
* @param table
* @param method
* @param tag
* @param version
* @return
* @throws Exception
*/
@Override
public JSONObject getStructure(@NotNull String table, String method, String tag, int version) throws Exception {
String cacheKey = AbstractVerifier.getCacheKeyForRequest(method, tag);
SortedMap<Integer, JSONObject> versionedMap = AbstractVerifier.REQUEST_MAP.get(cacheKey);
JSONObject result = versionedMap == null ? null : versionedMap.get(Integer.valueOf(version));
if (result == null) { // version <= 0 时使用最新,version > 0 时使用 > version 的最接近版本(最小版本)
Set<Entry<Integer, JSONObject>> set = versionedMap == null ? null : versionedMap.entrySet();
if (set != null && set.isEmpty() == false) {