01: /*
02: * $Id: AnnotationValidationInterceptor.java 502294 2007-02-01 17:28:00Z niallp $
03: *
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "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
33: ValidationInterceptor {
34:
35: /** Auto-generated serialization id */
36: private static final long serialVersionUID = 1813272797367431184L;
37:
38: protected String doIntercept(ActionInvocation invocation)
39: throws Exception {
40:
41: Object action = invocation.getAction();
42: if (action != null) {
43: Method method = getActionMethod(action.getClass(),
44: invocation.getProxy().getMethod());
45: SkipValidation skip = (SkipValidation) method
46: .getAnnotation(SkipValidation.class);
47: if (skip != null) {
48: return invocation.invoke();
49: }
50: }
51:
52: return super .doIntercept(invocation);
53: }
54:
55: // FIXME: This is copied from DefaultActionInvocation but should be exposed through the interface
56: protected Method getActionMethod(Class actionClass,
57: String methodName) throws NoSuchMethodException {
58: Method method;
59: try {
60: method = actionClass.getMethod(methodName, new Class[0]);
61: } catch (NoSuchMethodException e) {
62: // hmm -- OK, try doXxx instead
63: try {
64: String altMethodName = "do"
65: + methodName.substring(0, 1).toUpperCase()
66: + methodName.substring(1);
67: method = actionClass.getMethod(altMethodName,
68: new Class[0]);
69: } catch (NoSuchMethodException e1) {
70: // throw the original one
71: throw e;
72: }
73: }
74: return method;
75: }
76: }
|