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 import java.util.Arrays;
26 import java.util.Collection;
27
28 import org.apache.commons.lang.ArrayUtils;
29
30 import com.opensymphony.xwork2.ActionInvocation;
31 import com.opensymphony.xwork2.util.AnnotationUtils;
32 import com.opensymphony.xwork2.validator.ValidationInterceptor;
33
34 /***
35 * Extends the xwork validation interceptor to also check for a @SkipValidation
36 * annotation, and if found, don't validate this action method
37 */
38 public class AnnotationValidationInterceptor extends ValidationInterceptor {
39
40 /*** Auto-generated serialization id */
41 private static final long serialVersionUID = 1813272797367431184L;
42
43 protected String doIntercept(ActionInvocation invocation) throws Exception {
44
45 Object action = invocation.getAction();
46 if (action != null) {
47 Method method = getActionMethod(action.getClass(), invocation.getProxy().getMethod());
48 Collection<Method> annotatedMethods = AnnotationUtils.getAnnotatedMethods(action.getClass(), SkipValidation.class);
49 if (annotatedMethods.contains(method))
50 return invocation.invoke();
51
52
53 Class clazz = action.getClass().getSuperclass();
54 while (clazz != null) {
55 annotatedMethods = AnnotationUtils.getAnnotatedMethods(clazz, SkipValidation.class);
56 if (annotatedMethods != null) {
57 for (Method annotatedMethod : annotatedMethods) {
58 if (annotatedMethod.getName().equals(method.getName())
59 && Arrays.equals(annotatedMethod.getParameterTypes(), method.getParameterTypes())
60 && Arrays.equals(annotatedMethod.getExceptionTypes(), method.getExceptionTypes()))
61 return invocation.invoke();
62 }
63 }
64 clazz = clazz.getSuperclass();
65 }
66 }
67
68 return super.doIntercept(invocation);
69 }
70
71
72 protected Method getActionMethod(Class actionClass, String methodName) throws NoSuchMethodException {
73 Method method;
74 try {
75 method = actionClass.getMethod(methodName, new Class[0]);
76 } catch (NoSuchMethodException e) {
77
78 try {
79 String altMethodName = "do" + methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
80 method = actionClass.getMethod(altMethodName, new Class[0]);
81 } catch (NoSuchMethodException e1) {
82
83 throw e;
84 }
85 }
86 return method;
87 }
88 }