Validate & CommonUtils

常有校验参数的需求,却又没有合适的工具可以使用
因此仿造spring web的校验机制,结合hibernate的校验功能,实现了这样一个小工具
其实还有一些有趣的小工具,虽然可能有各种的瑕疵或者各种原因没能竣工,不过还是蛮有趣的
欢迎去看看 to github line

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.ca.util.utils;

import com.google.common.base.Stopwatch;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.internal.metadata.core.ConstraintHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

import static com.google.common.base.Preconditions.checkArgument;

/**
* 参数校验工具
* Spring的参数校验好像就是基于Hibernate的Validator实现类
* 在寻找一个直接调用一下,就可以触发参数校验的方式……但是没找到
* 自己写了一个依赖于Hibernate的,很麻烦的参数校验
* <p>
* updated at 17-01-13
* 修改了一下ValidateUtil, 解决了用map的单例问题,自己实现了NotBlank的校验器,不然每次都要校验notnull和notBlank,太麻烦了
*
* @author guohang.ding on 16-11-8
*/
public class ValidateUtil {

private static final ConstraintHelper helper = new ConstraintHelper();
private static final Logger LOG = LoggerFactory.getLogger(ValidateUtil.class);
private static final String PARAMS_INVALID = "Params_Invalid";
public static final String SYSTEM_ERROR = "System_Error";

private static final ConcurrentHashMap<Class<? extends ConstraintValidator<? extends Annotation, ?>>,
ConstraintValidator> cache = new ConcurrentHashMap<>();

static {
NotBlankValidator validator = new NotBlankValidator();
cache.put(org.hibernate.validator.internal.constraintvalidators.NotBlankValidator.class, validator);
}


@SuppressWarnings("unchecked")
public static void checkRequest(Object request) {
checkArgument(request != null, PARAMS_INVALID);
Stopwatch watch = Stopwatch.createStarted();

try {
boolean valid = true;
LOG.info("validate request: {}", request);

Field[] fields = request.getClass().getDeclaredFields();
if (ArrayUtils.isEmpty(fields)) {
LOG.info("empty fields for request: {}", request.getClass());
return;
}

Annotation[] annotations;
for (Field field : fields) {
annotations = field.getDeclaredAnnotations();
if (ArrayUtils.isEmpty(annotations)) {
continue;
}
for (Annotation annotation : annotations) {
if (annotation.annotationType().getAnnotation(Constraint.class) == null) {
continue;
}
if (CollectionUtils.isEmpty(helper.getAllValidatorClasses(annotation.annotationType()))) {
continue;
}
for (Class<? extends ConstraintValidator<? extends Annotation, ?>> clazz
: helper.getAllValidatorClasses(annotation.annotationType())) {
try {
ConstraintValidator validator = getValidator(clazz);
validator.initialize(annotation);

field.setAccessible(true);
Object req = field.get(request);
valid = validator.isValid(req, null);
if (!valid) {
LOG.error("invalid params, req-{}, val-{}, annotation-{}", req, field.getName(), annotation);
break;
}
} catch (Exception e) {
LOG.error("validator validate error", e);
throw new IllegalArgumentException(SYSTEM_ERROR);
}
}
checkArgument(valid, PARAMS_INVALID);
}
}
} finally {
LOG.info("valid cost {}", watch.elapsed(TimeUnit.MILLISECONDS));
}
}

private static ConstraintValidator getValidator(Class<? extends ConstraintValidator<? extends Annotation, ?>> clazz)
throws IllegalAccessException, InstantiationException {
ConstraintValidator validator = cache.get(clazz);
if (validator == null) {
LOG.info("init validator: {}", clazz);
validator = clazz.newInstance();
ConstraintValidator value = cache.putIfAbsent(clazz, validator);
if (value != null) {
validator = value;
}
}

return validator;
}

private static class NotBlankValidator implements ConstraintValidator<NotBlank, CharSequence> {

public void initialize(NotBlank annotation) {
}

public boolean isValid(CharSequence charSequence, ConstraintValidatorContext constraintValidatorContext) {
return charSequence != null && charSequence.toString().trim().length() > 0;
}
}
}
喜欢的大佬,路过赏点儿呗~