REM:
1. 更新IPAddress库版本
2. 增加自定义枚举校验器
3. 修正IPv4规范化函数
4. 更新部分接口数据校验方式
5. 移除部分多余的校验器
This commit is contained in:
HuangXin 2020-09-02 21:17:20 +08:00
parent 26cfc8cd2f
commit 53f6ef7cca
18 changed files with 466 additions and 65 deletions

View File

@ -199,7 +199,7 @@
<dependency> <dependency>
<groupId>com.github.seancfoley</groupId> <groupId>com.github.seancfoley</groupId>
<artifactId>ipaddress</artifactId> <artifactId>ipaddress</artifactId>
<version>5.2.1</version> <version>5.3.2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.jetbrains</groupId> <groupId>org.jetbrains</groupId>

View File

@ -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})" + "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}:$";
/**
* 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. * The type Protocol.
* *

View File

@ -31,4 +31,6 @@ public class DisposeConfigValue {
* The constant DEFAULT_DISPOSE_BANDWIDTH. * The constant DEFAULT_DISPOSE_BANDWIDTH.
*/ */
public static volatile int DEFAULT_DISPOSE_BANDWIDTH=1024; public static volatile int DEFAULT_DISPOSE_BANDWIDTH=1024;
public static volatile String SPLIT_CHAR=",";
} }

View File

@ -6,6 +6,8 @@ import inet.ipaddr.IPAddressString;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.stream.Collectors;
/** /**
* The type Helper. * The type Helper.
@ -83,7 +85,21 @@ public class Helper {
if (addr.isIPv6()) { if (addr.isIPv6()) {
return addr.toFullString().toUpperCase(); return addr.toFullString().toUpperCase();
} else { } 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;
} }
} }
} }

View File

@ -43,7 +43,7 @@ public class DisposeConfigure {
private Boolean usedPrivacyProtect; private Boolean usedPrivacyProtect;
/** /**
* The Default dispose band. * The Default dispose band width.
*/ */
private Integer defaultDisposeBandWidth; private Integer defaultDisposeBandWidth;
@ -57,5 +57,6 @@ public class DisposeConfigure {
DisposeConfigValue.USED_PRIVACY_PROTECT = Optional.ofNullable(usedPrivacyProtect).orElse(true); DisposeConfigValue.USED_PRIVACY_PROTECT = Optional.ofNullable(usedPrivacyProtect).orElse(true);
DisposeConfigValue.CALL_ERROR_RETRY_TIMES = Optional.ofNullable(callErrorRetryTimes).orElse(5); DisposeConfigValue.CALL_ERROR_RETRY_TIMES = Optional.ofNullable(callErrorRetryTimes).orElse(5);
DisposeConfigValue.DEFAULT_DISPOSE_BANDWIDTH = Optional.ofNullable(defaultDisposeBandWidth).orElse(1024); DisposeConfigValue.DEFAULT_DISPOSE_BANDWIDTH = Optional.ofNullable(defaultDisposeBandWidth).orElse(1024);
DisposeConfigValue.SPLIT_CHAR = Optional.ofNullable(splitChar).orElse(",");
} }
} }

View File

@ -1,5 +1,11 @@
package com.dispose.pojo.dto.protocol.device.manager; 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.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
@ -7,6 +13,8 @@ import lombok.Builder;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import javax.validation.constraints.NotNull;
/** /**
* The type Add capacity info. * The type Add capacity info.
* *
@ -22,20 +30,26 @@ public class AddCapacityInfo {
/** /**
* The Capacity type. * The Capacity type.
*/ */
@NotNull(message = "capacityType 能力类型不能为空", groups = ValidGroups.AddDeviceValid.class)
@ValidBaseEnum(enumClass = DisposeCapacityType.class, message = "capacityType 字段取值为", groups = ValidGroups.AddDeviceValid.class)
private Integer capacityType; private Integer capacityType;
/** /**
* The Object type. * The Object type.
*/ */
@NotNull(message = "objectType 处置对象类型不能为空", groups = ValidGroups.AddDeviceValid.class)
@ValidBaseEnum(enumClass = DisposeObjectType.class, message = "objectType 字段取值为", groups = ValidGroups.AddDeviceValid.class)
private Integer objectType; private Integer objectType;
/** /**
* The Ip type. * The Ip type.
*/ */
@ValidBaseEnum(enumClass = IpAddrType.class, message = "objectType 字段取值为", groups = ValidGroups.AddDeviceValid.class)
private Integer ipType; private Integer ipType;
/** /**
* The Protect ip. * The Protect ip.
*/ */
@ValidIpSegment(message = "protectIp Ip地址格式错误", groups = ValidGroups.AddDeviceValid.class)
private String protectIp; private String protectIp;
} }

