1414
1515package apijson .framework ;
1616
17+ import static apijson .RequestMethod .DELETE ;
18+ import static apijson .RequestMethod .GET ;
19+ import static apijson .RequestMethod .GETS ;
20+ import static apijson .RequestMethod .HEAD ;
21+ import static apijson .RequestMethod .HEADS ;
22+ import static apijson .RequestMethod .POST ;
23+ import static apijson .RequestMethod .PUT ;
1724import static apijson .framework .APIJSONConstant .FUNCTION_ ;
1825
1926import java .io .IOException ;
2027import java .rmi .ServerException ;
21- import java .util .ArrayList ;
22- import java .util .Arrays ;
2328import java .util .LinkedHashMap ;
2429import java .util .List ;
2530import java .util .Map ;
3439import apijson .Log ;
3540import apijson .NotNull ;
3641import apijson .RequestMethod ;
37- import apijson .orm .AbstractVerifier ;
3842import apijson .StringUtil ;
3943import apijson .orm .AbstractFunctionParser ;
4044import apijson .orm .JSONRequest ;
@@ -49,9 +53,12 @@ public class APIJSONFunctionParser extends AbstractFunctionParser {
4953 public static final String TAG = "APIJSONFunctionParser" ;
5054
5155 @ NotNull
52- public static APIJSONCreator APIJSON_CREATOR ;
56+ public static APIJSONCreator <? extends Object > APIJSON_CREATOR ;
57+ @ NotNull
58+ public static final String [] ALL_METHODS ;
5359 static {
54- APIJSON_CREATOR = new APIJSONCreator ();
60+ APIJSON_CREATOR = new APIJSONCreator <>();
61+ ALL_METHODS = new String []{ GET .name (), HEAD .name (), GETS .name (), HEADS .name (), POST .name (), PUT .name (), DELETE .name () };
5562 }
5663
5764 private HttpSession session ;
@@ -109,7 +116,7 @@ public static JSONObject init(boolean shutdownWhenServerError) throws ServerExce
109116 * @return
110117 * @throws ServerException
111118 */
112- public static JSONObject init (APIJSONCreator creator ) throws ServerException {
119+ public static < T extends Object > JSONObject init (APIJSONCreator < T > creator ) throws ServerException {
113120 return init (false , creator );
114121 }
115122 /**初始化,加载所有远程函数配置,并校验是否已在应用层代码实现
@@ -118,7 +125,7 @@ public static JSONObject init(APIJSONCreator creator) throws ServerException {
118125 * @return
119126 * @throws ServerException
120127 */
121- public static JSONObject init (boolean shutdownWhenServerError , APIJSONCreator creator ) throws ServerException {
128+ public static < T extends Object > JSONObject init (boolean shutdownWhenServerError , APIJSONCreator < T > creator ) throws ServerException {
122129 return init (shutdownWhenServerError , creator , null );
123130 }
124131 /**初始化,加载所有远程函数配置,并校验是否已在应用层代码实现
@@ -128,9 +135,10 @@ public static JSONObject init(boolean shutdownWhenServerError, APIJSONCreator cr
128135 * @return
129136 * @throws ServerException
130137 */
131- public static JSONObject init (boolean shutdownWhenServerError , APIJSONCreator creator , JSONObject table ) throws ServerException {
138+ @ SuppressWarnings ("unchecked" )
139+ public static <T extends Object > JSONObject init (boolean shutdownWhenServerError , APIJSONCreator <T > creator , JSONObject table ) throws ServerException {
132140 if (creator == null ) {
133- creator = APIJSON_CREATOR ;
141+ creator = ( APIJSONCreator < T >) APIJSON_CREATOR ;
134142 }
135143 APIJSON_CREATOR = creator ;
136144
@@ -145,7 +153,7 @@ public static JSONObject init(boolean shutdownWhenServerError, APIJSONCreator cr
145153 request .putAll (functionItem .toArray (0 , 0 , FUNCTION_ ));
146154
147155
148- JSONObject response = creator .createParser ().setMethod (RequestMethod . GET ).setNeedVerify (true ).parseResponse (request );
156+ JSONObject response = creator .createParser ().setMethod (GET ).setNeedVerify (true ).parseResponse (request );
149157 if (JSONResponse .isSuccess (response ) == false ) {
150158 onServerError ("\n \n \n \n \n !!!! 查询远程函数异常 !!!\n " + response .getString (JSONResponse .KEY_MSG ) + "\n \n \n \n \n " , shutdownWhenServerError );
151159 }
@@ -158,7 +166,7 @@ public static JSONObject init(boolean shutdownWhenServerError, APIJSONCreator cr
158166 }
159167
160168
161- if (isAll ) { // 必须在测试 invoke 前把配置 put 进 FUNCTION_MAP!
169+ if (isAll ) { // 必须在测试 invoke 前把配置 put 进 FUNCTION_MAP! 如果要做成完全校验通过才更新 FUNCTION_MAP,但又不提供 忽略校验 参数,似乎无解
162170 FUNCTION_MAP = new LinkedHashMap <>();
163171 }
164172 Map <String , JSONObject > newMap = FUNCTION_MAP ; // 必须在测试 invoke 前把配置 put 进 FUNCTION_MAP! new LinkedHashMap<>();
@@ -183,18 +191,23 @@ public static JSONObject init(boolean shutdownWhenServerError, APIJSONCreator cr
183191 newMap .put (name , item ); // 必须在测试 invoke 前把配置 put 进 FUNCTION_MAP!
184192
185193 String [] methods = StringUtil .split (item .getString ("methods" ));
186- JSONObject r = new APIJSONParser (
187- methods == null || methods .length <= 0 ? RequestMethod .GET : RequestMethod .valueOf (methods [0 ])
188- , false
189- )
190- .setTag (item .getString (JSONRequest .KEY_TAG ))
191- .setVersion (item .getIntValue (JSONRequest .KEY_VERSION ))
192- .parseResponse (demo );
193-
194- if (JSONResponse .isSuccess (r ) == false ) {
195- onServerError (JSONResponse .getMsg (r ), shutdownWhenServerError );
194+
195+ if (methods == null || methods .length <= 0 ) {
196+ methods = ALL_METHODS ;
196197 }
197198
199+ for (String method : methods ) {
200+ JSONObject r = APIJSON_CREATOR .createParser ()
201+ .setMethod (RequestMethod .valueOf (method ))
202+ .setNeedVerify (false )
203+ .setTag (item .getString (JSONRequest .KEY_TAG ))
204+ .setVersion (item .getIntValue (JSONRequest .KEY_VERSION ))
205+ .parseResponse (demo );
206+
207+ if (JSONResponse .isSuccess (r ) == false ) {
208+ onServerError (JSONResponse .getMsg (r ), shutdownWhenServerError );
209+ }
210+ }
198211 }
199212
200213 // 必须在测试 invoke 前把配置 put 进 FUNCTION_MAP!
@@ -309,32 +322,6 @@ private static String getFunctionCall(String name, String arguments) {
309322 return name + "(" + StringUtil .getTrimedString (arguments ) + ")" ;
310323 }
311324
312- /**TODO 仅用来测试 "key-()":"getIdList()" 和 "key()":"getIdList()"
313- * @param request
314- * @return JSONArray 只能用JSONArray,用long[]会在SQLConfig解析崩溃
315- * @throws Exception
316- */
317- public JSONArray getIdList (@ NotNull JSONObject request ) {
318- return new JSONArray (new ArrayList <Object >(Arrays .asList (12 , 15 , 301 , 82001 , 82002 , 38710 )));
319- }
320-
321-
322- /**TODO 仅用来测试 "key-()":"verifyAccess()"
323- * @param request
324- * @return
325- * @throws Exception
326- */
327- public Object verifyAccess (@ NotNull JSONObject request ) throws Exception {
328- long userId = request .getLongValue (apijson .JSONObject .KEY_USER_ID );
329- String role = request .getString (apijson .JSONObject .KEY_ROLE );
330- if (AbstractVerifier .OWNER .equals (role ) && userId != APIJSONVerifier .getVisitorId (session )) {
331- throw new IllegalAccessException ("登录用户与角色OWNER不匹配!" );
332- }
333- return null ;
334- }
335-
336-
337-
338325
339326 public double plus (@ NotNull JSONObject request , String i0 , String i1 ) {
340327 return request .getDoubleValue (i0 ) + request .getDoubleValue (i1 );
0 commit comments