diff --git a/jap-core/src/main/java/com/fujieid/jap/core/annotation/NotEmpty.java b/jap-core/src/main/java/com/fujieid/jap/core/annotation/NotEmpty.java new file mode 100644 index 0000000000000000000000000000000000000000..7124db7896f822feb424f61ea1b28644953c494f --- /dev/null +++ b/jap-core/src/main/java/com/fujieid/jap/core/annotation/NotEmpty.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2020-2040, 北京符节科技有限公司 (support@fujieid.com & https://www.fujieid.com). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.fujieid.jap.core.annotation; + +import java.lang.annotation.*; + +/** + * The annotated element must not be {@code null} nor empty. Supported types are: + *

+ * + * @author yadong.zhang (yadong.zhang0415(a)gmail.com) + * @version 1.0.0 + * @since 1.0.6 + */ +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.FIELD, ElementType.PARAMETER}) +public @interface NotEmpty { + + String value() default ""; + + String message(); +} diff --git a/jap-core/src/main/java/com/fujieid/jap/core/util/JapValidator.java b/jap-core/src/main/java/com/fujieid/jap/core/util/JapValidator.java new file mode 100644 index 0000000000000000000000000000000000000000..27d8db25d80adcbfb26b0fd195982cde9e330054 --- /dev/null +++ b/jap-core/src/main/java/com/fujieid/jap/core/util/JapValidator.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2020-2040, 北京符节科技有限公司 (support@fujieid.com & https://www.fujieid.com). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.fujieid.jap.core.util; + +import cn.hutool.core.util.ArrayUtil; +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.ReflectUtil; +import com.fujieid.jap.core.annotation.NotEmpty; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; + +/** + * @author yadong.zhang (yadong.zhang0415(a)gmail.com) + * @version 1.0.0 + * @since 1.0.6 + */ +public class JapValidator { + + public static List validate(Object o) { + List errors = new ArrayList<>(); + if (null == o) { + return errors; + } + Field[] fields = ReflectUtil.getFields(o.getClass()); + if (ArrayUtil.isEmpty(fields)) { + return errors; + } + for (Field field : fields) { + Annotation[] annotations = field.getAnnotations(); + if (ArrayUtil.isEmpty(annotations)) { + return errors; + } + for (Annotation annotation : annotations) { + if (annotation instanceof NotEmpty) { + Object object = ReflectUtil.getFieldValue(o, field); + if (ObjectUtil.isEmpty(object)) { + errors.add(((NotEmpty) annotation).message()); + } + } + } + } + return errors; + } +} diff --git a/jap-ldap/src/main/java/com/fujieid/jap/ldap/LdapConfig.java b/jap-ldap/src/main/java/com/fujieid/jap/ldap/LdapConfig.java index ed0b978e421cac1a1cef23124e93621dc774dc7a..8d82d382cb59a0f8f8973b079c667a5bb1ccb146 100644 --- a/jap-ldap/src/main/java/com/fujieid/jap/ldap/LdapConfig.java +++ b/jap-ldap/src/main/java/com/fujieid/jap/ldap/LdapConfig.java @@ -15,6 +15,7 @@ */ package com.fujieid.jap.ldap; +import com.fujieid.jap.core.annotation.NotEmpty; import com.fujieid.jap.core.config.AuthenticateConfig; /** @@ -26,18 +27,22 @@ public class LdapConfig extends AuthenticateConfig { /** * LDAP data source URL, such as ldap://localhost:389 */ + @NotEmpty(message = "LDAP data source URL cannot be empty.") private String url; /** * LDAP user name, such as: cn=admin,dc=example,dc=org */ + @NotEmpty(message = "LDAP bindDn cannot be empty.") private String bindDn; /** - * LDAP user password + * LDAP administrator password */ + @NotEmpty(message = "LDAP administrator password cannot be empty") private String credentials; /** * Basic catalog */ + @NotEmpty(message = "LDAP Basic catalog cannot be empty") private String baseDn; /** * Query conditions, such as: (&(objectClass=organizationalPerson)(uid=%s)) diff --git a/jap-ldap/src/test/java/com/fujieid/jap/ldap/LdapConfigTest.java b/jap-ldap/src/test/java/com/fujieid/jap/ldap/LdapConfigTest.java new file mode 100644 index 0000000000000000000000000000000000000000..f51ae8648c2f3703b2bc2704887956b75dd84caa --- /dev/null +++ b/jap-ldap/src/test/java/com/fujieid/jap/ldap/LdapConfigTest.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2020-2040, 北京符节科技有限公司 (support@fujieid.com & https://www.fujieid.com). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.fujieid.jap.ldap; + +import com.fujieid.jap.core.util.JapValidator; +import org.junit.Test; + +import java.util.List; + +/** + * @author yadong.zhang (yadong.zhang0415(a)gmail.com) + * @version 1.0.0 + * @since 1.0.0 + */ +public class LdapConfigTest { + + @Test + public void annotationTest() { + LdapConfig config = new LdapConfig() + .setUrl("1") + .setCredentials("a"); + List errors = JapValidator.validate(config); + for (String error : errors) { + System.out.println(error); + } + } +}