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  
20  import java.util.Map;
21  
22  import javax.servlet.http.HttpServletRequest;
23  
24  import org.aopalliance.intercept.MethodInterceptor;
25  import org.aopalliance.intercept.MethodInvocation;
26  import org.seasar.cubby.action.Action;
27  import org.seasar.cubby.action.ActionResult;
28  import org.seasar.cubby.controller.ActionContext;
29  import org.seasar.cubby.controller.CubbyConfiguration;
30  import org.seasar.cubby.controller.RequestParser;
31  import org.seasar.cubby.controller.ThreadContext;
32  import org.seasar.cubby.dxo.FormDxo;
33  
34  /**
35   * コントローラの初期化や実行結果のrequest/sessionへの反映などを行うインターセプタです。
36   * {@link Action#initialize()}、{@link Action#prerender()} の実行を行います。
37   * 
38   * @author agata
39   * @author baba
40   * @since 1.0
41   */
42  public class InitializeInterceptor implements MethodInterceptor {
43  
44  	private HttpServletRequest request;
45  
46  	private ActionContext context;
47  
48  	public void setRequest(final HttpServletRequest request) {
49  		this.request = request;
50  	}
51  
52  	public void setActionContext(final ActionContext context) {
53  		this.context = context;
54  	}
55  
56  	public Object invoke(final MethodInvocation invocation) throws Throwable {
57  		setupParams(context, request);
58  		setupForm(context, request);
59  
60  		final Action action = context.getAction();
61  		action.initialize();
62  
63  		final ActionResult result = (ActionResult) invocation.proceed();
64  		if (result != null) {
65  			result.prerender(context);
66  		}
67  
68  		return result;
69  	}
70  
71  	void setupParams(final ActionContext context,
72  			final HttpServletRequest request) {
73  		final CubbyConfiguration configuration = ThreadContext
74  				.getConfiguration();
75  		final RequestParser requestParser = configuration.getRequestParser();
76  		final Map<String, Object[]> parameterMap = requestParser
77  				.getParameterMap(request);
78  		request.setAttribute(ATTR_PARAMS, parameterMap);
79  	}
80  
81  	private void setupForm(final ActionContext context,
82  			final HttpServletRequest request) {
83  		final Object formBean = context.getFormBean();
84  		if (formBean != null) {
85  			final FormDxo formDxo = context.getFormDxo();
86  			final Map<String, Object[]> params = getParams(request);
87  			formDxo.convert(params, formBean);
88  		}
89  	}
90  
91  	@SuppressWarnings("unchecked")
92  	private static Map<String, Object[]> getParams(
93  			final HttpServletRequest request) {
94  		return (Map<String, Object[]>) request.getAttribute(ATTR_PARAMS);
95  	}
96  
97  }