View Javadoc

1   /*
2    * Copyright 2004-2010 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  
17  package org.seasar.cubby.internal.controller.impl;
18  
19  import static org.seasar.cubby.CubbyConstants.ATTR_PARAMS;
20  
21  import java.util.Iterator;
22  import java.util.Map;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.seasar.cubby.internal.controller.ActionProcessor;
28  import org.seasar.cubby.internal.controller.ActionResultWrapper;
29  import org.seasar.cubby.internal.controller.ThreadContext;
30  import org.seasar.cubby.plugin.Plugin;
31  import org.seasar.cubby.plugin.PluginRegistry;
32  import org.seasar.cubby.plugin.RequestProcessingInvocation;
33  import org.seasar.cubby.routing.PathInfo;
34  import org.seasar.cubby.routing.Routing;
35  import org.seasar.cubby.spi.ProviderFactory;
36  import org.seasar.cubby.spi.RequestParserProvider;
37  
38  /**
39   * 要求処理の実行情報の実装です。
40   * 
41   * @author baba
42   */
43  public class RequestProcessingInvocationImpl
44  		implements
45  			RequestProcessingInvocation {
46  
47  	/** アクションを処理します。 */
48  	private final ActionProcessor actionProcessor = new ActionProcessorImpl();
49  
50  	/** 要求。 */
51  	private final HttpServletRequest request;
52  
53  	/** 応答。 */
54  	private final HttpServletResponse response;
55  
56  	/** パスから取得した情報。 */
57  	private final PathInfo pathInfo;
58  
59  	/** プラグインのイテレータ。 */
60  	private final Iterator<Plugin> pluginsIterator;
61  
62  	/**
63  	 * インスタンス化します。
64  	 * 
65  	 * @param request
66  	 *            要求
67  	 * @param response
68  	 *            応答
69  	 * @param pathInfo
70  	 *            パスから取得した情報
71  	 */
72  	public RequestProcessingInvocationImpl(final HttpServletRequest request,
73  			final HttpServletResponse response, final PathInfo pathInfo) {
74  		this.request = request;
75  		this.response = response;
76  		this.pathInfo = pathInfo;
77  
78  		final PluginRegistry pluginRegistry = PluginRegistry.getInstance();
79  		this.pluginsIterator = pluginRegistry.getPlugins().iterator();
80  	}
81  
82  	/**
83  	 * {@inheritDoc}
84  	 */
85  	public Void proceed() throws Exception {
86  		if (pluginsIterator.hasNext()) {
87  			final Plugin plugin = pluginsIterator.next();
88  			plugin.invokeRequestProcessing(this);
89  		} else {
90  			final HttpServletRequest request = getRequest();
91  			final RequestParserProvider requestParserProvider = ProviderFactory
92  					.get(RequestParserProvider.class);
93  			final Map<String, Object[]> parameterMap = requestParserProvider
94  					.getParameterMap(request);
95  			request.setAttribute(ATTR_PARAMS, parameterMap);
96  			final Routing routing = pathInfo.dispatch(parameterMap);
97  
98  			ThreadContext.enter(request, response);
99  			try {
100 				final ActionResultWrapper actionResultWrapper = actionProcessor
101 						.process(request, response, routing);
102 				actionResultWrapper.execute(request, response);
103 			} finally {
104 				ThreadContext.exit();
105 			}
106 		}
107 		return null;
108 	}
109 
110 	/**
111 	 * {@inheritDoc}
112 	 */
113 	public HttpServletRequest getRequest() {
114 		return request;
115 	}
116 
117 	/**
118 	 * {@inheritDoc}
119 	 */
120 	public HttpServletResponse getResponse() {
121 		return response;
122 	}
123 
124 	/**
125 	 * {@inheritDoc}
126 	 */
127 	public PathInfo getPathInfo() {
128 		return pathInfo;
129 	}
130 
131 }