Skip to content

Commit 42aba60

Browse files
committed
Server:远程函数DemoFunction构造方法新增RequestMethod支持函数对方法的限制,实例创建从ObjectParser移到Parser减少不必要的内存占用;Operation新增UPDATE替代PUT避免和RequestMethod.PUT混淆
1 parent 862d9be commit 42aba60

File tree

8 files changed

+48
-35
lines changed

8 files changed

+48
-35
lines changed

APIJSON-Java-Server/APIJSONDemo/src/main/java/apijson/demo/server/DemoFunction.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@
4242
public class DemoFunction extends RemoteFunction {
4343
private static final String TAG = "DemoFunction";
4444

45+
private final RequestMethod method;
4546
private final HttpSession session;
46-
public DemoFunction(HttpSession session) {
47+
public DemoFunction(RequestMethod method, HttpSession session) {
48+
this.method = method;
4749
this.session = session;
4850
}
4951

@@ -73,11 +75,11 @@ public static void test() throws Exception {
7375
request.put("object", object);
7476

7577

76-
Log.i(TAG, "plus(1,-2) = " + new DemoFunction(null).invoke(request, "plus(i0,i1)"));
77-
Log.i(TAG, "count([1,2,4,10]) = " + new DemoFunction(null).invoke(request, "countArray(array)"));
78-
Log.i(TAG, "isContain([1,2,4,10], 10) = " + new DemoFunction(null).invoke(request, "isContain(array,id)"));
79-
Log.i(TAG, "getFromArray([1,2,4,10], 0) = " + new DemoFunction(null).invoke(request, "getFromArray(array,@position)"));
80-
Log.i(TAG, "getFromObject({key:true}, key) = " + new DemoFunction(null).invoke(request, "getFromObject(object,key)"));
78+
Log.i(TAG, "plus(1,-2) = " + new DemoFunction(null, null).invoke("plus(i0,i1)", request));
79+
Log.i(TAG, "count([1,2,4,10]) = " + new DemoFunction(null, null).invoke("countArray(array)", request));
80+
Log.i(TAG, "isContain([1,2,4,10], 10) = " + new DemoFunction(null, null).invoke("isContain(array,id)", request));
81+
Log.i(TAG, "getFromArray([1,2,4,10], 0) = " + new DemoFunction(null, null).invoke("getFromArray(array,@position)", request));
82+
Log.i(TAG, "getFromObject({key:true}, key) = " + new DemoFunction(null, null).invoke("getFromObject(object,key)", request));
8183

8284
forceUseable();
8385
}
@@ -138,16 +140,12 @@ private static void exitWithError(String msg) {
138140
}
139141

140142
/**反射调用
141-
* @param request
142143
* @param function 例如get(object,key),参数只允许引用,不能直接传值
144+
* @param json 不作为第一个参数,就不能远程调用invoke,避免死循环
143145
* @return
144146
*/
145-
public Object invoke(JSONObject request, String function) throws Exception {
146-
//TODO 不允许调用invoke,避免死循环
147-
// if (function.startsWith("invoke(")) {
148-
//
149-
// }
150-
return invoke(this, request, function);
147+
public Object invoke(String function, JSONObject json) throws Exception {
148+
return invoke(this, json, function);
151149
}
152150

153151

@@ -203,6 +201,10 @@ public Object verifyURLList(@NotNull JSONObject request, @NotNull String urlList
203201
* @throws Exception
204202
*/
205203
public int deleteCommentOfMoment(@NotNull JSONObject rq, @NotNull String momentId) throws Exception {
204+
if (method != RequestMethod.DELETE) {
205+
throw new UnsupportedOperationException("远程函数 deleteCommentOfMoment 只支持 DELETE 方法!");
206+
}
207+
206208
long mid = rq.getLongValue(momentId);
207209
if (mid <= 0 || rq.getIntValue(JSONResponse.KEY_COUNT) <= 0) {
208210
return 0;
@@ -230,6 +232,10 @@ public int deleteCommentOfMoment(@NotNull JSONObject rq, @NotNull String momentI
230232
* @return
231233
*/
232234
public int deleteChildComment(@NotNull JSONObject rq, @NotNull String toId) throws Exception {
235+
if (method != RequestMethod.DELETE) {
236+
throw new UnsupportedOperationException("远程函数 deleteChildComment 只支持 DELETE 方法!");
237+
}
238+
233239
long tid = rq.getLongValue(toId);
234240
if (tid <= 0 || rq.getIntValue(JSONResponse.KEY_COUNT) <= 0) {
235241
return 0;

APIJSON-Java-Server/APIJSONDemo/src/main/java/apijson/demo/server/DemoObjectParser.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public abstract class DemoObjectParser extends AbstractObjectParser {
3838
}
3939

4040

41-
private DemoFunction function;
41+
4242
/**for single object
4343
* @param parentPath
4444
* @param request
@@ -47,7 +47,6 @@ public abstract class DemoObjectParser extends AbstractObjectParser {
4747
*/
4848
public DemoObjectParser(HttpSession session, @NotNull JSONObject request, String parentPath, String name, SQLConfig arrayConfig) throws Exception {
4949
super(request, parentPath, name, arrayConfig);
50-
function = new DemoFunction(session);
5150
}
5251

5352
@Override
@@ -68,10 +67,4 @@ public SQLConfig newSQLConfig() throws Exception {
6867
}
6968

7069

71-
@Override
72-
public Object onFunctionParse(JSONObject json, String fun) throws Exception {
73-
return function.invoke(json, fun);
74-
}
75-
76-
7770
}

APIJSON-Java-Server/APIJSONDemo/src/main/java/apijson/demo/server/DemoParser.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ public JSONObject parseResponse(JSONObject request) {
7373
return super.parseResponse(request);
7474
}
7575

76+
private DemoFunction function;
77+
@Override
78+
public Object onFunctionParse(JSONObject json, String fun) throws Exception {
79+
if (function == null) {
80+
function = new DemoFunction(requestMethod, session);
81+
}
82+
return function.invoke(fun, json);
83+
}
84+
7685

7786
@Override
7887
public DemoObjectParser createObjectParser(JSONObject request, String parentPath, String name, SQLConfig arrayConfig) throws Exception {

APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server/AbstractObjectParser.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ public void onFunctionResponse(String type) throws Exception {
632632
}
633633

634634
public void parseFunction(JSONObject json, String key, String value) throws Exception {
635-
Object result = onFunctionParse(json, value);
635+
Object result = parser.onFunctionParse(json, value);
636636

637637
if (result != null) {
638638
String k = AbstractSQLConfig.getRealKey(method, key, false, false, "`"); //FIXME PG 是 "
@@ -658,10 +658,6 @@ public void onChildResponse() throws Exception {
658658
}
659659

660660

661-
@Override
662-
public Object onFunctionParse(JSONObject json, String function) throws Exception {
663-
return null;
664-
}
665661

666662
@Override
667663
public Object onReferenceParse(@NotNull String path) {

APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server/ObjectParser.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,6 @@ public interface ObjectParser {
7373
*/
7474
Object onReferenceParse(@NotNull String path);
7575

76-
/**解析远程函数
77-
* @param json
78-
* @param function
79-
* @return
80-
* @throws Exception
81-
*/
82-
Object onFunctionParse(JSONObject json, String function) throws Exception;
83-
8476
//TODO 改用 MySQL json_add,json_remove,json_contains 等函数!
8577
/**PUT key:[]
8678
* @param key

APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server/Operation.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ public enum Operation {
5151
/**
5252
* 强行放入,不存在时就添加,存在时就修改
5353
*/
54-
PUT,
54+
UPDATE,
55+
@Deprecated
56+
PUT, //use UPDATE instead,容易和 RequestMethod.PUT 混淆,最快在 4.0.0 移除,请尽快修改 Request 表 structure 字段对应值里的 PUT
5557
/**
5658
* 替换,当要被替换的对象存在时
5759
*/

APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server/Parser.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ public interface Parser<T> {
8686

8787
JSONArray onArrayParse(JSONObject request, String parentPath, String name) throws Exception;
8888

89+
/**解析远程函数
90+
* @param object
91+
* @param function
92+
* @return
93+
* @throws Exception
94+
*/
95+
Object onFunctionParse(JSONObject object, String function) throws Exception;
96+
8997
ObjectParser createObjectParser(JSONObject request, String parentPath, String name, SQLConfig arrayConfig) throws Exception;
9098

9199
int getMaxQueryCount();

APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server/Structure.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static zuo.biao.apijson.server.Operation.REPLACE;
2525
import static zuo.biao.apijson.server.Operation.TYPE;
2626
import static zuo.biao.apijson.server.Operation.UNIQUE;
27+
import static zuo.biao.apijson.server.Operation.UPDATE;
2728
import static zuo.biao.apijson.server.Operation.VERIFY;
2829

2930
import java.util.ArrayList;
@@ -227,6 +228,7 @@ public static JSONObject parse(String name, JSONObject target, JSONObject real
227228
JSONObject type = target.getJSONObject(TYPE.name());
228229
JSONObject verify = target.getJSONObject(VERIFY.name());
229230
JSONObject add = target.getJSONObject(ADD.name());
231+
JSONObject update = target.getJSONObject(UPDATE.name());
230232
JSONObject put = target.getJSONObject(PUT.name());
231233
JSONObject replace = target.getJSONObject(REPLACE.name());
232234

@@ -239,6 +241,7 @@ public static JSONObject parse(String name, JSONObject target, JSONObject real
239241
target.remove(TYPE.name());
240242
target.remove(VERIFY.name());
241243
target.remove(ADD.name());
244+
target.remove(UPDATE.name());
242245
target.remove(PUT.name());
243246
target.remove(REPLACE.name());
244247

@@ -359,6 +362,7 @@ public static JSONObject parse(String name, JSONObject target, JSONObject real
359362
real = operate(TYPE, type, real, creator);
360363
real = operate(VERIFY, verify, real, creator);
361364
real = operate(ADD, add, real, creator);
365+
real = operate(UPDATE, update, real, creator);
362366
real = operate(PUT, put, real, creator);
363367
real = operate(REPLACE, replace, real, creator);
364368
//校验与修改Request>>>>>>>>>>>>>>>>>
@@ -414,6 +418,9 @@ private static JSONObject operate(Operation opt, JSONObject targetChild, JSONObj
414418
else if (opt == VERIFY) {
415419
verify(tk, tv, real, creator);
416420
}
421+
else if (opt == UPDATE) {
422+
real.put(tk, tv);
423+
}
417424
else if (opt == PUT) {
418425
real.put(tk, tv);
419426
}

0 commit comments

Comments
 (0)