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
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
}
0 commit comments