13693261870
2024-07-16 577701a313e21448467558b0a507bb7196415674
添加参数验证
已修改4个文件
153 ■■■■ 文件已修改
src/main/java/com/se/simu/controller/WaterController.java 97 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/helper/WebHelper.java 13 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/service/WaterService.java 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/application-dev.yml 32 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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;
    }
}
src/main/java/com/se/simu/helper/WebHelper.java
@@ -1,6 +1,7 @@
package com.se.simu.helper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@@ -47,6 +48,7 @@
    /**
     * 获取主机IP
     *
     * @return
     */
    public static String getHostIp() {
@@ -271,7 +273,16 @@
    }
    /**
     * 输出str至前端
     * 输出str至页面
     */
    public static boolean writeStr2Page(HttpServletResponse res, HttpStatus status, String str) {
        res.setStatus(status.value());
        return writeStr2Page(res, str);
    }
    /**
     * 输出str至页面
     */
    public static boolean writeStr2Page(HttpServletResponse res, String str) {
        try {
src/main/java/com/se/simu/service/WaterService.java
@@ -1,6 +1,7 @@
package com.se.simu.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
/**
@@ -12,4 +13,14 @@
@Slf4j
@Service
public class WaterService {
    @Value("${sys.path.gdal}")
    String gdalPath;
    @Value("${sys.path.data}")
    String dataPath;
    public Object getLayer(String serviceName) {
        return null;
    }
}
src/main/resources/application-dev.yml
@@ -1,26 +1,6 @@
server:
  tomcat:
    uri-encoding: UTF-8
    max-connections: 5000
    max-http-form-post-size: 50MB
    threads:
      max: 2000
  port: 8079
  servlet:
    context-path: /simuserver
spring:
  mvc:
    pathmatch:
      # 解决Knife4j运行报错
      matching-strategy: ant_path_matcher
  application:
    name: simuserver
knife4j:
  # 是否开启
  enabled: true
  # 请求前缀
  pathMapping:
  # 是否开启增强模式
  enable: true
sys:
  path:
    # Gdal驱动目录
    gdal: E:/terrait/TianJin/Zip/release-1928-x64-dev/release-1928-x64/bin
    # 分析数据数据
    data: D:/simu/