|
16 | 16 |
|
17 | 17 | import static zuo.biao.apijson.RequestMethod.GET; |
18 | 18 | import static zuo.biao.apijson.RequestMethod.HEAD; |
19 | | -import static zuo.biao.apijson.RequestMethod.POST; |
20 | 19 | import static zuo.biao.apijson.RequestMethod.POST_GET; |
21 | 20 | import static zuo.biao.apijson.RequestMethod.POST_HEAD; |
22 | 21 | import static zuo.biao.apijson.RequestMethod.PUT; |
23 | 22 | import static zuo.biao.apijson.StringUtil.UTF_8; |
24 | 23 |
|
25 | 24 | import java.io.UnsupportedEncodingException; |
26 | 25 | import java.net.URLDecoder; |
27 | | -import java.util.ArrayList; |
28 | 26 | import java.util.HashMap; |
29 | | -import java.util.List; |
30 | 27 | import java.util.Map; |
31 | 28 | import java.util.Set; |
32 | 29 | import java.util.concurrent.TimeoutException; |
|
42 | 39 | import zuo.biao.apijson.Log; |
43 | 40 | import zuo.biao.apijson.RequestMethod; |
44 | 41 | import zuo.biao.apijson.StringUtil; |
| 42 | +import zuo.biao.apijson.StructureVerifier; |
45 | 43 | import zuo.biao.apijson.server.exception.ConditionNotMatchException; |
46 | 44 | import zuo.biao.apijson.server.exception.ConflictException; |
47 | 45 | import zuo.biao.apijson.server.sql.AccessVerifier; |
@@ -71,10 +69,10 @@ public Parser(RequestMethod method) { |
71 | 69 | } |
72 | 70 |
|
73 | 71 | private final RequestMethod requestMethod; |
74 | | - private final boolean noVerify; |
| 72 | + private final boolean noVerify;//方法免验证。但内容还是需要验证,避免服务端写错代码导致产生脏数据甚至清空Table |
75 | 73 | /** |
76 | 74 | * @param requestMethod null ? requestMethod = GET |
77 | | - * @param noVerify 仅限于为服务端提供免验证特权,普通请求不要设置为true! 如果对应Table有权限也建议用默认值false,保持和客户端权限一致 |
| 75 | + * @param noVerify 仅限于为服务端提供方法免验证特权,普通请求不要设置为true! 如果对应Table有权限也建议用默认值false,保持和客户端权限一致 |
78 | 76 | */ |
79 | 77 | public Parser(RequestMethod method, boolean noVerify) { |
80 | 78 | super(); |
@@ -115,15 +113,13 @@ public String parse(JSONObject request) { |
115 | 113 | public JSONObject parseResponse(String request) { |
116 | 114 | Log.d(TAG, "\n\n\n\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n" |
117 | 115 | + requestMethod + "/parseResponse request = \n" + request + "\n\n"); |
118 | | - |
119 | | - requestObject = parseRequest(request, requestMethod); |
120 | | - if (noVerify == false) { |
121 | | - try { |
122 | | - requestObject = getCorrectRequest(requestMethod, requestObject); |
123 | | - } catch (Exception e) { |
124 | | - return newErrorResult(e); |
125 | | - } |
| 116 | + |
| 117 | + try { |
| 118 | + requestObject = StructureVerifier.getCorrectRequest(requestMethod, parseRequest(request, requestMethod)); |
| 119 | + } catch (Exception e) { |
| 120 | + return newErrorResult(e); |
126 | 121 | } |
| 122 | + |
127 | 123 | return parseResponse(requestObject); |
128 | 124 | } |
129 | 125 | /**解析请求json并获取对应结果 |
@@ -287,184 +283,6 @@ public static JSONObject newErrorResult(Exception e) { |
287 | 283 |
|
288 | 284 |
|
289 | 285 |
|
290 | | - /**获取正确的请求,非GET请求必须是服务器指定的 |
291 | | - * @param method |
292 | | - * @param request |
293 | | - * @return |
294 | | - * @throws Exception |
295 | | - */ |
296 | | - public static JSONObject getCorrectRequest(RequestMethod method, JSONObject request) throws Exception { |
297 | | - return getCorrectRequest(method, request, null); |
298 | | - } |
299 | | - /**获取正确的请求,非GET请求必须是服务器指定的 |
300 | | - * @param method |
301 | | - * @param request |
302 | | - * @param queryHelper |
303 | | - * @return |
304 | | - */ |
305 | | - public static JSONObject getCorrectRequest(RequestMethod method, JSONObject request, QueryHelper queryHelper) |
306 | | - throws Exception { |
307 | | - if (isPublicMethod(method)) { |
308 | | - return request;//需要指定JSON结构的get请求可以改为post请求。一般只有对安全性要求高的才会指定,而这种情况用明文的GET方式几乎肯定不安全 |
309 | | - } |
310 | | - |
311 | | - String tag = request.getString(JSONRequest.KEY_TAG); |
312 | | - if (StringUtil.isNotEmpty(tag, true) == false) { |
313 | | - throw new IllegalArgumentException("请指定tag!一般是table名称"); |
314 | | - } |
315 | | - |
316 | | - //获取指定的JSON结构 <<<<<<<<<<<<<< |
317 | | - QueryConfig config = new QueryConfig(GET, "Request"); |
318 | | - config.setColumn("structure"); |
319 | | - |
320 | | - Map<String, Object> where = new HashMap<String, Object>(); |
321 | | - where.put("method", method.name()); |
322 | | - where.put(JSONRequest.KEY_TAG, tag); |
323 | | - config.setWhere(where); |
324 | | - |
325 | | - JSONObject object = null; |
326 | | - String error = ""; |
327 | | - if (queryHelper == null) { |
328 | | - queryHelper = new QueryHelper(); |
329 | | - } |
330 | | - try { |
331 | | - object = queryHelper.select(config); |
332 | | - } catch (Exception e) { |
333 | | - e.printStackTrace(); |
334 | | - error = e.getMessage(); |
335 | | - } |
336 | | - queryHelper.close(); |
337 | | - |
338 | | - if (object == null) {//empty表示随意操作 || object.isEmpty()) { |
339 | | - throw new UnsupportedOperationException("非GET请求必须是服务端允许的操作! \n " + error); |
340 | | - } |
341 | | - object = getJSONObject(object, "structure");//解决返回值套了一层 "structure":{} |
342 | | - |
343 | | - JSONObject target = null; |
344 | | - if (isTableKey(tag) && object.containsKey(tag) == false) {//tag是table名 |
345 | | - target = new JSONObject(true); |
346 | | - target.put(tag, object); |
347 | | - } else { |
348 | | - target = object; |
349 | | - } |
350 | | - //获取指定的JSON结构 >>>>>>>>>>>>>> |
351 | | - |
352 | | - request.remove(JSONRequest.KEY_TAG); |
353 | | - return fillTarget(method, target, request, ""); |
354 | | - } |
355 | | - |
356 | | - |
357 | | - public static final String NECESSARY_COLUMNS = "necessaryColumns"; |
358 | | - public static final String DISALLOW_COLUMNS = "disallowColumns"; |
359 | | - |
360 | | - /**从request提取target指定的内容 |
361 | | - * @param target |
362 | | - * @param request |
363 | | - * @return |
364 | | - */ |
365 | | - public static JSONObject fillTarget(RequestMethod method |
366 | | - , JSONObject target, final JSONObject request, String requestName) throws Exception { |
367 | | - Log.i(TAG, "filterTarget requestName = " + requestName |
368 | | - + " target = \n" + JSON.toJSONString(target) |
369 | | - + "\n request = \n" + JSON.toJSONString(request) + "\n >> return null;"); |
370 | | - if (target == null || request == null) {// || request.isEmpty()) { |
371 | | - Log.i(TAG, "filterTarget target == null || request == null >> return null;"); |
372 | | - return null; |
373 | | - } |
374 | | - |
375 | | - /**方法三:遍历request,transferredRequest只添加target所包含的object |
376 | | - * ,且移除target中DISALLOW_COLUMNS,期间判断NECESSARY_COLUMNS是否都有 |
377 | | - */ |
378 | | - String necessarys = StringUtil.getNoBlankString(target.getString(NECESSARY_COLUMNS)); |
379 | | - String[] necessaryColumns = StringUtil.split(necessarys); |
380 | | - |
381 | | - //判断必要字段是否都有 |
382 | | - if (necessaryColumns != null) { |
383 | | - for (String s : necessaryColumns) { |
384 | | - if (request.containsKey(s) == false) { |
385 | | - throw new IllegalArgumentException(requestName |
386 | | - + "不能缺少 " + s + " 等[" + necessarys + "]内的任何字段!"); |
387 | | - } |
388 | | - } |
389 | | - } |
390 | | - |
391 | | - String disallows = StringUtil.getNoBlankString(target.getString(DISALLOW_COLUMNS)); |
392 | | - String[] disallowColumns = null; |
393 | | - |
394 | | - Set<String> set = request.keySet(); |
395 | | - if ("!".equals(disallows)) {//所有非necessaryColumns,改成 !necessary 更好 |
396 | | - if (set != null) { |
397 | | - List<String> disallowList = new ArrayList<String>(); |
398 | | - for (String key : set) { |
399 | | - if (isContainKeyInArray(key, necessaryColumns) == false) { |
400 | | - disallowList.add(key); |
401 | | - } |
402 | | - } |
403 | | - disallowColumns = disallowList.toArray(new String[]{}); |
404 | | - } |
405 | | - } else { |
406 | | - disallowColumns = StringUtil.split(disallows); |
407 | | - } |
408 | | - |
409 | | - |
410 | | - //填充target |
411 | | - JSONObject transferredRequest = new JSONObject(true); |
412 | | - if (set != null) { |
413 | | - Object value; |
414 | | - JSONObject result; |
415 | | - for (String key : set) { |
416 | | - value = request.get(key); |
417 | | - if (value instanceof JSONObject) {//JSONObject,往下一级提取 |
418 | | - if (target.containsKey(key)) {//只填充target有的object |
419 | | - result = fillTarget(method, getJSONObject(target, key), (JSONObject) value, key);//往下一级提取 |
420 | | - Log.i(TAG, "fillTarget key = " + key + "; result = " + result); |
421 | | - if (result == null || result.isEmpty()) {//只添加!=null的值,可能数据库返回数据不够count |
422 | | - throw new IllegalArgumentException(requestName |
423 | | - + "不能缺少 " + key + " 等[" + necessarys + "]内的任何JSONObject!"); |
424 | | - } |
425 | | - if (method == POST && result.containsKey(QueryConfig.ID) == false) {//为注册用户返回id |
426 | | - result.put(QueryConfig.ID, System.currentTimeMillis()); |
427 | | - } |
428 | | - transferredRequest.put(key, result); |
429 | | - } |
430 | | - } else {//JSONArray或其它Object |
431 | | - if (isContainKeyInArray(key, disallowColumns)) { |
432 | | - throw new IllegalArgumentException(requestName |
433 | | - + "不允许传 " + key + " 等[" + disallows + "]内的任何字段!"); |
434 | | - } |
435 | | - transferredRequest.put(key, value); |
436 | | - } |
437 | | - } |
438 | | - } |
439 | | - |
440 | | - Log.i(TAG, "filterTarget return transferredRequest = " + JSON.toJSONString(transferredRequest)); |
441 | | - return transferredRequest; |
442 | | - } |
443 | | - |
444 | | - /**array至少有一个值在request的key中 |
445 | | - * @param key |
446 | | - * @param array |
447 | | - * @return |
448 | | - */ |
449 | | - public static boolean isContainKeyInArray(String key, String[] array) { |
450 | | - if (array == null || array.length <= 0 || key == null) { |
451 | | - Log.i(TAG, "isContainKeyInArray" |
452 | | - + " array == null || array.length <= 0 || key == null >> return false;"); |
453 | | - return false; |
454 | | - } |
455 | | - |
456 | | - for (String s : array) { |
457 | | - if (key.equals(s)) { |
458 | | - return true; |
459 | | - } |
460 | | - } |
461 | | - |
462 | | - return false; |
463 | | - } |
464 | | - |
465 | | - |
466 | | - |
467 | | - |
468 | 286 |
|
469 | 287 | /**获取单个对象,该对象处于parentObject内 |
470 | 288 | * @param parentPath parentObject的路径 |
|
0 commit comments