Skip to content

Commit 1eeaa02

Browse files
committed
Server:同步eclipse版至idea版
1 parent fedcfa9 commit 1eeaa02

File tree

19 files changed

+1284
-1215
lines changed

19 files changed

+1284
-1215
lines changed

APIJSON-Java-Server/APIJSON-Idea/src/main/java/apijson/demo/server/Controller.java

Lines changed: 223 additions & 105 deletions
Large diffs are not rendered by default.

APIJSON-Java-Server/APIJSON-Idea/src/main/java/apijson/demo/server/Function.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ public static void test() throws Exception {
6060
jsonMap.put("map", map);
6161

6262

63-
Log.i(TAG, "plus = " + invoke(jsonMap, "plus(long:i0,long:i1)"));
64-
Log.i(TAG, "count = " + invoke(jsonMap, "count(Collection:collection)"));
65-
Log.i(TAG, "isContain = " + invoke(jsonMap, "isContain(Collection:collection,Object:id)"));
66-
Log.i(TAG, "get(Map:map,key) = " + invoke(jsonMap, "get(Map:map,key)"));
67-
Log.i(TAG, "get(Collection:collection,int:@position) = " + invoke(jsonMap, "get(Collection:collection,int:@position)"));
68-
Log.i(TAG, "Integer:get(Map:map,key) = " + invoke(jsonMap, "Integer:get(Map:map,key)"));
63+
Log.i(TAG, "plus(1, -2) = " + invoke(jsonMap, "plus(long:i0,long:i1)"));
64+
Log.i(TAG, "count([1,2,4,10]) = " + invoke(jsonMap, "count(Collection:collection)"));
65+
Log.i(TAG, "isContain([1,2,4,10], 10) = " + invoke(jsonMap, "isContain(Collection:collection,Object:id)"));
66+
Log.i(TAG, "get({key:true}, key) = " + invoke(jsonMap, "get(Map:map,key)"));
67+
Log.i(TAG, "get([1,2,4,10], 0) = " + invoke(jsonMap, "get(Collection:collection,int:@position)"));
68+
Log.i(TAG, "Integer:get({key:true}, key) = " + invoke(jsonMap, "Integer:get(Map:map,key)"));
6969
}
7070

