| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent f20e3cf commit ec1509e
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -39,19 +39,19 @@ public class DemoFunctionParser extends APIJSONFunctionParser { | |||
| 39 | 39 | public static final String TAG = "DemoFunctionParser"; | |
| 40 | 40 | ||
| 41 | 41 | public DemoFunctionParser() { | |
| 42 | - this(null, null, 0, null); | ||
| 42 | + this(null, null, 0, null, null); | ||
| 43 | 43 | } | |
| 44 | - public DemoFunctionParser(RequestMethod method, String tag, int version, HttpSession session) { | ||
| 45 | - super(method, tag, version, session); | ||
| 44 | + public DemoFunctionParser(RequestMethod method, String tag, int version, JSONObject request, HttpSession session) { | ||
| 45 | + super(method, tag, version, request, session); | ||
| 46 | 46 | } | |
| 47 | 47 | ||
| 48 | 48 | /** | |
| 49 | - * @param request | ||
| 49 | + * @param current | ||
| 50 | 50 | * @return | |
| 51 | 51 | * @throws Exception | |
| 52 | 52 | */ | |
| 53 | - public Object verifyIdList(@NotNull JSONObject request, @NotNull String idList) throws Exception { | ||
| 54 | - Object obj = request.get(idList); | ||
| 53 | + public Object verifyIdList(@NotNull JSONObject current, @NotNull String idList) throws Exception { | ||
| 54 | + Object obj = current.get(idList); | ||
| 55 | 55 | if (obj instanceof Collection == false) { | |
| 56 | 56 | throw new IllegalArgumentException(idList + " 不符合 Array 类型! 结构必须是 [] !"); | |
| 57 | 57 | } | |
@@ -72,8 +72,8 @@ public Object verifyIdList(@NotNull JSONObject request, @NotNull String idList) | |||
| 72 | 72 | * @return | |
| 73 | 73 | * @throws Exception | |
| 74 | 74 | */ | |
| 75 | - public Object verifyURLList(@NotNull JSONObject request, @NotNull String urlList) throws Exception { | ||
| 76 | - Object obj = request.get(urlList); | ||
| 75 | + public Object verifyURLList(@NotNull JSONObject current, @NotNull String urlList) throws Exception { | ||
| 76 | + Object obj = current.get(urlList); | ||
| 77 | 77 | if (obj instanceof Collection == false) { | |
| 78 | 78 | throw new IllegalArgumentException(urlList + " 不符合 Array 类型! 结构必须是 [] !"); | |
| 79 | 79 | } | |
@@ -95,9 +95,9 @@ public Object verifyURLList(@NotNull JSONObject request, @NotNull String urlList | |||
| 95 | 95 | * @return | |
| 96 | 96 | * @throws Exception | |
| 97 | 97 | */ | |
| 98 | - public int deleteCommentOfMoment(@NotNull JSONObject rq, @NotNull String momentId) throws Exception { | ||
| 99 | - long mid = rq.getLongValue(momentId); | ||
| 100 | - if (mid <= 0 || rq.getIntValue(JSONResponse.KEY_COUNT) <= 0) { | ||
| 98 | + public int deleteCommentOfMoment(@NotNull JSONObject current, @NotNull String momentId) throws Exception { | ||
| 99 | + long mid = current.getLongValue(momentId); | ||
| 100 | + if (mid <= 0 || current.getIntValue(JSONResponse.KEY_COUNT) <= 0) { | ||
| 101 | 101 | return 0; | |
| 102 | 102 | } | |
| 103 | 103 | ||
@@ -122,9 +122,9 @@ public int deleteCommentOfMoment(@NotNull JSONObject rq, @NotNull String momentI | |||
| 122 | 122 | * @param toId | |
| 123 | 123 | * @return | |
| 124 | 124 | */ | |
| 125 | - public int deleteChildComment(@NotNull JSONObject rq, @NotNull String toId) throws Exception { | ||
| 126 | - long tid = rq.getLongValue(toId); | ||
| 127 | - if (tid <= 0 || rq.getIntValue(JSONResponse.KEY_COUNT) <= 0) { | ||
| 125 | + public int deleteChildComment(@NotNull JSONObject current, @NotNull String toId) throws Exception { | ||
| 126 | + long tid = current.getLongValue(toId); | ||
| 127 | + if (tid <= 0 || current.getIntValue(JSONResponse.KEY_COUNT) <= 0) { | ||
| 128 | 128 | return 0; | |
| 129 | 129 | } | |
| 130 | 130 | ||
@@ -190,7 +190,7 @@ private JSONArray getChildCommentIdList(long tid) { | |||
| 190 | 190 | * @return JSONArray 只能用JSONArray,用long[]会在SQLConfig解析崩溃 | |
| 191 | 191 | * @throws Exception | |
| 192 | 192 | */ | |
| 193 | - public JSONArray getIdList(@NotNull JSONObject request) { | ||
| 193 | + public JSONArray getIdList(@NotNull JSONObject current) { | ||
| 194 | 194 | return new JSONArray(new ArrayList<Object>(Arrays.asList(12, 15, 301, 82001, 82002, 38710))); | |
| 195 | 195 | } | |
| 196 | 196 | ||
@@ -200,9 +200,9 @@ public JSONArray getIdList(@NotNull JSONObject request) { | |||
| 200 | 200 | * @return | |
| 201 | 201 | * @throws Exception | |
| 202 | 202 | */ | |
| 203 | - public Object verifyAccess(@NotNull JSONObject request) throws Exception { | ||
| 204 | - long userId = request.getLongValue(JSONRequest.KEY_USER_ID); | ||
| 205 | - RequestRole role = RequestRole.get(request.getString(JSONRequest.KEY_ROLE)); | ||
| 203 | + public Object verifyAccess(@NotNull JSONObject current) throws Exception { | ||
| 204 | + long userId = current.getLongValue(JSONRequest.KEY_USER_ID); | ||
| 205 | + RequestRole role = RequestRole.get(current.getString(JSONRequest.KEY_ROLE)); | ||
| 206 | 206 | if (role == RequestRole.OWNER && userId != DemoVerifier.getVisitorId(getSession())) { | |
| 207 | 207 | throw new IllegalAccessException("登录用户与角色OWNER不匹配!"); | |
| 208 | 208 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -49,10 +49,10 @@ public APIJSONFunctionParser() { | |||
| 49 | 49 | this(null); | |
| 50 | 50 | } | |
| 51 | 51 | public APIJSONFunctionParser(HttpSession session) { | |
| 52 | - this(null, null, 0, session); | ||
| 52 | + this(null, null, 0, null, session); | ||
| 53 | 53 | } | |
| 54 | - public APIJSONFunctionParser(RequestMethod method, String tag, int version, HttpSession session) { | ||
| 55 | - super(method, tag, version); | ||
| 54 | + public APIJSONFunctionParser(RequestMethod method, String tag, int version, JSONObject request, HttpSession session) { | ||
| 55 | + super(method, tag, version, request); | ||
| 56 | 56 | setSession(session); | |
| 57 | 57 | } | |
| 58 | 58 | public HttpSession getSession() { | |
@@ -216,7 +216,7 @@ public static void test(APIJSONFunctionParser function) throws Exception { | |||
| 216 | 216 | request.put("object", object); | |
| 217 | 217 | ||
| 218 | 218 | if (function == null) { | |
| 219 | - function = new APIJSONFunctionParser(null, null, 1, null); | ||
| 219 | + function = new APIJSONFunctionParser(null, null, 1, null, null); | ||
| 220 | 220 | } | |
| 221 | 221 | ||
| 222 | 222 | Log.i(TAG, "plus(1,-2) = " + function.invoke("plus(i0,i1)", request)); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -112,17 +112,24 @@ public FunctionParser getFunctionParser() { | |||
| 112 | 112 | return functionParser; | |
| 113 | 113 | } | |
| 114 | 114 | @Override | |
| 115 | - public Object onFunctionParse(JSONObject json, String fun) throws Exception { | ||
| 115 | + public Object onFunctionParse(String key, String function, String parentPath, String currentName, JSONObject currentObject) throws Exception { | ||
| 116 | 116 | if (functionParser == null) { | |
| 117 | 117 | functionParser = createFunctionParser(); | |
| 118 | 118 | functionParser.setMethod(getMethod()); | |
| 119 | 119 | functionParser.setTag(getTag()); | |
| 120 | 120 | functionParser.setVersion(getVersion()); | |
| 121 | + functionParser.setRequest(requestObject); | ||
| 122 | + | ||
| 121 | 123 | if (functionParser instanceof APIJSONFunctionParser) { | |
| 122 | 124 | ((APIJSONFunctionParser) functionParser).setSession(getSession()); | |
| 123 | 125 | } | |
| 124 | 126 | } | |
| 125 | - return functionParser.invoke(fun, json); | ||
| 127 | + functionParser.setKey(key); | ||
| 128 | + functionParser.setParentPath(parentPath); | ||
| 129 | + functionParser.setCurrentName(currentName); | ||
| 130 | + functionParser.setCurrentObject(currentObject); | ||
| 131 | + | ||
| 132 | + return functionParser.invoke(function, currentObject); | ||
| 126 | 133 | } | |
| 127 | 134 | ||
| 128 | 135 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -45,13 +45,15 @@ public class AbstractFunctionParser implements FunctionParser { | |||
| 45 | 45 | private RequestMethod method; | |
| 46 | 46 | private String tag; | |
| 47 | 47 | private int version; | |
| 48 | + private JSONObject request; | ||
| 48 | 49 | public AbstractFunctionParser() { | |
| 49 | - this(null, null, 0); | ||
| 50 | + this(null, null, 0, null); | ||
| 50 | 51 | } | |
| 51 | - public AbstractFunctionParser(RequestMethod method, String tag, int version) { | ||
| 52 | + public AbstractFunctionParser(RequestMethod method, String tag, int version, @NotNull JSONObject request) { | ||
| 52 | 53 | setMethod(method == null ? RequestMethod.GET : method); | |
| 53 | 54 | setTag(tag); | |
| 54 | 55 | setVersion(version); | |
| 56 | + setRequest(request); | ||
| 55 | 57 | } | |
| 56 | 58 | ||
| 57 | 59 | @Override | |
@@ -81,6 +83,62 @@ public AbstractFunctionParser setVersion(int version) { | |||
| 81 | 83 | this.version = version; | |
| 82 | 84 | return this; | |
| 83 | 85 | } | |
| 86 | + | ||
| 87 | + private String key; | ||
| 88 | + @Override | ||
| 89 | + public String getKey() { | ||
| 90 | + return key; | ||
| 91 | + } | ||
| 92 | + @Override | ||
| 93 | + public AbstractFunctionParser setKey(String key) { | ||
| 94 | + this.key = key; | ||
| 95 | + return this; | ||
| 96 | + } | ||
| 97 | + | ||
| 98 | + private String parentPath; | ||
| 99 | + @Override | ||
| 100 | + public String getParentPath() { | ||
| 101 | + return parentPath; | ||
| 102 | + } | ||
| 103 | + @Override | ||
| 104 | + public AbstractFunctionParser setParentPath(String parentPath) { | ||
| 105 | + this.parentPath = parentPath; | ||
| 106 | + return this; | ||
| 107 | + } | ||
| 108 | + private String currentName; | ||
| 109 | + @Override | ||
| 110 | + public String getCurrentName() { | ||
| 111 | + return currentName; | ||
| 112 | + } | ||
| 113 | + @Override | ||
| 114 | + public AbstractFunctionParser setCurrentName(String currentName) { | ||
| 115 | + this.currentName = currentName; | ||
| 116 | + return this; | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + @NotNull | ||
| 120 | + @Override | ||
| 121 | + public JSONObject getRequest() { | ||
| 122 | + return request; | ||
| 123 | + } | ||
| 124 | + @Override | ||
| 125 | + public AbstractFunctionParser setRequest(@NotNull JSONObject request) { | ||
| 126 | + this.request = request; | ||
| 127 | + return this; | ||
| 128 | + } | ||
| 129 | + | ||
| 130 | + private JSONObject currentObject; | ||
| 131 | + @NotNull | ||
| 132 | + @Override | ||
| 133 | + public JSONObject getCurrentObject() { | ||
| 134 | + return currentObject; | ||
| 135 | + } | ||
| 136 | + @Override | ||
| 137 | + public AbstractFunctionParser setCurrentObject(@NotNull JSONObject currentObject) { | ||
| 138 | + this.currentObject = currentObject; | ||
| 139 | + return this; | ||
| 140 | + } | ||
| 141 | + | ||
| 84 | 142 | ||
| 85 | 143 | /**反射调用 | |
| 86 | 144 | * @param function 例如get(object,key),参数只允许引用,不能直接传值 | |
@@ -93,12 +151,12 @@ public Object invoke(@NotNull String function, @NotNull JSONObject currentObject | |||
| 93 | 151 | } | |
| 94 | 152 | ||
| 95 | 153 | /**反射调用 | |
| 96 | - * @param fun | ||
| 154 | + * @param parser | ||
| 97 | 155 | * @param request | |
| 98 | 156 | * @param function 例如get(Map:map,key),参数只允许引用,不能直接传值 | |
| 99 | 157 | * @return {@link #invoke(AbstractFunctionParser, String, Class[], Object[])} | |
| 100 | 158 | */ | |
| 101 | - public static Object invoke(@NotNull AbstractFunctionParser fun, @NotNull String function, @NotNull JSONObject currentObject) throws Exception { | ||
| 159 | + public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull String function, @NotNull JSONObject currentObject) throws Exception { | ||
| 102 | 160 | ||
| 103 | 161 | FunctionBean fb = parseFunction(function, currentObject, false); | |
| 104 | 162 | ||
@@ -108,21 +166,21 @@ public static Object invoke(@NotNull AbstractFunctionParser fun, @NotNull String | |||
| 108 | 166 | } | |
| 109 | 167 | ||
| 110 | 168 | int v = row.getIntValue("version"); | |
| 111 | - if (fun.getVersion() < v) { | ||
| 112 | - throw new UnsupportedOperationException("不允许 version = " + fun.getVersion() + " 的请求调用远程函数 " + fb.getMethod() + " ! 必须满足 version >= " + v + " !"); | ||
| 169 | + if (parser.getVersion() < v) { | ||
| 170 | + throw new UnsupportedOperationException("不允许 version = " + parser.getVersion() + " 的请求调用远程函数 " + fb.getMethod() + " ! 必须满足 version >= " + v + " !"); | ||
| 113 | 171 | } | |
| 114 | 172 | String t = row.getString("tag"); | |
| 115 | - if (t != null && t.equals(fun.getTag()) == false) { | ||
| 116 | - throw new UnsupportedOperationException("不允许 tag = " + fun.getTag() + " 的请求调用远程函数 " + fb.getMethod() + " ! 必须满足 tag = " + t + " !"); | ||
| 173 | + if (t != null && t.equals(parser.getTag()) == false) { | ||
| 174 | + throw new UnsupportedOperationException("不允许 tag = " + parser.getTag() + " 的请求调用远程函数 " + fb.getMethod() + " ! 必须满足 tag = " + t + " !"); | ||
| 117 | 175 | } | |
| 118 | 176 | String[] methods = StringUtil.split(row.getString("methods")); | |
| 119 | 177 | List<String> ml = methods == null || methods.length <= 0 ? null : Arrays.asList(methods); | |
| 120 | - if (ml != null && ml.contains(fun.getMethod().toString()) == false) { | ||
| 121 | - throw new UnsupportedOperationException("不允许 method = " + fun.getMethod() + " 的请求调用远程函数 " + fb.getMethod() + " ! 必须满足 method 在 " + methods + "内 !"); | ||
| 178 | + if (ml != null && ml.contains(parser.getMethod().toString()) == false) { | ||
| 179 | + throw new UnsupportedOperationException("不允许 method = " + parser.getMethod() + " 的请求调用远程函数 " + fb.getMethod() + " ! 必须满足 method 在 " + methods + "内 !"); | ||
| 122 | 180 | } | |
| 123 | 181 | ||
| 124 | 182 | try { | |
| 125 | - return invoke(fun, fb.getMethod(), fb.getTypes(), fb.getValues()); | ||
| 183 | + return invoke(parser, fb.getMethod(), fb.getTypes(), fb.getValues()); | ||
| 126 | 184 | } catch (Exception e) { | |
| 127 | 185 | if (e instanceof NoSuchMethodException) { | |
| 128 | 186 | throw new IllegalArgumentException("字符 " + function + " 对应的远程函数 " + getFunction(fb.getMethod(), fb.getKeys()) + " 不在后端工程的DemoFunction内!" | |
@@ -143,6 +201,16 @@ public static Object invoke(@NotNull AbstractFunctionParser fun, @NotNull String | |||
| 143 | 201 | } | |
| 144 | 202 | ||
| 145 | 203 | } | |
| 204 | + | ||
| 205 | + /**反射调用 | ||
| 206 | + * @param methodName | ||
| 207 | + * @param parameterTypes | ||
| 208 | + * @param args | ||
| 209 | + * @return | ||
| 210 | + */ | ||
| 211 | + public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull String methodName, @NotNull Class<?>[] parameterTypes, @NotNull Object[] args) throws Exception { | ||
| 212 | + return parser.getClass().getMethod(methodName, parameterTypes).invoke(parser, args); | ||
| 213 | + } | ||
| 146 | 214 | ||
| 147 | 215 | /**解析函数 | |
| 148 | 216 | * @param function | |
@@ -231,16 +299,6 @@ else if (v instanceof JSONArray) { // Collection) { | |||
| 231 | 299 | } | |
| 232 | 300 | ||
| 233 | 301 | ||
| 234 | - /**反射调用 | ||
| 235 | - * @param methodName | ||
| 236 | - * @param parameterTypes | ||
| 237 | - * @param args | ||
| 238 | - * @return | ||
| 239 | - */ | ||
| 240 | - public static Object invoke(@NotNull AbstractFunctionParser fun, @NotNull String methodName, @NotNull Class<?>[] parameterTypes, @NotNull Object[] args) throws Exception { | ||
| 241 | - return fun.getClass().getMethod(methodName, parameterTypes).invoke(fun, args); | ||
| 242 | - } | ||
| 243 | - | ||
| 244 | 302 | /** | |
| 245 | 303 | * @param method | |
| 246 | 304 | * @param keys | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -70,6 +70,7 @@ public AbstractObjectParser setParser(AbstractParser<?> parser) { | |||
| 70 | 70 | protected final List<Join> joinList; | |
| 71 | 71 | protected final boolean isTable; | |
| 72 | 72 | protected final String path; | |
| 73 | + protected final String name; | ||
| 73 | 74 | protected final String table; | |
| 74 | 75 | protected final String alias; | |
| 75 | 76 | ||
@@ -97,6 +98,7 @@ public AbstractObjectParser(@NotNull JSONObject request, String parentPath, Stri | |||
| 97 | 98 | ||
| 98 | 99 | this.type = arrayConfig == null ? 0 : arrayConfig.getType(); | |
| 99 | 100 | this.joinList = arrayConfig == null ? null : arrayConfig.getJoinList(); | |
| 101 | + this.name = name; | ||
| 100 | 102 | this.path = AbstractParser.getAbsPath(parentPath, name); | |
| 101 | 103 | ||
| 102 | 104 | apijson.orm.Entry<String, String> entry = Pair.parseEntry(name, true); | |
@@ -400,7 +402,7 @@ else if (value instanceof String) { // 引用赋值路径 | |||
| 400 | 402 | type = "-"; | |
| 401 | 403 | k = k.substring(0, k.length() - 1); | |
| 402 | 404 | ||
| 403 | - parseFunction(request, k, (String) value); | ||
| 405 | + parseFunction(k, (String) value, parentPath, name, request); | ||
| 404 | 406 | } | |
| 405 | 407 | else { | |
| 406 | 408 | if (k.endsWith("+")) { | |
@@ -742,22 +744,22 @@ public void onFunctionResponse(String type) throws Exception { | |||
| 742 | 744 | for (Entry<String, String> entry : functionSet) { | |
| 743 | 745 | ||
| 744 | 746 | // parseFunction(json, entry.getKey(), entry.getValue()); | |
| 745 | - parseFunction(response, entry.getKey(), entry.getValue()); | ||
| 747 | + parseFunction(entry.getKey(), entry.getValue(), parentPath, name, response); | ||
| 746 | 748 | } | |
| 747 | 749 | } | |
| 748 | 750 | } | |
| 749 | 751 | ||
| 750 | - public void parseFunction(JSONObject json, String key, String value) throws Exception { | ||
| 752 | + public void parseFunction(String key, String value, String parentPath, String currentName, JSONObject currentObject) throws Exception { | ||
| 751 | 753 | Object result; | |
| 752 | 754 | if (key.startsWith("@")) { //TODO 以后这种小众功能从 ORM 移出,作为一个 plugin/APIJSONProcedure | |
| 753 | - FunctionBean fb = AbstractFunctionParser.parseFunction(value, json, true); | ||
| 755 | + FunctionBean fb = AbstractFunctionParser.parseFunction(value, currentObject, true); | ||
| 754 | 756 | ||
| 755 | 757 | SQLConfig config = newSQLConfig(true); | |
| 756 | 758 | config.setProcedure(fb.toFunctionCallString(true)); | |
| 757 | 759 | result = parseResponse(config, true); | |
| 758 | 760 | } | |
| 759 | 761 | else { | |
| 760 | - result = parser.onFunctionParse(json, value); | ||
| 762 | + result = parser.onFunctionParse(key, value, parentPath, currentName, currentObject); | ||
| 761 | 763 | } | |
| 762 | 764 | ||
| 763 | 765 | if (result != null) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,21 +16,44 @@ | |||
| 16 | 16 | ||
| 17 | 17 | import com.alibaba.fastjson.JSONObject; | |
| 18 | 18 | ||
| 19 | + import apijson.NotNull; | ||
| 19 | 20 | import apijson.RequestMethod; | |
| 20 | 21 | ||
| 21 | - /**解析器 | ||
| 22 | + | ||
| 23 | + /**远程函数解析器 | ||
| 22 | 24 | * @author Lemon | |
| 23 | 25 | */ | |
| 24 | 26 | public interface FunctionParser { | |
| 25 | 27 | ||
| 28 | + Object invoke(@NotNull String function, @NotNull JSONObject currentObject) throws Exception; | ||
| 29 | + | ||
| 26 | 30 | RequestMethod getMethod(); | |
| 27 | - String getTag(); | ||
| 28 | - int getVersion(); | ||
| 29 | - | ||
| 30 | 31 | FunctionParser setMethod(RequestMethod method); | |
| 32 | + | ||
| 33 | + String getTag(); | ||
| 31 | 34 | FunctionParser setTag(String tag); | |
| 35 | + | ||
| 36 | + int getVersion(); | ||
| 32 | 37 | AbstractFunctionParser setVersion(int version); | |
| 33 | 38 | ||
| 34 | - Object invoke(String function, JSONObject currentObject) throws Exception; | ||
| 39 | + @NotNull | ||
| 40 | + JSONObject getRequest(); | ||
| 41 | + FunctionParser setRequest(@NotNull JSONObject request); | ||
| 42 | + | ||
| 43 | + | ||
| 44 | + String getKey(); | ||
| 45 | + FunctionParser setKey(String key); | ||
| 46 | + | ||
| 47 | + String getParentPath(); | ||
| 48 | + FunctionParser setParentPath(String parentPath); | ||
| 49 | + | ||
| 50 | + String getCurrentName(); | ||
| 51 | + FunctionParser setCurrentName(String currentName); | ||
| 52 | + | ||
| 53 | + @NotNull | ||
| 54 | + JSONObject getCurrentObject(); | ||
| 55 | + FunctionParser setCurrentObject(@NotNull JSONObject currentObject); | ||
| 56 | + | ||
| 57 | + | ||
| 35 | 58 | ||
| 36 | 59 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments