Skip to content

Commit 1b2b65b

Browse files
committed
新增 ENABLE_REMOTE_FUNCTION, ENABLE_SCRIPT_FUNCTION, ENABLE_VERIFY_ROLE, ENABLE_VERIFY_CONTENT 等配置
1 parent 21df376 commit 1b2b65b

File tree

3 files changed

+122
-46
lines changed

3 files changed

+122
-46
lines changed

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.io.FileReader;
99
import java.lang.reflect.InvocationTargetException;
10+
import java.lang.reflect.Method;
1011
import java.util.Arrays;
1112
import java.util.HashMap;
1213
import java.util.List;
@@ -32,6 +33,13 @@
3233
public class AbstractFunctionParser implements FunctionParser {
3334
// private static final String TAG = "AbstractFunctionParser";
3435

36+
/**开启支持远程函数
37+
*/
38+
public static boolean ENABLE_REMOTE_FUNCTION = true;
39+
/**开启支持远程函数中的 JavaScript 脚本形式
40+
*/
41+
public static boolean ENABLE_SCRIPT_FUNCTION = true;
42+
3543
public static final int TYPE_REMOTE_FUNCTION = 0;
3644
//public static final int TYPE_SQL_FUNCTION = 1;
3745
public static final int TYPE_SCRIPT_FUNCTION = 1;
@@ -160,6 +168,10 @@ public Object invoke(@NotNull String function, @NotNull JSONObject currentObject
160168
* @return {@link #invoke(AbstractFunctionParser, String, Class[], Object[])}
161169
*/
162170
public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull String function, @NotNull JSONObject currentObject) throws Exception {
171+
if (ENABLE_REMOTE_FUNCTION == false) {
172+
throw new UnsupportedOperationException("AbstractFunctionParser.ENABLE_REMOTE_FUNCTION" +
173+
" == false 时不支持远程函数!如需支持则设置 AbstractFunctionParser.ENABLE_REMOTE_FUNCTION = true !");
174+
}
163175

164176
FunctionBean fb = parseFunction(function, currentObject, false);
165177

@@ -170,15 +182,19 @@ public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull Str
170182

171183
int type = row.getIntValue("type");
172184
if (type < TYPE_REMOTE_FUNCTION || type > TYPE_SCRIPT_FUNCTION) {
173-
throw new UnsupportedOperationException("type = " + type + " 不合法!必须是 [0, 1, 2] 中的一个 !");
185+
throw new UnsupportedOperationException("type = " + type + " 不合法!必须是 [0, 1] 中的一个 !");
186+
}
187+
if (ENABLE_SCRIPT_FUNCTION == false && type == TYPE_SCRIPT_FUNCTION) {
188+
throw new UnsupportedOperationException("type = " + type + " 不合法!AbstractFunctionParser.ENABLE_SCRIPT_FUNCTION" +
189+
" == false 时不支持远程函数中的脚本形式!如需支持则设置 AbstractFunctionParser.ENABLE_SCRIPT_FUNCTION = true !");
174190
}
175191

176192

177193
int version = row.getIntValue("version");
178194
if (parser.getVersion() < version) {
179195
throw new UnsupportedOperationException("不允许 version = " + parser.getVersion() + " 的请求调用远程函数 " + fb.getMethod() + " ! 必须满足 version >= " + version + " !");
180196
}
181-
String tag = row.getString("tag");
197+
String tag = row.getString("tag"); // TODO 改为 tags,类似 methods 支持多个 tag。或者干脆不要?因为目前非开放请求全都只能后端指定
182198
if (tag != null && tag.equals(parser.getTag()) == false) {
183199
throw new UnsupportedOperationException("不允许 tag = " + parser.getTag() + " 的请求调用远程函数 " + fb.getMethod() + " ! 必须满足 tag = " + tag + " !");
184200
}
@@ -227,7 +243,13 @@ public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull Str
227243
if (type == TYPE_SCRIPT_FUNCTION) {
228244
return invokeScript(parser, methodName, parameterTypes, args, currentObject);
229245
}
230-
return parser.getClass().getMethod(methodName, parameterTypes).invoke(parser, args);
246+
247+
Method m = parser.getClass().getMethod(methodName, parameterTypes);
248+
//费性能,还是初始化时做更好
249+
//if (m.getReturnType().getSimpleName().equals(returnType) == false) {
250+
// throw new IllegalArgumentTypeException("");
251+
//}
252+
return m.invoke(parser, args);
231253
}
232254

233255
public static Invocable INVOCABLE;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ public AbstractParser() {
120120
* @param method null ? requestMethod = GET
121121
*/
122122
public AbstractParser(RequestMethod method) {
123-
this(method, true);
123+
super();
124+
setMethod(method);
125+
setNeedVerifyRole(AbstractVerifier.ENABLE_VERIFY_ROLE);
126+
setNeedVerifyContent(AbstractVerifier.ENABLE_VERIFY_CONTENT);
124127
}
125128
/**
126129
* @param method null ? requestMethod = GET

0 commit comments

Comments
 (0)