View Javadoc

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