View Javadoc

1   /*
2    * $Id: AnnotationValidationInterceptor.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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      // FIXME: This is copied from DefaultActionInvocation but should be exposed through the interface
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              // hmm -- OK, try doXxx instead
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                  // throw the original one
64                  throw e;
65              }
66          }
67          return method;
68      }
69  }