4545public class DemoFunction extends RemoteFunction {
4646 private static final String TAG = "DemoFunction" ;
4747
48- private final RequestMethod method ;
4948 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 ) ;
5251 this .session = session ;
5352 }
5453
@@ -78,11 +77,11 @@ public static void test() throws Exception {
7877 request .put ("object" , object );
7978
8079
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 ));
8685
8786 }
8887
@@ -112,7 +111,7 @@ public static JSONObject init(boolean shutdownWhenServerError) throws ServerExce
112111 request .putAll (functionItem .toArray (0 , 0 , FUNCTION_ ));
113112 } //Function[]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
114113
115-
114+
116115 JSONObject response = new DemoParser (RequestMethod .GET , true ).parseResponse (request );
117116 if (JSONResponse .isSuccess (response ) == false ) {
118117 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
135134 if (demo == null ) {
136135 onServerError ("字段 demo 的值必须为合法且非 null 的 JSONObejct 字符串!" , shutdownWhenServerError );
137136 }
137+ String name = item .getString ("name" );
138138 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" )));
140140 }
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 );
142154
143- JSONObject r = new DemoParser (RequestMethod .GET , true ).parseResponse (demo );
144155 if (JSONResponse .isSuccess (r ) == false ) {
145156 onServerError (JSONResponse .getMsg (r ), shutdownWhenServerError );
146157 }
158+
147159 }
148160
149161 return response ;
@@ -223,10 +235,6 @@ public Object verifyURLList(@NotNull JSONObject request, @NotNull String urlList
223235 * @throws Exception
224236 */
225237 public int deleteCommentOfMoment (@ NotNull JSONObject rq , @ NotNull String momentId ) throws Exception {
226- if (method != RequestMethod .DELETE ) {
227- throw new UnsupportedOperationException ("远程函数 deleteCommentOfMoment 只支持 DELETE 方法!" );
228- }
229-
230238 long mid = rq .getLongValue (momentId );
231239 if (mid <= 0 || rq .getIntValue (JSONResponse .KEY_COUNT ) <= 0 ) {
232240 return 0 ;
@@ -254,10 +262,6 @@ public int deleteCommentOfMoment(@NotNull JSONObject rq, @NotNull String momentI
254262 * @return
255263 */
256264 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-
261265 long tid = rq .getLongValue (toId );
262266 if (tid <= 0 || rq .getIntValue (JSONResponse .KEY_COUNT ) <= 0 ) {
263267 return 0 ;
@@ -324,10 +328,10 @@ private JSONArray getChildCommentIdList(long tid) {
324328 * @return
325329 * @throws ServerException
326330 */
327- public JSONObject getFunctionDemo (@ NotNull JSONObject request ) throws ServerException {
331+ public JSONObject getFunctionDemo (@ NotNull JSONObject request ) {
328332 JSONObject demo = JSON .parseObject (request .getString ("demo" ));
329333 if (demo == null ) {
330- throw new ServerException ( "服务器内部错误,字段 demo 的值必须为合法且非 null 的 JSONObejct 字符串!" );
334+ demo = new JSONObject ( );
331335 }
332336 if (demo .containsKey ("result()" ) == false ) {
333337 demo .put ("result()" , getFunctionCall (request .getString ("name" ), request .getString ("arguments" )));
@@ -349,15 +353,15 @@ public String getFunctionDetail(@NotNull JSONObject request) {
349353 * @return
350354 */
351355 private static String getFunctionCall (String name , String arguments ) {
352- return name + "(" + arguments + ")" ;
356+ return name + "(" + StringUtil . getTrimedString ( arguments ) + ")" ;
353357 }
354358
355359 /**TODO 仅用来测试 "key-()":"getIdList()" 和 "key()":"getIdList()"
356360 * @param request
357361 * @return JSONArray 只能用JSONArray,用long[]会在SQLConfig解析崩溃
358362 * @throws Exception
359363 */
360- public JSONArray getIdList (@ NotNull JSONObject request ) throws Exception {
364+ public JSONArray getIdList (@ NotNull JSONObject request ) {
361365 return new JSONArray (new ArrayList <Object >(Arrays .asList (12 , 15 , 301 , 82001 , 82002 , 38710 )));
362366 }
363367
0 commit comments