提交 f3baf3c9 编写于 作者: 智布道's avatar 智布道 👁

feat: Verify non-empty

上级 320d14fa
/*
* Copyright (c) 2020-2040, 北京符节科技有限公司 (support@fujieid.com & https://www.fujieid.com).
* <p>
* 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
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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:
* <ul>
* <li>{@code CharSequence} (length of character sequence is evaluated)</li>
* <li>{@code Collection} (collection size is evaluated)</li>
* <li>{@code Map} (map size is evaluated)</li>
* <li>Array (array length is evaluated)</li>
* </ul>
*
* @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();
}
/*
* Copyright (c) 2020-2040, 北京符节科技有限公司 (support@fujieid.com & https://www.fujieid.com).
* <p>
* 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
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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<String> validate(Object o) {
List<String> 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;
}
}
......@@ -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))
......
/*
* Copyright (c) 2020-2040, 北京符节科技有限公司 (support@fujieid.com & https://www.fujieid.com).
* <p>
* 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
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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<String> errors = JapValidator.validate(config);
for (String error : errors) {
System.out.println(error);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册