Skip to content

Commit 65a262b

Browse files
committed
Server:新增PUT请求的key+,key-
1 parent 3718230 commit 65a262b

File tree

4 files changed

+142
-24
lines changed

4 files changed

+142
-24
lines changed

APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/JSON.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,5 +208,48 @@ public static String format(JSONObject object) {
208208
return toJSONString(object, SerializerFeature.PrettyFormat);
209209
}
210210

211+
/**判断是否为JSONObject
212+
* @param obj instanceof String ? parseObject
213+
* @return
214+
*/
215+
public static boolean isJSONObject(Object obj) {
216+
if (obj instanceof JSONObject) {
217+
return true;
218+
}
219+
if (obj instanceof String) {
220+
try {
221+
JSONObject json = parseObject((String) obj);
222+
return json != null && json.isEmpty() == false;
223+
} catch (Exception e) {
224+
//太长 System.out.println(TAG + "select while (rs.next()){ >> i = "
225+
// + i + " try { json = JSON.parse((String) value);"
226+
// + ">> } catch (Exception e) {\n" + e.getMessage());
227+
}
228+
}
229+
230+
return false;
231+
}
232+
/**判断是否为JSONArray
233+
* @param obj instanceof String ? parseArray
234+
* @return
235+
*/
236+
public static boolean isJSONArray(Object obj) {
237+
if (obj instanceof JSONArray) {
238+
return true;
239+
}
240+
if (obj instanceof String) {
241+
try {
242+
JSONArray json = parseArray((String) obj);
243+
return json != null && json.isEmpty() == false;
244+
} catch (Exception e) {
245+
//太长 System.out.println(TAG + "select while (rs.next()){ >> i = "
246+
// + i + " try { json = JSON.parse((String) value);"
247+
// + ">> } catch (Exception e) {\n" + e.getMessage());
248+
}
249+
}
250+
251+
return false;
252+
}
253+
211254

212255
}

APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/server/QueryConfig.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,17 @@ public static String getLimitString(int page, int count) {
202202

203203
/**获取筛选方法
204204
* @return
205+
* @throws Exception
205206
*/
206-
public String getWhereString() {
207+
public String getWhereString() throws Exception {
207208
return getWhereString(getMethod(), getWhere());
208209
}
209210
/**获取筛选方法
210211
* @param where
211212
* @return
213+
* @throws Exception
212214
*/
213-
public static String getWhereString(RequestMethod method, Map<String, Object> where) {
215+
public static String getWhereString(RequestMethod method, Map<String, Object> where) throws Exception {
214216
Set<String> set = where == null ? null : where.keySet();
215217
if (set != null && set.size() > 0) {
216218
if (RequestParser.isGetMethod(method) == false && method != RequestMethod.POST_GET
@@ -224,7 +226,7 @@ public static String getWhereString(RequestMethod method, Map<String, Object> wh
224226
for (String key : set) {
225227
Log.d(TAG, "getWhereString key = " + key);
226228
//避免筛选到全部 value = key == null ? null : where.get(key);
227-
if (key == null || key.startsWith("@") || key.endsWith("()")) {//关键字||方法
229+
if (key == null || key.startsWith("@") || key.endsWith("()")) {//关键字||方法, +或-直接报错
228230
Log.d(TAG, "getWhereString key == null || key.startsWith(@) || key.endsWith(()) >> continue;");
229231
continue;
230232
}
@@ -241,7 +243,7 @@ public static String getWhereString(RequestMethod method, Map<String, Object> wh
241243
value = where.get(key);
242244

243245
try {
244-
key = RequestParser.getRealKey(key, false);
246+
key = RequestParser.getRealKey(method, key, false);
245247
} catch (Exception e) {
246248
Log.e(TAG, "getObject getWhereString try { key = RequestParser.getRealKey(key, false);"
247249
+ " >> } catch (Exception e) {");
@@ -377,15 +379,17 @@ public static synchronized QueryConfig newQueryConfig(RequestMethod method, Stri
377379

378380
/**
379381
* @return
382+
* @throws Exception
380383
*/
381-
public String getSQL() {
384+
public String getSQL() throws Exception {
382385
return getSQL(this);
383386
}
384387
/**
385388
* @param config
386389
* @return
390+
* @throws Exception
387391
*/
388-
public static String getSQL(QueryConfig config) {
392+
public static String getSQL(QueryConfig config) throws Exception {
389393
if (config == null) {
390394
System.out.println("QueryConfig: getSQL config == null >> return null;");
391395
return null;

APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/server/RequestParser.java

Lines changed: 82 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.io.UnsupportedEncodingException;
1919
import java.net.URLDecoder;
20+
import java.rmi.AlreadyBoundException;
2021
import java.util.ArrayList;
2122
import java.util.HashMap;
2223
import java.util.LinkedHashMap;
@@ -25,6 +26,9 @@
2526
import java.util.Set;
2627
import java.util.regex.Pattern;
2728

29+
import javax.management.OperationsException;
30+
31+
import com.alibaba.fastjson.JSONArray;
2832
import com.alibaba.fastjson.JSONObject;
2933

3034
import 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();

APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/server/sql/QueryHelper2.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,13 @@ public JSONObject select(QueryConfig config) {
9595
System.out.println(TAG + "select config==null||StringUtil.isNotEmpty(config.getTable(), true)==false>>return null;");
9696
return null;
9797
}
98-
final String sql = config.getSQL();
98+
String sql = null;
99+
try {
100+
sql = config.getSQL();
101+
} catch (Exception e1) {
102+
// TODO Auto-generated catch block
103+
e1.printStackTrace();
104+
}
99105
final int position = config.getPosition();
100106

101107
if (cacheMap == null) {

0 commit comments

Comments
 (0)