View Javadoc

1   /*
2    * Copyright 2004-2008 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.routing.impl;
17  
18  import java.util.Collections;
19  import java.util.List;
20  import java.util.regex.Matcher;
21  import java.util.regex.Pattern;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.seasar.cubby.routing.InternalForwardInfo;
27  import org.seasar.cubby.routing.PathResolver;
28  import org.seasar.cubby.routing.Router;
29  import org.seasar.cubby.util.CubbyUtils;
30  import org.seasar.framework.log.Logger;
31  
32  /**
33   * ルーターの実装です。
34   * 
35   * @author baba
36   * @since 1.0.0
37   */
38  public class RouterImpl implements Router {
39  
40  	/** ロガー */
41  	private static final Logger logger = Logger.getLogger(RouterImpl.class);
42  
43  	/** 空の対象外パターンのリスト */
44  	private static final List<Pattern> EMPTY_IGNORE_PATH_PATTERNS = Collections
45  			.emptyList();
46  
47  	/** フォワードするための情報を抽出するクラス。 */
48  	private PathResolver pathResolver;
49  
50  	/**
51  	 * フォワードするための情報を抽出するクラスを設定します。
52  	 * 
53  	 * @param pathResolver
54  	 *            フォワードするための情報を抽出するクラス
55  	 */
56  	public void setPathResolver(final PathResolver pathResolver) {
57  		this.pathResolver = pathResolver;
58  	}
59  
60  	/**
61  	 * {@inheritDoc}
62  	 */
63  	public InternalForwardInfo routing(final HttpServletRequest request,
64  			final HttpServletResponse response) {
65  		return routing(request, response, EMPTY_IGNORE_PATH_PATTERNS);
66  	}
67  
68  	/**
69  	 * {@inheritDoc}
70  	 */
71  	public InternalForwardInfo routing(final HttpServletRequest request,
72  			final HttpServletResponse response, List<Pattern> ignorePathPatterns) {
73  		final String path = CubbyUtils.getPath(request);
74  		if (logger.isDebugEnabled()) {
75  			logger.log("DCUB0006", new Object[] { path });
76  		}
77  
78  		if (isIgnorePath(path, ignorePathPatterns)) {
79  			return null;
80  		}
81  
82  		final InternalForwardInfo internalForwardInfo = pathResolver
83  				.getInternalForwardInfo(path, request.getMethod(), request
84  						.getCharacterEncoding());
85  		return internalForwardInfo;
86  	}
87  
88  	/**
89  	 * 指定された path が ignorePathPatterns にマッチするかを示します。
90  	 * 
91  	 * @param path
92  	 *            パス
93  	 * @param ignorePathPatterns
94  	 *            対象外パターンのリスト
95  	 * @return path が ignorePathPatterns にマッチする場合は <code>true</code>、そうでない場合は
96  	 *         <code>false</code>
97  	 */
98  	private boolean isIgnorePath(final String path,
99  			List<Pattern> ignorePathPatterns) {
100 		for (final Pattern pattern : ignorePathPatterns) {
101 			final Matcher matcher = pattern.matcher(path);
102 			if (matcher.matches()) {
103 				return true;
104 			}
105 		}
106 		return false;
107 	}
108 
109 }