Skip to content

Commit

Permalink
完善同一个请求内多种不同操作的关键词,新增支持 @post: "User", @gets: { "Privacy": "Privacy-p…
Browse files Browse the repository at this point in the history
…hone" } 等简化写法
  • Loading branch information
TommyLemon committed Sep 2, 2023
1 parent d4be7ce commit eccf252
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 86 deletions.
28 changes: 27 additions & 1 deletion APIJSONORM/src/main/java/apijson/JSONObject.java
Expand Up @@ -6,6 +6,7 @@
package apijson;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -151,7 +152,16 @@ public JSONObject setUserIdIn(List<Object> list) {
public static final String KEY_ORDER = "@order"; //排序方式
public static final String KEY_RAW = "@raw"; // 自定义原始 SQL 片段
public static final String KEY_JSON = "@json"; //SQL Server 把字段转为 JSON 输出
public static final String KEY_METHOD = "@method"; //json对象配置操作方法
public static final String KEY_METHOD = "@method"; // json 对象配置操作方法
public static final String KEY_GET = "@get"; // json 对象配置操作方法
public static final String KEY_GETS = "@gets"; // json 对象配置操作方法
public static final String KEY_HEAD = "@head"; // json 对象配置操作方法
public static final String KEY_HEADS = "@heads"; // json 对象配置操作方法
public static final String KEY_POST = "@post"; // json 对象配置操作方法
public static final String KEY_PUT = "@put"; // json 对象配置操作方法
public static final String KEY_DELETE = "@delete"; // json 对象配置操作方法

public static final Map<String, RequestMethod> KEY_METHOD_ENUM_MAP;

public static final List<String> TABLE_KEY_LIST;
static {
Expand All @@ -174,6 +184,22 @@ public JSONObject setUserIdIn(List<Object> list) {
TABLE_KEY_LIST.add(KEY_RAW);
TABLE_KEY_LIST.add(KEY_JSON);
TABLE_KEY_LIST.add(KEY_METHOD);
TABLE_KEY_LIST.add(KEY_GET);
TABLE_KEY_LIST.add(KEY_GETS);
TABLE_KEY_LIST.add(KEY_HEAD);
TABLE_KEY_LIST.add(KEY_HEADS);
TABLE_KEY_LIST.add(KEY_POST);
TABLE_KEY_LIST.add(KEY_PUT);
TABLE_KEY_LIST.add(KEY_DELETE);

KEY_METHOD_ENUM_MAP = new LinkedHashMap<>();
KEY_METHOD_ENUM_MAP.put(KEY_GET, RequestMethod.GET);
KEY_METHOD_ENUM_MAP.put(KEY_GETS, RequestMethod.GETS);
KEY_METHOD_ENUM_MAP.put(KEY_HEAD, RequestMethod.HEAD);
KEY_METHOD_ENUM_MAP.put(KEY_HEADS, RequestMethod.HEADS);
KEY_METHOD_ENUM_MAP.put(KEY_POST, RequestMethod.POST);
KEY_METHOD_ENUM_MAP.put(KEY_PUT, RequestMethod.PUT);
KEY_METHOD_ENUM_MAP.put(KEY_DELETE, RequestMethod.DELETE);
}

//@key关键字都放这个类 >>>>>>>>>>>>>>>>>>>>>>
Expand Down
20 changes: 13 additions & 7 deletions APIJSONORM/src/main/java/apijson/RequestMethod.java
Expand Up @@ -5,6 +5,9 @@

package apijson;

import java.util.Arrays;
import java.util.List;

/**请求方法,对应org.springframework.web.bind.annotation.RequestMethod,多出GETS,HEADS方法
* @author Lemon
*/
Expand Down Expand Up @@ -41,17 +44,20 @@ public enum RequestMethod {
PUT,

/**
* json包含多条语句,支持增删改查,函数调用
* 删除数据
*/
CRUD,
DELETE,

/**
* 删除数据
* json 包含多条语句,支持增删改查、函数调用
*/
DELETE;

public static final RequestMethod[] ALL = new RequestMethod[]{ GET, HEAD, GETS, HEADS, POST, PUT, CRUD, DELETE};
CRUD;

public static final RequestMethod[] ALL = new RequestMethod[]{ GET, HEAD, GETS, HEADS, POST, PUT, DELETE, CRUD };
public static final List<String> ALL_NAME_LIST = Arrays.asList(
GET.name(), HEAD.name(), GETS.name(), HEADS.name(), POST.name(), PUT.name(), DELETE.name(), CRUD.name()
);

/**是否为GET请求方法
* @param method
* @param containPrivate 包含私密(非明文)获取方法GETS
Expand Down
144 changes: 86 additions & 58 deletions APIJSONORM/src/main/java/apijson/orm/AbstractParser.java
Expand Up @@ -410,11 +410,12 @@ public JSONObject parseResponse(JSONObject request) {
requestObject = request;
try {
setVersion(requestObject.getIntValue(JSONRequest.KEY_VERSION));
requestObject.remove(JSONRequest.KEY_VERSION);

if (getMethod() != RequestMethod.CRUD) {
setTag(requestObject.getString(JSONRequest.KEY_TAG));
requestObject.remove(JSONRequest.KEY_TAG);
}
requestObject.remove(JSONRequest.KEY_VERSION);
} catch (Exception e) {
return extendErrorResult(requestObject, e, requestMethod, getRequestURL(), isRoot);
}
Expand Down Expand Up @@ -2089,7 +2090,7 @@ protected JSONObject getRequestStructure(RequestMethod method, String tag, int v
}

protected JSONObject batchVerify(RequestMethod method, String tag, int version, String name, @NotNull JSONObject request, int maxUpdateCount, SQLCreator creator) throws Exception {
JSONObject jsonObject = new JSONObject(true);
JSONObject correctRequest = new JSONObject(true);
List<String> removeTmpKeys = new ArrayList<>(); // 请求json里面的临时变量,不需要带入后面的业务中,比如 @post、@get等

Set<String> reqSet = request == null ? null : request.keySet();
Expand All @@ -2098,49 +2099,82 @@ protected JSONObject batchVerify(RequestMethod method, String tag, int version,
}

for (String key : reqSet) {
// key重复直接抛错(xxx:alias, xxx:alias[])
if (jsonObject.containsKey(key) || jsonObject.containsKey(key + apijson.JSONObject.KEY_ARRAY)) {
throw new IllegalArgumentException("对象名重复,请添加别名区分 ! ,重复对象名为: " + key);
// key 重复直接抛错(xxx:alias, xxx:alias[])
if (correctRequest.containsKey(key) || correctRequest.containsKey(key + apijson.JSONObject.KEY_ARRAY)) {
throw new IllegalArgumentException("对象名重复,请添加别名区分 ! 重复对象名为: " + key);
}

// @post、@get等RequestMethod
// @post、@get 等 RequestMethod
try {
if (key.startsWith("@") && getEnum(RequestMethod.class, key.substring(1).toUpperCase(), null) != null) {
RequestMethod keyMethod = apijson.orm.JSONRequest.KEY_METHOD_ENUM_MAP.get(key);
if (keyMethod != null) {
// 如果不匹配,异常不处理即可
RequestMethod _method = RequestMethod.valueOf(key.substring(1).toUpperCase());
removeTmpKeys.add(key);

JSONObject obj = request.getJSONObject(key);
Set<String> set = obj == null ? new HashSet<>() : obj.keySet();
Object val = request.get(key);
JSONObject obj = val instanceof JSONObject ? request.getJSONObject(key) : null;
if (obj == null) {
if (val instanceof String) {
String[] tbls = StringUtil.split((String) val);
if (tbls != null && tbls.length > 0) {
obj = new JSONObject(true);
for (int i = 0; i < tbls.length; i++) {
String tbl = tbls[i];
if (obj.containsKey(tbl)) {
throw new ConflictException(key + ": value 中 " + tbl + " 已经存在,不能重复!");
}
obj.put(tbl, new JSONObject(true));
}
}
}
else {
throw new IllegalArgumentException(key + ": value 中 value 类型错误,只能是 String 或 JSONObject {} !");
}
}

Set<Entry<String, Object>> set = obj == null ? new HashSet<>() : obj.entrySet();

for (String objKey : set) {
for (Entry<String, Object> objEntry : set) {
String objKey = objEntry == null ? null : objEntry.getKey();
if (objKey == null) {
continue;
}

Map<String, Object> objAttrMap = new HashMap<>();
objAttrMap.put(apijson.JSONObject.KEY_METHOD, _method);
objAttrMap.put(apijson.JSONObject.KEY_METHOD, keyMethod);
keyObjectAttributesMap.put(objKey, objAttrMap);
JSONObject objAttrJson = obj.getJSONObject(objKey);
Set<Entry<String, Object>> objSet = objAttrJson == null ? new HashSet<>() : objAttrJson.entrySet();

for (Entry<String, Object> entry : objSet) {
String objAttrKey = entry == null ? null : entry.getKey();
if (objAttrKey == null) {
continue;
Object objVal = objEntry.getValue();
JSONObject objAttrJson = objVal instanceof JSONObject ? obj.getJSONObject(objKey) : null;
if (objAttrJson == null) {
if (objVal instanceof String) {
objAttrMap.put(JSONRequest.KEY_TAG, objVal);
}
else {
throw new IllegalArgumentException(key + ": { " + objKey + ": value 中 value 类型错误,只能是 String 或 JSONObject {} !");
}
}
else {
Set<Entry<String, Object>> objSet = objAttrJson == null ? new HashSet<>() : objAttrJson.entrySet();

switch (objAttrKey) {
case apijson.JSONObject.KEY_DATASOURCE:
case apijson.JSONObject.KEY_SCHEMA:
case apijson.JSONObject.KEY_DATABASE:
case JSONRequest.KEY_VERSION:
case apijson.JSONObject.KEY_ROLE:
case JSONRequest.KEY_TAG:
objAttrMap.put(objAttrKey, entry.getValue());
break;
default:
break;
for (Entry<String, Object> entry : objSet) {
String objAttrKey = entry == null ? null : entry.getKey();
if (objAttrKey == null) {
continue;
}

switch (objAttrKey) {
case apijson.JSONObject.KEY_DATASOURCE:
case apijson.JSONObject.KEY_SCHEMA:
case apijson.JSONObject.KEY_DATABASE:
case JSONRequest.KEY_VERSION:
case apijson.JSONObject.KEY_ROLE:
case JSONRequest.KEY_TAG:
objAttrMap.put(objAttrKey, entry.getValue());
break;
default:
break;
}
}
}
}
Expand Down Expand Up @@ -2189,15 +2223,17 @@ protected JSONObject batchVerify(RequestMethod method, String tag, int version,
}

if (key.startsWith("@") || key.endsWith("@")) {
jsonObject.put(key, obj);
correctRequest.put(key, obj);
continue;
}

if (obj instanceof JSONObject || obj instanceof JSONArray) {
RequestMethod _method = null;
RequestMethod _method;
if (obj instanceof JSONObject) {
_method = RequestMethod.valueOf(request.getJSONObject(key).getString(apijson.JSONObject.KEY_METHOD).toUpperCase());
String combine = request.getJSONObject(key).getString(KEY_COMBINE);
JSONObject tblObj = request.getJSONObject(key);
String mn = tblObj == null ? null : tblObj.getString(apijson.JSONObject.KEY_METHOD);
_method = mn == null ? null : RequestMethod.valueOf(mn);
String combine = _method == null ? null : tblObj.getString(KEY_COMBINE);
if (combine != null && RequestMethod.isPublicMethod(_method) == false) {
throw new IllegalArgumentException(key + ":{} 里的 @combine:value 不合法!开放请求 GET、HEAD 才允许传 @combine:value !");
}
Expand All @@ -2207,22 +2243,14 @@ protected JSONObject batchVerify(RequestMethod method, String tag, int version,
if (attrMap == null) {
if (method == RequestMethod.CRUD) {
_method = GET;
if (attrMap == null) {
Map<String, Object> objAttrMap = new HashMap<>();
objAttrMap.put(apijson.JSONObject.KEY_METHOD, GET);
keyObjectAttributesMap.put(key, objAttrMap);
} else {
attrMap.put(apijson.JSONObject.KEY_METHOD, GET);
}
Map<String, Object> objAttrMap = new HashMap<>();
objAttrMap.put(apijson.JSONObject.KEY_METHOD, GET);
keyObjectAttributesMap.put(key, objAttrMap);
} else {
_method = method;
if (attrMap == null) {
Map<String, Object> objAttrMap = new HashMap<>();
objAttrMap.put(apijson.JSONObject.KEY_METHOD, method);
keyObjectAttributesMap.put(key, objAttrMap);
} else {
attrMap.put(apijson.JSONObject.KEY_METHOD, method);
}
Map<String, Object> objAttrMap = new HashMap<>();
objAttrMap.put(apijson.JSONObject.KEY_METHOD, method);
keyObjectAttributesMap.put(key, objAttrMap);
}
} else {
_method = (RequestMethod) attrMap.get(apijson.JSONObject.KEY_METHOD);
Expand All @@ -2236,42 +2264,42 @@ protected JSONObject batchVerify(RequestMethod method, String tag, int version,

// get请求不校验
if (RequestMethod.isPublicMethod(_method)) {
jsonObject.put(key, obj);
correctRequest.put(key, obj);
continue;
}

if(tag != null && !tag.contains(":")) {
if (tag != null && ! tag.contains(":")) {
JSONObject object = getRequestStructure(_method, tag, version);
JSONObject ret = objectVerify(_method, tag, version, name, request, maxUpdateCount, creator, object);
jsonObject.putAll(ret);
correctRequest.putAll(ret);
break;
}

String _tag = buildTag(request, key, method, tag);
JSONObject object = getRequestStructure(_method, _tag, version);
if(method == RequestMethod.CRUD && StringUtil.isEmpty(tag, true)) {
if (method == RequestMethod.CRUD && StringUtil.isEmpty(tag, true)) {
JSONObject requestItem = new JSONObject();
requestItem.put(key, obj);
JSONObject ret = objectVerify(_method, _tag, version, name, requestItem, maxUpdateCount, creator, object);
jsonObject.put(key, ret.get(key));
correctRequest.put(key, ret.get(key));
} else {
return objectVerify(_method, _tag, version, name, request, maxUpdateCount, creator, object);
}
} else {
jsonObject.put(key, obj);
correctRequest.put(key, obj);
}
} catch (Exception e) {
e.printStackTrace();
throw new Exception(e);
}
}

// 这里是requestObject ref request 的引用, 删除不需要的临时变量
// 这里是 requestObject ref request 的引用, 删除不需要的临时变量
for (String removeKey : removeTmpKeys) {
request.remove(removeKey);
}

return jsonObject;
return correctRequest;
}

public static <E extends Enum<E>> E getEnum(final Class<E> enumClass, final String enumName, final E defaultEnum) {
Expand All @@ -2284,7 +2312,7 @@ public static <E extends Enum<E>> E getEnum(final Class<E> enumClass, final Stri
return defaultEnum;
}
}

protected void setRequestAttribute(String key, boolean isArray, String attrKey, @NotNull JSONObject request) {
Map<String, Object> attrMap = keyObjectAttributesMap.get(isArray ? key + apijson.JSONObject.KEY_ARRAY : key);
Object attrVal = attrMap == null ? null : attrMap.get(attrKey);
Expand All @@ -2308,7 +2336,7 @@ protected String buildTag(JSONObject request, String key, RequestMethod method,
}
return tag;
}


protected JSONObject objectVerify(RequestMethod method, String tag, int version, String name, @NotNull JSONObject request
, int maxUpdateCount, SQLCreator creator, JSONObject object) throws Exception {
Expand All @@ -2317,7 +2345,7 @@ protected JSONObject objectVerify(RequestMethod method, String tag, int version,
// JSONObject clone 浅拷贝没用,Structure.parse 会导致 structure 里面被清空,第二次从缓存里取到的就是 {}
return getVerifier().verifyRequest(method, name, target, request, maxUpdateCount, getGlobalDatabase(), getGlobalSchema(), creator);
}

/***
* 兼容url crud, 获取真实method
* @param method = crud
Expand Down

0 comments on commit eccf252

Please sign in to comment.