7171
/**反射调用
@@ -126,10 +126,6 @@ public long plus(long i0, long i1) {
126126
return i0 + i1;
127127
}
128128

129-
public JSONObject newVerify(String phone) {
130-
return new Controller().postVerify(phone);
131-
}
132-
133129

134130
//判断是否为空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
135131
/**判断collection是否为空
Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414

1515
package apijson.demo.server;
1616

17+
import static zuo.biao.apijson.RequestMethod.DELETE;
18+
import static zuo.biao.apijson.RequestMethod.GET;
19+
import static zuo.biao.apijson.RequestMethod.HEAD;
20+
import static zuo.biao.apijson.RequestMethod.POST;
21+
import static zuo.biao.apijson.RequestMethod.POST_GET;
22+
import static zuo.biao.apijson.RequestMethod.POST_HEAD;
23+
import static zuo.biao.apijson.RequestMethod.PUT;
24+
1725
import java.util.ArrayList;
1826
import java.util.HashMap;
1927
import java.util.List;
@@ -32,26 +40,31 @@
3240
import apijson.demo.server.model.User;
3341
import apijson.demo.server.model.Verify;
3442
import zuo.biao.apijson.JSON;
43+
import zuo.biao.apijson.JSONResponse;
3544
import zuo.biao.apijson.Log;
3645
import zuo.biao.apijson.MethodAccess;
3746
import zuo.biao.apijson.RequestMethod;
3847
import zuo.biao.apijson.RequestRole;
3948
import zuo.biao.apijson.model.Column;
4049
import zuo.biao.apijson.model.Table;
4150
import zuo.biao.apijson.model.Test;
51+
import zuo.biao.apijson.server.JSONRequest;
52+
import zuo.biao.apijson.server.Parser;
53+
import zuo.biao.apijson.server.exception.ConflictException;
4254
import zuo.biao.apijson.server.exception.NotLoggedInException;
4355
import zuo.biao.apijson.server.sql.SQLConfig;
4456

4557
/**权限验证类
4658
* @author Lemon
4759
*/
48-
public class AccessVerifier {
49-
private static final String TAG = "AccessVerifier";
60+
public class Verifier {
61+
private static final String TAG = "Verifier";
5062

5163

5264
public static final String KEY_PASSWORD = "password";
5365
public static final String KEY_LOGIN_PASSWORD = "loginPassword";
5466
public static final String KEY_PAY_PASSWORD = "payPassword";
67+
public static final String KEY_OLD_PASSWORD = "oldPassword";
5568

5669

5770
// <TableName, <METHOD, allowRoles>>
@@ -81,13 +94,13 @@ private static HashMap<RequestMethod, RequestRole[]> getAccessMap(MethodAccess a
8194
}
8295

8396
HashMap<RequestMethod, RequestRole[]> map = new HashMap<>();
84-
map.put(RequestMethod.GET, access.GET());
85-
map.put(RequestMethod.HEAD, access.HEAD());
86-
map.put(RequestMethod.POST_GET, access.POST_GET());
87-
map.put(RequestMethod.POST_HEAD, access.POST_HEAD());
88-
map.put(RequestMethod.POST, access.POST());
89-
map.put(RequestMethod.PUT, access.PUT());
90-
map.put(RequestMethod.DELETE, access.DELETE());
97+
map.put(GET, access.GET());
98+
map.put(HEAD, access.HEAD());
99+
map.put(POST_GET, access.POST_GET());
100+
map.put(POST_HEAD, access.POST_HEAD());
101+
map.put(POST, access.POST());
102+
map.put(PUT, access.PUT());
103+
map.put(DELETE, access.DELETE());
91104

92105
return map;
93106
}
@@ -161,7 +174,7 @@ public static boolean verify(SQLConfig config, User visitor) throws Exception {
161174
}
162175
if (list.contains(new Long("" + id)) == false) {//Integer等转为Long才能正确判断。强转崩溃
163176
if (method == null) {
164-
method = RequestMethod.GET;
177+
method = GET;
165178
}
166179
throw new IllegalAccessException(userIdkey + " = " + id + " 的 " + table
167180
+ " 不允许 " + role.name() + " 用户的 " + method.name() + " 请求!");
@@ -197,7 +210,7 @@ public static void verifyRole(String table, RequestMethod method, RequestRole ro
197210
Log.d(TAG, "verifyRole table = " + table + "; method = " + method + "; role = " + role);
198211
if (table != null) {
199212
if (method == null) {
200-
method = RequestMethod.GET;
213+
method = GET;
201214
}
202215
if (role == null) {
203216
role = RequestRole.UNKNOWN;
@@ -218,6 +231,7 @@ public static void verifyRole(String table, RequestMethod method, RequestRole ro
218231
* @throws Exception
219232
*/
220233
public static void verifyLogin(HttpSession session) throws Exception {
234+
Log.d(TAG, "verifyLogin session.getId() = " + (session == null ? null : session.getId()));
221235
verifyLogin(getUserId(session));
222236
}
223237
/**登录校验
@@ -231,6 +245,50 @@ public static void verifyLogin(Long userId) throws Exception {
231245
throw new NotLoggedInException("未登录,请登录后再操作!");
232246
}
233247
}
248+
249+
250+
251+
/**验证是否重复
252+
* @param table
253+
* @param key
254+
* @param value
255+
* @throws Exception
256+
*/
257+
public static void verifyRepeat(String table, String key, Object value) throws Exception {
258+
verifyRepeat(table, key, value, 0);
259+
}
260+
/**验证是否重复
261+
* @param table
262+
* @param key
263+
* @param value
264+
* @param exceptId 不包含id
265+
* @throws Exception
266+
*/
267+
public static void verifyRepeat(String table, String key, Object value, long exceptId) throws Exception {
268+
if (key == null || value == null) {
269+
Log.e(TAG, "verifyRepeat key == null || value == null >> return;");
270+
return;
271+
}
272+
if (value instanceof JSON) {
273+
throw new UnsupportedDataTypeException(key + ":value 中value的类型不能为JSON!");
274+
}
275+
276+
JSONRequest request = new JSONRequest(key, value);
277+
if (exceptId > 0) {//允许修改自己的属性为该属性原来的值
278+
request.put(JSONRequest.KEY_ID + "!", exceptId);
279+
}
280+
JSONObject repeat = new Parser(HEAD, true).parseResponse(
281+
new JSONRequest(table, request)
282+
);
283+
repeat = repeat == null ? null : repeat.getJSONObject(table);
284+
if (repeat == null) {
285+
throw new Exception("服务器内部错误 verifyRepeat repeat == null");
286+
}
287+
if (repeat.getIntValue(JSONResponse.KEY_COUNT) > 0) {
288+
throw new ConflictException(key + ": " + value + " 已经存在,不能重复!");
289+
}
290+
}
291+
234292

235293
/**获取来访用户的id
236294
* @author Lemon
@@ -267,6 +325,7 @@ public static JSONObject removeAccessInfo(JSONObject requestObject) {
267325
requestObject.remove(KEY_PASSWORD);
268326
requestObject.remove(KEY_LOGIN_PASSWORD);
269327
requestObject.remove(KEY_PAY_PASSWORD);
328+
requestObject.remove(KEY_OLD_PASSWORD);
270329
}
271330
return requestObject;
272331
}

APIJSON-Java-Server/APIJSON-Idea/src/main/java/apijson/demo/server/model/BaseModel.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,53 @@ public static double value(Double value) {
250250
}
251251
//获取非基本类型对应基本类型的非空值 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
252252

253+
/**index是否在arr长度范围内
254+
* @param index
255+
* @param array
256+
* @return
257+
*/
258+
public static boolean isIndexInRange(Integer index, Object[] array) {
259+
return index != null && index >= 0 && index < count(array);
260+
}
261+
262+
/**获取在arr长度范围内的index
263+
* defaultIndex = 0
264+
* @param index
265+
* @param array
266+
* @return
267+
*/
268+
public static int getIndexInRange(Integer index, Object[] array) {
269+
return getIndexInRange(index, array, 0);
270+
}
271+
/**获取在arr长度范围内的index
272+
* @param index
273+
* @param array
274+
* @param defaultIndex
275+
* @return
276+
*/
277+
public static int getIndexInRange(Integer index, Object[] array, int defaultIndex) {
278+
return isIndexInRange(index, array) ? index : defaultIndex;
279+
}
280+
281+
/**获取在arr长度范围内的index
282+
* defaultIndex = 0
283+
* @param <T>
284+
* @param index
285+
* @param array
286+
* @return
287+
*/
288+
public static <T> T getInRange(Integer index, T[] array) {
289+
return getInRange(index, array, 0);
290+
}
291+
/**获取在arr长度范围内的index
292+
* @param <T>
293+
* @param index
294+
* @param array
295+
* @param defaultIndex
296+
* @return
297+
*/
298+
public static <T> T getInRange(Integer index, T[] array, int defaultIndex) {
299+
return get(array, getIndexInRange(index, array, defaultIndex));
300+
}
301+
253302
}

APIJSON-Java-Server/APIJSON-Idea/src/main/java/zuo/biao/apijson/JSON.java

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,24 @@ public static String getCorrectJson(String s) {
6060
public static String getCorrectJson(String s, boolean isArray) {
6161
s = StringUtil.getTrimedString(s);
6262
// if (isArray) {
63-
// if (s.startsWith("\"")) {
63+
// while (s.startsWith("\"")) {
6464
// s = s.substring(1);
6565
// }
66-
// if (s.endsWith("\"")) {
66+
// while (s.endsWith("\"")) {
6767
// s = s.substring(0, s.length() - 1);
6868
// }
6969
// }
7070
return s;//isJsonCorrect(s) ? s : null;
7171
}
7272

73-
/**json转JSONObject
73+
/**obj转JSONObject
7474
* @param json
7575
* @return
7676
*/
7777
public static JSONObject parseObject(Object obj) {
78+
if (obj instanceof JSONObject) {
79+
return (JSONObject) obj;
80+
}
7881
return parseObject(toJSONString(obj));
7982
}
8083
/**json转JSONObject
@@ -114,12 +117,16 @@ public static <T> T parseObject(JSONObject object, Class<T> clazz) {
114117
* @return
115118
*/
116119
public static <T> T parseObject(String json, Class<T> clazz) {
117-
try {
118-
int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
119-
features |= Feature.OrderedField.getMask();
120-
return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), clazz, features);
121-
} catch (Exception e) {
122-
Log.i(TAG, "parseObject catch \n" + e.getMessage());
120+
if (clazz == null) {
121+
Log.e(TAG, "parseObject clazz == null >> return null;");
122+
} else {
123+
try {
124+
int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
125+
features |= Feature.OrderedField.getMask();
126+
return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), clazz, features);
127+
} catch (Exception e) {
128+
Log.i(TAG, "parseObject catch \n" + e.getMessage());
129+
}
123130
}
124131
return null;
125132
}
@@ -131,6 +138,16 @@ public static <T> T parseObject(String json, Class<T> clazz) {
131138
public static JSONArray parseArray(List<Object> list) {
132139
return new JSONArray(list);
133140
}
141+
/**obj转JSONArray
142+
* @param json
143+
* @return
144+
*/
145+
public static JSONArray parseArray(Object obj) {
146+
if (obj instanceof JSONArray) {
147+
return (JSONArray) obj;
148+
}
149+
return parseArray(toJSONString(obj));
150+
}
134151
/**json转JSONArray
135152
* @param json
136153
* @return
@@ -157,10 +174,14 @@ public static <T> List<T> parseArray(JSONArray array, Class<T> clazz) {
157174
* @return
158175
*/
159176
public static <T> List<T> parseArray(String json, Class<T> clazz) {
160-
try {
161-
return com.alibaba.fastjson.JSON.parseArray(getCorrectJson(json, true), clazz);
162-
} catch (Exception e) {
163-
Log.i(TAG, "parseArray catch \n" + e.getMessage());
177+
if (clazz == null) {
178+
Log.e(TAG, "parseArray clazz == null >> return null;");
179+
} else {
180+
try {
181+
return com.alibaba.fastjson.JSON.parseArray(getCorrectJson(json, true), clazz);
182+
} catch (Exception e) {
183+
Log.i(TAG, "parseArray catch \n" + e.getMessage());
184+
}
164185
}
165186
return null;
166187
}

0 commit comments

Comments
 (0)