1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.cubby.interceptor; |
17 | |
|
18 | |
import static org.seasar.cubby.CubbyConstants.ATTR_PARAMS; |
19 | |
import static org.seasar.cubby.CubbyConstants.ATTR_VALIDATION_FAIL; |
20 | |
|
21 | |
import java.util.Collections; |
22 | |
import java.util.List; |
23 | |
import java.util.Map; |
24 | |
|
25 | |
import javax.servlet.http.HttpServletRequest; |
26 | |
|
27 | |
import org.aopalliance.intercept.MethodInterceptor; |
28 | |
import org.aopalliance.intercept.MethodInvocation; |
29 | |
import org.seasar.cubby.CubbyConstants; |
30 | |
import org.seasar.cubby.action.Action; |
31 | |
import org.seasar.cubby.action.ActionErrors; |
32 | |
import org.seasar.cubby.action.Forward; |
33 | |
import org.seasar.cubby.action.Validation; |
34 | |
import org.seasar.cubby.controller.ActionContext; |
35 | |
import org.seasar.cubby.validator.ValidationProcessor; |
36 | |
import org.seasar.cubby.validator.ValidationRule; |
37 | |
import org.seasar.cubby.validator.ValidationRules; |
38 | |
import org.seasar.framework.beans.BeanDesc; |
39 | |
import org.seasar.framework.beans.PropertyDesc; |
40 | |
import org.seasar.framework.beans.factory.BeanDescFactory; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
public class ValidationInterceptor implements MethodInterceptor { |
58 | |
|
59 | 1 | private static final ValidationRules NULL_VALIDATION_RULES = new ValidationRules() { |
60 | 1 | public List<ValidationRule> getRules() { |
61 | 0 | return Collections.emptyList(); |
62 | |
} |
63 | |
}; |
64 | |
|
65 | |
private ValidationProcessor validationProcessor; |
66 | |
|
67 | |
private HttpServletRequest request; |
68 | |
|
69 | |
private ActionContext context; |
70 | |
|
71 | 0 | public ValidationInterceptor() { |
72 | 0 | } |
73 | |
|
74 | |
public void setValidationProcessor( |
75 | |
final ValidationProcessor validationProcessor) { |
76 | 0 | this.validationProcessor = validationProcessor; |
77 | 0 | } |
78 | |
|
79 | |
public void setRequest(final HttpServletRequest request) { |
80 | 0 | this.request = request; |
81 | 0 | } |
82 | |
|
83 | |
public void setActionContext(final ActionContext context) { |
84 | 0 | this.context = context; |
85 | 0 | } |
86 | |
|
87 | |
public Object invoke(final MethodInvocation invocation) throws Throwable { |
88 | 0 | final Validation validation = context.getValidation(); |
89 | |
|
90 | |
final boolean success; |
91 | 0 | if (validation == null) { |
92 | 0 | success = true; |
93 | |
} else { |
94 | 0 | final Map<String, Object[]> params = getParams(request); |
95 | 0 | final Object form = context.getFormBean(); |
96 | 0 | final ActionErrors errors = context.getAction().getErrors(); |
97 | 0 | final ValidationRules rules = getValidationRules(context); |
98 | 0 | success = validationProcessor.process(errors, params, form, rules); |
99 | |
} |
100 | |
|
101 | |
final Object result; |
102 | 0 | if (success) { |
103 | 0 | result = invocation.proceed(); |
104 | |
} else { |
105 | 0 | request.setAttribute(ATTR_VALIDATION_FAIL, true); |
106 | 0 | final String path = validation.errorPage(); |
107 | 0 | result = new Forward(path); |
108 | |
} |
109 | |
|
110 | 0 | return result; |
111 | |
} |
112 | |
|
113 | |
private ValidationRules getValidationRules(final ActionContext context) { |
114 | 0 | final Validation validation = context.getValidation(); |
115 | |
final ValidationRules rules; |
116 | 0 | if (validation == null) { |
117 | 0 | rules = NULL_VALIDATION_RULES; |
118 | |
} else { |
119 | 0 | final Action action = context.getAction(); |
120 | 0 | final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(action |
121 | |
.getClass()); |
122 | 0 | final PropertyDesc propertyDesc = beanDesc |
123 | |
.getPropertyDesc(validation.rules()); |
124 | 0 | rules = (ValidationRules) propertyDesc.getValue(action); |
125 | |
} |
126 | 0 | return rules; |
127 | |
} |
128 | |
|
129 | |
@SuppressWarnings("unchecked") |
130 | |
private static Map<String, Object[]> getParams( |
131 | |
final HttpServletRequest request) { |
132 | 0 | return (Map<String, Object[]>) request.getAttribute(ATTR_PARAMS); |
133 | |
} |
134 | |
|
135 | |
} |