From 53f6ef7ccabfddafa1d853796b291c1d611f1f8d Mon Sep 17 00:00:00 2001 From: HuangXin Date: Wed, 2 Sep 2020 21:17:20 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E6=9B=B4=E6=96=B0IPAddress?= =?UTF-8?q?=E5=BA=93=E7=89=88=E6=9C=AC=202.=20=E5=A2=9E=E5=8A=A0=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E6=9E=9A=E4=B8=BE=E6=A0=A1=E9=AA=8C=E5=99=A8?= =?UTF-8?q?=203.=20=E4=BF=AE=E6=AD=A3IPv4=E8=A7=84=E8=8C=83=E5=8C=96?= =?UTF-8?q?=E5=87=BD=E6=95=B0=204.=20=E6=9B=B4=E6=96=B0=E9=83=A8=E5=88=86?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=95=B0=E6=8D=AE=E6=A0=A1=E9=AA=8C=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=205.=20=E7=A7=BB=E9=99=A4=E9=83=A8=E5=88=86=E5=A4=9A?= =?UTF-8?q?=E4=BD=99=E7=9A=84=E6=A0=A1=E9=AA=8C=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../java/com/dispose/common/ConstValue.java | 48 +++++++++++++++ .../dispose/common/DisposeConfigValue.java | 2 + src/main/java/com/dispose/common/Helper.java | 18 +++++- .../com/dispose/config/DisposeConfigure.java | 3 +- .../device/manager/AddCapacityInfo.java | 14 +++++ .../device/manager/AddDeviceInfo.java | 14 ++++- .../protocol/device/manager/AddDeviceReq.java | 2 + .../pojo/dto/protocol/task/TaskStartReq.java | 23 +++---- .../validation/valids/ValidBaseEnum.java | 51 ++++++++++++++++ .../validation/valids/ValidBaseEnumArray.java | 51 ++++++++++++++++ .../validation/valids/ValidIpPort.java | 33 ++++++++++ ...DosAttackType.java => ValidIpSegment.java} | 8 +-- .../valids/impl/ValidBaseEnumArrayImpl.java | 58 ++++++++++++++++++ .../valids/impl/ValidBaseEnumImpl.java | 61 +++++++++++++++++++ .../valids/impl/ValidDdosAttackTypeImpl.java | 46 -------------- .../valids/impl/ValidIpPortImpl.java | 48 +++++++++++++++ .../valids/impl/ValidIpSegmentImpl.java | 49 +++++++++++++++ 18 files changed, 466 insertions(+), 65 deletions(-) create mode 100644 src/main/java/com/dispose/validation/valids/ValidBaseEnum.java create mode 100644 src/main/java/com/dispose/validation/valids/ValidBaseEnumArray.java create mode 100644 src/main/java/com/dispose/validation/valids/ValidIpPort.java rename src/main/java/com/dispose/validation/valids/{ValidDDosAttackType.java => ValidIpSegment.java} (79%) create mode 100644 src/main/java/com/dispose/validation/valids/impl/ValidBaseEnumArrayImpl.java create mode 100644 src/main/java/com/dispose/validation/valids/impl/ValidBaseEnumImpl.java delete mode 100644 src/main/java/com/dispose/validation/valids/impl/ValidDdosAttackTypeImpl.java create mode 100644 src/main/java/com/dispose/validation/valids/impl/ValidIpPortImpl.java create mode 100644 src/main/java/com/dispose/validation/valids/impl/ValidIpSegmentImpl.java diff --git a/pom.xml b/pom.xml index 1a0e7a1f..db51795b 100644 --- a/pom.xml +++ b/pom.xml @@ -199,7 +199,7 @@ com.github.seancfoley ipaddress - 5.2.1 + 5.3.2 org.jetbrains diff --git a/src/main/java/com/dispose/common/ConstValue.java b/src/main/java/com/dispose/common/ConstValue.java index 948965da..ae997d6f 100644 --- a/src/main/java/com/dispose/common/ConstValue.java +++ b/src/main/java/com/dispose/common/ConstValue.java @@ -29,6 +29,54 @@ public class ConstValue { "4}){1,3}|:)$|^([\\da-fA-F]{1,4}:){4}((:[\\da-fA-F]{1,4}){1,2}|:)$|^([\\da-fA-F]{1,4}:){5}:([\\da-fA-F]{1,4})" + "?$|^([\\da-fA-F]{1,4}:){6}:$"; + /** + * The constant IP_ADDR_SEGMENT_REG. + */ + public static final String IP_ADDR_SEGMENT_REG = "^(((((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}" + + "(25[0-5]|2[0-4]\\d|[01]?\\d\\d?))-(((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?))" + + ")|(((([\\da-fA-F]{1,4}:){6}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?))|(::" + + "([\\da-fA-F]{1,4}:){0,4}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?))|(" + + "([\\da-fA-F]{1,4}:):([\\da-fA-F]{1,4}:){0,3}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}" + + "(25[0-5]|2[0-4]\\d|[01]?\\d\\d?))|(([\\da-fA-F]{1,4}:){2}:([\\da-fA-F]{1,4}:){0,2}(" + + "(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?))|(([\\da-fA-F]{1,4}:){3}:" + + "([\\da-fA-F]{1,4}:){0,1}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?))|(" + + "([\\da-fA-F]{1,4}:){4}:((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?))|(" + + "([\\da-fA-F]{1,4}:){7}[\\da-fA-F]{1,4})|(:((:[\\da-fA-F]{1,4}){1,6}|:))|([\\da-fA-F]{1,4}:((:[\\da-fA-F]{1," + + "4}){1,5}|:))|(([\\da-fA-F]{1,4}:){2}((:[\\da-fA-F]{1,4}){1,4}|:))|(([\\da-fA-F]{1,4}:){3}((:[\\da-fA-F]{1," + + "4}){1,3}|:))|(([\\da-fA-F]{1,4}:){4}((:[\\da-fA-F]{1,4}){1,2}|:))|(([\\da-fA-F]{1,4}:){5}:([\\da-fA-F]{1,4})" + + "?)|(([\\da-fA-F]{1,4}:){6}:))-((([\\da-fA-F]{1,4}:){6}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}" + + "(25[0-5]|2[0-4]\\d|[01]?\\d\\d?))|(::([\\da-fA-F]{1,4}:){0,4}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}" + + "(25[0-5]|2[0-4]\\d|[01]?\\d\\d?))|(([\\da-fA-F]{1,4}:):([\\da-fA-F]{1,4}:){0,3}(" + + "(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?))|(([\\da-fA-F]{1,4}:){2}:" + + "([\\da-fA-F]{1,4}:){0,2}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?))|(" + + "([\\da-fA-F]{1,4}:){3}:([\\da-fA-F]{1,4}:){0,1}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}" + + "(25[0-5]|2[0-4]\\d|[01]?\\d\\d?))|(([\\da-fA-F]{1,4}:){4}:((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}" + + "(25[0-5]|2[0-4]\\d|[01]?\\d\\d?))|(([\\da-fA-F]{1,4}:){7}[\\da-fA-F]{1,4})|(:((:[\\da-fA-F]{1,4}){1,6}|:))|" + + "([\\da-fA-F]{1,4}:((:[\\da-fA-F]{1,4}){1,5}|:))|(([\\da-fA-F]{1,4}:){2}((:[\\da-fA-F]{1,4}){1,4}|:))|(" + + "([\\da-fA-F]{1,4}:){3}((:[\\da-fA-F]{1,4}){1,3}|:))|(([\\da-fA-F]{1,4}:){4}((:[\\da-fA-F]{1,4}){1,2}|:))|(" + + "([\\da-fA-F]{1,4}:){5}:([\\da-fA-F]{1,4})?)|(([\\da-fA-F]{1,4}:){6}:)))|(((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\" + + ".){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)(\\/(\\d|[1-2]\\d|3[0-2]))?)|((([\\da-fA-F]{1,4}:){6}(" + + "(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))" + + "?)|(::([\\da-fA-F]{1,4}:){0,4}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)(\\/" + + "([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?)|(([\\da-fA-F]{1,4}:):([\\da-fA-F]{1,4}:){0,3}(" + + "(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))" + + "?)|(([\\da-fA-F]{1,4}:){2}:([\\da-fA-F]{1,4}:){0,2}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}" + + "(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?)|(([\\da-fA-F]{1,4}:){3}:" + + "([\\da-fA-F]{1,4}:){0,1}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)(\\/" + + "([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?)|(([\\da-fA-F]{1,4}:){4}:((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}" + + "(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?)|(([\\da-fA-F]{1,4}:)" + + "{7}[\\da-fA-F]{1,4}(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?)|(:((:[\\da-fA-F]{1,4}){1,6}|:)(\\/([1-9]?\\d|(1" + + "([0-1]\\d|2[0-8]))))?)|([\\da-fA-F]{1,4}:((:[\\da-fA-F]{1,4}){1,5}|:)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?)" + + "|(([\\da-fA-F]{1,4}:){2}((:[\\da-fA-F]{1,4}){1,4}|:)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?)|(([\\da-fA-F]{1," + + "4}:){3}((:[\\da-fA-F]{1,4}){1,3}|:)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?)|(([\\da-fA-F]{1,4}:){4}(" + + "(:[\\da-fA-F]{1,4}){1,2}|:)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?)|(([\\da-fA-F]{1,4}:){5}:([\\da-fA-F]{1," + + "4})?(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?)|(([\\da-fA-F]{1,4}:){6}:(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?)))$"; + + /** + * The constant IP_PORT_REG. + */ + public static final String IP_PORT_REG="^[1-9]$|(^[1-9][0-9]$)|(^[1-9][0-9][0-9]$)|(^[1-9][0-9][0-9][0-9]$)|(^[1-6][0-5][0-5][0-3][0-5]$)"; + /** * The type Protocol. * diff --git a/src/main/java/com/dispose/common/DisposeConfigValue.java b/src/main/java/com/dispose/common/DisposeConfigValue.java index 235438bf..69742cc8 100644 --- a/src/main/java/com/dispose/common/DisposeConfigValue.java +++ b/src/main/java/com/dispose/common/DisposeConfigValue.java @@ -31,4 +31,6 @@ public class DisposeConfigValue { * The constant DEFAULT_DISPOSE_BANDWIDTH. */ public static volatile int DEFAULT_DISPOSE_BANDWIDTH=1024; + + public static volatile String SPLIT_CHAR=","; } diff --git a/src/main/java/com/dispose/common/Helper.java b/src/main/java/com/dispose/common/Helper.java index 2b9754ec..c6e0c120 100644 --- a/src/main/java/com/dispose/common/Helper.java +++ b/src/main/java/com/dispose/common/Helper.java @@ -6,6 +6,8 @@ import inet.ipaddr.IPAddressString; import java.sql.Timestamp; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.Arrays; +import java.util.stream.Collectors; /** * The type Helper. @@ -83,7 +85,21 @@ public class Helper { if (addr.isIPv6()) { return addr.toFullString().toUpperCase(); } else { - return addr.toNormalizedString(); + + String ip; + String mask = ""; + + if (!ipAddr.contains("/")) { + ip = ipAddr; + } else { + ip = ipAddr.substring(0, ipAddr.indexOf("/")); + mask = ipAddr.substring(ipAddr.indexOf("/")); + } + + + return Arrays.stream(ip.split("\\.")) + .map(Integer::parseInt).map(String::valueOf) + .collect(Collectors.joining(".")) + mask; } } } diff --git a/src/main/java/com/dispose/config/DisposeConfigure.java b/src/main/java/com/dispose/config/DisposeConfigure.java index 25f140da..15ea184d 100644 --- a/src/main/java/com/dispose/config/DisposeConfigure.java +++ b/src/main/java/com/dispose/config/DisposeConfigure.java @@ -43,7 +43,7 @@ public class DisposeConfigure { private Boolean usedPrivacyProtect; /** - * The Default dispose band. + * The Default dispose band width. */ private Integer defaultDisposeBandWidth; @@ -57,5 +57,6 @@ public class DisposeConfigure { DisposeConfigValue.USED_PRIVACY_PROTECT = Optional.ofNullable(usedPrivacyProtect).orElse(true); DisposeConfigValue.CALL_ERROR_RETRY_TIMES = Optional.ofNullable(callErrorRetryTimes).orElse(5); DisposeConfigValue.DEFAULT_DISPOSE_BANDWIDTH = Optional.ofNullable(defaultDisposeBandWidth).orElse(1024); + DisposeConfigValue.SPLIT_CHAR = Optional.ofNullable(splitChar).orElse(","); } } diff --git a/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddCapacityInfo.java b/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddCapacityInfo.java index 927964a5..c2fc7237 100644 --- a/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddCapacityInfo.java +++ b/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddCapacityInfo.java @@ -1,5 +1,11 @@ package com.dispose.pojo.dto.protocol.device.manager; +import com.dispose.common.DisposeCapacityType; +import com.dispose.common.DisposeObjectType; +import com.dispose.common.IpAddrType; +import com.dispose.validation.group.ValidGroups; +import com.dispose.validation.valids.ValidBaseEnum; +import com.dispose.validation.valids.ValidIpSegment; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonPropertyOrder; import lombok.AllArgsConstructor; @@ -7,6 +13,8 @@ import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import javax.validation.constraints.NotNull; + /** * The type Add capacity info. * @@ -22,20 +30,26 @@ public class AddCapacityInfo { /** * The Capacity type. */ + @NotNull(message = "capacityType 能力类型不能为空", groups = ValidGroups.AddDeviceValid.class) + @ValidBaseEnum(enumClass = DisposeCapacityType.class, message = "capacityType 字段取值为", groups = ValidGroups.AddDeviceValid.class) private Integer capacityType; /** * The Object type. */ + @NotNull(message = "objectType 处置对象类型不能为空", groups = ValidGroups.AddDeviceValid.class) + @ValidBaseEnum(enumClass = DisposeObjectType.class, message = "objectType 字段取值为", groups = ValidGroups.AddDeviceValid.class) private Integer objectType; /** * The Ip type. */ + @ValidBaseEnum(enumClass = IpAddrType.class, message = "objectType 字段取值为", groups = ValidGroups.AddDeviceValid.class) private Integer ipType; /** * The Protect ip. */ + @ValidIpSegment(message = "protectIp Ip地址格式错误", groups = ValidGroups.AddDeviceValid.class) private String protectIp; } diff --git a/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceInfo.java b/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceInfo.java index f13f293c..687d8788 100644 --- a/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceInfo.java +++ b/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceInfo.java @@ -1,6 +1,11 @@ package com.dispose.pojo.dto.protocol.device.manager; +import com.dispose.common.DisposeDeviceType; +import com.dispose.common.HttpType; import com.dispose.validation.group.ValidGroups; +import com.dispose.validation.valids.ValidBaseEnum; +import com.dispose.validation.valids.ValidIpAddr; +import com.dispose.validation.valids.ValidIpPort; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonPropertyOrder; import lombok.AllArgsConstructor; @@ -23,29 +28,35 @@ import java.util.List; @NoArgsConstructor @AllArgsConstructor @JsonPropertyOrder({"ipAddr", "ipPort", "deviceType", "areaCode", "deviceName", "manufacturer", - "model", "version", "userName", "password", "urlPath", "urlType", "readme", "capacity"}) + "model", "version", "userName", "password", "urlPath", "urlType", "readme", "capacity"}) @JsonInclude(JsonInclude.Include.NON_NULL) public class AddDeviceInfo { /** * The Ip addr. */ @NotBlank(message = "ipAddr IP地址不能为空", groups = ValidGroups.UpgradeDeviceValid.class) + @ValidIpAddr(message = "disposeIp Ip地址格式错误", groups = ValidGroups.UpgradeDeviceValid.class) private String ipAddr; /** * The Ip port. */ @NotNull(message = "ipPort 端口不能为null, 默认端口用空字符串表示", groups = ValidGroups.UpgradeDeviceValid.class) + @ValidIpPort(message = "ipPort 端口取值范围为 [1, 65536]", groups = ValidGroups.UpgradeDeviceValid.class) private String ipPort; /** * The Device type. */ + @NotNull(message = "deviceType 设备类型不能为空", groups = ValidGroups.UpgradeDeviceValid.class) + @ValidBaseEnum(enumClass = DisposeDeviceType.class, message = "deviceType 字段取值为", groups = + ValidGroups.UpgradeDeviceValid.class) private Integer deviceType; /** * The Area code. */ + @NotNull(message = "areaCode 字段不能为空", groups = ValidGroups.UpgradeDeviceValid.class) private Integer areaCode; /** @@ -88,6 +99,7 @@ public class AddDeviceInfo { * The Url type. */ @NotNull(message = "urlType URL类型不能为空", groups = ValidGroups.AddDeviceValid.class) + @ValidBaseEnum(enumClass = HttpType.class, message = "urlType 字段取值为", groups = ValidGroups.AddDeviceValid.class) private Integer urlType; /** diff --git a/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceReq.java b/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceReq.java index a930150f..fe498904 100644 --- a/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceReq.java +++ b/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceReq.java @@ -9,6 +9,7 @@ import lombok.NoArgsConstructor; import javax.validation.Valid; import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; import java.util.List; /** @@ -26,6 +27,7 @@ public class AddDeviceReq { * The Items. */ @NotNull(message = "items 设备列表不能为空", groups = ValidGroups.AddDeviceValid.class) + @Size(min = 1, message = "items 至少需要有一个元素", groups = ValidGroups.AddDeviceValid.class) @Valid private List items; } diff --git a/src/main/java/com/dispose/pojo/dto/protocol/task/TaskStartReq.java b/src/main/java/com/dispose/pojo/dto/protocol/task/TaskStartReq.java index 4c1c9841..7e47c0bd 100644 --- a/src/main/java/com/dispose/pojo/dto/protocol/task/TaskStartReq.java +++ b/src/main/java/com/dispose/pojo/dto/protocol/task/TaskStartReq.java @@ -1,15 +1,18 @@ package com.dispose.pojo.dto.protocol.task; +import com.dispose.common.DDoSAttackType; +import com.dispose.common.DisposeCapacityType; +import com.dispose.common.NetflowDirection; import com.dispose.validation.group.ValidGroups; import com.dispose.validation.valids.ValidArrayIpAddr; -import com.dispose.validation.valids.ValidDDosAttackType; +import com.dispose.validation.valids.ValidBaseEnum; +import com.dispose.validation.valids.ValidBaseEnumArray; import com.dispose.validation.valids.ValidIpAddr; import com.fasterxml.jackson.annotation.JsonInclude; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; -import org.hibernate.validator.constraints.Range; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; @@ -34,9 +37,8 @@ public class TaskStartReq { * The Type. */ @NotNull(message = "type 处置类型不能为空", groups = ValidGroups.TaskStartReqCommonValid.class) - @Range(min = 0, max = 3, - message = "type 字段取值为 [0, 3]", - groups = ValidGroups.TaskStartReqCommonValid.class) + @ValidBaseEnum(enumClass = DisposeCapacityType.class, message = "type 字段取值为", groups = + ValidGroups.TaskStartReqCommonValid.class) private Integer type; /** * The Dispose ip. @@ -60,16 +62,15 @@ public class TaskStartReq { /** * The Flow direction. */ - @Range(min = 0, max = 2, - message = "flowDirection 字段取值为 [0, 2]", - groups = ValidGroups.TaskStartReqCommonValid.class) + @ValidBaseEnum(enumClass = NetflowDirection.class, message = "flowDirection 字段取值为", groups = + ValidGroups.TaskStartReqCommonValid.class) private Integer flowDirection; /** * The Attack type. */ - @ValidDDosAttackType(message = "attackType 攻击类型参数错误", groups = ValidGroups.TaskStartReqCommonValid.class) - @Size(min = 1, message = "attackType 必须指定最少一种攻击类型", - groups = ValidGroups.TaskStartReqCommonValid.class) + @ValidBaseEnumArray(enumClass = DDoSAttackType.class, message = "attackType 字段取值为", groups = + ValidGroups.TaskStartReqCommonValid.class) + @Size(min = 1, message = "attackType 必须指定最少一种攻击类型", groups = ValidGroups.TaskStartReqCommonValid.class) private Integer[] attackType; /** * The Flow bandwidth. diff --git a/src/main/java/com/dispose/validation/valids/ValidBaseEnum.java b/src/main/java/com/dispose/validation/valids/ValidBaseEnum.java new file mode 100644 index 00000000..d3f98103 --- /dev/null +++ b/src/main/java/com/dispose/validation/valids/ValidBaseEnum.java @@ -0,0 +1,51 @@ +package com.dispose.validation.valids; + +import com.dispose.common.BaseEnum; +import com.dispose.validation.valids.impl.ValidBaseEnumImpl; + +import javax.validation.Constraint; +import javax.validation.Payload; +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * The interface Valid custom base enum. + * + * @author + */ +@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Constraint(validatedBy = {ValidBaseEnumImpl.class}) +public @interface ValidBaseEnum { + /** + * Enum class class. + * + * @return the class + */ + Class enumClass(); + + /** + * Message string. + * + * @return the string + */ + String message(); + + /** + * Groups class [ ]. + * + * @return the class [ ] + */ + Class[] groups() default {}; + + /** + * Payload class [ ]. + * + * @return the class [ ] + */ + Class[] payload() default {}; +} diff --git a/src/main/java/com/dispose/validation/valids/ValidBaseEnumArray.java b/src/main/java/com/dispose/validation/valids/ValidBaseEnumArray.java new file mode 100644 index 00000000..e3a28d93 --- /dev/null +++ b/src/main/java/com/dispose/validation/valids/ValidBaseEnumArray.java @@ -0,0 +1,51 @@ +package com.dispose.validation.valids; + +import com.dispose.common.BaseEnum; +import com.dispose.validation.valids.impl.ValidBaseEnumArrayImpl; + +import javax.validation.Constraint; +import javax.validation.Payload; +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * The interface Valid custom base enum array. + * + * @author + */ +@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Constraint(validatedBy = {ValidBaseEnumArrayImpl.class}) +public @interface ValidBaseEnumArray { + /** + * Enum class class. + * + * @return the class + */ + Class enumClass(); + + /** + * Message string. + * + * @return the string + */ + String message(); + + /** + * Groups class [ ]. + * + * @return the class [ ] + */ + Class[] groups() default {}; + + /** + * Payload class [ ]. + * + * @return the class [ ] + */ + Class[] payload() default {}; +} diff --git a/src/main/java/com/dispose/validation/valids/ValidIpPort.java b/src/main/java/com/dispose/validation/valids/ValidIpPort.java new file mode 100644 index 00000000..4ff19f75 --- /dev/null +++ b/src/main/java/com/dispose/validation/valids/ValidIpPort.java @@ -0,0 +1,33 @@ +package com.dispose.validation.valids; + +import com.dispose.validation.valids.impl.ValidIpPortImpl; + +import javax.validation.Constraint; +import javax.validation.Payload; +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Constraint(validatedBy = {ValidIpPortImpl.class}) +public @interface ValidIpPort { + String message(); + + /** + * Groups class [ ]. + * + * @return the class [ ] + */ + Class[] groups() default {}; + + /** + * Payload class [ ]. + * + * @return the class [ ] + */ + Class[] payload() default {}; +} diff --git a/src/main/java/com/dispose/validation/valids/ValidDDosAttackType.java b/src/main/java/com/dispose/validation/valids/ValidIpSegment.java similarity index 79% rename from src/main/java/com/dispose/validation/valids/ValidDDosAttackType.java rename to src/main/java/com/dispose/validation/valids/ValidIpSegment.java index a4e25a9e..ff7aacc4 100644 --- a/src/main/java/com/dispose/validation/valids/ValidDDosAttackType.java +++ b/src/main/java/com/dispose/validation/valids/ValidIpSegment.java @@ -1,6 +1,6 @@ package com.dispose.validation.valids; -import com.dispose.validation.valids.impl.ValidDdosAttackTypeImpl; +import com.dispose.validation.valids.impl.ValidIpSegmentImpl; import javax.validation.Constraint; import javax.validation.Payload; @@ -11,15 +11,15 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** - * The interface Valid d dos attack type. + * The interface Valid ip segment. * * @author */ @Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) @Documented -@Constraint(validatedBy = {ValidDdosAttackTypeImpl.class}) -public @interface ValidDDosAttackType { +@Constraint(validatedBy = {ValidIpSegmentImpl.class}) +public @interface ValidIpSegment { /** * Message string. * diff --git a/src/main/java/com/dispose/validation/valids/impl/ValidBaseEnumArrayImpl.java b/src/main/java/com/dispose/validation/valids/impl/ValidBaseEnumArrayImpl.java new file mode 100644 index 00000000..3ad8fad9 --- /dev/null +++ b/src/main/java/com/dispose/validation/valids/impl/ValidBaseEnumArrayImpl.java @@ -0,0 +1,58 @@ +package com.dispose.validation.valids.impl; + +import com.dispose.common.BaseEnum; +import com.dispose.validation.valids.ValidBaseEnumArray; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +/** + * The type Valid custom base enum array. + * + * @author + */ +public class ValidBaseEnumArrayImpl implements ConstraintValidator { + /** + * The Enum class. + */ + private Class enumClass; + + /** + * The Message. + */ + private String message; + + /** + * Initialize. + * + * @param constraintAnnotation the constraint annotation + */ + @Override + public void initialize(ValidBaseEnumArray constraintAnnotation) { + this.enumClass = constraintAnnotation.enumClass(); + this.message = constraintAnnotation.message(); + } + + /** + * Is valid boolean. + * + * @param integers the integers + * @param constraintValidatorContext the constraint validator context + * @return the boolean + */ + @Override + public boolean isValid(Integer[] integers, ConstraintValidatorContext constraintValidatorContext) { + constraintValidatorContext.disableDefaultConstraintViolation(); + List enumValues = + Arrays.stream(enumClass.getEnumConstants()).map(BaseEnum::getValue).collect(Collectors.toList()); + + String errMsg = this.message + " " + enumValues.toString(); + + constraintValidatorContext.buildConstraintViolationWithTemplate(errMsg).addConstraintViolation(); + + return Arrays.stream(integers).allMatch(enumValues::contains); + } +} diff --git a/src/main/java/com/dispose/validation/valids/impl/ValidBaseEnumImpl.java b/src/main/java/com/dispose/validation/valids/impl/ValidBaseEnumImpl.java new file mode 100644 index 00000000..1542e33d --- /dev/null +++ b/src/main/java/com/dispose/validation/valids/impl/ValidBaseEnumImpl.java @@ -0,0 +1,61 @@ +package com.dispose.validation.valids.impl; + +import com.dispose.common.BaseEnum; +import com.dispose.validation.valids.ValidBaseEnum; +import lombok.extern.slf4j.Slf4j; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +/** + * The type Valid custom base enum. + * + * @author + */ +@Slf4j +public class ValidBaseEnumImpl implements ConstraintValidator { + + /** + * The Enum class. + */ + private Class enumClass; + + /** + * The Message. + */ + private String message; + + /** + * Initialize. + * + * @param constraintAnnotation the constraint annotation + */ + @Override + public void initialize(ValidBaseEnum constraintAnnotation) { + this.enumClass = constraintAnnotation.enumClass(); + this.message = constraintAnnotation.message(); + } + + /** + * Is valid boolean. + * + * @param integer the integer + * @param constraintValidatorContext the constraint validator context + * @return the boolean + */ + @Override + public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) { + constraintValidatorContext.disableDefaultConstraintViolation(); + List enumValues = + Arrays.stream(enumClass.getEnumConstants()).map(BaseEnum::getValue).collect(Collectors.toList()); + + String errMsg = this.message + " " + enumValues.toString(); + + constraintValidatorContext.buildConstraintViolationWithTemplate(errMsg).addConstraintViolation(); + + return enumValues.contains(integer); + } +} diff --git a/src/main/java/com/dispose/validation/valids/impl/ValidDdosAttackTypeImpl.java b/src/main/java/com/dispose/validation/valids/impl/ValidDdosAttackTypeImpl.java deleted file mode 100644 index 40398bda..00000000 --- a/src/main/java/com/dispose/validation/valids/impl/ValidDdosAttackTypeImpl.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.dispose.validation.valids.impl; - -import com.dispose.common.DDoSAttackType; -import com.dispose.validation.valids.ValidDDosAttackType; - -import javax.validation.ConstraintValidator; -import javax.validation.ConstraintValidatorContext; -import java.util.Arrays; - -/** - * The type Valid ddos attack type. - * - * @author - */ -public class ValidDdosAttackTypeImpl implements ConstraintValidator { - /** - * Initialize. - * - * @param constraintAnnotation the constraint annotation - */ - @Override - public void initialize(ValidDDosAttackType constraintAnnotation) { - } - - /** - * Is valid boolean. - * - * @param integers the integers - * @param constraintValidatorContext the constraint validator context - * @return the boolean - */ - @Override - public boolean isValid(Integer[] integers, ConstraintValidatorContext constraintValidatorContext) { - if (integers == null) { - return true; - } - - for (Integer i : integers) { - if (Arrays.stream(DDoSAttackType.values()).noneMatch(v -> v.getValue().equals(i))) { - return false; - } - } - - return true; - } -} diff --git a/src/main/java/com/dispose/validation/valids/impl/ValidIpPortImpl.java b/src/main/java/com/dispose/validation/valids/impl/ValidIpPortImpl.java new file mode 100644 index 00000000..3cd0561f --- /dev/null +++ b/src/main/java/com/dispose/validation/valids/impl/ValidIpPortImpl.java @@ -0,0 +1,48 @@ +package com.dispose.validation.valids.impl; + +import com.dispose.validation.valids.ValidIpPort; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; + +/** + * The type Valid ip port. + * + * @author + */ +public class ValidIpPortImpl implements ConstraintValidator { + /** + * Initialize. + * + * @param constraintAnnotation the constraint annotation + */ + @Override + public void initialize(ValidIpPort constraintAnnotation) { + + } + + /** + * Is valid boolean. + * + * @param s the s + * @param constraintValidatorContext the constraint validator context + * @return the boolean + */ + @Override + public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) { + final int minSocketPort = 1; + final int maxSocketPort = 65535; + + try { + int port = Integer.parseInt(s); + + if (port >= minSocketPort && port <= maxSocketPort) { + return true; + } + } catch (Exception e) { + return false; + } + + return false; + } +} diff --git a/src/main/java/com/dispose/validation/valids/impl/ValidIpSegmentImpl.java b/src/main/java/com/dispose/validation/valids/impl/ValidIpSegmentImpl.java new file mode 100644 index 00000000..a8fdadae --- /dev/null +++ b/src/main/java/com/dispose/validation/valids/impl/ValidIpSegmentImpl.java @@ -0,0 +1,49 @@ +package com.dispose.validation.valids.impl; + +import com.dispose.common.ConstValue; +import com.dispose.common.DisposeConfigValue; +import com.dispose.validation.valids.ValidIpSegment; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; +import java.util.regex.Pattern; + +/** + * The type Valid ip segment. + * + * @author + */ +public class ValidIpSegmentImpl implements ConstraintValidator { + /** + * Initialize. + * + * @param constraintAnnotation the constraint annotation + */ + @Override + public void initialize(ValidIpSegment constraintAnnotation) { + + } + + /** + * Is valid boolean. + * + * @param str the str + * @param constraintValidatorContext the constraint validator context + * @return the boolean + */ + @Override + public boolean isValid(String str, ConstraintValidatorContext constraintValidatorContext) { + if (str == null || str.length() == 0) { + return false; + } + + for (String s : str.split(DisposeConfigValue.SPLIT_CHAR)) { + + if (!Pattern.matches(ConstValue.IP_ADDR_SEGMENT_REG, s.replace(" ", ""))) { + return false; + } + } + + return true; + } +}