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  		 * @param pathInfo
    91  		 *            パスから取得した情報
    92  		 */
    93  		public RequestProcessingInvocationImpl(
    94  				final HttpServletRequest request,
    95  				final HttpServletResponse response, final PathInfo pathInfo) {
    96  			this.request = request;
    97  			this.response = response;
    98  			this.pathInfo = pathInfo;
    99
    100 			final PluginRegistry pluginRegistry = PluginRegistry.getInstance();
    101 			this.pluginsIterator = pluginRegistry.getPlugins().iterator();
    102 		}
    103
    104 		/**
    105 		 * {@inheritDoc}
    106 		 */
    107 		public Void proceed() throws Exception {
    108 			if (pluginsIterator.hasNext()) {
    109 				final Plugin plugin = pluginsIterator.next();
    110 				plugin.invokeRequestProcessing(this);
    111 			} else {
    112 				final HttpServletRequest request = getRequest();
    113 				final RequestParserProvider requestParserProvider = ProviderFactory
    114 						.get(RequestParserProvider.class);
    115 				final Map<String, Object[]> parameterMap = requestParserProvider
    116 						.getParameterMap(request);
    117 				request.setAttribute(ATTR_PARAMS, parameterMap);
    118 				final Routing routing = pathInfo.dispatch(parameterMap);
    119 				final Command command = new Command() {
    120
    121 					public void execute(final HttpServletRequest request,
    122 							final HttpServletResponse response)
    123 							throws Exception {
    124 						final ActionResultWrapper actionResultWrapper = actionProcessor
    125 								.process(request, response, routing);
    126 						actionResultWrapper.execute(request, response);
    127 					}
    128
    129 				};
    130 				ThreadContext.runInContext(request, response, command);
    131 			}
    132 			return null;
    133 		}
    134
    135 		/**
    136 		 * {@inheritDoc}
    137 		 */
    138 		public HttpServletRequest getRequest() {
    139 			return request;
    140 		}
    141
    142 		/**
    143 		 * {@inheritDoc}
    144 		 */
    145 		public HttpServletResponse getResponse() {
    146 			return response;
    147 		}
    148
    149 		/**
    150 		 * {@inheritDoc}
    151 		 */
    152 		public PathInfo getPathInfo() {
    153 			return pathInfo;
    154 		}
    155
    156 	}
    157
    158 }