Skip to content

Commit f400f09

Browse files
committed
优化通过脚本语言校验参数的代码
1 parent b601195 commit f400f09

File tree

1 file changed

+22
-31
lines changed

1 file changed

+22
-31
lines changed

APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,39 +1236,41 @@ public static <T extends Object> JSONObject parse(@NotNull final RequestMethod m
12361236
if (ifIsStr) {
12371237
String ifStr = (String) _if;
12381238
int ind = ifStr.indexOf(":");
1239-
String lang = ind < 0 ? null : ifStr.substring(0, ind);
1240-
ScriptEngine engine = getScriptEngine(StringUtil.isName(lang) ? lang : null);
1241-
engine.eval(preCode + "\n" + _if);
1239+
String lang = ind < 0 || ind > 20 ? null : ifStr.substring(0, ind);
1240+
boolean isName = StringUtil.isName(lang);
1241+
ScriptEngine engine = getScriptEngine(isName ? lang : null);
1242+
engine.eval(preCode + "\n" + (isName ? ifStr.substring(ind + 1) : ifStr));
12421243
}
12431244
else {
12441245
for (Map.Entry<String, Object> entry : ifSet) {
12451246
String k = entry == null ? null : entry.getKey();
1246-
// if (condKeys.contains(k)) {
1247-
// throw new IllegalArgumentException("Request 表 structure 配置的 " + ON.name()
1248-
// + ":{ " + k + ":value } 中 key 不合法,不允许传 [" + StringUtil.join(condKeys.toArray(new String[]{})) + "] 中的任何一个 !");
1249-
// }
1247+
// if (condKeys.contains(k)) {
1248+
// throw new IllegalArgumentException("Request 表 structure 配置的 " + ON.name()
1249+
// + ":{ " + k + ":value } 中 key 不合法,不允许传 [" + StringUtil.join(condKeys.toArray(new String[]{})) + "] 中的任何一个 !");
1250+
// }
12501251

12511252
Object v = k == null ? null : entry.getValue();
12521253
if (v instanceof String) {
12531254
int ind = k.indexOf(":");
1254-
String lang = ind < 0 ? null : k.substring(0, ind);
1255-
ScriptEngine engine = getScriptEngine(StringUtil.isName(lang) ? lang : null);
1256-
k = ind < 0 ? k : k.substring(ind + 1);
1255+
String lang = ind < 0 || ind > 20 ? null : k.substring(0, ind);
1256+
boolean isName = StringUtil.isName(lang);
1257+
ScriptEngine engine = getScriptEngine(isName ? lang : null);
1258+
k = isName ? k.substring(ind + 1) : k;
12571259

12581260
boolean isElse = StringUtil.isEmpty(k, false); // 其它直接报错,不允许传 StringUtil.isEmpty(k, true) || "ELSE".equals(k);
12591261
// String code = preCode + "\n\n" + (StringUtil.isEmpty(v, false) ? k : (isElse ? v : "if (" + k + ") {\n " + v + "\n}"));
12601262
String code = preCode + "\n\n" + (isElse ? v : "if (" + k + ") {\n " + v + "\n}");
12611263

1262-
// ScriptExecutor executor = new JavaScriptExecutor();
1263-
// executor.execute(null, real, )
1264+
// ScriptExecutor executor = new JavaScriptExecutor();
1265+
// executor.execute(null, real, )
12641266

12651267
engine.eval(code);
12661268

1267-
// PARSER_CREATOR.createFunctionParser()
1268-
// .setCurrentObject(real)
1269-
// .setKey(k)
1270-
// .setMethod(method)
1271-
// .invoke()
1269+
// PARSER_CREATOR.createFunctionParser()
1270+
// .setCurrentObject(real)
1271+
// .setKey(k)
1272+
// .setMethod(method)
1273+
// .invoke()
12721274
continue;
12731275
}
12741276

@@ -1288,22 +1290,11 @@ public static <T extends Object> JSONObject parse(@NotNull final RequestMethod m
12881290
}
12891291

12901292
public static ScriptEngine getScriptEngine(String lang) {
1291-
List<ScriptEngineFactory> list = StringUtil.isEmpty(lang, true) ? null : SCRIPT_ENGINE_MANAGER.getEngineFactories();
1292-
1293-
ScriptEngine engine = null;
1294-
if (list == null || list.isEmpty()) {
1295-
engine = SCRIPT_ENGINE; // StringUtil.isEmpty(lang) ? SCRIPT_ENGINE : null;
1296-
}
1297-
else {
1298-
for (ScriptEngineFactory sef : list) {
1299-
if (sef != null && lang.equals(sef.getEngineName())) {
1300-
engine = sef.getScriptEngine();
1301-
}
1302-
}
1303-
}
1293+
boolean isEmpty = StringUtil.isEmpty(lang, true);
1294+
ScriptEngine engine = isEmpty ? SCRIPT_ENGINE : SCRIPT_ENGINE_MANAGER.getEngineByName(lang);
13041295

13051296
if (engine == null) {
1306-
throw new NullPointerException("找不到可执行 " + (StringUtil.isEmpty(lang, true) ? "js" : lang) + " 脚本的引擎!engine == null!");
1297+
throw new NullPointerException("找不到可执行 " + (isEmpty ? "js" : lang) + " 脚本的引擎!engine == null!");
13071298
}
13081299

13091300
return engine;

0 commit comments

Comments
 (0)