1717
1818import java .io .UnsupportedEncodingException ;
1919import java .net .URLDecoder ;
20+ import java .rmi .AlreadyBoundException ;
2021import java .util .ArrayList ;
2122import java .util .HashMap ;
2223import java .util .LinkedHashMap ;
2526import java .util .Set ;
2627import java .util .regex .Pattern ;
2728
29+ import javax .management .OperationsException ;
30+
31+ import com .alibaba .fastjson .JSONArray ;
2832import com .alibaba .fastjson .JSONObject ;
2933
3034import zuo .biao .apijson .JSON ;
@@ -45,9 +49,15 @@ public class RequestParser {
4549 public static final String SEPARATOR = StringUtil .SEPARATOR ;
4650
4751 private RequestMethod requestMethod ;
52+ /**
53+ * GET
54+ */
4855 public RequestParser () {
4956 this (null );
5057 }
58+ /**
59+ * @param requestMethod null ? requestMethod = GET
60+ */
5161 public RequestParser (RequestMethod requestMethod ) {
5262 super ();
5363 if (requestMethod == null ) {
@@ -438,9 +448,9 @@ && isInRelationMap(path) == false) {
438448 for (String key : set ) {
439449 value = transferredRequest .containsKey (key ) ? transferredRequest .get (key ) : request .get (key );
440450 if (value instanceof JSONObject ) {//JSONObject,往下一级提取
441- if (isArrayKey (key )) {//json array
451+ if (isArrayKey (key )) {//APIJSON Array
442452 result = getArray (path , config , key , (JSONObject ) value );
443- } else {//json object
453+ } else {//APIJSON Object
444454 result = getObject (path , isFirst == false || nameIsNumber == false //[]里第一个不能为[]
445455 ? null : config , key , (JSONObject ) value );
446456 isFirst = false ;
@@ -449,6 +459,60 @@ && isInRelationMap(path) == false) {
449459 if (result != null && result .isEmpty () == false ) {//只添加!=null的值,可能数据库返回数据不够count
450460 transferredRequest .put (key , result );
451461 }
462+ } else if (requestMethod == RequestMethod .PUT && JSON .isJSONArray (value )) {//PUT JSONArray
463+ JSONArray array = ((JSONArray ) value );
464+ if (array != null && array .isEmpty () == false && isTableKey (name )) {
465+ int putType = 0 ;
466+ if (key .endsWith ("+" )) {//add
467+ putType = 1 ;
468+ } else if (key .endsWith ("-" )) {//remove
469+ putType = 2 ;
470+ } else {//replace
471+ throw new IllegalAccessException ("PUT " + path + ", PUT Array不允许 " + key + " 这种没有 + 或 - 结尾的key!不允许整个替换掉原来的Array!" );
472+ }
473+ String realKey = getRealKey (requestMethod , key , false );
474+
475+ //GET > add all 或 remove all > PUT > remove key
476+
477+ //GET <<<<<<<<<<<<<<<<<<<<<<<<<
478+ JSONObject arrayRequest = new JSONObject ();
479+ arrayRequest .put (Table .ID , request .get (Table .ID ));
480+ // arrayRequest.setColumns(realKey);//put请求会对id添加功能符?
481+ arrayRequest .put (JSONRequest .KEY_COLUMNS , realKey );
482+ JSONRequest getRequest = new JSONRequest (name , arrayRequest );
483+ JSONObject response = new RequestParser ().parseResponse (getRequest );
484+ //GET >>>>>>>>>>>>>>>>>>>>>>>>>
485+
486+
487+ //add all 或 remove all <<<<<<<<<<<<<<<<<<<<<<<<<
488+ response = response == null ? null : response .getJSONObject (name );
489+ JSONArray targetArray = response == null ? null : response .getJSONArray (realKey );
490+ if (targetArray == null ) {
491+ targetArray = new JSONArray ();
492+ }
493+ for (Object obj : array ) {
494+ if (obj == null ) {
495+ continue ;
496+ }
497+ if (putType == 1 ) {
498+ if (targetArray .contains (obj )) {
499+ throw new ConflictException ("PUT " + path + ", " + realKey + ":" + obj + " 已存在!" );
500+ }
501+ targetArray .add (obj );
502+ } else if (putType == 2 ) {
503+ if (targetArray .contains (obj ) == false ) {
504+ throw new NullPointerException ("PUT " + path + ", " + realKey + ":" + obj + " 不存在!" );
505+ }
506+ targetArray .remove (obj );
507+ }
508+ }
509+
510+ //add all 或 remove all >>>>>>>>>>>>>>>>>>>>>>>>>
511+
512+ //PUT <<<<<<<<<<<<<<<<<<<<<<<<<
513+ transferredRequest .put (realKey , targetArray );
514+ //PUT >>>>>>>>>>>>>>>>>>>>>>>>>
515+ }
452516 } else {//JSONArray或其它Object,直接填充
453517 transferredRequest .put (key , value );
454518 if (key .startsWith ("@" ) && QueryConfig .keyList .contains (key ) == false ) {
@@ -464,7 +528,7 @@ && isInRelationMap(path) == false) {
464528 throw new IllegalArgumentException ("\" key@\" : 后面必须为依赖路径String!" );
465529 }
466530 System .out .println ("getObject StringUtil.isPath(value) >> parseRelation = " + parseRelation );
467- String replaceKey = getRealKey (key , false );
531+ String replaceKey = getRealKey (requestMethod , key , false );
468532 if (parseRelation ) {
469533 transferredRequest .put (replaceKey , getValueByPath (relationMap .get (getPath (path , replaceKey ))));
470534 // relationMap.remove(path + SEPARATOR + key);
@@ -479,7 +543,7 @@ && isInRelationMap(path) == false) {
479543 }
480544 }
481545
482- if (containRelation == false && isTableKey (name )) {//提高性能 isObjectKey(name)) {
546+ if (containRelation == false && isTableKey (name )) {//提高性能
483547 if (parseRelation == false || isInRelationMap (path )) {//避免覆盖原来已经获取的
484548 // relationMap.remove(path);
485549 QueryConfig config2 = newQueryConfig (name , transferredRequest );
@@ -489,7 +553,7 @@ && isInRelationMap(path) == false) {
489553 .setPosition (parentConfig .getPosition ());//避免position > 0的object获取不到
490554 }
491555
492- transferredRequest = getSQLObject (config2 );//不管用:暂时用这个解决返回多余空数据
556+ transferredRequest = getSQLObject (config2 );
493557 //
494558 // JSONObject result = getSQLObject(config2);
495559 // if (result != null && result.isEmpty() == false) {//解决获取失败导致不能获取里面JSONObject
@@ -502,16 +566,8 @@ && isInRelationMap(path) == false) {
502566 Set <String > functionSet = functionMap .keySet ();
503567 if (functionSet != null && functionSet .isEmpty () == false ) {
504568 for (String key : functionSet ) {
505- // try {
506- transferredRequest .put (getRealKey (key , false )
569+ transferredRequest .put (getRealKey (requestMethod , key , false )
507570 , Function .invoke (transferredRequest , functionMap .get (key )));
508- // } catch (Exception e) {
509- // Log.e(TAG, "getObject containRelation == false && isTableKey(name)"
510- // + " >> transferredRequest.put(getRealKey(key, false),"
511- // + " Function.invoke(transferredRequest, functionMap.get(key)));"
512- // + " >> } catch (Exception e) {");
513- // e.printStackTrace();
514- // }
515571 }
516572 }
517573
@@ -646,11 +702,12 @@ private JSONObject getArray(String parentPath, QueryConfig parentConfig, String
646702
647703 System .out .println (TAG + "getArray return " + JSON .toJSONString (transferredRequest ) + "\n >>>>>>>>>>>>>>>\n \n \n " );
648704
705+ //可能部分情况下还是会返回,应该在getObject内解析了relation后 relationMap.remove(path + SEPARATOR + key);
649706 if (parseRelation == false && isInRelationMap (path ) == false ) {//parseRelation == true时不会添加进去
650707 transferredRequest .remove (JSONRequest .KEY_PAGE );
651708 transferredRequest .remove (JSONRequest .KEY_COUNT );
652709 }
653-
710+
654711 return transferredRequest ;
655712 }
656713
@@ -865,7 +922,6 @@ public static boolean isWord(String key) {
865922 */
866923
867924 /**获取客户端实际需要的key
868- * #作为方法引用符号,()作为包含关系?% & | {} [] <> < 这些呢?
869925 * <br> "userId@":"/User/id" //@根据路径依赖,@始终在最后。value是'/'分隔的字符串。
870926 * <br> "isPraised()":"isContain(Collection:idList,long:id)" //()使用方法,value是方法表达式。不能与@并用。
871927 * <br> "content$":"%searchKey%" //$搜索,右边紧跟key。value是搜索表达式。
@@ -874,7 +930,7 @@ public static boolean isWord(String key) {
874930 * @param key
875931 * @return
876932 */
877- public static String getRealKey (String originKey , boolean isTableKey ) throws Exception {
933+ public static String getRealKey (RequestMethod method , String originKey , boolean isTableKey ) throws Exception {
878934 Log .i (TAG , "getRealKey originKey = " + originKey );
879935 if (originKey == null || isArrayKey (originKey )) {
880936 Log .w (TAG , "getRealKey originKey == null || isArrayKey(originKey) >> return originKey;" );
@@ -890,8 +946,17 @@ public static String getRealKey(String originKey, boolean isTableKey) throws Exc
890946 key = key .substring (0 , key .lastIndexOf ("()" ));
891947 } else if (key .endsWith ("@" )) {//引用,引用对象查询完后处理。fillTarget中暂时不用处理,因为非GET请求都是由给定的id确定,不需要引用
892948 key = key .substring (0 , key .lastIndexOf ("@" ));
949+ } else if (key .endsWith ("+" )) {//延长,PUT查询时处理
950+ if (method == RequestMethod .PUT ) {//不为PUT就抛异常
951+ key = key .substring (0 , key .lastIndexOf ("+" ));
952+ }
953+ } else if (key .endsWith ("-" )) {//缩减,PUT查询时处理
954+ if (method == RequestMethod .PUT ) {//不为PUT就抛异常
955+ key = key .substring (0 , key .lastIndexOf ("-" ));
956+ }
893957 }
894958
959+
895960 //"User:toUser":User转换"toUser":User, User为查询同名Table得到的JSONObject。交给客户端处理更好?不,查询就得截取
896961 if (isTableKey ) {//不允许在column key中使用Type:key形式
897962 key = TypeValueKeyEntry .parseKeyEntry (key ).getKey ();
0 commit comments