From a5c568392b05d204ac66c408705ffe6ecd7b19cd Mon Sep 17 00:00:00 2001 From: Yohanes Date: Wed, 5 Apr 2023 19:49:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=82=E6=95=B0=E6=A3=80=E6=9F=A5=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB=E5=A2=9E=E5=8A=A0=E6=9C=80=E5=A4=A7=E9=95=BF?= =?UTF-8?q?=E5=BA=A6=E6=A3=80=E6=9F=A5=E6=96=B9=E6=B3=95,=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=B8=B8=E9=87=8F=E6=B1=A0=E5=A2=9E=E5=8A=A0=E8=B6=85?= =?UTF-8?q?=E5=87=BA=E9=95=BF=E5=BA=A6=E6=8F=90=E7=A4=BA=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/constant/ExceptionConstant.java | 2 + .../server/util/ParamCheckUtil.java | 138 ++++++++++-------- 2 files changed, 79 insertions(+), 61 deletions(-) diff --git a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/constant/ExceptionConstant.java b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/constant/ExceptionConstant.java index 6ed4c3e9..ef2ba6f9 100644 --- a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/constant/ExceptionConstant.java +++ b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/constant/ExceptionConstant.java @@ -41,6 +41,8 @@ public enum ExceptionConstant { PARAMS_NOT_NULL(10, "参数[{}]不能为空"), PARAMS_NOT_EMPTY(11, "参数[{}]不能为空"), + FILED_LENGTH_OUT(12 ,"{}不能超出长度{}"), + // 用户管理(11000) // license管理(12000) LICENSE_NAME_CANNOT_REPEAT(12000, "license名称不能重复"), diff --git a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/util/ParamCheckUtil.java b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/util/ParamCheckUtil.java index c921d34e..f57b7677 100644 --- a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/util/ParamCheckUtil.java +++ b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/util/ParamCheckUtil.java @@ -9,82 +9,98 @@ import java.util.Map; import java.util.Set; /** - * * @author: aoshiguchen * @date: 2022/8/1 */ public class ParamCheckUtil { - public static void checkNotNull(Object obj, String name) { - if (null == obj) { - throw ServiceException.create(ExceptionConstant.PARAMS_NOT_NULL, name); - } - } + public static void checkNotNull(Object obj, String name) { + if (null == obj) { + throw ServiceException.create(ExceptionConstant.PARAMS_NOT_NULL, name); + } + } - public static void checkNotEmpty(String str, String name) { - if (StrUtil.isEmpty(str)) { - throw ServiceException.create(ExceptionConstant.PARAMS_NOT_EMPTY, name); - } - } + public static void checkNotEmpty(String str, String name) { + if (StrUtil.isEmpty(str)) { + throw ServiceException.create(ExceptionConstant.PARAMS_NOT_EMPTY, name); + } + } - public static void checkNotEmpty(Collection collection, String name) { - if (null == collection || collection.isEmpty()) { - throw ServiceException.create(ExceptionConstant.PARAMS_NOT_EMPTY, name); - } - } + public static void checkNotEmpty(Collection collection, String name) { + if (null == collection || collection.isEmpty()) { + throw ServiceException.create(ExceptionConstant.PARAMS_NOT_EMPTY, name); + } + } - public static void checkNotEmpty(Map map, String name) { - if (null == map || map.isEmpty()) { - throw ServiceException.create(ExceptionConstant.PARAMS_NOT_EMPTY, name); - } - } + public static void checkNotEmpty(Map map, String name) { + if (null == map || map.isEmpty()) { + throw ServiceException.create(ExceptionConstant.PARAMS_NOT_EMPTY, name); + } + } - public static void checkNotEmpty(Set set, String name) { - if (null == set || set.isEmpty()) { - throw ServiceException.create(ExceptionConstant.PARAMS_NOT_EMPTY, name); - } - } + public static void checkNotEmpty(Set set, String name) { + if (null == set || set.isEmpty()) { + throw ServiceException.create(ExceptionConstant.PARAMS_NOT_EMPTY, name); + } + } - public static void checkMustNull(Object obj, ExceptionConstant constant, Object... params) { - if (null != obj) { - throw ServiceException.create(constant, params); - } - } + public static void checkMustNull(Object obj, ExceptionConstant constant, Object... params) { + if (null != obj) { + throw ServiceException.create(constant, params); + } + } - public static void checkNotNull(Object obj, ExceptionConstant constant, Object... params) { - if (null == obj) { - throw ServiceException.create(constant, params); - } - } + public static void checkNotNull(Object obj, ExceptionConstant constant, Object... params) { + if (null == obj) { + throw ServiceException.create(constant, params); + } + } - public static void checkNotEmpty(String str, ExceptionConstant constant, Object... params) { - if (StrUtil.isEmpty(str)) { - throw ServiceException.create(constant, params); - } - } + public static void checkNotEmpty(String str, ExceptionConstant constant, Object... params) { + if (StrUtil.isEmpty(str)) { + throw ServiceException.create(constant, params); + } + } - public static void checkNotEmpty(Collection collection, ExceptionConstant constant, Object... params) { - if (null == collection || collection.isEmpty()) { - throw ServiceException.create(constant, params); - } - } + public static void checkNotEmpty(Collection collection, ExceptionConstant constant, Object... params) { + if (null == collection || collection.isEmpty()) { + throw ServiceException.create(constant, params); + } + } - public static void checkNotEmpty(Map map, ExceptionConstant constant, Object... params) { - if (null == map || map.isEmpty()) { - throw ServiceException.create(constant, params); - } - } + public static void checkNotEmpty(Map map, ExceptionConstant constant, Object... params) { + if (null == map || map.isEmpty()) { + throw ServiceException.create(constant, params); + } + } - public static void checkNotEmpty(Set set, ExceptionConstant constant, Object... params) { - if (null == set || set.isEmpty()) { - throw ServiceException.create(constant, params); - } - } + public static void checkNotEmpty(Set set, ExceptionConstant constant, Object... params) { + if (null == set || set.isEmpty()) { + throw ServiceException.create(constant, params); + } + } + + public static void checkExpression(boolean expression, ExceptionConstant constant, Object... params) { + if (!expression) { + throw ServiceException.create(constant, params); + } + } + + /** + * check max length , if the string length exceeded ,an exception is thrown + * if the string is empty , it's not checked + * 检查最大长度,超出长度,则抛出异常 . 如果字符串本身为空,则不进行检查 + * + * @param str String + * @param maxLength Maximum length + * @param params Exception parameters + * @throws {@link ServiceException} + */ + public static void checkMaxLength(String str, int maxLength, Object... params) { + if (!StrUtil.isEmpty(str) && maxLength < str.length()) { + throw ServiceException.create(ExceptionConstant.FILED_LENGTH_OUT, params); + } + } - public static void checkExpression(boolean expression, ExceptionConstant constant, Object... params) { - if (!expression) { - throw ServiceException.create(constant, params); - } - } }