From 577701a313e21448467558b0a507bb7196415674 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期二, 16 七月 2024 14:14:33 +0800
Subject: [PATCH] 添加参数验证

---
 src/main/java/com/se/simu/controller/WaterController.java |   97 +++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 86 insertions(+), 11 deletions(-)

diff --git a/src/main/java/com/se/simu/controller/WaterController.java b/src/main/java/com/se/simu/controller/WaterController.java
index 50b6b1f..cd9e4bc 100644
--- a/src/main/java/com/se/simu/controller/WaterController.java
+++ b/src/main/java/com/se/simu/controller/WaterController.java
@@ -1,15 +1,19 @@
 package com.se.simu.controller;
 
+import com.se.simu.helper.StringHelper;
+import com.se.simu.helper.WebHelper;
 import com.se.simu.service.WaterService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.http.HttpStatus;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 import javax.annotation.Resource;
+import javax.servlet.http.HttpServletResponse;
 
 /**
  * 鍐呮稘鎺у埗鍣�
@@ -25,6 +29,12 @@
     @Resource
     WaterService waterService;
 
+    private final static int MIN_SIZE = 10;
+
+    private final static int MAX_SIZE = 10000000;
+
+    private final static long Y2000 = 949334400000L;
+
     @ApiOperation(value = "鑾峰彇褰撳墠鏃堕棿")
     @GetMapping("/getTime")
     public Object getTime() {
@@ -33,25 +43,90 @@
 
     @ApiOperation(value = "鑾峰彇鍏冩暟鎹俊鎭�")
     @GetMapping("/{serviceName}/layer.json")
-    public Object getLayer(@PathVariable String serviceName) {
-        return serviceName;
+    public void getLayer(@PathVariable String serviceName, HttpServletResponse res) {
+        try {
+            if (!validate(serviceName, res)) {
+                return;
+            }
+
+            //
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
+            WebHelper.writeStr2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
+        }
     }
 
     @ApiOperation(value = "鑾峰彇鍦板舰楂樺害鍥�")
-    @GetMapping("/{serviceName}/terrain?width={width}&height={height}")
-    public Object getTerraMap(@PathVariable String serviceName, @PathVariable Integer width, @PathVariable Integer height) {
-        return serviceName + "," + width + "," + height;
+    @GetMapping("/{serviceName}/terrain")
+    public void getTerraMap(@PathVariable String serviceName, Integer width, Integer height, HttpServletResponse res) {
+        try {
+            if (!validate(serviceName, width, height, res)) {
+                return;
+            }
+
+            //
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
+            WebHelper.writeStr2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
+        }
     }
 
     @ApiOperation(value = "鑾峰彇姘撮潰楂樺害鍥�")
-    @GetMapping("/{serviceName}/waterMap?width={width}&height={height}&time={timestamp}")
-    public Object getWaterMap(@PathVariable String serviceName, @PathVariable Integer width, @PathVariable Integer height, @PathVariable Long timestamp) {
-        return serviceName + "," + width + "," + height + "," + timestamp;
+    @GetMapping("/{serviceName}/waterMap")
+    public void getWaterMap(@PathVariable String serviceName, Integer width, Integer height, Long timestamp, HttpServletResponse res) {
+        try {
+            if (!validate(serviceName, width, height, timestamp, res)) {
+                return;
+            }
+
+            //
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
+            WebHelper.writeStr2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
+        }
     }
 
     @ApiOperation(value = "鑾峰彇姘存祦鍚戞祦閫熷浘")
-    @GetMapping("/{serviceName}/flowMap?width={width}&height={height}&time={timestamp}")
-    public Object getFlowMap(@PathVariable String serviceName, @PathVariable Integer width, @PathVariable Integer height, @PathVariable Long timestamp) {
-        return serviceName + "," + width + "," + height + "," + timestamp;
+    @GetMapping("/{serviceName}/flowMap")
+    public void getFlowMap(@PathVariable String serviceName, Integer width, Integer height, Long timestamp, HttpServletResponse res) {
+        try {
+            if (!validate(serviceName, width, height, timestamp, res)) {
+                return;
+            }
+
+            //
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
+            WebHelper.writeStr2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
+        }
+    }
+
+    private boolean validate(String serviceName, HttpServletResponse res) {
+        if (StringHelper.isEmpty(serviceName)) {
+            return WebHelper.writeStr2Page(res, HttpStatus.BAD_REQUEST, "鏈嶅姟鍚嶄笉鑳戒负绌�");
+        }
+
+        return true;
+    }
+
+    private boolean validate(String serviceName, Integer width, Integer height, HttpServletResponse res) {
+        return validate(serviceName, width, height, Y2000, res);
+    }
+
+    private boolean validate(String serviceName, Integer width, Integer height, Long timestamp, HttpServletResponse res) {
+        if (StringHelper.isEmpty(serviceName)) {
+            return WebHelper.writeStr2Page(res, HttpStatus.BAD_REQUEST, "鏈嶅姟鍚嶄笉鑳戒负绌�");
+        }
+        if (null == width || width < MIN_SIZE || width > MAX_SIZE) {
+            return WebHelper.writeStr2Page(res, HttpStatus.BAD_REQUEST, "鍥惧儚瀹藉害涓嶈兘涓虹┖涓斿彇鍊艰寖鍥翠负" + MIN_SIZE + "~" + MAX_SIZE + "涔嬮棿");
+        }
+        if (null == height || height < MIN_SIZE || height > MAX_SIZE) {
+            return WebHelper.writeStr2Page(res, HttpStatus.BAD_REQUEST, "鍥惧儚楂樺害涓嶈兘涓虹┖涓斿彇鍊艰寖鍥翠负" + MIN_SIZE + "~" + MAX_SIZE + "涔嬮棿");
+        }
+        if (null == timestamp || timestamp < Y2000) {
+            return WebHelper.writeStr2Page(res, HttpStatus.BAD_REQUEST, "鏃堕棿涓嶈兘涓虹┖涓斿ぇ浜�2000骞�");
+        }
+
+        return true;
     }
 }

--
Gitblit v1.9.3