View File

@ -1,6 +1,11 @@
package com.dispose.pojo.dto.protocol.device.manager; 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.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.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
@ -23,29 +28,35 @@ import java.util.List;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@JsonPropertyOrder({"ipAddr", "ipPort", "deviceType", "areaCode", "deviceName", "manufacturer", @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) @JsonInclude(JsonInclude.Include.NON_NULL)
public class AddDeviceInfo { public class AddDeviceInfo {
/** /**
* The Ip addr. * The Ip addr.
*/ */
@NotBlank(message = "ipAddr IP地址不能为空", groups = ValidGroups.UpgradeDeviceValid.class) @NotBlank(message = "ipAddr IP地址不能为空", groups = ValidGroups.UpgradeDeviceValid.class)
@ValidIpAddr(message = "disposeIp Ip地址格式错误", groups = ValidGroups.UpgradeDeviceValid.class)
private String ipAddr; private String ipAddr;
/** /**
* The Ip port. * The Ip port.
*/ */
@NotNull(message = "ipPort 端口不能为null, 默认端口用空字符串表示", groups = ValidGroups.UpgradeDeviceValid.class) @NotNull(message = "ipPort 端口不能为null, 默认端口用空字符串表示", groups = ValidGroups.UpgradeDeviceValid.class)
@ValidIpPort(message = "ipPort 端口取值范围为 [1, 65536]", groups = ValidGroups.UpgradeDeviceValid.class)
private String ipPort; private String ipPort;
/** /**
* The Device type. * The Device type.
*/ */
@NotNull(message = "deviceType 设备类型不能为空", groups = ValidGroups.UpgradeDeviceValid.class)
@ValidBaseEnum(enumClass = DisposeDeviceType.class, message = "deviceType 字段取值为", groups =
ValidGroups.UpgradeDeviceValid.class)
private Integer deviceType; private Integer deviceType;
/** /**
* The Area code. * The Area code.
*/ */
@NotNull(message = "areaCode 字段不能为空", groups = ValidGroups.UpgradeDeviceValid.class)
private Integer areaCode; private Integer areaCode;
/** /**
@ -88,6 +99,7 @@ public class AddDeviceInfo {
* The Url type. * The Url type.
*/ */
@NotNull(message = "urlType URL类型不能为空", groups = ValidGroups.AddDeviceValid.class) @NotNull(message = "urlType URL类型不能为空", groups = ValidGroups.AddDeviceValid.class)
@ValidBaseEnum(enumClass = HttpType.class, message = "urlType 字段取值为", groups = ValidGroups.AddDeviceValid.class)
private Integer urlType; private Integer urlType;
/** /**

View File

@ -9,6 +9,7 @@ import lombok.NoArgsConstructor;
import javax.validation.Valid; import javax.validation.Valid;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List; import java.util.List;
/** /**
@ -26,6 +27,7 @@ public class AddDeviceReq {
* The Items. * The Items.
*/ */
@NotNull(message = "items 设备列表不能为空", groups = ValidGroups.AddDeviceValid.class) @NotNull(message = "items 设备列表不能为空", groups = ValidGroups.AddDeviceValid.class)
@Size(min = 1, message = "items 至少需要有一个元素", groups = ValidGroups.AddDeviceValid.class)
@Valid @Valid
private List<AddDeviceInfo> items; private List<AddDeviceInfo> items;
} }

View File

@ -1,15 +1,18 @@
package com.dispose.pojo.dto.protocol.task; 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.group.ValidGroups;
import com.dispose.validation.valids.ValidArrayIpAddr; 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.dispose.validation.valids.ValidIpAddr;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Range;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
@ -34,9 +37,8 @@ public class TaskStartReq {
* The Type. * The Type.
*/ */
@NotNull(message = "type 处置类型不能为空", groups = ValidGroups.TaskStartReqCommonValid.class) @NotNull(message = "type 处置类型不能为空", groups = ValidGroups.TaskStartReqCommonValid.class)
@Range(min = 0, max = 3, @ValidBaseEnum(enumClass = DisposeCapacityType.class, message = "type 字段取值为", groups =
message = "type 字段取值为 [0, 3]", ValidGroups.TaskStartReqCommonValid.class)
groups = ValidGroups.TaskStartReqCommonValid.class)
private Integer type; private Integer type;
/** /**
* The Dispose ip. * The Dispose ip.
@ -60,16 +62,15 @@ public class TaskStartReq {
/** /**
* The Flow direction. * The Flow direction.
*/ */
@Range(min = 0, max = 2, @ValidBaseEnum(enumClass = NetflowDirection.class, message = "flowDirection 字段取值为", groups =
message = "flowDirection 字段取值为 [0, 2]", ValidGroups.TaskStartReqCommonValid.class)
groups = ValidGroups.TaskStartReqCommonValid.class)
private Integer flowDirection; private Integer flowDirection;
/** /**
* The Attack type. * The Attack type.
*/ */
@ValidDDosAttackType(message = "attackType 攻击类型参数错误", groups = ValidGroups.TaskStartReqCommonValid.class) @ValidBaseEnumArray(enumClass = DDoSAttackType.class, message = "attackType 字段取值为", groups =
@Size(min = 1, message = "attackType 必须指定最少一种攻击类型", ValidGroups.TaskStartReqCommonValid.class)
groups = ValidGroups.TaskStartReqCommonValid.class) @Size(min = 1, message = "attackType 必须指定最少一种攻击类型", groups = ValidGroups.TaskStartReqCommonValid.class)
private Integer[] attackType; private Integer[] attackType;
/** /**
* The Flow bandwidth. * The Flow bandwidth.

View File

@ -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 <huangxin@cmhi.chinamoblie.com>
*/
@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<? extends BaseEnum> enumClass();
/**
* Message string.
*
* @return the string
*/
String message();
/**
* Groups class [ ].
*
* @return the class [ ]
*/
Class<?>[] groups() default {};
/**
* Payload class [ ].
*
* @return the class [ ]
*/
Class<? extends Payload>[] payload() default {};
}

View File

@ -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 <huangxin@cmhi.chinamoblie.com>
*/
@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<? extends BaseEnum> enumClass();
/**
* Message string.
*
* @return the string
*/
String message();
/**
* Groups class [ ].
*
* @return the class [ ]
*/
Class<?>[] groups() default {};
/**
* Payload class [ ].
*
* @return the class [ ]
*/
Class<? extends Payload>[] payload() default {};
}

