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.Map; |
22 | |
|
23 | |
import javax.servlet.http.HttpServletRequest; |
24 | |
|
25 | |
import org.aopalliance.intercept.MethodInterceptor; |
26 | |
import org.aopalliance.intercept.MethodInvocation; |
27 | |
import org.seasar.cubby.CubbyConstants; |
28 | |
import org.seasar.cubby.action.Action; |
29 | |
import org.seasar.cubby.action.ActionErrors; |
30 | |
import org.seasar.cubby.action.Forward; |
31 | |
import org.seasar.cubby.action.Validation; |
32 | |
import org.seasar.cubby.controller.ActionContext; |
33 | |
import org.seasar.cubby.validator.ValidationProcessor; |
34 | |
import org.seasar.cubby.validator.ValidationRules; |
35 | |
import org.seasar.framework.beans.BeanDesc; |
36 | |
import org.seasar.framework.beans.PropertyDesc; |
37 | |
import org.seasar.framework.beans.factory.BeanDescFactory; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public class ValidationInterceptor implements MethodInterceptor { |
55 | |
|
56 | |
|
57 | |
private ValidationProcessor validationProcessor; |
58 | |
|
59 | |
|
60 | |
private HttpServletRequest request; |
61 | |
|
62 | |
|
63 | |
private ActionContext context; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | 0 | public ValidationInterceptor() { |
69 | 0 | } |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
public void setValidationProcessor( |
78 | |
final ValidationProcessor validationProcessor) { |
79 | 0 | this.validationProcessor = validationProcessor; |
80 | 0 | } |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
public void setRequest(final HttpServletRequest request) { |
89 | 0 | this.request = request; |
90 | 0 | } |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
public void setActionContext(final ActionContext context) { |
99 | 0 | this.context = context; |
100 | 0 | } |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
public Object invoke(final MethodInvocation invocation) throws Throwable { |
109 | 0 | final Validation validation = context.getValidation(); |
110 | |
final boolean success; |
111 | 0 | if (validation == null) { |
112 | 0 | success = true; |
113 | |
} else { |
114 | 0 | final Map<String, Object[]> params = getAttribute(request, |
115 | |
ATTR_PARAMS); |
116 | 0 | final Object form = context.getFormBean(); |
117 | 0 | final ActionErrors errors = context.getAction().getErrors(); |
118 | 0 | final ValidationRules rules = getValidationRules(context); |
119 | 0 | success = validationProcessor.process(errors, params, form, rules); |
120 | |
} |
121 | |
|
122 | |
final Object result; |
123 | 0 | if (success) { |
124 | 0 | result = invocation.proceed(); |
125 | |
} else { |
126 | 0 | request.setAttribute(ATTR_VALIDATION_FAIL, true); |
127 | 0 | final String path = validation.errorPage(); |
128 | 0 | result = new Forward(path); |
129 | |
} |
130 | |
|
131 | 0 | return result; |
132 | |
} |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
private ValidationRules getValidationRules(final ActionContext context) { |
142 | 0 | final Validation validation = context.getValidation(); |
143 | 0 | final Action action = context.getAction(); |
144 | 0 | final BeanDesc beanDesc = BeanDescFactory |
145 | |
.getBeanDesc(action.getClass()); |
146 | 0 | final PropertyDesc propertyDesc = beanDesc.getPropertyDesc(validation |
147 | |
.rules()); |
148 | 0 | final ValidationRules rules = (ValidationRules) propertyDesc |
149 | |
.getValue(action); |
150 | 0 | return rules; |
151 | |
} |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
@SuppressWarnings("unchecked") |
165 | |
private static <T> T getAttribute(final HttpServletRequest request, |
166 | |
final String name) { |
167 | 0 | return (T) request.getAttribute(name); |
168 | |
} |
169 | |
|
170 | |
} |