Line | Hits | Source |
---|---|---|
1 | /* | |
2 | Chrysalis Web Framework [http://chrysalis.sourceforge.net] | |
3 | Copyright (c) 2002, 2003, 2004, Paul Strack | |
4 | ||
5 | All rights reserved. | |
6 | ||
7 | Redistribution and use in source and binary forms, with or without | |
8 | modification, are permitted provided that the following conditions are met: | |
9 | ||
10 | 1. Redistributions of source code must retain the above copyright notice, this | |
11 | list of conditions and the following disclaimer. | |
12 | ||
13 | 2. Redistributions in binary form must reproduce the above copyright notice, | |
14 | this list of conditions and the following disclaimer in the documentation | |
15 | and/or other materials provided with the distribution. | |
16 | ||
17 | 3. Neither the name of the copyright holder nor the names of its contributors | |
18 | may be used to endorse or promote products derived from this software without | |
19 | specific prior written permission. | |
20 | ||
21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
22 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
23 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | |
25 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
26 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
27 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
28 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
31 | */ | |
32 | ||
33 | package org.chwf.servlet.filter; | |
34 | ||
35 | import java.util.Collections; | |
36 | import java.util.Enumeration; | |
37 | import java.util.Map; | |
38 | ||
39 | import javax.servlet.Filter; | |
40 | import javax.servlet.FilterConfig; | |
41 | import javax.servlet.ServletContext; | |
42 | import javax.servlet.ServletException; | |
43 | ||
44 | /** | |
45 | * Class containing info for filter and a reference to the filter object itself. | |
46 | * It also serves as the implementation class for FilterConfig. | |
47 | * | |
48 | * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a> | |
49 | */ | |
50 | public class FilterInfo implements FilterConfig { | |
51 | ||
52 | /** Constant for URI pattern matching everything, suitable for "==" tests. */ | |
53 | private static final String WILDCARD = "*"; | |
54 | ||
55 | /** The servlet context. */ | |
56 | private final ServletContext context; | |
57 | ||
58 | /** The filter name. */ | |
59 | private final String filterName; | |
60 | ||
61 | /** The filter. */ | |
62 | private final Filter filter; | |
63 | ||
64 | /** The filter's URI patterns. */ | |
65 | private final String[] uriPatterns; | |
66 | ||
67 | /** The filter's init parameters. */ | |
68 | private final Map initParameters; | |
69 | ||
70 | /** | |
71 | * Constructor. | |
72 | * | |
73 | * @param name The filter name. | |
74 | * @param allFilterConfig Containing configuration for all filters. | |
75 | * @throws ServletException If the filter cannot be initialized | |
76 | */ | |
77 | public FilterInfo(String name, AllFilterConfig allFilterConfig) | |
78 | 94 | throws ServletException { |
79 | try { | |
80 | 94 | this.filterName = name; |
81 | 94 | this.context = allFilterConfig.getContext(); |
82 | 94 | this.initParameters = allFilterConfig.getInitParameters(name); |
83 | 94 | this.uriPatterns = allFilterConfig.getURIPatterns(name); |
84 | 94 | initMatchAllPatterns(); |
85 | 94 | String filterClass = allFilterConfig.getFilterClass(name); |
86 | 94 | this.filter = (Filter) Class.forName(filterClass).newInstance(); |
87 | 94 | this.filter.init(this); |
88 | 93 | } catch (Exception ex) { |
89 | 1 | String msg = "Filter '" + name + "' object could not be initialized"; |
90 | 1 | throw new ServletException(msg, ex); |
91 | } | |
92 | 93 | } |
93 | ||
94 | /** | |
95 | * The filter object. | |
96 | * | |
97 | * @return The filter object. | |
98 | */ | |
99 | public Filter getFilter() { | |
100 | 76 | return this.filter; |
101 | } | |
102 | ||
103 | /** | |
104 | * Returns the filter-name of this filter as defined in the deployment | |
105 | * descriptor. | |
106 | * | |
107 | * @return The filter-name. | |
108 | */ | |
109 | public String getFilterName() { | |
110 | 1 | return this.filterName; |
111 | } | |
112 | ||
113 | /** | |
114 | * Returns a String containing the value of the named initialization | |
115 | * parameter, or null if the parameter does not exist. | |
116 | * | |
117 | * @param name A String specifying the name of the initialization parameter. | |
118 | * @return A String containing the value of the initialization parameter. | |
119 | */ | |
120 | public String getInitParameter(String name) { | |
121 | 1 | return (String) this.initParameters.get(name); |
122 | } | |
123 | ||
124 | /** | |
125 | * Returns a reference to the ServletContext in which the caller is executing. | |
126 | * | |
127 | * @return The ServletContext. | |
128 | */ | |
129 | public ServletContext getServletContext() { | |
130 | 15 | return this.context; |
131 | } | |
132 | ||
133 | /** | |
134 | * Returns the names of the servlet's initialization parameters as an | |
135 | * Enumeration of String objects, or an empty Enumeration if the servlet has | |
136 | * no initialization parameters. | |
137 | * | |
138 | * @return An Enumeration of String objects containing the names of the | |
139 | * servlet's initialization parameters. | |
140 | */ | |
141 | public Enumeration getInitParameterNames() { | |
142 | 1 | return Collections.enumeration(this.initParameters.keySet()); |
143 | } | |
144 | ||
145 | /** | |
146 | * True if the given URI matches (one of) the filter patterns. | |
147 | * | |
148 | * @param contextFreeURI The (context-free) URI. | |
149 | * @return True if the URI matches the filter patterns. | |
150 | */ | |
151 | public boolean isMatch(String contextFreeURI) { | |
152 | 15 | String[] patterns = getURIPatterns(); |
153 | 15 | if ((patterns.length > 0) && (patterns[0] == WILDCARD)) { |
154 | 6 | return true; |
155 | } | |
156 | 13 | for (int i = 0; i < patterns.length; i++) { |
157 | 9 | String pattern = patterns[i]; |
158 | 9 | if (pattern.startsWith(WILDCARD)) { |
159 | 4 | pattern = pattern.substring(1); |
160 | 4 | if (contextFreeURI.endsWith(pattern)) { |
161 | 2 | return true; |
162 | } | |
163 | 5 | } else if (pattern.endsWith(WILDCARD)) { |
164 | 4 | pattern = pattern.substring(0, pattern.length() - 1); |
165 | 4 | if (contextFreeURI.startsWith(pattern)) { |
166 | 2 | return true; |
167 | } | |
168 | 1 | } else if (pattern.equals(contextFreeURI)) { |
169 | 1 | return true; |
170 | } | |
171 | } | |
172 | 4 | return false; |
173 | } | |
174 | ||
175 | /** Initialize patterns matching all URI. */ | |
176 | private void initMatchAllPatterns() { | |
177 | 133 | for (int i = 0; i < this.uriPatterns.length; i++) { |
178 | 39 | String pattern = this.uriPatterns[i]; |
179 | 39 | if (pattern.equals(WILDCARD) || pattern.equals("/*")) { |
180 | 14 | this.uriPatterns[0] = WILDCARD; |
181 | } | |
182 | } | |
183 | 94 | } |
184 | ||
185 | /** | |
186 | * The filter's URI patterns. | |
187 | * | |
188 | * @return The filter's URI patterns. | |
189 | */ | |
190 | private String[] getURIPatterns() { | |
191 | 15 | return this.uriPatterns; |
192 | } | |
193 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |