45
45
public class DemoFunction extends RemoteFunction {
46
46
private static final String TAG = "DemoFunction" ;
47
47
48
- private final RequestMethod method ;
49
48
private final HttpSession session ;
50
- public DemoFunction (RequestMethod method , HttpSession session ) {
51
- this . method = method ;
49
+ public DemoFunction (RequestMethod method , String tag , int version , HttpSession session ) {
50
+ super ( method , tag , version ) ;
52
51
this .session = session ;
53
52
}
54
53
@@ -78,11 +77,11 @@ public static void test() throws Exception {
78
77
request .put ("object" , object );
79
78
80
79
81
- Log .i (TAG , "plus(1,-2) = " + new DemoFunction (null , null ).invoke ("plus(i0,i1)" , request ));
82
- Log .i (TAG , "count([1,2,4,10]) = " + new DemoFunction (null , null ).invoke ("countArray(array)" , request ));
83
- Log .i (TAG , "isContain([1,2,4,10], 10) = " + new DemoFunction (null , null ).invoke ("isContain(array,id)" , request ));
84
- Log .i (TAG , "getFromArray([1,2,4,10], 0) = " + new DemoFunction (null , null ).invoke ("getFromArray(array,@position)" , request ));
85
- Log .i (TAG , "getFromObject({key:true}, key) = " + new DemoFunction (null , null ).invoke ("getFromObject(object,key)" , request ));
80
+ Log .i (TAG , "plus(1,-2) = " + new DemoFunction (null , null , 1 , null ).invoke ("plus(i0,i1)" , request ));
81
+ Log .i (TAG , "count([1,2,4,10]) = " + new DemoFunction (null , null , 1 , null ).invoke ("countArray(array)" , request ));
82
+ Log .i (TAG , "isContain([1,2,4,10], 10) = " + new DemoFunction (null , null , 1 , null ).invoke ("isContain(array,id)" , request ));
83
+ Log .i (TAG , "getFromArray([1,2,4,10], 0) = " + new DemoFunction (null , null , 1 , null ).invoke ("getFromArray(array,@position)" , request ));
84
+ Log .i (TAG , "getFromObject({key:true}, key) = " + new DemoFunction (null , null , 1 , null ).invoke ("getFromObject(object,key)" , request ));
86
85
87
86
}
88
87
@@ -112,7 +111,7 @@ public static JSONObject init(boolean shutdownWhenServerError) throws ServerExce
112
111
request .putAll (functionItem .toArray (0 , 0 , FUNCTION_ ));
113
112
} //Function[]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
114
113
115
-
114
+
116
115
JSONObject response = new DemoParser (RequestMethod .GET , true ).parseResponse (request );
117
116
if (JSONResponse .isSuccess (response ) == false ) {
118
117
onServerError ("\n \n \n \n \n !!!! 查询远程函数异常 !!!\n " + response .getString (JSONResponse .KEY_MSG ) + "\n \n \n \n \n " , shutdownWhenServerError );
@@ -135,15 +134,28 @@ public static JSONObject init(boolean shutdownWhenServerError) throws ServerExce
135
134
if (demo == null ) {
136
135
onServerError ("字段 demo 的值必须为合法且非 null 的 JSONObejct 字符串!" , shutdownWhenServerError );
137
136
}
137
+ String name = item .getString ("name" );
138
138
if (demo .containsKey ("result()" ) == false ) {
139
- demo .put ("result()" , getFunctionCall (item . getString ( " name" ) , item .getString ("arguments" )));
139
+ demo .put ("result()" , getFunctionCall (name , item .getString ("arguments" )));
140
140
}
141
- demo .put (JSONRequest .KEY_COLUMN , "id,name,arguments,demo" );
141
+ // demo.put(JSONRequest.KEY_TAG, item.getString(JSONRequest.KEY_TAG));
142
+ // demo.put(JSONRequest.KEY_VERSION, item.getInteger(JSONRequest.KEY_VERSION));
143
+
144
+ FUNCTION_MAP .put (name , item ); //必须在测试 invoke 前!
145
+
146
+ String [] methods = StringUtil .split (item .getString ("methods" ));
147
+ JSONObject r = new DemoParser (
148
+ methods == null || methods .length <= 0 ? RequestMethod .GET : RequestMethod .valueOf (methods [0 ])
149
+ , true
150
+ )
151
+ .setTag (item .getString (JSONRequest .KEY_TAG ))
152
+ .setVersion (item .getIntValue (JSONRequest .KEY_VERSION ))
153
+ .parseResponse (demo );
142
154
143
- JSONObject r = new DemoParser (RequestMethod .GET , true ).parseResponse (demo );
144
155
if (JSONResponse .isSuccess (r ) == false ) {
145
156
onServerError (JSONResponse .getMsg (r ), shutdownWhenServerError );
146
157
}
158
+
147
159
}
148
160
149
161
return response ;
@@ -223,10 +235,6 @@ public Object verifyURLList(@NotNull JSONObject request, @NotNull String urlList
223
235
* @throws Exception
224
236
*/
225
237
public int deleteCommentOfMoment (@ NotNull JSONObject rq , @ NotNull String momentId ) throws Exception {
226
- if (method != RequestMethod .DELETE ) {
227
- throw new UnsupportedOperationException ("远程函数 deleteCommentOfMoment 只支持 DELETE 方法!" );
228
- }
229
-
230
238
long mid = rq .getLongValue (momentId );
231
239
if (mid <= 0 || rq .getIntValue (JSONResponse .KEY_COUNT ) <= 0 ) {
232
240
return 0 ;
@@ -254,10 +262,6 @@ public int deleteCommentOfMoment(@NotNull JSONObject rq, @NotNull String momentI
254
262
* @return
255
263
*/
256
264
public int deleteChildComment (@ NotNull JSONObject rq , @ NotNull String toId ) throws Exception {
257
- if (method != RequestMethod .DELETE ) { //TODO 如果这样的判断太多,可以把 DemoFunction 分成对应不同 RequestMethod 的 GetFunciton 等,创建时根据 method 判断用哪个
258
- throw new UnsupportedOperationException ("远程函数 deleteChildComment 只支持 DELETE 方法!" );
259
- }
260
-
261
265
long tid = rq .getLongValue (toId );
262
266
if (tid <= 0 || rq .getIntValue (JSONResponse .KEY_COUNT ) <= 0 ) {
263
267
return 0 ;
@@ -324,10 +328,10 @@ private JSONArray getChildCommentIdList(long tid) {
324
328
* @return
325
329
* @throws ServerException
326
330
*/
327
- public JSONObject getFunctionDemo (@ NotNull JSONObject request ) throws ServerException {
331
+ public JSONObject getFunctionDemo (@ NotNull JSONObject request ) {
328
332
JSONObject demo = JSON .parseObject (request .getString ("demo" ));
329
333
if (demo == null ) {
330
- throw new ServerException ( "服务器内部错误,字段 demo 的值必须为合法且非 null 的 JSONObejct 字符串!" );
334
+ demo = new JSONObject ( );
331
335
}
332
336
if (demo .containsKey ("result()" ) == false ) {
333
337
demo .put ("result()" , getFunctionCall (request .getString ("name" ), request .getString ("arguments" )));
@@ -349,15 +353,15 @@ public String getFunctionDetail(@NotNull JSONObject request) {
349
353
* @return
350
354
*/
351
355
private static String getFunctionCall (String name , String arguments ) {
352
- return name + "(" + arguments + ")" ;
356
+ return name + "(" + StringUtil . getTrimedString ( arguments ) + ")" ;
353
357
}
354
358
355
359
/**TODO 仅用来测试 "key-()":"getIdList()" 和 "key()":"getIdList()"
356
360
* @param request
357
361
* @return JSONArray 只能用JSONArray,用long[]会在SQLConfig解析崩溃
358
362
* @throws Exception
359
363
*/
360
- public JSONArray getIdList (@ NotNull JSONObject request ) throws Exception {
364
+ public JSONArray getIdList (@ NotNull JSONObject request ) {
361
365
return new JSONArray (new ArrayList <Object >(Arrays .asList (12 , 15 , 301 , 82001 , 82002 , 38710 )));
362
366
}
363
367
0 commit comments