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   * @since 1.0.0
45   */
46  public class RouterImpl implements Router {
47  
48  	/** ロガー */
49  	private static final Logger logger = LoggerFactory
50  			.getLogger(RouterImpl.class);
51  
52  	/** 空の対象外パターンのリスト */
53  	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  		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  		final String path = RequestUtils.getPath(request);
71  		if (logger.isDebugEnabled()) {
72  			logger.debug(format("DCUB0006", path));
73  		}
74  
75  		final Routing routing = RequestUtils
76  				.getAttribute(request, ATTR_ROUTING);
77  		if (routing != null) {
78  			request.removeAttribute(ATTR_ROUTING);
79  			final PathInfo pathInfo = new ForwardFromActionPathInfo(routing);
80  			return pathInfo;
81  		}
82  
83  		if (isIgnorePath(path, ignorePathPatterns)) {
84  			return null;
85  		}
86  
87  		final PathResolverProvider pathResolverProvider = ProviderFactory
88  				.get(PathResolverProvider.class);
89  		final PathResolver pathResolver = pathResolverProvider
90  				.getPathResolver();
91  		final PathInfo pathInfo = pathResolver.getPathInfo(path, request
92  				.getMethod(), request.getCharacterEncoding());
93  		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 		for (final Pattern pattern : ignorePathPatterns) {
109 			final Matcher matcher = pattern.matcher(path);
110 			if (matcher.matches()) {
111 				return true;
112 			}
113 		}
114 		return false;
115 	}
116 
117 	static class ForwardFromActionPathInfo implements PathInfo {
118 
119 		private final Routing routing;
120 
121 		private final Map<String, String[]> uriParameters = Collections
122 				.emptyMap();
123 
124 		public ForwardFromActionPathInfo(final Routing routing) {
125 			this.routing = routing;
126 		}
127 
128 		public Map<String, String[]> getURIParameters() {
129 			return uriParameters;
130 		}
131 
132 		public Routing dispatch(final Map<String, Object[]> parameterMap) {
133 			return routing;
134 		}
135 
136 	}
137 
138 }