Skip to content

Commit ec1509e

Browse files
committed
Server:完善远程函数的环境变量,方便做更细致的操作
1 parent f20e3cf commit ec1509e

File tree

7 files changed

+151
-57
lines changed

7 files changed

+151
-57
lines changed

APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/boot/DemoFunctionParser.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,19 @@ public class DemoFunctionParser extends APIJSONFunctionParser {
3939
public static final String TAG = "DemoFunctionParser";
4040

4141
public DemoFunctionParser() {
42-
this(null, null, 0, null);
42+
this(null, null, 0, null, null);
4343
}
44-
public DemoFunctionParser(RequestMethod method, String tag, int version, HttpSession session) {
45-
super(method, tag, version, session);
44+
public DemoFunctionParser(RequestMethod method, String tag, int version, JSONObject request, HttpSession session) {
45+
super(method, tag, version, request, session);
4646
}
4747

4848
/**
49-
* @param request
49+
* @param current
5050
* @return
5151
* @throws Exception
5252
*/
53-
public Object verifyIdList(@NotNull JSONObject request, @NotNull String idList) throws Exception {
54-
Object obj = request.get(idList);
53+
public Object verifyIdList(@NotNull JSONObject current, @NotNull String idList) throws Exception {
54+
Object obj = current.get(idList);
5555
if (obj instanceof Collection == false) {
5656
throw new IllegalArgumentException(idList + " 不符合 Array 类型! 结构必须是 [] !");
5757
}
@@ -72,8 +72,8 @@ public Object verifyIdList(@NotNull JSONObject request, @NotNull String idList)
7272
* @return
7373
* @throws Exception
7474
*/
75-
public Object verifyURLList(@NotNull JSONObject request, @NotNull String urlList) throws Exception {
76-
Object obj = request.get(urlList);
75+
public Object verifyURLList(@NotNull JSONObject current, @NotNull String urlList) throws Exception {
76+
Object obj = current.get(urlList);
7777
if (obj instanceof Collection == false) {
7878
throw new IllegalArgumentException(urlList + " 不符合 Array 类型! 结构必须是 [] !");
7979
}
@@ -95,9 +95,9 @@ public Object verifyURLList(@NotNull JSONObject request, @NotNull String urlList
9595
* @return
9696
* @throws Exception
9797
*/
98-
public int deleteCommentOfMoment(@NotNull JSONObject rq, @NotNull String momentId) throws Exception {
99-
long mid = rq.getLongValue(momentId);
100-
if (mid <= 0 || rq.getIntValue(JSONResponse.KEY_COUNT) <= 0) {
98+
public int deleteCommentOfMoment(@NotNull JSONObject current, @NotNull String momentId) throws Exception {
99+
long mid = current.getLongValue(momentId);
100+
if (mid <= 0 || current.getIntValue(JSONResponse.KEY_COUNT) <= 0) {
101101
return 0;
102102
}
103103

@@ -122,9 +122,9 @@ public int deleteCommentOfMoment(@NotNull JSONObject rq, @NotNull String momentI
122122
* @param toId
123123
* @return
124124
*/
125-
public int deleteChildComment(@NotNull JSONObject rq, @NotNull String toId) throws Exception {
126-
long tid = rq.getLongValue(toId);
127-
if (tid <= 0 || rq.getIntValue(JSONResponse.KEY_COUNT) <= 0) {
125+
public int deleteChildComment(@NotNull JSONObject current, @NotNull String toId) throws Exception {
126+
long tid = current.getLongValue(toId);
127+
if (tid <= 0 || current.getIntValue(JSONResponse.KEY_COUNT) <= 0) {
128128
return 0;
129129
}
130130

@@ -190,7 +190,7 @@ private JSONArray getChildCommentIdList(long tid) {
190190
* @return JSONArray 只能用JSONArray,用long[]会在SQLConfig解析崩溃
191191
* @throws Exception
192192
*/
193-
public JSONArray getIdList(@NotNull JSONObject request) {
193+
public JSONArray getIdList(@NotNull JSONObject current) {
194194
return new JSONArray(new ArrayList<Object>(Arrays.asList(12, 15, 301, 82001, 82002, 38710)));
195195
}
196196

@@ -200,9 +200,9 @@ public JSONArray getIdList(@NotNull JSONObject request) {
200200
* @return
201201
* @throws Exception
202202
*/
203-
public Object verifyAccess(@NotNull JSONObject request) throws Exception {
204-
long userId = request.getLongValue(JSONRequest.KEY_USER_ID);
205-
RequestRole role = RequestRole.get(request.getString(JSONRequest.KEY_ROLE));
203+
public Object verifyAccess(@NotNull JSONObject current) throws Exception {
204+
long userId = current.getLongValue(JSONRequest.KEY_USER_ID);
205+
RequestRole role = RequestRole.get(current.getString(JSONRequest.KEY_ROLE));
206206
if (role == RequestRole.OWNER && userId != DemoVerifier.getVisitorId(getSession())) {
207207
throw new IllegalAccessException("登录用户与角色OWNER不匹配!");
208208
}

APIJSON-Java-Server/APIJSONFramework/src/main/java/apijson/framework/APIJSONFunctionParser.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ public APIJSONFunctionParser() {
4949
this(null);
5050
}
5151
public APIJSONFunctionParser(HttpSession session) {
52-
this(null, null, 0, session);
52+
this(null, null, 0, null, session);
5353
}
54-
public APIJSONFunctionParser(RequestMethod method, String tag, int version, HttpSession session) {
55-
super(method, tag, version);
54+
public APIJSONFunctionParser(RequestMethod method, String tag, int version, JSONObject request, HttpSession session) {
55+
super(method, tag, version, request);
5656
setSession(session);
5757
}
5858
public HttpSession getSession() {
@@ -216,7 +216,7 @@ public static void test(APIJSONFunctionParser function) throws Exception {
216216
request.put("object", object);
217217

218218
if (function == null) {
219-
function = new APIJSONFunctionParser(null, null, 1, null);
219+
function = new APIJSONFunctionParser(null, null, 1, null, null);
220220
}
221221

222222
Log.i(TAG, "plus(1,-2) = " + function.invoke("plus(i0,i1)", request));

APIJSON-Java-Server/APIJSONFramework/src/main/java/apijson/framework/APIJSONParser.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,24 @@ public FunctionParser getFunctionParser() {
112112
return functionParser;
113113
}
114114
@Override
115-
public Object onFunctionParse(JSONObject json, String fun) throws Exception {
115+
public Object onFunctionParse(String key, String function, String parentPath, String currentName, JSONObject currentObject) throws Exception {
116116
if (functionParser == null) {
117117
functionParser = createFunctionParser();
118118
functionParser.setMethod(getMethod());
119119
functionParser.setTag(getTag());
120120
functionParser.setVersion(getVersion());
121+
functionParser.setRequest(requestObject);
122+
121123
if (functionParser instanceof APIJSONFunctionParser) {
122124
((APIJSONFunctionParser) functionParser).setSession(getSession());
123125
}
124126
}
125-
return functionParser.invoke(fun, json);
127+
functionParser.setKey(key);
128+
functionParser.setParentPath(parentPath);
129+
functionParser.setCurrentName(currentName);
130+
functionParser.setCurrentObject(currentObject);
131+
132+
return functionParser.invoke(function, currentObject);
126133
}
127134

128135

APIJSON-Java-Server/APIJSONORM/src/main/java/apijson/orm/AbstractFunctionParser.java

Lines changed: 79 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ public class AbstractFunctionParser implements FunctionParser {
4545
private RequestMethod method;
4646
private String tag;
4747
private int version;
48+
private JSONObject request;
4849
public AbstractFunctionParser() {
49-
this(null, null, 0);
50+
this(null, null, 0, null);
5051
}
51-
public AbstractFunctionParser(RequestMethod method, String tag, int version) {
52+
public AbstractFunctionParser(RequestMethod method, String tag, int version, @NotNull JSONObject request) {
5253
setMethod(method == null ? RequestMethod.GET : method);
5354
setTag(tag);
5455
setVersion(version);
56+
setRequest(request);
5557
}
5658

5759
@Override
@@ -81,6 +83,62 @@ public AbstractFunctionParser setVersion(int version) {
8183
this.version = version;
8284
return this;
8385
}
86+
87+
private String key;
88+
@Override
89+
public String getKey() {
90+
return key;
91+
}
92+
@Override
93+
public AbstractFunctionParser setKey(String key) {
94+
this.key = key;
95+
return this;
96+
}
97+
98+
private String parentPath;
99+
@Override
100+
public String getParentPath() {
101+
return parentPath;
102+
}
103+
@Override
104+
public AbstractFunctionParser setParentPath(String parentPath) {
105+
this.parentPath = parentPath;
106+
return this;
107+
}
108+
private String currentName;
109+
@Override
110+
public String getCurrentName() {
111+
return currentName;
112+
}
113+
@Override
114+
public AbstractFunctionParser setCurrentName(String currentName) {
115+
this.currentName = currentName;
116+
return this;
117+
}
118+
119+
@NotNull
120+
@Override
121+
public JSONObject getRequest() {
122+
return request;
123+
}
124+
@Override
125+
public AbstractFunctionParser setRequest(@NotNull JSONObject request) {
126+
this.request = request;
127+
return this;
128+
}
129+
130+
private JSONObject currentObject;
131+
@NotNull
132+
@Override
133+
public JSONObject getCurrentObject() {
134+
return currentObject;
135+
}
136+
@Override
137+
public AbstractFunctionParser setCurrentObject(@NotNull JSONObject currentObject) {
138+
this.currentObject = currentObject;
139+
return this;
140+
}
141+
84142

85143
/**反射调用
86144
* @param function 例如get(object,key),参数只允许引用,不能直接传值
@@ -93,12 +151,12 @@ public Object invoke(@NotNull String function, @NotNull JSONObject currentObject
93151
}
94152

95153
/**反射调用
96-
* @param fun
154+
* @param parser
97155
* @param request
98156
* @param function 例如get(Map:map,key),参数只允许引用,不能直接传值
99157
* @return {@link #invoke(AbstractFunctionParser, String, Class[], Object[])}
100158
*/
101-
public static Object invoke(@NotNull AbstractFunctionParser fun, @NotNull String function, @NotNull JSONObject currentObject) throws Exception {
159+
public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull String function, @NotNull JSONObject currentObject) throws Exception {
102160

103161
FunctionBean fb = parseFunction(function, currentObject, false);
104162

@@ -108,21 +166,21 @@ public static Object invoke(@NotNull AbstractFunctionParser fun, @NotNull String
108166
}
109167

110168
int v = row.getIntValue("version");
111-
if (fun.getVersion() < v) {
112-
throw new UnsupportedOperationException("不允许 version = " + fun.getVersion() + " 的请求调用远程函数 " + fb.getMethod() + " ! 必须满足 version >= " + v + " !");
169+
if (parser.getVersion() < v) {
170+
throw new UnsupportedOperationException("不允许 version = " + parser.getVersion() + " 的请求调用远程函数 " + fb.getMethod() + " ! 必须满足 version >= " + v + " !");
113171
}
114172
String t = row.getString("tag");
115-
if (t != null && t.equals(fun.getTag()) == false) {
116-
throw new UnsupportedOperationException("不允许 tag = " + fun.getTag() + " 的请求调用远程函数 " + fb.getMethod() + " ! 必须满足 tag = " + t + " !");
173+
if (t != null && t.equals(parser.getTag()) == false) {
174+
throw new UnsupportedOperationException("不允许 tag = " + parser.getTag() + " 的请求调用远程函数 " + fb.getMethod() + " ! 必须满足 tag = " + t + " !");
117175
}
118176
String[] methods = StringUtil.split(row.getString("methods"));
119177
List<String> ml = methods == null || methods.length <= 0 ? null : Arrays.asList(methods);
120-
if (ml != null && ml.contains(fun.getMethod().toString()) == false) {
121-
throw new UnsupportedOperationException("不允许 method = " + fun.getMethod() + " 的请求调用远程函数 " + fb.getMethod() + " ! 必须满足 method 在 " + methods + "内 !");
178+
if (ml != null && ml.contains(parser.getMethod().toString()) == false) {
179+
throw new UnsupportedOperationException("不允许 method = " + parser.getMethod() + " 的请求调用远程函数 " + fb.getMethod() + " ! 必须满足 method 在 " + methods + "内 !");
122180
}
123181

124182
try {
125-
return invoke(fun, fb.getMethod(), fb.getTypes(), fb.getValues());
183+
return invoke(parser, fb.getMethod(), fb.getTypes(), fb.getValues());
126184
} catch (Exception e) {
127185
if (e instanceof NoSuchMethodException) {
128186
throw new IllegalArgumentException("字符 " + function + " 对应的远程函数 " + getFunction(fb.getMethod(), fb.getKeys()) + " 不在后端工程的DemoFunction内!"
@@ -143,6 +201,16 @@ public static Object invoke(@NotNull AbstractFunctionParser fun, @NotNull String
143201
}
144202

145203
}
204+
205+
/**反射调用
206+
* @param methodName
207+
* @param parameterTypes
208+
* @param args
209+
* @return
210+
*/
211+
public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull String methodName, @NotNull Class<?>[] parameterTypes, @NotNull Object[] args) throws Exception {
212+
return parser.getClass().getMethod(methodName, parameterTypes).invoke(parser, args);
213+
}
146214

147215
/**解析函数
148216
* @param function
@@ -231,16 +299,6 @@ else if (v instanceof JSONArray) { // Collection) {
231299
}
232300

233301

234-
/**反射调用
235-
* @param methodName
236-
* @param parameterTypes
237-
* @param args
238-
* @return
239-
*/
240-
public static Object invoke(@NotNull AbstractFunctionParser fun, @NotNull String methodName, @NotNull Class<?>[] parameterTypes, @NotNull Object[] args) throws Exception {
241-
return fun.getClass().getMethod(methodName, parameterTypes).invoke(fun, args);
242-
}
243-
244302
/**
245303
* @param method
246304
* @param keys

APIJSON-Java-Server/APIJSONORM/src/main/java/apijson/orm/AbstractObjectParser.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public AbstractObjectParser setParser(AbstractParser<?> parser) {
7070
protected final List<Join> joinList;
7171
protected final boolean isTable;
7272
protected final String path;
73+
protected final String name;
7374
protected final String table;
7475
protected final String alias;
7576

@@ -97,6 +98,7 @@ public AbstractObjectParser(@NotNull JSONObject request, String parentPath, Stri
9798

9899
this.type = arrayConfig == null ? 0 : arrayConfig.getType();
99100
this.joinList = arrayConfig == null ? null : arrayConfig.getJoinList();
101+
this.name = name;
100102
this.path = AbstractParser.getAbsPath(parentPath, name);
101103

102104
apijson.orm.Entry<String, String> entry = Pair.parseEntry(name, true);
@@ -400,7 +402,7 @@ else if (value instanceof String) { // 引用赋值路径
400402
type = "-";
401403
k = k.substring(0, k.length() - 1);
402404

403-
parseFunction(request, k, (String) value);
405+
parseFunction(k, (String) value, parentPath, name, request);
404406
}
405407
else {
406408
if (k.endsWith("+")) {
@@ -742,22 +744,22 @@ public void onFunctionResponse(String type) throws Exception {
742744
for (Entry<String, String> entry : functionSet) {
743745

744746
// parseFunction(json, entry.getKey(), entry.getValue());
745-
parseFunction(response, entry.getKey(), entry.getValue());
747+
parseFunction(entry.getKey(), entry.getValue(), parentPath, name, response);
746748
}
747749
}
748750
}
749751

750-
public void parseFunction(JSONObject json, String key, String value) throws Exception {
752+
public void parseFunction(String key, String value, String parentPath, String currentName, JSONObject currentObject) throws Exception {
751753
Object result;
752754
if (key.startsWith("@")) { //TODO 以后这种小众功能从 ORM 移出,作为一个 plugin/APIJSONProcedure
753-
FunctionBean fb = AbstractFunctionParser.parseFunction(value, json, true);
755+
FunctionBean fb = AbstractFunctionParser.parseFunction(value, currentObject, true);
754756

755757
SQLConfig config = newSQLConfig(true);
756758
config.setProcedure(fb.toFunctionCallString(true));
757759
result = parseResponse(config, true);
758760
}
759761
else {
760-
result = parser.onFunctionParse(json, value);
762+
result = parser.onFunctionParse(key, value, parentPath, currentName, currentObject);
761763
}
762764

763765
if (result != null) {

APIJSON-Java-Server/APIJSONORM/src/main/java/apijson/orm/FunctionParser.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,44 @@
1616

1717
import com.alibaba.fastjson.JSONObject;
1818

19+
import apijson.NotNull;
1920
import apijson.RequestMethod;
2021

21-
/**解析器
22+
23+
/**远程函数解析器
2224
* @author Lemon
2325
*/
2426
public interface FunctionParser {
2527

28+
Object invoke(@NotNull String function, @NotNull JSONObject currentObject) throws Exception;
29+
2630
RequestMethod getMethod();
27-
String getTag();
28-
int getVersion();
29-
3031
FunctionParser setMethod(RequestMethod method);
32+
33+
String getTag();
3134
FunctionParser setTag(String tag);
35+
36+
int getVersion();
3237
AbstractFunctionParser setVersion(int version);
3338

34-
Object invoke(String function, JSONObject currentObject) throws Exception;
39+
@NotNull
40+
JSONObject getRequest();
41+
FunctionParser setRequest(@NotNull JSONObject request);
42+
43+
44+
String getKey();
45+
FunctionParser setKey(String key);
46+
47+
String getParentPath();
48+
FunctionParser setParentPath(String parentPath);
49+
50+
String getCurrentName();
51+
FunctionParser setCurrentName(String currentName);
52+
53+
@NotNull
54+
JSONObject getCurrentObject();
55+
FunctionParser setCurrentObject(@NotNull JSONObject currentObject);
56+
57+
3558

3659
}

0 commit comments

Comments
 (0)