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); - } - } }