Repository navigation
Expand file tree
/
Copy pathSizeHandler.java
More file actions
63 lines (54 loc) · 1.98 KB
/
Copy pathSizeHandler.java
File metadata and controls
63 lines (54 loc) · 1.98 KB
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
package act.validation;
import act.ActComponent;
import act.app.ActionContext;
import act.conf.AppConfig;
import act.controller.ActionMethodParamAnnotationHandlerPlugin;
import org.osgl.util.C;
import org.osgl.util.E;
import javax.validation.MessageInterpolator;
import javax.validation.constraints.Size;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
@ActComponent
public class SizeHandler extends ActionMethodParamAnnotationHandlerPlugin {
@Override
public Set<Class<? extends Annotation>> listenTo() {
Set<Class<? extends Annotation>> set = C.newSet();
set.add(Size.class);
return set;
}
@Override
public void handle(String paramName, Object paramVal, Annotation annotation, ActionContext context) {
if (null == paramVal) {
return;
}
int size = sizeOf(paramVal);
Size theAnno = (Size) annotation;
if (size < theAnno.min() || size > theAnno.max()) {
context.addViolation(new ActionMethodParamConstraintViolation<Object>(paramVal, theAnno.message(), theAnno, context));
}
}
private int sizeOf(Object val) {
if (val instanceof CharSequence) {
return ((CharSequence)val).length();
} else if (val instanceof Collection) {
return ((Collection) val).size();
} else if (val instanceof Map) {
return ((Map) val).size();
} else if (val.getClass().isArray()) {
return Array.getLength(val);
} else {
throw E.unexpected("Unexpected para val type: %s", val.getClass());
}
}
private String interpolate(Size sizeAnno, int curSize, ActionContext context) {
AppConfig config = context.config();
String tmpl = sizeAnno.message();
MessageInterpolator interpolator = config.validationMessageInterpolator();
//MessageInterpolator.Context mc =
return null;
}
}