Coverage Report - org.seasar.cubby.internal.controller.impl.RequestProcessorImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
RequestProcessorImpl
100%
6/6
N/A
1.143
RequestProcessorImpl$RequestProcessingInvocationImpl
91%
22/24
100%
2/2
1.143
RequestProcessorImpl$RequestProcessingInvocationImpl$1
100%
4/4
N/A
1.143
 
 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  1
 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  1
                 final HttpServletRequest wrappedRequest = new CubbyHttpServletRequestWrapper(
 54  
                                 request, pathInfo.getURIParameters());
 55  1
                 final RequestProcessingInvocation invocation = new RequestProcessingInvocationImpl(
 56  
                                 wrappedRequest, response, pathInfo);
 57  1
                 invocation.proceed();
 58  1
         }
 59  
 
 60  
         /**
 61  
          * 要求処理の実行情報の実装です。
 62  
          * 
 63  
          * @author baba
 64  
          */
 65  3
         static class RequestProcessingInvocationImpl implements
 66  
                         RequestProcessingInvocation {
 67  
 
 68  
                 /** アクションを処理します。 */
 69  1
                 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  1
                                 final HttpServletResponse response, final PathInfo pathInfo) {
 96  1
                         this.request = request;
 97  1
                         this.response = response;
 98  1
                         this.pathInfo = pathInfo;
 99  
 
 100  1
                         final PluginRegistry pluginRegistry = PluginRegistry.getInstance();
 101  1
                         this.pluginsIterator = pluginRegistry.getPlugins().iterator();
 102  1
                 }
 103  
 
 104  
                 /**
 105  
                  * {@inheritDoc}
 106  
                  */
 107  
                 public Void proceed() throws Exception {
 108  2
                         if (pluginsIterator.hasNext()) {
 109  1
                                 final Plugin plugin = pluginsIterator.next();
 110  1
                                 plugin.invokeRequestProcessing(this);
 111  1
                         } else {
 112  1
                                 final HttpServletRequest request = getRequest();
 113  1
                                 final RequestParserProvider requestParserProvider = ProviderFactory
 114  
                                                 .get(RequestParserProvider.class);
 115  1
                                 final Map<String, Object[]> parameterMap = requestParserProvider
 116  
                                                 .getParameterMap(request);
 117  1
                                 request.setAttribute(ATTR_PARAMS, parameterMap);
 118  1
                                 final Routing routing = pathInfo.dispatch(parameterMap);
 119  1
                                 final Command command = new Command() {
 120  
 
 121  
                                         public void execute(final HttpServletRequest request,
 122  
                                                         final HttpServletResponse response)
 123  
                                                         throws Exception {
 124  1
                                                 final ActionResultWrapper actionResultWrapper = actionProcessor
 125  
                                                                 .process(request, response, routing);
 126  1
                                                 actionResultWrapper.execute(request, response);
 127  1
                                         }
 128  
 
 129  
                                 };
 130  1
                                 ThreadContext.runInContext(request, response, command);
 131  
                         }
 132  2
                         return null;
 133  
                 }
 134  
 
 135  
                 /**
 136  
                  * {@inheritDoc}
 137  
                  */
 138  
                 public HttpServletRequest getRequest() {
 139  1
                         return request;
 140  
                 }
 141  
 
 142  
                 /**
 143  
                  * {@inheritDoc}
 144  
                  */
 145  
                 public HttpServletResponse getResponse() {
 146  0
                         return response;
 147  
                 }
 148  
 
 149  
                 /**
 150  
                  * {@inheritDoc}
 151  
                  */
 152  
                 public PathInfo getPathInfo() {
 153  0
                         return pathInfo;
 154  
                 }
 155  
 
 156  
         }
 157  
 
 158  
 }