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.controller.impl;
17  
18  import javax.servlet.FilterChain;
19  import javax.servlet.http.HttpServletRequest;
20  import javax.servlet.http.HttpServletResponse;
21  
22  import org.seasar.cubby.action.ActionResult;
23  import org.seasar.cubby.controller.ActionContext;
24  import org.seasar.cubby.controller.ActionDef;
25  import org.seasar.cubby.controller.ActionDefBuilder;
26  import org.seasar.cubby.controller.ActionProcessor;
27  import org.seasar.cubby.exception.ActionRuntimeException;
28  import org.seasar.cubby.filter.CubbyHttpServletRequestWrapper;
29  import org.seasar.framework.log.Logger;
30  
31  /**
32   * リクエストのパスを元にアクションメソッドを決定して実行するクラスの実装です。
33   * 
34   * @author baba
35   * @since 1.0.0
36   */
37  public class ActionProcessorImpl implements ActionProcessor {
38  
39  	/** ロガー。 */
40  	private static final Logger logger = Logger
41  			.getLogger(ActionProcessorImpl.class);
42  
43  	/** アクションのコンテキスト。 */
44  	private ActionContext context;
45  
46  	/** アクションの定義を組み立てるビルダ。 */
47  	private ActionDefBuilder actionDefBuilder;
48  
49  	/**
50  	 * アクションのコンテキストを設定します。
51  	 * 
52  	 * @param context
53  	 *            アクションのコンテキスト
54  	 */
55  	public void setActionContext(final ActionContext context) {
56  		this.context = context;
57  	}
58  
59  	/**
60  	 * アクションの定義を組み立てるビルダを設定します。
61  	 * 
62  	 * @param actionDefBuilder
63  	 *            アクションの定義を組み立てるビルダ
64  	 */
65  	public void setActionDefBuilder(final ActionDefBuilder actionDefBuilder) {
66  		this.actionDefBuilder = actionDefBuilder;
67  	}
68  
69  	/**
70  	 * {@inheritDoc}
71  	 */
72  	public ActionResult process(final HttpServletRequest request,
73  			final HttpServletResponse response, final FilterChain chain)
74  			throws Exception {
75  		final ActionDef actionDef = actionDefBuilder.build(request);
76  		if (actionDef != null) {
77  			context.initialize(actionDef);
78  			if (logger.isDebugEnabled()) {
79  				logger
80  						.log("DCUB0004",
81  								new Object[] { request.getRequestURI() });
82  				logger.log("DCUB0005", new Object[] { context.getMethod() });
83  			}
84  			final ActionResult result = context.invoke();
85  			if (result == null) {
86  				throw new ActionRuntimeException("ECUB0101",
87  						new Object[] { context.getMethod() });
88  			}
89  			final HttpServletRequest wrappedRequest = new CubbyHttpServletRequestWrapper(
90  					request, context);
91  			result.execute(context, wrappedRequest, response);
92  			return result;
93  		} else {
94  			final HttpServletRequest wrappedRequest = new CubbyHttpServletRequestWrapper(
95  					request, context);
96  			chain.doFilter(wrappedRequest, response);
97  			return null;
98  		}
99  	}
100 
101 }