View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.interceptor;
17  
18  import static org.seasar.cubby.CubbyConstants.ATTR_PARAMS;
19  import static org.seasar.cubby.CubbyConstants.ATTR_VALIDATION_FAIL;
20  
21  import java.util.Collections;
22  import java.util.List;
23  import java.util.Map;
24  
25  import javax.servlet.http.HttpServletRequest;
26  
27  import org.aopalliance.intercept.MethodInterceptor;
28  import org.aopalliance.intercept.MethodInvocation;
29  import org.seasar.cubby.CubbyConstants;
30  import org.seasar.cubby.action.Action;
31  import org.seasar.cubby.action.ActionErrors;
32  import org.seasar.cubby.action.Forward;
33  import org.seasar.cubby.action.Validation;
34  import org.seasar.cubby.controller.ActionContext;
35  import org.seasar.cubby.validator.ValidationProcessor;
36  import org.seasar.cubby.validator.ValidationRule;
37  import org.seasar.cubby.validator.ValidationRules;
38  import org.seasar.framework.beans.BeanDesc;
39  import org.seasar.framework.beans.PropertyDesc;
40  import org.seasar.framework.beans.factory.BeanDescFactory;
41  
42  /**
43   * 入力検証を行います。
44   * <p>
45   * 入力検証が失敗した場合
46   * <ul>
47   * <li>またリクエスト中の入力検証エラーフラグを <code>true</code> に設定します。</li>
48   * <li>アクションメソッドに設定された{@link Validation#errorPage()}へフォワードします。</li>
49   * </ul>
50   * </p>
51   * 
52   * @see CubbyConstants#ATTR_VALIDATION_FAIL 入力検証エラーフラグの属性名
53   * @author agata
54   * @author baba
55   * @since 1.0
56   */
57  public class ValidationInterceptor implements MethodInterceptor {
58  
59  	private static final ValidationRules NULL_VALIDATION_RULES = new ValidationRules() {
60  		public List<ValidationRule> getRules() {
61  			return Collections.emptyList();
62  		}
63  	};
64  
65  	private ValidationProcessor validationProcessor;
66  
67  	private HttpServletRequest request;
68  
69  	private ActionContext context;
70  
71  	public ValidationInterceptor() {
72  	}
73  
74  	public void setValidationProcessor(
75  			final ValidationProcessor validationProcessor) {
76  		this.validationProcessor = validationProcessor;
77  	}
78  
79  	public void setRequest(final HttpServletRequest request) {
80  		this.request = request;
81  	}
82  
83  	public void setActionContext(final ActionContext context) {
84  		this.context = context;
85  	}
86  
87  	public Object invoke(final MethodInvocation invocation) throws Throwable {
88  		final Validation validation = context.getValidation();
89  
90  		final boolean success;
91  		if (validation == null) {
92  			success = true;
93  		} else {
94  			final Map<String, Object[]> params = getParams(request);
95  			final Object form = context.getFormBean();
96  			final ActionErrors errors = context.getAction().getErrors();
97  			final ValidationRules rules = getValidationRules(context);
98  			success = validationProcessor.process(errors, params, form, rules);
99  		}
100 
101 		final Object result;
102 		if (success) {
103 			result = invocation.proceed();
104 		} else {
105 			request.setAttribute(ATTR_VALIDATION_FAIL, true);
106 			final String path = validation.errorPage();
107 			result = new Forward(path);
108 		}
109 
110 		return result;
111 	}
112 
113 	private ValidationRules getValidationRules(final ActionContext context) {
114 		final Validation validation = context.getValidation();
115 		final ValidationRules rules;
116 		if (validation == null) {
117 			rules = NULL_VALIDATION_RULES;
118 		} else {
119 			final Action action = context.getAction();
120 			final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(action
121 					.getClass());
122 			final PropertyDesc propertyDesc = beanDesc
123 					.getPropertyDesc(validation.rules());
124 			rules = (ValidationRules) propertyDesc.getValue(action);
125 		}
126 		return rules;
127 	}
128 
129 	@SuppressWarnings("unchecked")
130 	private static Map<String, Object[]> getParams(
131 			final HttpServletRequest request) {
132 		return (Map<String, Object[]>) request.getAttribute(ATTR_PARAMS);
133 	}
134 
135 }