|
42 | 42 | public class DemoFunction extends RemoteFunction { |
43 | 43 | private static final String TAG = "DemoFunction"; |
44 | 44 |
|
| 45 | + private final RequestMethod method; |
45 | 46 | private final HttpSession session; |
46 | | - public DemoFunction(HttpSession session) { |
| 47 | + public DemoFunction(RequestMethod method, HttpSession session) { |
| 48 | + this.method = method; |
47 | 49 | this.session = session; |
48 | 50 | } |
49 | 51 |
|
@@ -73,11 +75,11 @@ public static void test() throws Exception { |
73 | 75 | request.put("object", object); |
74 | 76 |
|
75 | 77 |
|
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)); |
81 | 83 |
|
82 | 84 | forceUseable(); |
83 | 85 | } |
@@ -138,16 +140,12 @@ private static void exitWithError(String msg) { |
138 | 140 | } |
139 | 141 |
|
140 | 142 | /**反射调用 |
141 | | - * @param request |
142 | 143 | * @param function 例如get(object,key),参数只允许引用,不能直接传值 |
| 144 | + * @param json 不作为第一个参数,就不能远程调用invoke,避免死循环 |
143 | 145 | * @return |
144 | 146 | */ |
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); |
151 | 149 | } |
152 | 150 |
|
153 | 151 |
|
@@ -203,6 +201,10 @@ public Object verifyURLList(@NotNull JSONObject request, @NotNull String urlList |
203 | 201 | * @throws Exception |
204 | 202 | */ |
205 | 203 | public int deleteCommentOfMoment(@NotNull JSONObject rq, @NotNull String momentId) throws Exception { |
| 204 | + if (method != RequestMethod.DELETE) { |
| 205 | + throw new UnsupportedOperationException("远程函数 deleteCommentOfMoment 只支持 DELETE 方法!"); |
| 206 | + } |
| 207 | + |
206 | 208 | long mid = rq.getLongValue(momentId); |
207 | 209 | if (mid <= 0 || rq.getIntValue(JSONResponse.KEY_COUNT) <= 0) { |
208 | 210 | return 0; |
@@ -230,6 +232,10 @@ public int deleteCommentOfMoment(@NotNull JSONObject rq, @NotNull String momentI |
230 | 232 | * @return |
231 | 233 | */ |
232 | 234 | public int deleteChildComment(@NotNull JSONObject rq, @NotNull String toId) throws Exception { |
| 235 | + if (method != RequestMethod.DELETE) { |
| 236 | + throw new UnsupportedOperationException("远程函数 deleteChildComment 只支持 DELETE 方法!"); |
| 237 | + } |
| 238 | + |
233 | 239 | long tid = rq.getLongValue(toId); |
234 | 240 | if (tid <= 0 || rq.getIntValue(JSONResponse.KEY_COUNT) <= 0) { |
235 | 241 | return 0; |
|
0 commit comments