View Javadoc

    1   /*
    2    * $Id: AnnotationValidationInterceptor.java 502294 2007-02-01 17:28:00Z niallp $
    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  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      // FIXME: This is copied from DefaultActionInvocation but should be exposed through the interface
    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              // hmm -- OK, try doXxx instead
    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                  // throw the original one
    63                  throw e;
    64              }
    65          }
    66          return method;
    67      }
    68  }