Coverage Report - org.seasar.cubby.internal.routing.impl.RouterImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
RouterImpl
96%
24/25
80%
8/10
0
RouterImpl$ForwardFromActionPathInfo
83%
5/6
N/A
0
 
 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.routing.impl;
 17  
 
 18  
 import static org.seasar.cubby.CubbyConstants.ATTR_ROUTING;
 19  
 import static org.seasar.cubby.internal.util.LogMessages.format;
 20  
 
 21  
 import java.util.Collections;
 22  
 import java.util.List;
 23  
 import java.util.Map;
 24  
 import java.util.regex.Matcher;
 25  
 import java.util.regex.Pattern;
 26  
 
 27  
 import javax.servlet.http.HttpServletRequest;
 28  
 import javax.servlet.http.HttpServletResponse;
 29  
 
 30  
 import org.seasar.cubby.internal.routing.Router;
 31  
 import org.seasar.cubby.internal.util.RequestUtils;
 32  
 import org.seasar.cubby.routing.PathInfo;
 33  
 import org.seasar.cubby.routing.PathResolver;
 34  
 import org.seasar.cubby.routing.Routing;
 35  
 import org.seasar.cubby.spi.PathResolverProvider;
 36  
 import org.seasar.cubby.spi.ProviderFactory;
 37  
 import org.slf4j.Logger;
 38  
 import org.slf4j.LoggerFactory;
 39  
 
 40  
 /**
 41  
  * ルーターの実装です。
 42  
  * 
 43  
  * @author baba
 44  
  * @since 1.0.0
 45  
  */
 46  3
 public class RouterImpl implements Router {
 47  
 
 48  
         /** ロガー */
 49  1
         private static final Logger logger = LoggerFactory
 50  
                         .getLogger(RouterImpl.class);
 51  
 
 52  
         /** 空の対象外パターンのリスト */
 53  1
         private static final List<Pattern> EMPTY_IGNORE_PATH_PATTERNS = Collections
 54  
                         .emptyList();
 55  
 
 56  
         /**
 57  
          * {@inheritDoc}
 58  
          */
 59  
         public PathInfo routing(final HttpServletRequest request,
 60  
                         final HttpServletResponse response) {
 61  2
                 return routing(request, response, EMPTY_IGNORE_PATH_PATTERNS);
 62  
         }
 63  
 
 64  
         /**
 65  
          * {@inheritDoc}
 66  
          */
 67  
         public PathInfo routing(final HttpServletRequest request,
 68  
                         final HttpServletResponse response,
 69  
                         final List<Pattern> ignorePathPatterns) {
 70  3
                 final String path = RequestUtils.getPath(request);
 71  3
                 if (logger.isDebugEnabled()) {
 72  3
                         logger.debug(format("DCUB0006", path));
 73  
                 }
 74  
 
 75  3
                 final Routing routing = RequestUtils
 76  
                                 .getAttribute(request, ATTR_ROUTING);
 77  3
                 if (routing != null) {
 78  1
                         request.removeAttribute(ATTR_ROUTING);
 79  1
                         final PathInfo pathInfo = new ForwardFromActionPathInfo(routing);
 80  1
                         return pathInfo;
 81  
                 }
 82  
 
 83  2
                 if (isIgnorePath(path, ignorePathPatterns)) {
 84  1
                         return null;
 85  
                 }
 86  
 
 87  1
                 final PathResolverProvider pathResolverProvider = ProviderFactory
 88  
                                 .get(PathResolverProvider.class);
 89  1
                 final PathResolver pathResolver = pathResolverProvider
 90  
                                 .getPathResolver();
 91  1
                 final PathInfo pathInfo = pathResolver.getPathInfo(path, request
 92  
                                 .getMethod(), request.getCharacterEncoding());
 93  1
                 return pathInfo;
 94  
         }
 95  
 
 96  
         /**
 97  
          * 指定された path が ignorePathPatterns にマッチするかを示します。
 98  
          * 
 99  
          * @param path
 100  
          *            パス
 101  
          * @param ignorePathPatterns
 102  
          *            対象外パターンのリスト
 103  
          * @return path が ignorePathPatterns にマッチする場合は <code>true</code>、そうでない場合は
 104  
          *         <code>false</code>
 105  
          */
 106  
         private boolean isIgnorePath(final String path,
 107  
                         final List<Pattern> ignorePathPatterns) {
 108  2
                 for (final Pattern pattern : ignorePathPatterns) {
 109  1
                         final Matcher matcher = pattern.matcher(path);
 110  1
                         if (matcher.matches()) {
 111  1
                                 return true;
 112  
                         }
 113  0
                 }
 114  1
                 return false;
 115  
         }
 116  
 
 117  3
         static class ForwardFromActionPathInfo implements PathInfo {
 118  
 
 119  
                 private final Routing routing;
 120  
 
 121  1
                 private final Map<String, String[]> uriParameters = Collections
 122  
                                 .emptyMap();
 123  
 
 124  1
                 public ForwardFromActionPathInfo(final Routing routing) {
 125  1
                         this.routing = routing;
 126  1
                 }
 127  
 
 128  
                 public Map<String, String[]> getURIParameters() {
 129  0
                         return uriParameters;
 130  
                 }
 131  
 
 132  
                 public Routing dispatch(final Map<String, Object[]> parameterMap) {
 133  1
                         return routing;
 134  
                 }
 135  
 
 136  
         }
 137  
 
 138  
 }