1
- /*Copyright ©2016 TommyLemon (https://github.com/TommyLemon /APIJSON)
1
+ /*Copyright ©2016 APIJSON (https://github.com/APIJSON)
2
2
3
3
Licensed under the Apache License, Version 2.0 (the "License");
4
4
you may not use this file except in compliance with the License.
18
18
import apijson .JSONRequest ;
19
19
import apijson .orm .*;
20
20
21
+ import apijson .orm .exception .CommonException ;
21
22
import jakarta .servlet .http .HttpSession ;
22
23
23
24
import java .rmi .ServerException ;
38
39
public class APIJSONController <T , M extends Map <String , Object >, L extends List <Object >> {
39
40
public static final String TAG = "APIJSONController" ;
40
41
41
- @ NotNull
42
- public static APIJSONCreator <?, ? extends Map <String , Object >, ? extends List <Object >> APIJSON_CREATOR ;
43
- static {
44
- APIJSON_CREATOR = new APIJSONCreator <Object , JSONObject , JSONArray >();
45
- }
46
-
47
42
public String getRequestURL () {
48
43
return null ;
49
44
}
50
45
51
46
public APIJSONParser <T , M , L > newParser (HttpSession session , RequestMethod method ) {
52
- @ SuppressWarnings ("unchecked" )
53
- APIJSONParser <T , M , L > parser = (APIJSONParser <T , M , L >) APIJSON_CREATOR .createParser ();
47
+ APIJSONParser <T , M , L > parser = APIJSONApplication .createParser ();
54
48
parser .setMethod (method );
55
49
parser .setSession (session );
56
50
parser .setRequestURL (getRequestURL ());
57
51
return parser ;
58
52
}
59
53
54
+ public static APIJSONParser <?, ? extends Map <String , Object >, ? extends List <Object >> ERR_PARSER = APIJSONApplication .createParser ();
55
+
56
+ /**新建带状态内容的JSONObject
57
+ * @param code
58
+ * @param msg
59
+ * @return
60
+ */
61
+ public static <M extends Map <String , Object >> M newResult (int code , String msg ) {
62
+ return newResult (code , msg , null );
63
+ }
64
+
65
+ /**
66
+ * 添加JSONObject的状态内容,一般用于错误提示结果
67
+ *
68
+ * @param code
69
+ * @param msg
70
+ * @param warn
71
+ * @return
72
+ */
73
+ public static <M extends Map <String , Object >> M newResult (int code , String msg , String warn ) {
74
+ return newResult (code , msg , warn , false );
75
+ }
76
+
77
+ /**
78
+ * 新建带状态内容的JSONObject
79
+ *
80
+ * @param code
81
+ * @param msg
82
+ * @param warn
83
+ * @param isRoot
84
+ * @return
85
+ */
86
+ public static <M extends Map <String , Object >> M newResult (int code , String msg , String warn , boolean isRoot ) {
87
+ return extendResult (null , code , msg , warn , isRoot );
88
+ }
89
+
90
+ /**
91
+ * 添加JSONObject的状态内容,一般用于错误提示结果
92
+ *
93
+ * @param object
94
+ * @param code
95
+ * @param msg
96
+ * @return
97
+ */
98
+ public static <M extends Map <String , Object >> M extendResult (M object , int code , String msg , String warn , boolean isRoot ) {
99
+ return (M ) ERR_PARSER .extendResult (JSON .createJSONObject (object ), code , msg , warn , isRoot );
100
+ }
101
+
102
+
103
+ /**
104
+ * 添加请求成功的状态内容
105
+ *
106
+ * @param object
107
+ * @return
108
+ */
109
+ public M extendSuccessResult (M object ) {
110
+ return extendSuccessResult (object , false );
111
+ }
112
+
113
+ public M extendSuccessResult (M object , boolean isRoot ) {
114
+ return extendSuccessResult (object , null , isRoot );
115
+ }
116
+
117
+ /**添加请求成功的状态内容
118
+ * @param object
119
+ * @param isRoot
120
+ * @return
121
+ */
122
+ public static <M extends Map <String , Object >> M extendSuccessResult (M object , String warn , boolean isRoot ) {
123
+ return extendResult (object , JSONResponse .CODE_SUCCESS , JSONResponse .MSG_SUCCEED , warn , isRoot );
124
+ }
125
+
126
+ /**获取请求成功的状态内容
127
+ * @return
128
+ */
129
+ public static <M extends Map <String , Object >> M newSuccessResult () {
130
+ return newSuccessResult (null );
131
+ }
132
+
133
+ /**获取请求成功的状态内容
134
+ * @param warn
135
+ * @return
136
+ */
137
+ public static <M extends Map <String , Object >> M newSuccessResult (String warn ) {
138
+ return newSuccessResult (warn , false );
139
+ }
140
+
141
+ /**获取请求成功的状态内容
142
+ * @param warn
143
+ * @param isRoot
144
+ * @return
145
+ */
146
+ public static <M extends Map <String , Object >> M newSuccessResult (String warn , boolean isRoot ) {
147
+ return newResult (JSONResponse .CODE_SUCCESS , JSONResponse .MSG_SUCCEED , warn , isRoot );
148
+ }
149
+
150
+ /**添加请求成功的状态内容
151
+ * @param object
152
+ * @param e
153
+ * @return
154
+ */
155
+ public static <M extends Map <String , Object >> M extendErrorResult (M object , Throwable e ) {
156
+ return extendErrorResult (object , e , false );
157
+ }
158
+ /**添加请求成功的状态内容
159
+ * @param object
160
+ * @param e
161
+ * @param isRoot
162
+ * @return
163
+ */
164
+ public static <M extends Map <String , Object >> M extendErrorResult (M object , Throwable e , boolean isRoot ) {
165
+ return extendErrorResult (object , e , null , null , isRoot );
166
+ }
167
+ /**添加请求成功的状态内容
168
+ * @param object
169
+ * @return
170
+ */
171
+ public static <M extends Map <String , Object >> M extendErrorResult (M object , Throwable e , RequestMethod requestMethod , String url , boolean isRoot ) {
172
+ return (M ) ERR_PARSER .extendErrorResult (JSON .createJSONObject (object ), e , requestMethod , url , isRoot );
173
+ }
174
+
175
+ public static <M extends Map <String , Object >> M newErrorResult (Exception e ) {
176
+ return newErrorResult (e , false );
177
+ }
178
+ public static <M extends Map <String , Object >> M newErrorResult (Exception e , boolean isRoot ) {
179
+ return (M ) ERR_PARSER .newErrorResult (e , isRoot );
180
+ }
181
+
182
+
60
183
public String parse (RequestMethod method , String request , HttpSession session ) {
61
184
return newParser (session , method ).parse (request );
62
185
}
63
186
64
187
public String parseByTag (RequestMethod method , String tag , Map <String , String > params , String request , HttpSession session ) {
65
188
APIJSONParser <T , M , L > parser = newParser (null , null );
66
- M req = parser .wrapRequest (method , tag , JSON .parseObject (request ), false , ( JSONCreator < M , L >) APIJSON_CREATOR );
189
+ M req = parser .wrapRequest (method , tag , JSON .parseObject (request ), false );
67
190
if (req == null ) {
68
191
req = JSON .createJSONObject ();
69
192
}
@@ -208,7 +331,7 @@ public String crudByTag(String method, String tag, Map<String, String> params, S
208
331
// * @see {@link RequestMethod#GET}
209
332
// */
210
333
// public String listByTag(String tag, String request, HttpSession session) {
211
- // return parseByTag(GET, tag + apijson.JSONObject .KEY_ARRAY, request, session);
334
+ // return parseByTag(GET, tag + apijson.JSONMap .KEY_ARRAY, request, session);
212
335
// }
213
336
214
337
/**获取
@@ -396,7 +519,7 @@ public String router(String method, String tag, Map<String, String> params, Stri
396
519
}
397
520
398
521
@ SuppressWarnings ("unchecked" )
399
- APIJSONCreator <T , M , L > creator = (APIJSONCreator <T , M , L >) APIJSONParser . APIJSON_CREATOR ;
522
+ APIJSONCreator <T , M , L > creator = (APIJSONCreator <T , M , L >) APIJSONApplication . DEFAULT_APIJSON_CREATOR ;
400
523
if (result == null && Log .DEBUG && APIJSONVerifier .DOCUMENT_MAP .isEmpty ()) {
401
524
402
525
//获取指定的JSON结构 <<<<<<<<<<<<<<
@@ -576,7 +699,7 @@ public M reload(String type) {
576
699
* @param defaults
577
700
* @return 返回类型设置为 Object 是为了子类重写时可以有返回值,避免因为冲突而另写一个有返回值的登录方法
578
701
*/
579
- public Object login (@ NotNull HttpSession session , Visitor <Long > visitor , Integer version , Boolean format , M defaults ) {
702
+ public Object login (@ NotNull HttpSession session , @ NotNull Visitor <Long > visitor , Integer version , Boolean format , M defaults ) {
580
703
//登录状态保存至session
581
704
session .setAttribute (VISITOR_ID , visitor .getId ()); //用户id
582
705
session .setAttribute (VISITOR_ , visitor ); //用户
@@ -599,7 +722,7 @@ public Object logout(@NotNull HttpSession session) {
599
722
600
723
601
724
602
- // public JSONObject listMethod(String request) {
725
+ // public JSONMap listMethod(String request) {
603
726
// if (Log.DEBUG == false) {
604
727
// return APIJSONParser.newErrorResult(new IllegalAccessException("非 DEBUG 模式下不允许使用 UnitAuto 单元测试!"));
605
728
// }
@@ -610,10 +733,10 @@ public Object logout(@NotNull HttpSession session) {
610
733
// AsyncContext asyncContext = servletRequest.startAsync();
611
734
//
612
735
// final boolean[] called = new boolean[] { false };
613
- // MethodUtil.Listener<JSONObject > listener = new MethodUtil.Listener<JSONObject >() {
736
+ // MethodUtil.Listener<JSONMap > listener = new MethodUtil.Listener<JSONMap >() {
614
737
//
615
738
// @Override
616
- // public void complete(JSONObject data, Method method, InterfaceProxy proxy, Object... extras) throws Exception {
739
+ // public void complete(JSONMap data, Method method, InterfaceProxy proxy, Object... extras) throws Exception {
617
740
//
618
741
// ServletResponse servletResponse = called[0] ? null : asyncContext.getResponse();
619
742
// if (servletResponse == null) { // || servletResponse.isCommitted()) { // isCommitted 在高并发时可能不准,导致写入多次
@@ -646,7 +769,7 @@ public Object logout(@NotNull HttpSession session) {
646
769
// MethodUtil.invokeMethod(request, null, listener);
647
770
// }
648
771
// catch (Exception e) {
649
- // Log.e(TAG, "invokeMethod try { JSONObject req = JSON.parseObject(request); ... } catch (Exception e) { \n" + e.getMessage());
772
+ // Log.e(TAG, "invokeMethod try { JSONMap req = JSON.parseObject(request); ... } catch (Exception e) { \n" + e.getMessage());
650
773
// try {
651
774
// listener.complete(MethodUtil.JSON_CALLBACK.newErrorResult(e));
652
775
// }
0 commit comments