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