View File

@ -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<? extends Payload>[] payload() default {};
}

View File

@ -1,6 +1,6 @@
package com.dispose.validation.valids; 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.Constraint;
import javax.validation.Payload; import javax.validation.Payload;
@ -11,15 +11,15 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
/** /**
* The interface Valid d dos attack type. * The interface Valid ip segment.
* *
* @author <huangxin@cmhi.chinamoblie.com> * @author <huangxin@cmhi.chinamoblie.com>
*/ */
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER}) @Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Documented @Documented
@Constraint(validatedBy = {ValidDdosAttackTypeImpl.class}) @Constraint(validatedBy = {ValidIpSegmentImpl.class})
public @interface ValidDDosAttackType { public @interface ValidIpSegment {
/** /**
* Message string. * Message string.
* *

View File

@ -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 <huangxin@cmhi.chinamoblie.com>
*/
public class ValidBaseEnumArrayImpl implements ConstraintValidator<ValidBaseEnumArray, Integer[]> {
/**
* The Enum class.
*/
private Class<? extends BaseEnum> 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<Integer> 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);
}
}

View File

@ -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 <huangxin@cmhi.chinamoblie.com>
*/
@Slf4j
public class ValidBaseEnumImpl implements ConstraintValidator<ValidBaseEnum, Integer> {
/**
* The Enum class.
*/
private Class<? extends BaseEnum> 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<Integer> 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);
}
}

View File

@ -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 <huangxin@cmhi.chinamoblie.com>
*/
public class ValidDdosAttackTypeImpl implements ConstraintValidator<ValidDDosAttackType, Integer[]> {
/**
* 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;
}
}

View File

@ -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 <huangxin@cmhi.chinamoblie.com>
*/
public class ValidIpPortImpl implements ConstraintValidator<ValidIpPort, String> {
/**
* 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;
}
}

View File

@ -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 <huangxin@cmhi.chinamoblie.com>
*/
public class ValidIpSegmentImpl implements ConstraintValidator<ValidIpSegment, String> {
/**
* 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;
}
}