-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathAPIJSONController.java
executable file
·450 lines (387 loc) · 15 KB
/
APIJSONController.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
/*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.framework;
import apijson.*;
import apijson.orm.AbstractParser;
import apijson.orm.Parser;
import apijson.orm.Visitor;
import jakarta.servlet.http.HttpSession;
import java.rmi.ServerException;
import java.util.List;
import java.util.Map;
import static apijson.JSON.toJSONString;
import static apijson.RequestMethod.*;
import static apijson.framework.APIJSONConstant.*;
/**APIJSON base controller,建议在子项目被 @RestController 注解的类继承它或通过它的实例调用相关方法
* <br > 全通过 HTTP POST 来请求:
* <br > 1.减少代码 - 客户端无需写 HTTP GET, HTTP PUT 等各种方式的请求代码
* <br > 2.提高性能 - 无需 URL encode 和 decode
* <br > 3.调试方便 - 建议使用 APIAuto-机器学习自动化接口管理工具(https://github.com/TommyLemon/APIAuto)
* @author Lemon
*/
public class APIJSONController<T, M extends Map<String, Object>, L extends List<Object>> {
public static final String TAG = "APIJSONController";
@NotNull
public static APIJSONCreator<?, ? extends Map<String, Object>, ? extends List<Object>> APIJSON_CREATOR;
static {
APIJSON_CREATOR = new APIJSONCreator<Object, JSONObject, JSONArray>();
}
public String getRequestURL() {
return null;
}
public APIJSONParser<T, M, L> newParser(HttpSession session, RequestMethod method) {
@SuppressWarnings("unchecked")
APIJSONParser<T, M, L> parser = (APIJSONParser<T, M, L>) APIJSON_CREATOR.createParser();
parser.setMethod(method);
parser.setSession(session);
parser.setRequestURL(getRequestURL());
return parser;
}
public String parse(RequestMethod method, String request, HttpSession session) {
return newParser(session, method).parse(request);
}
public String parseByTag(RequestMethod method, String tag, Map<String, String> params, String request, HttpSession session) {
APIJSONParser<T, M, L> parser = newParser(null, null);
M req = parser.wrapRequest(method, tag, JSON.parseObject(request), false, (JSONCreator<M, L>) APIJSON_CREATOR);
if (req == null) {
req = JSON.createJSONObject();
}
if (params != null && params.isEmpty() == false) {
req.putAll(params);
}
return newParser(session, method).parse(req);
}
//通用接口,非事务型操作 和 简单事务型操作 都可通过这些接口自动化实现<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/**增删改查统一入口,这个一个方法可替代以下 7 个方法,牺牲一点路由解析性能来提升一些开发效率
* @param method
* @param request
* @param session
* @return
*/
public String crud(String method, String request, HttpSession session) {
if (METHODS.contains(method)) {
return parse(RequestMethod.valueOf(method.toUpperCase()), request, session);
}
Parser<T, M, L> parser = newParser(null, null);
return toJSONString(parser.newErrorResult(
new IllegalArgumentException("URL 路径 /{method} 中 method 值 "
+ method + " 错误!只允许 " + METHODS + " 中的一个!")
));
}
/**获取
* @param request 只用String,避免encode后未decode
* @param session
* @return
* @see {@link RequestMethod#GET}
*/
public String get(String request, HttpSession session) {
return parse(GET, request, session);
}
/**计数
* @param request 只用String,避免encode后未decode
* @param session
* @return
* @see {@link RequestMethod#HEAD}
*/
public String head(String request, HttpSession session) {
return parse(HEAD, request, session);
}
/**限制性GET,request和response都非明文,浏览器看不到,用于对安全性要求高的GET请求
* @param request 只用String,避免encode后未decode
* @param session
* @return
* @see {@link RequestMethod#GETS}
*/
public String gets(String request, HttpSession session) {
return parse(GETS, request, session);
}
/**限制性HEAD,request和response都非明文,浏览器看不到,用于对安全性要求高的HEAD请求
* @param request 只用String,避免encode后未decode
* @param session
* @return
* @see {@link RequestMethod#HEADS}
*/
public String heads(String request, HttpSession session) {
return parse(HEADS, request, session);
}
/**新增
* @param request 只用String,避免encode后未decode
* @param session
* @return
* @see {@link RequestMethod#POST}
*/
public String post(String request, HttpSession session) {
return parse(POST, request, session);
}
/**修改
* @param request 只用String,避免encode后未decode
* @param session
* @return
* @see {@link RequestMethod#PUT}
*/
public String put(String request, HttpSession session) {
return parse(PUT, request, session);
}
/**删除
* @param request 只用String,避免encode后未decode
* @param session
* @return
* @see {@link RequestMethod#DELETE}
*/
public String delete(String request, HttpSession session) {
return parse(DELETE, request, session);
}
/**支持全局事物、批量执行多条语句
* @param request 只用String,避免encode后未decode
* @param session
* @return
* @see {@link RequestMethod#GET}
*/
public String crud(String request, HttpSession session) {
return parse(CRUD, request, session);
}
//通用接口,非事务型操作 和 简单事务型操作 都可通过这些接口自动化实现>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//通用接口,非事务型操作 和 简单事务型操作 都可通过这些接口自动化实现<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/**增删改查统一入口,这个一个方法可替代以下 7 个方法,牺牲一些路由解析性能来提升一点开发效率
* @param method
* @param tag
* @param params
* @param request
* @param session
* @return
*/
public String crudByTag(String method, String tag, Map<String, String> params, String request, HttpSession session) {
if (METHODS.contains(method)) {
return parseByTag(RequestMethod.valueOf(method.toUpperCase()), tag, params, request, session);
}
Parser<T, M, L> parser = newParser(null, null);
return toJSONString(parser.newErrorResult(
new IllegalArgumentException("URL 路径 /{method}/{tag} 中 method 值 "
+ method + " 错误!只允许 " + METHODS + " 中的一个!")
));
}
// /**获取列表
// * @param request 只用String,避免encode后未decode
// * @param session
// * @return
// * @see {@link RequestMethod#GET}
// */
// public String listByTag(String tag, String request, HttpSession session) {
// return parseByTag(GET, tag + apijson.JSONObject.KEY_ARRAY, request, session);
// }
/**获取
* @param request 只用String,避免encode后未decode
* @param session
* @return
* @see {@link RequestMethod#GET}
*/
public String getByTag(String tag, Map<String, String> params, String request, HttpSession session) {
return parseByTag(GET, tag, params, request, session);
}
/**计数
* @param request 只用String,避免encode后未decode
* @param session
* @return
* @see {@link RequestMethod#HEAD}
*/
public String headByTag(String tag, Map<String, String> params, String request, HttpSession session) {
return parseByTag(HEAD, tag, params, request, session);
}
/**限制性GET,request和response都非明文,浏览器看不到,用于对安全性要求高的GET请求
* @param request 只用String,避免encode后未decode
* @param session
* @return
* @see {@link RequestMethod#GETS}
*/
public String getsByTag(String tag, Map<String, String> params, String request, HttpSession session) {
return parseByTag(GETS, tag, params, request, session);
}
/**限制性HEAD,request和response都非明文,浏览器看不到,用于对安全性要求高的HEAD请求
* @param request 只用String,避免encode后未decode
* @param session
* @return
* @see {@link RequestMethod#HEADS}
*/
public String headsByTag(String tag, Map<String, String> params, String request, HttpSession session) {
return parseByTag(HEADS, tag, params, request, session);
}
/**新增
* @param request 只用String,避免encode后未decode
* @param session
* @return
* @see {@link RequestMethod#POST}
*/
public String postByTag(String tag, Map<String, String> params, String request, HttpSession session) {
return parseByTag(POST, tag, params, request, session);
}
/**修改
* @param request 只用String,避免encode后未decode
* @param session
* @return
* @see {@link RequestMethod#PUT}
*/
public String putByTag(String tag, Map<String, String> params, String request, HttpSession session) {
return parseByTag(PUT, tag, params, request, session);
}
/**删除
* @param request 只用String,避免encode后未decode
* @param session
* @return
* @see {@link RequestMethod#DELETE}
*/
public String deleteByTag(String tag, Map<String, String> params, String request, HttpSession session) {
return parseByTag(DELETE, tag, params, request, session);
}
//通用接口,非事务型操作 和 简单事务型操作 都可通过这些接口自动化实现>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
/**重新加载配置
* @return
* @see
* <pre>
{
"type": "ALL", //重载对象,ALL, FUNCTION, REQUEST, ACCESS,非必须
"phone": "13000082001",
"verify": "1234567" //验证码,对应类型为 Verify.TYPE_RELOAD
}
* </pre>
*/
public M reload(String type) {
Parser<T, M, L> parser = newParser(null, null);
M result = parser.newSuccessResult();
boolean reloadAll = StringUtil.isEmpty(type, true) || "ALL".equals(type);
if (reloadAll || "ACCESS".equals(type)) {
try {
if (reloadAll == false && APIJSONVerifier.ENABLE_VERIFY_ROLE == false) {
throw new UnsupportedOperationException("AbstractVerifier.ENABLE_VERIFY_ROLE == false 时不支持校验角色权限!" +
"如需支持则设置 AbstractVerifier.ENABLE_VERIFY_ROLE = true !");
}
if (APIJSONVerifier.ENABLE_VERIFY_ROLE) {
result.put(ACCESS_, APIJSONVerifier.initAccess());
}
} catch (ServerException e) {
e.printStackTrace();
result.put(ACCESS_, parser.newErrorResult(e));
}
}
if (reloadAll || "FUNCTION".equals(type)) {
try {
if (reloadAll == false && APIJSONFunctionParser.ENABLE_REMOTE_FUNCTION == false) {
throw new UnsupportedOperationException("AbstractFunctionParser.ENABLE_REMOTE_FUNCTION" +
" == false 时不支持远程函数!如需支持则设置 AbstractFunctionParser.ENABLE_REMOTE_FUNCTION = true !");
}
if (APIJSONFunctionParser.ENABLE_REMOTE_FUNCTION) {
result.put(FUNCTION_, APIJSONFunctionParser.init());
}
} catch (ServerException e) {
e.printStackTrace();
result.put(FUNCTION_, parser.newErrorResult(e));
}
}
if (reloadAll || "REQUEST".equals(type)) {
try {
if (reloadAll == false && APIJSONVerifier.ENABLE_VERIFY_CONTENT == false) {
throw new UnsupportedOperationException("AbstractVerifier.ENABLE_VERIFY_CONTENT == false 时不支持校验请求传参内容!" +
"如需支持则设置 AbstractVerifier.ENABLE_VERIFY_CONTENT = true !");
}
if (APIJSONVerifier.ENABLE_VERIFY_CONTENT) {
result.put(REQUEST_, APIJSONVerifier.initRequest());
}
} catch (ServerException e) {
e.printStackTrace();
result.put(REQUEST_, parser.newErrorResult(e));
}
}
return result;
}
/**用户登录
* @param session
* @param visitor
* @param version
* @param format
* @param defaults
* @return 返回类型设置为 Object 是为了子类重写时可以有返回值,避免因为冲突而另写一个有返回值的登录方法
*/
public Object login(@NotNull HttpSession session, Visitor<Long> visitor, Integer version, Boolean format, JSONObject defaults) {
//登录状态保存至session
session.setAttribute(VISITOR_ID, visitor.getId()); //用户id
session.setAttribute(VISITOR_, visitor); //用户
session.setAttribute(VERSION, version); //全局默认版本号
session.setAttribute(FORMAT, format); //全局默认格式化配置
session.setAttribute(DEFAULTS, defaults); //给每个请求JSON最外层加的字段
return null;
}
/**退出登录,清空session
* @param session
* @return 返回类型设置为 Object 是为了子类重写时可以有返回值,避免因为冲突而另写一个有返回值的登录方法
*/
public Object logout(@NotNull HttpSession session) {
Object userId = APIJSONVerifier.getVisitorId(session);//必须在session.invalidate();前!
Log.d(TAG, "logout userId = " + userId + "; session.getId() = " + (session == null ? null : session.getId()));
session.invalidate();
return null;
}
// public JSONObject listMethod(String request) {
// if (Log.DEBUG == false) {
// return APIJSONParser.newErrorResult(new IllegalAccessException("非 DEBUG 模式下不允许使用 UnitAuto 单元测试!"));
// }
// return MethodUtil.listMethod(request);
// }
//
// public void invokeMethod(String request, HttpServletRequest servletRequest) {
// AsyncContext asyncContext = servletRequest.startAsync();
//
// final boolean[] called = new boolean[] { false };
// MethodUtil.Listener<JSONObject> listener = new MethodUtil.Listener<JSONObject>() {
//
// @Override
// public void complete(JSONObject data, Method method, InterfaceProxy proxy, Object... extras) throws Exception {
//
// ServletResponse servletResponse = called[0] ? null : asyncContext.getResponse();
// if (servletResponse == null) { // || servletResponse.isCommitted()) { // isCommitted 在高并发时可能不准,导致写入多次
// Log.w(TAG, "invokeMethod listener.complete servletResponse == null || servletResponse.isCommitted() >> return;");
// return;
// }
// called[0] = true;
//
// servletResponse.setCharacterEncoding(servletRequest.getCharacterEncoding());
// servletResponse.setContentType(servletRequest.getContentType());
// servletResponse.getWriter().println(data);
// asyncContext.complete();
// }
// };
//
// if (Log.DEBUG == false) {
// try {
// listener.complete(MethodUtil.JSON_CALLBACK.newErrorResult(new IllegalAccessException("非 DEBUG 模式下不允许使用 UnitAuto 单元测试!")));
// }
// catch (Exception e1) {
// e1.printStackTrace();
// asyncContext.complete();
// }
//
// return;
// }
//
//
// try {
// MethodUtil.invokeMethod(request, null, listener);
// }
// catch (Exception e) {
// Log.e(TAG, "invokeMethod try { JSONObject req = JSON.parseObject(request); ... } catch (Exception e) { \n" + e.getMessage());
// try {
// listener.complete(MethodUtil.JSON_CALLBACK.newErrorResult(e));
// }
// catch (Exception e1) {
// e1.printStackTrace();
// asyncContext.complete();
// }
// }
// }
}