Explorar el Código

[monitor] 自定义校验器注解@HostValid

tomsun28 hace 4 años
padre
commit
4257b19164

+ 8 - 4
collector/server/src/main/java/com/usthe/collector/dispatch/timer/WheelTimerTask.java

@@ -65,8 +65,10 @@ public class WheelTimerTask implements TimerTask {
                     if (value.startsWith("^_^")) {
                         value = value.replaceAll("\\^_\\^", "");
                         Configmap param = configmap.get(value);
-                        value = (String) param.getValue();
-                        jsonObject.addProperty(entry.getKey(), value);
+                        if (param != null) {
+                            value = (String) param.getValue();
+                            jsonObject.addProperty(entry.getKey(), value);
+                        }
                     }
                 } else {
                     jsonObject.add(entry.getKey(), replaceSpecialValue(entry.getValue(), configmap));
@@ -82,8 +84,10 @@ public class WheelTimerTask implements TimerTask {
                     if (value.startsWith("^_^")) {
                         value = value.replaceAll("\\^_\\^", "");
                         Configmap param = configmap.get(value);
-                        value = (String) param.getValue();
-                        jsonArray.set(i, new JsonPrimitive(value));
+                        if (param != null) {
+                            value = (String) param.getValue();
+                            jsonArray.set(i, new JsonPrimitive(value));
+                        }
                     }
                 } else {
                     jsonArray.set(i, replaceSpecialValue(element, configmap));

+ 35 - 0
common/src/main/java/com/usthe/common/util/IpDomainUtil.java

@@ -0,0 +1,35 @@
+package com.usthe.common.util;
+
+import sun.net.util.IPAddressUtil;
+
+import java.util.regex.Pattern;
+
+/**
+ * ipv4 ipv6 domain 工具类
+ * @author tomsun28
+ * @date 2021/11/17 19:56
+ */
+public class IpDomainUtil {
+
+    /**
+     * 域名校验正则
+     */
+    private static final Pattern DOMAIN_PATTERN =
+            Pattern.compile("^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://)?(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\\/])+$");
+
+    /**
+     * 校验判断是否是 ip或者domain
+     * @param ipDomain ip domain string
+     * @return true-yes false-no
+     */
+    public static boolean validateIpDomain(String ipDomain) {
+        if (IPAddressUtil.isIPv4LiteralAddress(ipDomain)) {
+            return true;
+        }
+        if (IPAddressUtil.isIPv6LiteralAddress(ipDomain)) {
+            return true;
+        }
+        return DOMAIN_PATTERN.matcher(ipDomain).matches();
+    }
+
+}

+ 2 - 0
manager/src/main/java/com/usthe/manager/pojo/entity/Monitor.java

@@ -1,5 +1,6 @@
 package com.usthe.manager.pojo.entity;
 
+import com.usthe.manager.support.valid.HostValid;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.AllArgsConstructor;
@@ -61,6 +62,7 @@ public class Monitor {
      */
     @ApiModelProperty(value = "监控的对端host", example = "192.167.25.11", accessMode = READ_WRITE, position = 4)
     @Length(max = 100)
+    @HostValid
     private String host;
 
     /**

+ 20 - 0
manager/src/main/java/com/usthe/manager/support/valid/HostParamValidator.java

@@ -0,0 +1,20 @@
+package com.usthe.manager.support.valid;
+
+import com.usthe.common.util.IpDomainUtil;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+/**
+ * host注解数据自定义校验器
+ * @author tomsun28
+ * @date 2021/11/17 19:44
+ */
+public class HostParamValidator implements ConstraintValidator<HostValid, String> {
+
+    @Override
+    public boolean isValid(String value, ConstraintValidatorContext context) {
+        // 判断value是否满足ipv4 ipv5 域名 格式
+        return IpDomainUtil.validateIpDomain(value);
+    }
+}

+ 29 - 0
manager/src/main/java/com/usthe/manager/support/valid/HostValid.java

@@ -0,0 +1,29 @@
+package com.usthe.manager.support.valid;
+
+import javax.validation.Constraint;
+import javax.validation.Payload;
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.PARAMETER;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+/**
+ * host注解数据自定义校验器注解
+ * @author tomsun28
+ * @date 2021/11/17 19:42
+ */
+@Target({ FIELD, PARAMETER })
+@Retention(RUNTIME)
+@Documented
+@Constraint(validatedBy = HostParamValidator.class)
+public @interface HostValid {
+
+    String message() default "Host value is invalid,must ipv4, ipv6 or domain";
+
+    Class<?>[] groups() default {};
+
+    Class<? extends Payload>[] payload() default {};
+}

+ 0 - 2
manager/src/main/resources/define/app/A-example.yml

@@ -11,8 +11,6 @@ configmap:
     type: 1
   - key: password
     type: 2
-  - key: param1
-    type: 1
 # 指标组列表
 metrics:
   # 第一个监控指标组 cpu

+ 1 - 0
manager/src/main/resources/define/param/A-example.yml

@@ -29,5 +29,6 @@ param:
   - field: ssl
     name: 启动SSL
     type: radio
+    required: false
     # 当type为radio单选框,checkbox复选框时,option表示可选项值列表
     option: Yes,No