1- /*Copyright ©2016 TommyLemon (https://github.com/TommyLemon /APIJSON)
1+ /*Copyright ©2016 APIJSON (https://github.com/APIJSON)
22
33Licensed under the Apache License, Version 2.0 (the "License");
44you may not use this file except in compliance with the License.
1818import apijson .JSONRequest ;
1919import apijson .orm .*;
2020
21+ import apijson .orm .exception .CommonException ;
2122import jakarta .servlet .http .HttpSession ;
2223
2324import java .rmi .ServerException ;
3839public class APIJSONController <T , M extends Map <String , Object >, L extends List <Object >> {
3940 public static final String TAG = "APIJSONController" ;
4041
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-
4742 public String getRequestURL () {
4843 return null ;
4944 }
5045
5146 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 ();
5448 parser .setMethod (method );
5549 parser .setSession (session );
5650 parser .setRequestURL (getRequestURL ());
5751 return parser ;
5852 }
5953
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+
60183 public String parse (RequestMethod method , String request , HttpSession session ) {
61184 return newParser (session , method ).parse (request );
62185 }
63186
64187 public String parseByTag (RequestMethod method , String tag , Map <String , String > params , String request , HttpSession session ) {
65188 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 );
67190 if (req == null ) {
68191 req = JSON .createJSONObject ();
69192 }
@@ -208,7 +331,7 @@ public String crudByTag(String method, String tag, Map<String, String> params, S
208331// * @see {@link RequestMethod#GET}
209332// */
210333// 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);
212335// }
213336
214337 /**获取
@@ -396,7 +519,7 @@ public String router(String method, String tag, Map<String, String> params, Stri
396519 }
397520
398521 @ 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 ;
400523 if (result == null && Log .DEBUG && APIJSONVerifier .DOCUMENT_MAP .isEmpty ()) {
401524
402525 //获取指定的JSON结构 <<<<<<<<<<<<<<
@@ -576,7 +699,7 @@ public M reload(String type) {
576699 * @param defaults
577700 * @return 返回类型设置为 Object 是为了子类重写时可以有返回值,避免因为冲突而另写一个有返回值的登录方法
578701 */
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 ) {
580703 //登录状态保存至session
581704 session .setAttribute (VISITOR_ID , visitor .getId ()); //用户id
582705 session .setAttribute (VISITOR_ , visitor ); //用户
@@ -599,7 +722,7 @@ public Object logout(@NotNull HttpSession session) {
599722
600723
601724
602- // public JSONObject listMethod(String request) {
725+ // public JSONMap listMethod(String request) {
603726// if (Log.DEBUG == false) {
604727// return APIJSONParser.newErrorResult(new IllegalAccessException("非 DEBUG 模式下不允许使用 UnitAuto 单元测试!"));
605728// }
@@ -610,10 +733,10 @@ public Object logout(@NotNull HttpSession session) {
610733// AsyncContext asyncContext = servletRequest.startAsync();
611734//
612735// final boolean[] called = new boolean[] { false };
613- // MethodUtil.Listener<JSONObject > listener = new MethodUtil.Listener<JSONObject >() {
736+ // MethodUtil.Listener<JSONMap > listener = new MethodUtil.Listener<JSONMap >() {
614737//
615738// @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 {
617740//
618741// ServletResponse servletResponse = called[0] ? null : asyncContext.getResponse();
619742// if (servletResponse == null) { // || servletResponse.isCommitted()) { // isCommitted 在高并发时可能不准,导致写入多次
@@ -646,7 +769,7 @@ public Object logout(@NotNull HttpSession session) {
646769// MethodUtil.invokeMethod(request, null, listener);
647770// }
648771// 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());
650773// try {
651774// listener.complete(MethodUtil.JSON_CALLBACK.newErrorResult(e));
652775// }
0 commit comments