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.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   */
    45  public class RouterImpl implements Router {
    46
    47  	/** ロガー */
    48  	private static final Logger logger = LoggerFactory
    49  			.getLogger(RouterImpl.class);
    50
    51  	/** 空の対象外パターンのリスト */
    52  	private static final List<Pattern> EMPTY_IGNORE_PATH_PATTERNS = Collections
    53  			.emptyList();
    54
    55  	/**
    56  	 * {@inheritDoc}
    57  	 */
    58  	public PathInfo routing(final HttpServletRequest request,
    59  			final HttpServletResponse response) {
    60  		return routing(request, response, EMPTY_IGNORE_PATH_PATTERNS);
    61  	}
    62
    63  	/**
    64  	 * {@inheritDoc}
    65  	 */
    66  	public PathInfo routing(final HttpServletRequest request,
    67  			final HttpServletResponse response,
    68  			final List<Pattern> ignorePathPatterns) {
    69  		final String path = RequestUtils.getPath(request);
    70  		if (logger.isDebugEnabled()) {
    71  			logger.debug(format("DCUB0006", path));
    72  		}
    73
    74  		final Routing routing = RequestUtils
    75  				.getAttribute(request, ATTR_ROUTING);
    76  		if (routing != null) {
    77  			request.removeAttribute(ATTR_ROUTING);
    78  			final PathInfo pathInfo = new ForwardFromActionPathInfo(routing);
    79  			return pathInfo;
    80  		}
    81
    82  		if (isIgnorePath(path, ignorePathPatterns)) {
    83  			return null;
    84  		}
    85
    86  		final PathResolverProvider pathResolverProvider = ProviderFactory
    87  				.get(PathResolverProvider.class);
    88  		final PathResolver pathResolver = pathResolverProvider
    89  				.getPathResolver();
    90  		final PathInfo pathInfo = pathResolver.getPathInfo(path, request
    91  				.getMethod(), request.getCharacterEncoding());
    92  		return pathInfo;
    93  	}
    94
    95  	/**
    96  	 * 指定された path が ignorePathPatterns にマッチするかを示します。
    97  	 *
    98  	 * @param path
    99  	 *            パス
    100 	 * @param ignorePathPatterns
    101 	 *            対象外パターンのリスト
    102 	 * @return path が ignorePathPatterns にマッチする場合は <code>true</code>、そうでない場合は
    103 	 *         <code>false</code>
    104 	 */
    105 	private boolean isIgnorePath(final String path,
    106 			final List<Pattern> ignorePathPatterns) {
    107 		for (final Pattern pattern : ignorePathPatterns) {
    108 			final Matcher matcher = pattern.matcher(path);
    109 			if (matcher.matches()) {
    110 				return true;
    111 			}
    112 		}
    113 		return false;
    114 	}
    115
    116 	static class ForwardFromActionPathInfo implements PathInfo {
    117
    118 		private final Routing routing;
    119
    120 		private final Map<String, String[]> uriParameters = Collections
    121 				.emptyMap();
    122
    123 		public ForwardFromActionPathInfo(final Routing routing) {
    124 			this.routing = routing;
    125 		}
    126
    127 		public Map<String, String[]> getURIParameters() {
    128 			return uriParameters;
    129 		}
    130
    131 		public Routing dispatch(final Map<String, Object[]> parameterMap) {
    132 			return routing;
    133 		}
    134
    135 	}
    136
    137 }