Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
RequestRoutingFilter |
|
| 0.0;0 |
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.filter; | |
17 | ||
18 | import java.io.IOException; | |
19 | import java.util.ArrayList; | |
20 | import java.util.List; | |
21 | import java.util.StringTokenizer; | |
22 | import java.util.regex.Pattern; | |
23 | ||
24 | import javax.servlet.Filter; | |
25 | import javax.servlet.FilterChain; | |
26 | import javax.servlet.FilterConfig; | |
27 | import javax.servlet.RequestDispatcher; | |
28 | import javax.servlet.ServletException; | |
29 | import javax.servlet.ServletRequest; | |
30 | import javax.servlet.ServletResponse; | |
31 | import javax.servlet.http.HttpServletRequest; | |
32 | import javax.servlet.http.HttpServletResponse; | |
33 | ||
34 | import org.seasar.cubby.routing.InternalForwardInfo; | |
35 | import org.seasar.cubby.routing.Router; | |
36 | import org.seasar.framework.container.SingletonS2Container; | |
37 | import org.seasar.framework.log.Logger; | |
38 | import org.seasar.framework.util.StringUtil; | |
39 | ||
40 | /** | |
41 | * リクエストされたURLを適切なアクションに振り分けるフィルタ。 | |
42 | * <p> | |
43 | * {@link Router} によって {@link InternalForwardInfo} | |
44 | * を抽出し、そこに保持された情報をもとにフォワードします。 | |
45 | * </p> | |
46 | * | |
47 | * @author baba | |
48 | * @since 1.0.0 | |
49 | */ | |
50 | 0 | public class RequestRoutingFilter implements Filter { |
51 | ||
52 | /** ロガー */ | |
53 | 0 | private static final Logger logger = Logger |
54 | .getLogger(RequestRoutingFilter.class); | |
55 | ||
56 | /** ルーティングの対象外とするパスの初期パラメータ名 */ | |
57 | public static final String IGNORE_PATH_PATTERN = "ignorePathPattern"; | |
58 | ||
59 | /** ルーティングの対象外とするパスの正規表現パターンのリスト */ | |
60 | 0 | private final List<Pattern> ignorePathPatterns = new ArrayList<Pattern>(); |
61 | ||
62 | /** | |
63 | * このフィルタを初期化します。 | |
64 | * <p> | |
65 | * 使用可能な初期化パラメータ <table> <thead> | |
66 | * <th> 初期化パラメータ名 </th> | |
67 | * <th> 初期化パラメータの値 </th> | |
68 | * <th> 例 </th> | |
69 | * </thead> <thead> | |
70 | * <tr> | |
71 | * <td>{@link #IGNORE_PATH_PATTERN}</td> | |
72 | * <td>ルーティングの対象外とするパスの正規表現をカンマ区切りで指定します。 HotDeploy | |
73 | * 時のパフォーマンスにも影響するので、画像やスクリプトを特定のディレクトリに | |
74 | * 格納していてアクションを実行するパスと明確に区別できる場合はできる限り指定するようにしてください。</td> | |
75 | * <td> | |
76 | * | |
77 | * <pre> | |
78 | * <param-name>ignorePathPattern&lt/param-name> | |
79 | * <param-value>/img/.*,/js/.*<param-name> | |
80 | * </pre> | |
81 | * | |
82 | * この例では /img と /js 以下のパスをルーティングの対象外にします。 </td> | |
83 | * </tr> | |
84 | * </thead> | |
85 | * </p> | |
86 | * | |
87 | * @param config | |
88 | * Filter 設定のためのオブジェクト | |
89 | * @exception ServletException | |
90 | * 初期化処理で例外が発生した場合 | |
91 | */ | |
92 | public void init(final FilterConfig config) throws ServletException { | |
93 | 0 | final String ignorePathPatternString = config |
94 | .getInitParameter(IGNORE_PATH_PATTERN); | |
95 | 0 | if (!StringUtil.isEmpty(ignorePathPatternString)) { |
96 | ||
97 | 0 | for (final StringTokenizer tokenizer = new StringTokenizer( |
98 | 0 | ignorePathPatternString, ","); tokenizer.hasMoreTokens();) { |
99 | 0 | final String token = tokenizer.nextToken(); |
100 | 0 | final Pattern pattern = Pattern.compile(token); |
101 | 0 | ignorePathPatterns.add(pattern); |
102 | 0 | } |
103 | } | |
104 | 0 | } |
105 | ||
106 | /** | |
107 | * {@inheritDoc} | |
108 | */ | |
109 | public void destroy() { | |
110 | 0 | } |
111 | ||
112 | /** | |
113 | * フィルタリングを行います。 | |
114 | * <p> | |
115 | * リクエストされた URI に対応する内部フォワード用のパスが {@link Router} から取得できた場合は、 | |
116 | * その内部フォワード用のパスへフォワードします。フォワード先は {@link CubbyFilter} が処理することを期待します。 | |
117 | * マッチするパターンがなかった場合はフィルタチェインで次のフィルタに処理を移譲します。 | |
118 | * </p> | |
119 | * | |
120 | * @param request | |
121 | * リクエスト | |
122 | * @param response | |
123 | * レスポンス | |
124 | * @param chain | |
125 | * フィルタチェイン | |
126 | * @throws IOException | |
127 | * リクエストディスパッチャやフィルタチェインで例外が発生した場合 | |
128 | * @throws ServletException | |
129 | * リクエストディスパッチャやフィルタチェインで例外が発生した場合 | |
130 | * @see Router#routing(HttpServletRequest, HttpServletResponse, List) | |
131 | * @see CubbyFilter | |
132 | */ | |
133 | public void doFilter(final ServletRequest request, | |
134 | final ServletResponse response, final FilterChain chain) | |
135 | throws IOException, ServletException { | |
136 | ||
137 | 0 | final Router router = SingletonS2Container.getComponent(Router.class); |
138 | ||
139 | 0 | final InternalForwardInfo internalForwardInfo = router.routing( |
140 | (HttpServletRequest) request, (HttpServletResponse) response, | |
141 | ignorePathPatterns); | |
142 | 0 | if (internalForwardInfo != null) { |
143 | 0 | final String internalForwardPath = internalForwardInfo |
144 | .getInternalForwardPath(); | |
145 | 0 | if (logger.isDebugEnabled()) { |
146 | 0 | logger.log("DCUB0001", new Object[] { internalForwardPath }); |
147 | } | |
148 | 0 | final RequestDispatcher requestDispatcher = request |
149 | .getRequestDispatcher(internalForwardPath); | |
150 | 0 | requestDispatcher.forward(request, response); |
151 | 0 | } else { |
152 | 0 | chain.doFilter(request, response); |
153 | } | |
154 | 0 | } |
155 | ||
156 | } |