Skip to content

Commit a0735fb

Browse files
committed
新增 ENABLE_REMOTE_FUNCTION, ENABLE_SCRIPT_FUNCTION, ENABLE_VERIFY_ROLE, ENABLE_VERIFY_CONTENT 等配置
1 parent 71278df commit a0735fb

File tree

4 files changed

+118
-94
lines changed

4 files changed

+118
-94
lines changed

src/main/java/apijson/framework/APIJSONApplication.java

Lines changed: 62 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -73,70 +73,68 @@ public static <T extends Object> void init(boolean shutdownWhenServerError, @Not
7373
APIJSONController.APIJSON_CREATOR = creator;
7474

7575

76-
System.out.println("\n\n\n开始初始化: Access 权限校验配置 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
77-
try {
78-
APIJSONVerifier.initAccess(shutdownWhenServerError, creator);
79-
}
80-
catch (Throwable e) {
81-
e.printStackTrace();
82-
if (shutdownWhenServerError) {
83-
onServerError("权Access 限校验配置 初始化失败!", shutdownWhenServerError);
84-
}
85-
}
86-
System.out.println("\n完成初始化: Access 权限校验配置 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
87-
88-
89-
90-
System.out.println("\n\n\n开始初始化: Function 远程函数配置 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
91-
try {
92-
APIJSONFunctionParser.init(shutdownWhenServerError, creator);
93-
}
94-
catch (Throwable e) {
95-
e.printStackTrace();
96-
if (shutdownWhenServerError) {
97-
onServerError("Function 远程函数配置 初始化失败!", shutdownWhenServerError);
98-
}
99-
}
100-
System.out.println("\n完成初始化: Function 远程函数配置 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
101-
102-
System.out.println("开始测试: Function 远程函数 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
103-
try {
104-
APIJSONFunctionParser.test();
105-
}
106-
catch (Throwable e) {
107-
e.printStackTrace();
108-
if (shutdownWhenServerError) {
109-
onServerError("Function 远程函数配置 测试失败!", shutdownWhenServerError);
110-
}
111-
}
112-
System.out.println("\n完成测试: Function 远程函数 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
113-
114-
115-
116-
System.out.println("\n\n\n开始初始化: Request 请求参数校验配置 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
117-
try {
118-
APIJSONVerifier.initRequest(shutdownWhenServerError, creator);
119-
}
120-
catch (Throwable e) {
121-
e.printStackTrace();
122-
if (shutdownWhenServerError) {
123-
onServerError("Request 请求参数校验配置 初始化失败!", shutdownWhenServerError);
124-
}
125-
}
126-
System.out.println("\n完成初始化: Request 请求参数校验校验配置 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
127-
128-
System.out.println("\n\n\n开始测试: Request 请求参数校验 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
129-
try {
130-
APIJSONVerifier.testStructure();
131-
}
132-
catch (Throwable e) {
133-
e.printStackTrace();
134-
if (shutdownWhenServerError) {
135-
onServerError("Request 请求参数校验 测试失败!", shutdownWhenServerError);
136-
}
137-
}
138-
System.out.println("\n完成测试: Request 请求参数校验 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
139-
76+
if (APIJSONVerifier.ENABLE_VERIFY_ROLE) {
77+
System.out.println("\n\n\n开始初始化: Access 权限校验配置 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
78+
try {
79+
APIJSONVerifier.initAccess(shutdownWhenServerError, creator);
80+
} catch (Throwable e) {
81+
e.printStackTrace();
82+
if (shutdownWhenServerError) {
83+
onServerError("权Access 限校验配置 初始化失败!", shutdownWhenServerError);
84+
}
85+
}
86+
System.out.println("\n完成初始化: Access 权限校验配置 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
87+
}
88+
89+
90+
if (APIJSONFunctionParser.ENABLE_REMOTE_FUNCTION) {
91+
System.out.println("\n\n\n开始初始化: Function 远程函数配置 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
92+
try {
93+
APIJSONFunctionParser.init(shutdownWhenServerError, creator);
94+
} catch (Throwable e) {
95+
e.printStackTrace();
96+
if (shutdownWhenServerError) {
97+
onServerError("Function 远程函数配置 初始化失败!", shutdownWhenServerError);
98+
}
99+
}
100+
System.out.println("\n完成初始化: Function 远程函数配置 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
101+
102+
System.out.println("开始测试: Function 远程函数 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
103+
try {
104+
APIJSONFunctionParser.test();
105+
} catch (Throwable e) {
106+
e.printStackTrace();
107+
if (shutdownWhenServerError) {
108+
onServerError("Function 远程函数配置 测试失败!", shutdownWhenServerError);
109+
}
110+
}
111+
System.out.println("\n完成测试: Function 远程函数 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
112+
}
113+
114+
115+
if (APIJSONVerifier.ENABLE_VERIFY_CONTENT) {
116+
System.out.println("\n\n\n开始初始化: Request 请求参数校验配置 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
117+
try {
118+
APIJSONVerifier.initRequest(shutdownWhenServerError, creator);
119+
} catch (Throwable e) {
120+
e.printStackTrace();
121+
if (shutdownWhenServerError) {
122+
onServerError("Request 请求参数校验配置 初始化失败!", shutdownWhenServerError);
123+
}
124+
}
125+
System.out.println("\n完成初始化: Request 请求参数校验校验配置 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
126+
127+
System.out.println("\n\n\n开始测试: Request 请求参数校验 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
128+
try {
129+
APIJSONVerifier.testStructure();
130+
} catch (Throwable e) {
131+
e.printStackTrace();
132+
if (shutdownWhenServerError) {
133+
onServerError("Request 请求参数校验 测试失败!", shutdownWhenServerError);
134+
}
135+
}
136+
System.out.println("\n完成测试: Request 请求参数校验 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
137+
}
140138

141139

142140

src/main/java/apijson/framework/APIJSONController.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,16 +323,30 @@ public JSONObject reload(String type) {
323323

324324
if (reloadAll || "ACCESS".equals(type)) {
325325
try {
326-
result.put(ACCESS_, APIJSONVerifier.initAccess());
327-
} catch (ServerException e) {
326+
if (reloadAll == false && APIJSONVerifier.ENABLE_VERIFY_ROLE == false) {
327+
throw new UnsupportedOperationException("AbstractVerifier.ENABLE_VERIFY_ROLE == false 时不支持校验角色权限!" +
328+
"如需支持则设置 AbstractVerifier.ENABLE_VERIFY_ROLE = true !");
329+
}
330+
331+
if (APIJSONVerifier.ENABLE_VERIFY_ROLE) {
332+
result.put(ACCESS_, APIJSONVerifier.initAccess());
333+
}
334+
} catch (ServerException e) {
328335
e.printStackTrace();
329336
result.put(ACCESS_, APIJSONParser.newErrorResult(e));
330337
}
331338
}
332339

333340
if (reloadAll || "FUNCTION".equals(type)) {
334341
try {
335-
result.put(FUNCTION_, APIJSONFunctionParser.init());
342+
if (reloadAll == false && APIJSONFunctionParser.ENABLE_REMOTE_FUNCTION == false) {
343+
throw new UnsupportedOperationException("AbstractFunctionParser.ENABLE_REMOTE_FUNCTION" +
344+
" == false 时不支持远程函数!如需支持则设置 AbstractFunctionParser.ENABLE_REMOTE_FUNCTION = true !");
345+
}
346+
347+
if (APIJSONFunctionParser.ENABLE_REMOTE_FUNCTION) {
348+
result.put(FUNCTION_, APIJSONFunctionParser.init());
349+
}
336350
} catch (ServerException e) {
337351
e.printStackTrace();
338352
result.put(FUNCTION_, APIJSONParser.newErrorResult(e));
@@ -341,7 +355,14 @@ public JSONObject reload(String type) {
341355

342356
if (reloadAll || "REQUEST".equals(type)) {
343357
try {
344-
result.put(REQUEST_, APIJSONVerifier.initRequest());
358+
if (reloadAll == false && APIJSONVerifier.ENABLE_VERIFY_CONTENT == false) {
359+
throw new UnsupportedOperationException("AbstractVerifier.ENABLE_VERIFY_CONTENT == false 时不支持校验请求传参内容!" +
360+
"如需支持则设置 AbstractVerifier.ENABLE_VERIFY_CONTENT = true !");
361+
}
362+
363+
if (APIJSONVerifier.ENABLE_VERIFY_CONTENT) {
364+
result.put(REQUEST_, APIJSONVerifier.initRequest());
365+
}
345366
} catch (ServerException e) {
346367
e.printStackTrace();
347368
result.put(REQUEST_, APIJSONParser.newErrorResult(e));

src/main/java/apijson/framework/APIJSONFunctionParser.java

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -150,35 +150,36 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
150150
//if (Log.DEBUG == false) {
151151
// function.put(APIJSONConstant.KEY_DEBUG, 0);
152152
//}
153-
//
153+
//
154154
//JSONRequest functionItem = new JSONRequest();
155155
//functionItem.put(FUNCTION_, function);
156156
//
157-
//JSONObject script = isAll ? new JSONRequest() : table;
158-
////if (Log.DEBUG == false) {
159-
//// script.put(APIJSONConstant.KEY_DEBUG, 0);
160-
////}
157+
//JSONObject script = new JSONRequest(); // isAll ? new JSONRequest() : table;
158+
//script.put("simple", 0);
159+
//if (Log.DEBUG == false) {
160+
// script.put(APIJSONConstant.KEY_DEBUG, 0);
161+
//}
162+
// 不能用这个来优化,因为可能配置了不校验远程函数是否存在
161163
//{ // name{}@ <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
162-
// JSONRequest nameInAt = new JSONRequest();
163-
// nameInAt.put("from", "Function");
164-
// { // Function <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
165-
// JSONRequest fun = new JSONRequest();
166-
// fun.setColumn("name");
167-
// nameInAt.put("Function", fun);
168-
// } // Function >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
169-
//
170-
// script.put("simple", 0);
171-
// script.put("name{}@", nameInAt);
164+
//JSONRequest nameInAt = new JSONRequest();
165+
//nameInAt.put("from", "Function");
166+
//{ // Function <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
167+
// JSONRequest fun = new JSONRequest();
168+
// fun.setColumn("name");
169+
// nameInAt.put("Function", fun);
170+
//} // Function >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
171+
172+
//script.put("name{}@", nameInAt);
172173
//} // name{}@ >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
173-
//
174+
174175
//JSONRequest scriptItem = new JSONRequest();
175176
//scriptItem.put(SCRIPT_, script);
176-
//
177+
177178
JSONObject request = new JSONObject();
178179
//request.putAll(functionItem.toArray(0, 0, FUNCTION_));
179180
//request.putAll(scriptItem.toArray(0, 0, SCRIPT_));
180181

181-
// TODO 用这个来优化
182+
// 可以用它,因为 Function 表必须存在,没有绕过校验的配置 // 不能用这个来优化,因为可能配置了不校验远程函数是否存在
182183
{ // [] <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
183184
JSONRequest item = new JSONRequest();
184185

@@ -187,14 +188,14 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
187188
if (Log.DEBUG == false) {
188189
function.put(APIJSONConstant.KEY_DEBUG, 0);
189190
}
190-
item.put("Function", function);
191+
item.put(FUNCTION_, function);
191192
} // Function >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
192193

193-
{ // Script <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
194+
if (ENABLE_SCRIPT_FUNCTION) { // Script <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
194195
JSONRequest script = new JSONRequest();
195196
script.put("name@", "/Function/name");
196197
script.put("simple", 0);
197-
item.put("Script", script);
198+
item.put(SCRIPT_, script);
198199
} // Script >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
199200

200201
request.putAll(item.toArray(0, 0));
@@ -206,7 +207,7 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
206207
onServerError("\n\n\n\n\n !!!! 查询远程函数异常 !!!\n" + response.getString(JSONResponse.KEY_MSG) + "\n\n\n\n\n", shutdownWhenServerError);
207208
}
208209

209-
JSONArray scriptList = response.getJSONArray("[]"); // .getJSONArray(SCRIPT_ + "[]");
210+
JSONArray scriptList = response.getJSONArray(SCRIPT_ + "[]");
210211
if (scriptList != null && scriptList.isEmpty() == false) {
211212
if (isAll) {
212213
SCRIPT_MAP = new LinkedHashMap<>();
@@ -215,7 +216,7 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
215216
for (int i = 0; i < scriptList.size(); i++) {
216217
JSONObject item = scriptList.getJSONObject(i);
217218
item = item == null ? null : item.getJSONObject(SCRIPT_);
218-
if (item == null) {
219+
if (item == null) { // 关联查不到很正常
219220
continue;
220221
}
221222

@@ -233,7 +234,7 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
233234
}
234235
}
235236

236-
JSONArray list = scriptList; // response.getJSONArray(FUNCTION_ + "[]");
237+
JSONArray list = response.getJSONArray(FUNCTION_ + "[]");
237238
int size = list == null ? 0 : list.size();
238239
if (isAll && size <= 0) {
239240
Log.w(TAG, "init isAll && size <= 0,,没有可用的远程函数");

src/main/java/apijson/framework/APIJSONVerifier.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,12 @@ public static <T> JSONObject init(APIJSONCreator<T> creator) throws ServerExcept
9797
*/
9898
public static <T> JSONObject init(boolean shutdownWhenServerError, APIJSONCreator<T> creator) throws ServerException {
9999
JSONObject result = new JSONObject(true);
100-
result.put(ACCESS_, initAccess(shutdownWhenServerError, creator));
101-
result.put(REQUEST_, initRequest(shutdownWhenServerError, creator));
100+
if (ENABLE_VERIFY_ROLE) {
101+
result.put(ACCESS_, initAccess(shutdownWhenServerError, creator));
102+
}
103+
if (ENABLE_VERIFY_CONTENT) {
104+
result.put(REQUEST_, initRequest(shutdownWhenServerError, creator));
105+
}
102106
return result;
103107
}
104108

0 commit comments

Comments
 (0)