1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.interceptor.validation;
23
24 import java.lang.reflect.Method;
25
26 import com.opensymphony.xwork2.ActionInvocation;
27 import com.opensymphony.xwork2.validator.ValidationInterceptor;
28
29 /***
30 * Extends the xwork validation interceptor to also check for a @SkipValidation
31 * annotation, and if found, don't validate this action method
32 */
33 public class AnnotationValidationInterceptor extends ValidationInterceptor {
34
35 /*** Auto-generated serialization id */
36 private static final long serialVersionUID = 1813272797367431184L;
37
38 protected String doIntercept(ActionInvocation invocation) throws Exception {
39
40 Object action = invocation.getAction();
41 if (action != null) {
42 Method method = getActionMethod(action.getClass(), invocation.getProxy().getMethod());
43 SkipValidation skip = (SkipValidation) method.getAnnotation(SkipValidation.class);
44 if (skip != null) {
45 return invocation.invoke();
46 }
47 }
48
49 return super.doIntercept(invocation);
50 }
51
52
53 protected Method getActionMethod(Class actionClass, String methodName) throws NoSuchMethodException {
54 Method method;
55 try {
56 method = actionClass.getMethod(methodName, new Class[0]);
57 } catch (NoSuchMethodException e) {
58
59 try {
60 String altMethodName = "do" + methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
61 method = actionClass.getMethod(altMethodName, new Class[0]);
62 } catch (NoSuchMethodException e1) {
63
64 throw e;
65 }
66 }
67 return method;
68 }
69 }