| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4f0bbe3 commit afe38fd
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,14 +16,18 @@ | |||
| 16 | 16 | ||
| 17 | 17 | import java.util.ArrayList; | |
| 18 | 18 | import java.util.Arrays; | |
| 19 | + import java.util.Collection; | ||
| 19 | 20 | import java.util.List; | |
| 20 | 21 | ||
| 22 | + import javax.servlet.http.HttpSession; | ||
| 23 | + | ||
| 21 | 24 | import com.alibaba.fastjson.JSONArray; | |
| 22 | 25 | import com.alibaba.fastjson.JSONObject; | |
| 23 | 26 | ||
| 24 | 27 | import apijson.demo.server.model.BaseModel; | |
| 25 | 28 | import zuo.biao.apijson.Log; | |
| 26 | 29 | import zuo.biao.apijson.RequestRole; | |
| 30 | + import zuo.biao.apijson.StringUtil; | ||
| 27 | 31 | import zuo.biao.apijson.server.Function; | |
| 28 | 32 | import zuo.biao.apijson.server.NotNull; | |
| 29 | 33 | ||
@@ -33,7 +37,11 @@ | |||
| 33 | 37 | */ | |
| 34 | 38 | public class DemoFunction extends Function implements FunctionList { | |
| 35 | 39 | private static final String TAG = "DemoFunction"; | |
| 36 | - | ||
| 40 | + | ||
| 41 | + private final HttpSession session; | ||
| 42 | + public DemoFunction(HttpSession session) { | ||
| 43 | + this.session = session; | ||
| 44 | + } | ||
| 37 | 45 | ||
| 38 | 46 | public static void test() throws Exception { | |
| 39 | 47 | int i0 = 1, i1 = -2; | |
@@ -61,35 +69,76 @@ public static void test() throws Exception { | |||
| 61 | 69 | request.put("object", object); | |
| 62 | 70 | ||
| 63 | 71 | ||
| 64 | - Log.i(TAG, "plus(1,-2) = " + invoke(request, "plus(i0,i1)")); | ||
| 65 | - Log.i(TAG, "count([1,2,4,10]) = " + invoke(request, "countArray(array)")); | ||
| 66 | - Log.i(TAG, "isContain([1,2,4,10], 10) = " + invoke(request, "isContain(array,id)")); | ||
| 67 | - Log.i(TAG, "getFromArray([1,2,4,10], 0) = " + invoke(request, "getFromArray(array,@position)")); | ||
| 68 | - Log.i(TAG, "getFromObject({key:true}, key) = " + invoke(request, "getFromObject(object,key)")); | ||
| 72 | + Log.i(TAG, "plus(1,-2) = " + new DemoFunction(null).invoke(request, "plus(i0,i1)")); | ||
| 73 | + Log.i(TAG, "count([1,2,4,10]) = " + new DemoFunction(null).invoke(request, "countArray(array)")); | ||
| 74 | + Log.i(TAG, "isContain([1,2,4,10], 10) = " + new DemoFunction(null).invoke(request, "isContain(array,id)")); | ||
| 75 | + Log.i(TAG, "getFromArray([1,2,4,10], 0) = " + new DemoFunction(null).invoke(request, "getFromArray(array,@position)")); | ||
| 76 | + Log.i(TAG, "getFromObject({key:true}, key) = " + new DemoFunction(null).invoke(request, "getFromObject(object,key)")); | ||
| 69 | 77 | ||
| 70 | 78 | } | |
| 71 | 79 | ||
| 72 | 80 | ||
| 73 | 81 | ||
| 74 | - public static final DemoFunction instance; | ||
| 75 | - static { | ||
| 76 | - instance = new DemoFunction(); | ||
| 77 | - } | ||
| 82 | + | ||
| 78 | 83 | /**反射调用 | |
| 79 | 84 | * @param request | |
| 80 | 85 | * @param function 例如get(object,key),参数只允许引用,不能直接传值 | |
| 81 | 86 | * @return | |
| 82 | 87 | */ | |
| 83 | - public static Object invoke(JSONObject request, String function) throws Exception { | ||
| 88 | + public Object invoke(JSONObject request, String function) throws Exception { | ||
| 84 | 89 | //TODO 不允许调用invoke,避免死循环 | |
| 85 | 90 | // if (function.startsWith("invoke(")) { | |
| 86 | 91 | // | |
| 87 | 92 | // } | |
| 88 | - return invoke(instance, request, function); | ||
| 93 | + return invoke(this, request, function); | ||
| 89 | 94 | } | |
| 90 | 95 | ||
| 91 | 96 | ||
| 92 | - | ||
| 97 | + | ||
| 98 | + /** | ||
| 99 | + * @param request | ||
| 100 | + * @return | ||
| 101 | + * @throws Exception | ||
| 102 | + */ | ||
| 103 | + public Object verifyIdList(@NotNull JSONObject request, @NotNull String idList) throws Exception { | ||
| 104 | + Object obj = request.get(idList); | ||
| 105 | + if (obj instanceof Collection == false) { | ||
| 106 | + throw new IllegalArgumentException(idList + " 不符合 Array 类型! 结构必须是 [] !"); | ||
| 107 | + } | ||
| 108 | + JSONArray array = (JSONArray) obj; | ||
| 109 | + if (array != null) { | ||
| 110 | + for (int i = 0; i < array.size(); i++) { | ||
| 111 | + if (array.get(i) instanceof Long == false && array.get(i) instanceof Integer == false) { | ||
| 112 | + throw new IllegalArgumentException(idList + " 内字符 " + array.getString(i) + " 不符合 Long 类型!"); | ||
| 113 | + } | ||
| 114 | + } | ||
| 115 | + } | ||
| 116 | + return null; | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + | ||
| 120 | + /** | ||
| 121 | + * @param request | ||
| 122 | + * @return | ||
| 123 | + * @throws Exception | ||
| 124 | + */ | ||
| 125 | + public Object verifyURLList(@NotNull JSONObject request, @NotNull String urlList) throws Exception { | ||
| 126 | + Object obj = request.get(urlList); | ||
| 127 | + if (obj instanceof Collection == false) { | ||
| 128 | + throw new IllegalArgumentException(urlList + " 不符合 Array 类型! 结构必须是 [] !"); | ||
| 129 | + } | ||
| 130 | + JSONArray array = (JSONArray) obj; | ||
| 131 | + if (array != null) { | ||
| 132 | + for (int i = 0; i < array.size(); i++) { | ||
| 133 | + if (StringUtil.isUrl(array.getString(i)) == false) { | ||
| 134 | + throw new IllegalArgumentException(urlList + " 内字符 " + array.getString(i) + " 不符合 URL 格式!"); | ||
| 135 | + } | ||
| 136 | + } | ||
| 137 | + } | ||
| 138 | + return null; | ||
| 139 | + } | ||
| 140 | + | ||
| 141 | + | ||
| 93 | 142 | ||
| 94 | 143 | /**TODO 仅用来测试 "key-()":"getIdList()" 和 "key()":"getIdList()" | |
| 95 | 144 | * @param request | |
@@ -99,6 +148,7 @@ public static Object invoke(JSONObject request, String function) throws Exceptio | |||
| 99 | 148 | public JSONArray getIdList(@NotNull JSONObject request) throws Exception { | |
| 100 | 149 | return new JSONArray(new ArrayList<Object>(Arrays.asList(12, 15, 301, 82001, 82002, 38710))); | |
| 101 | 150 | } | |
| 151 | + | ||
| 102 | 152 | ||
| 103 | 153 | /**TODO 仅用来测试 "key-()":"verifyAccess()" | |
| 104 | 154 | * @param request | |
@@ -108,8 +158,8 @@ public JSONArray getIdList(@NotNull JSONObject request) throws Exception { | |||
| 108 | 158 | public Object verifyAccess(@NotNull JSONObject request) throws Exception { | |
| 109 | 159 | long userId = request.getLongValue(zuo.biao.apijson.JSONObject.KEY_USER_ID); | |
| 110 | 160 | RequestRole role = RequestRole.get(request.getString(zuo.biao.apijson.JSONObject.KEY_ROLE)); | |
| 111 | - if (userId != 70793 && role == RequestRole.ADMIN) { | ||
| 112 | - throw new IllegalAccessException("verifyAccess:ADMIN账号只能为70793!"); | ||
| 161 | + if (role == RequestRole.OWNER && userId != DemoVerifier.getVisitorId(session)) { | ||
| 162 | + throw new IllegalAccessException("登录用户与角色OWNER不匹配!"); | ||
| 113 | 163 | } | |
| 114 | 164 | return null; | |
| 115 | 165 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,6 +14,7 @@ | |||
| 14 | 14 | ||
| 15 | 15 | package apijson.demo.server; | |
| 16 | 16 | ||
| 17 | + import javax.servlet.http.HttpSession; | ||
| 17 | 18 | import javax.validation.constraints.NotNull; | |
| 18 | 19 | ||
| 19 | 20 | import com.alibaba.fastjson.JSONObject; | |
@@ -37,14 +38,16 @@ public abstract class DemoObjectParser extends AbstractObjectParser { | |||
| 37 | 38 | } | |
| 38 | 39 | ||
| 39 | 40 | ||
| 41 | + private DemoFunction function; | ||
| 40 | 42 | /**for single object | |
| 41 | 43 | * @param parentPath | |
| 42 | 44 | * @param request | |
| 43 | 45 | * @param name | |
| 44 | 46 | * @throws Exception | |
| 45 | 47 | */ | |
| 46 | - public DemoObjectParser(@NotNull JSONObject request, String parentPath, String name, SQLConfig arrayConfig) throws Exception { | ||
| 48 | + public DemoObjectParser(HttpSession session, @NotNull JSONObject request, String parentPath, String name, SQLConfig arrayConfig) throws Exception { | ||
| 47 | 49 | super(request, parentPath, name, arrayConfig); | |
| 50 | + function = new DemoFunction(session); | ||
| 48 | 51 | } | |
| 49 | 52 | ||
| 50 | 53 | @Override | |
@@ -66,8 +69,8 @@ public SQLConfig newSQLConfig() throws Exception { | |||
| 66 | 69 | ||
| 67 | 70 | ||
| 68 | 71 | @Override | |
| 69 | - public Object onFunctionParse(JSONObject json, String function) throws Exception { | ||
| 70 | - return DemoFunction.invoke(json, function); | ||
| 72 | + public Object onFunctionParse(JSONObject json, String fun) throws Exception { | ||
| 73 | + return function.invoke(json, fun); | ||
| 71 | 74 | } | |
| 72 | 75 | ||
| 73 | 76 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -73,7 +73,7 @@ public SQLExecutor createSQLExecutor() { | |||
| 73 | 73 | @Override | |
| 74 | 74 | public DemoObjectParser createObjectParser(JSONObject request, String parentPath, String name, SQLConfig arrayConfig) throws Exception { | |
| 75 | 75 | ||
| 76 | - return new DemoObjectParser(request, parentPath, name, arrayConfig) { | ||
| 76 | + return new DemoObjectParser(session, request, parentPath, name, arrayConfig) { | ||
| 77 | 77 | ||
| 78 | 78 | //TODO 删除,onPUTArrayParse改用MySQL函数JSON_ADD, JSON_REMOVE等 | |
| 79 | 79 | @Override | |
| Back | FazBrowse Home | New Git URL |
0 commit comments