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.*;
19  import static org.seasar.cubby.interceptor.InterceptorUtils.getAction;
20  import static org.seasar.cubby.interceptor.InterceptorUtils.getActionClass;
21  import static org.seasar.cubby.interceptor.InterceptorUtils.getMethod;
22  
23  import java.lang.reflect.Method;
24  import java.util.Map;
25  
26  import javax.servlet.http.HttpServletRequest;
27  
28  import org.aopalliance.intercept.MethodInterceptor;
29  import org.aopalliance.intercept.MethodInvocation;
30  import org.seasar.cubby.action.Action;
31  import org.seasar.cubby.action.ActionResult;
32  import org.seasar.cubby.controller.RequestParameterBinder;
33  import org.seasar.cubby.util.CubbyUtils;
34  
35  /**
36   * アクションの初期化やリクエストパラメータからフォームオブジェクトへのバインドなどを行うインターセプタです。
37   * 
38   * @author agata
39   * @author baba
40   * @since 1.0.0
41   */
42  public class InitializeInterceptor implements MethodInterceptor {
43  
44  	/** リクエスト。 */
45  	private HttpServletRequest request;
46  
47  	/** リクエストパラメータをオブジェクトへバインドするクラス。 */
48  	private RequestParameterBinder requestParameterBinder;
49  
50  	/**
51  	 * リクエストを設定します。
52  	 * 
53  	 * @param request
54  	 *            リクエスト
55  	 */
56  	public void setRequest(final HttpServletRequest request) {
57  		this.request = request;
58  	}
59  
60  	/**
61  	 * リクエストパラメータをオブジェクトへバインドするクラスを設定します。
62  	 * 
63  	 * @param requestParameterBinder
64  	 *            リクエストパラメータをオブジェクトへバインドするクラス
65  	 */
66  	public void setRequestParameterBinder(
67  			final RequestParameterBinder requestParameterBinder) {
68  		this.requestParameterBinder = requestParameterBinder;
69  	}
70  
71  	/**
72  	 * {@inheritDoc}
73  	 * <p>
74  	 * 以下のようなフローでアクションメソッドを実行します。
75  	 * <ul>
76  	 * <li>{@link Action#invokeInitializeMethod(Method)} を呼び出してアクションを初期化します。</li>
77  	 * <li>{@link RequestParameterBinder#bind(Map, Object, Method)}
78  	 * によってリクエストパラメータをフォームオブジェクトにバインドします。</li>
79  	 * <li>アクションメソッドを呼び出します。</li>
80  	 * <li>メソッドの実行結果を返します。</li>
81  	 * </ul>
82  	 * </p>
83  	 */
84  	public Object invoke(final MethodInvocation invocation) throws Throwable {
85  		final Map<String, Object[]> parameterMap = CubbyUtils.getAttribute(
86  				request, ATTR_PARAMS);
87  
88  		final Action action = getAction(invocation);
89  		final Class<? extends Action> actionClass = getActionClass(invocation);
90  		final Method method = getMethod(invocation);
91  		action.invokeInitializeMethod(method);
92  		final Object form = CubbyUtils.getFormBean(action, actionClass, method);
93  		if (form != null) {
94  			requestParameterBinder.bind(parameterMap, form, actionClass, method);
95  		}
96  
97  		final ActionResult result = (ActionResult) invocation.proceed();
98  		return result;
99  	}
100 
101 }