Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SendErrorFilter |
|
| 2.75;2.75 |
1 | /* | |
2 | * Copyright 2004-2010 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 | ||
17 | package org.seasar.cubby.filter; | |
18 | ||
19 | import java.io.IOException; | |
20 | import java.util.ArrayList; | |
21 | import java.util.List; | |
22 | import java.util.StringTokenizer; | |
23 | import java.util.regex.Matcher; | |
24 | import java.util.regex.Pattern; | |
25 | ||
26 | import javax.servlet.Filter; | |
27 | import javax.servlet.FilterChain; | |
28 | import javax.servlet.FilterConfig; | |
29 | import javax.servlet.ServletException; | |
30 | import javax.servlet.ServletRequest; | |
31 | import javax.servlet.ServletResponse; | |
32 | import javax.servlet.http.HttpServletRequest; | |
33 | import javax.servlet.http.HttpServletResponse; | |
34 | ||
35 | import org.seasar.cubby.internal.util.StringUtils; | |
36 | ||
37 | /** | |
38 | * 適用された要求に対して、異常系の HTTP ステータスコードを返す {@link Filter} です。 | |
39 | * | |
40 | * @author baba | |
41 | */ | |
42 | 5 | public class SendErrorFilter implements Filter { |
43 | ||
44 | /** 応答の HTTP ステータスコードの初期パラメータ名。 */ | |
45 | public static final String STATUS_CODE = "statusCode"; | |
46 | ||
47 | /** 対象外とするパスの初期パラメータ名。 */ | |
48 | public static final String IGNORE_PATH_PATTERN = "ignorePathPattern"; | |
49 | ||
50 | /** 応答の HTTP ステータスコード (デフォルトは 403 Forbidden)。 */ | |
51 | 5 | private int statusCode = HttpServletResponse.SC_FORBIDDEN; |
52 | ||
53 | /** 対象外とするパスの正規表現パターンのリスト。 */ | |
54 | 5 | private final List<Pattern> ignorePathPatterns = new ArrayList<Pattern>(); |
55 | ||
56 | /** | |
57 | * このフィルタを初期化します。 | |
58 | * <p> | |
59 | * <table> | |
60 | * <caption>使用可能な初期化パラメータ</caption> <thead> | |
61 | * <th>初期化パラメータ名</th> | |
62 | * <th>初期化パラメータの値</th> | |
63 | * <th>例</th> | |
64 | * </thead> <thead> | |
65 | * <tr> | |
66 | * <td>{@link #STATUS_CODE}</td> | |
67 | * <td>応答の HTTP ステータスコードを指定します。指定しなかった場合は | |
68 | * {@link HttpServletResponse#SC_FORBIDDEN} を返します。</td> | |
69 | * <td></td> | |
70 | * <tr> | |
71 | * <td>{@link #IGNORE_PATH_PATTERN}</td> | |
72 | * <td>対象外とするパスの正規表現をカンマ区切りで指定します。 filter-mapping の url-pattern | |
73 | * で指定する、このフィルタを適用する URL のうち、適用を除外したいパスを指定してください。</td> | |
74 | * <td> | |
75 | * | |
76 | * <pre> | |
77 | * <filter> | |
78 | * <filter-name>sendErrorFilter</filter-name> | |
79 | * <filter-class>org.seasar.cubby.filter.SendErrorFilter</filter-class> | |
80 | * <init-param> | |
81 | * <param-name>statusCode;</param-name> | |
82 | * <param-value>404<param-name> | |
83 | * </init-param> | |
84 | * <init-param> | |
85 | * <param-name>ignorePathPattern</param-name> | |
86 | * <param-value>/index.jsp<param-name> | |
87 | * </init-param> | |
88 | * </filter> | |
89 | * | |
90 | * <filter-mapping> | |
91 | * <filter-name>sendErrorFilter<filter-name> | |
92 | * <url-pattern>*.jsp<url-pattern> | |
93 | * <dispatcher>REQUEST</dispatcher> | |
94 | * </filter-mapping> | |
95 | * </pre> | |
96 | * | |
97 | * この例では、 /index.jsp を除く *.jsp に要求があった場合に HTTP ステータスコード 404 (Not Found) | |
98 | * を返します。</td> | |
99 | * </tr> | |
100 | * </thead> | |
101 | * </p> | |
102 | * | |
103 | * @param config | |
104 | * Filter 設定のためのオブジェクト | |
105 | * @exception ServletException | |
106 | * 初期化処理で例外が発生した場合 | |
107 | */ | |
108 | public void init(final FilterConfig config) throws ServletException { | |
109 | 5 | final String statusCodeString = config.getInitParameter(STATUS_CODE); |
110 | 5 | if (statusCodeString != null) { |
111 | 4 | statusCode = Integer.parseInt(statusCodeString); |
112 | } | |
113 | 5 | final String ignorePathPatternString = config |
114 | .getInitParameter(IGNORE_PATH_PATTERN); | |
115 | 5 | if (!StringUtils.isEmpty(ignorePathPatternString)) { |
116 | ||
117 | 3 | for (final StringTokenizer tokenizer = new StringTokenizer( |
118 | 9 | ignorePathPatternString, ","); tokenizer.hasMoreTokens();) { |
119 | 6 | final String token = tokenizer.nextToken(); |
120 | 6 | final Pattern pattern = Pattern.compile(token); |
121 | 6 | ignorePathPatterns.add(pattern); |
122 | 6 | } |
123 | } | |
124 | 5 | } |
125 | ||
126 | /** | |
127 | * {@inheritDoc} | |
128 | */ | |
129 | public void destroy() { | |
130 | 5 | } |
131 | ||
132 | /** | |
133 | * {@link HttpServletResponse#sendError(int)} によって、異常系の HTTP ステータスコードを返します。 | |
134 | * | |
135 | * @param req | |
136 | * 要求 | |
137 | * @param res | |
138 | * 応答 | |
139 | * @param chain | |
140 | * フィルターチェーン | |
141 | * @throws IOException | |
142 | * 要求の転送や要求のチェーンがこの例外をスローする場合 | |
143 | * @throws ServletException | |
144 | * 要求の転送や要求のチェーンがこの例外をスローする場合 | |
145 | */ | |
146 | public void doFilter(final ServletRequest req, final ServletResponse res, | |
147 | final FilterChain chain) throws IOException, ServletException { | |
148 | ||
149 | 5 | final HttpServletRequest request = (HttpServletRequest) req; |
150 | 5 | final HttpServletResponse response = (HttpServletResponse) res; |
151 | ||
152 | 5 | if (isIgnore(request)) { |
153 | 2 | chain.doFilter(request, response); |
154 | } else { | |
155 | 3 | response.sendError(statusCode); |
156 | } | |
157 | 5 | } |
158 | ||
159 | /** | |
160 | * 指定された要求がこのフィルタの対象外であるかを示します。 | |
161 | * | |
162 | * @param request | |
163 | * 要求 | |
164 | * @return 指定された要求がこのフィルタの対象外である場合は <code>true</code>、そうでない場合は | |
165 | * <code>false</code> | |
166 | */ | |
167 | private boolean isIgnore(final HttpServletRequest request) { | |
168 | 5 | final String servletPath = request.getServletPath(); |
169 | 5 | for (final Pattern ignorePattern : ignorePathPatterns) { |
170 | 6 | final Matcher matcher = ignorePattern.matcher(servletPath); |
171 | 6 | if (matcher.matches()) { |
172 | 2 | return true; |
173 | } | |
174 | 4 | } |
175 | 3 | return false; |
176 | } | |
177 | ||
178 | } |