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.io.IOException; | |
36 | import java.util.Collections; | |
37 | import java.util.Iterator; | |
38 | import java.util.LinkedList; | |
39 | import java.util.List; | |
40 | import java.util.Map; | |
41 | import java.util.WeakHashMap; | |
42 | ||
43 | import javax.servlet.Filter; | |
44 | import javax.servlet.ServletException; | |
45 | import javax.servlet.ServletResponse; | |
46 | import javax.servlet.http.HttpServletRequest; | |
47 | ||
48 | import org.chwf.util.SequencedHashMap; | |
49 | ||
50 | /** | |
51 | * Contains a set of filters. | |
52 | * | |
53 | * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a> | |
54 | */ | |
55 | public class FilterSet { | |
56 | ||
57 | /** Map for filter info. */ | |
58 | 33 | private final Map filterInfoMap = new SequencedHashMap(); |
59 | ||
60 | /** | |
61 | * Map for caching chains. Synchronized for thread-safety, weak references | |
62 | * for more efficient memory usage. | |
63 | */ | |
64 | 33 | private final Map chains = Collections.synchronizedMap(new WeakHashMap()); |
65 | ||
66 | /** | |
67 | * Constructor. | |
68 | * | |
69 | * @param allFilterConfig The configuration for all filters. | |
70 | * @throws ServletException If any filter fails to be initialized. | |
71 | */ | |
72 | 33 | public FilterSet(AllFilterConfig allFilterConfig) throws ServletException { |
73 | ||
74 | 33 | Iterator names = allFilterConfig.getFilterNames(); |
75 | 149 | while (names.hasNext()) { |
76 | 83 | String name = names.next().toString(); |
77 | 83 | FilterInfo info = new FilterInfo(name, allFilterConfig); |
78 | 83 | getFilterInfoMap().put(name, info); |
79 | } | |
80 | 33 | } |
81 | ||
82 | /** | |
83 | * Run the chain for this request. When the chain is complete, the terminator | |
84 | * filter is invoked. This terminator filter should invoke the resource at the | |
85 | * end of the chain. | |
86 | * | |
87 | * @param request The request. | |
88 | * @param response The response. | |
89 | * @param terminator A filter that invokes the final resource invoked once | |
90 | * the chain is complete. | |
91 | * @throws ServletException For servlet errors. | |
92 | * @throws IOException For I/O errors. | |
93 | */ | |
94 | public void doChain( | |
95 | HttpServletRequest request, | |
96 | ServletResponse response, | |
97 | Filter terminator) | |
98 | throws IOException, ServletException { | |
99 | 6 | String uri = request.getRequestURI(); |
100 | 6 | FilterChainImpl chain = (FilterChainImpl) chains.get(uri); |
101 | 6 | if (chain == null) { |
102 | 6 | chain = new FilterChainImpl(getFilters(request)); |
103 | 6 | chains.put(uri, chain); |
104 | } | |
105 | 6 | chain.doChain(request, response, terminator); |
106 | 6 | } |
107 | ||
108 | /** Destroy all filters. */ | |
109 | public void destroy() { | |
110 | 4 | Iterator filters = getFilterInfoMap().values().iterator(); |
111 | 17 | while (filters.hasNext()) { |
112 | 9 | FilterInfo info = (FilterInfo) filters.next(); |
113 | 9 | info.getFilter().destroy(); |
114 | } | |
115 | 4 | } |
116 | ||
117 | /** | |
118 | * Get the filter info map. This map contains {@link FilterInfo} objects | |
119 | * indexed by the filter name. | |
120 | * | |
121 | * @return The filter info map. | |
122 | */ | |
123 | protected Map getFilterInfoMap() { | |
124 | 131 | return filterInfoMap; |
125 | } | |
126 | ||
127 | /** | |
128 | * Get the filters associated with a particular request. It matches against | |
129 | * the uri-pattern of the filters, checking against the filters in the | |
130 | * same order in which they appear in the configuration file. The values | |
131 | * returned by this function are not cached, so it is the responsibility | |
132 | * of the calling component to cache the filter chain. | |
133 | * | |
134 | * @param request The request. | |
135 | * @return The list of filters. | |
136 | */ | |
137 | private Filter[] getFilters(HttpServletRequest request) { | |
138 | 6 | String contextFreeURI = ResourceMapper.getContextFreeURI(request); |
139 | 6 | List list = new LinkedList(); |
140 | 6 | Iterator filterInfos = getFilterInfoMap().values().iterator(); |
141 | 26 | while (filterInfos.hasNext()) { |
142 | 14 | FilterInfo info = (FilterInfo) filterInfos.next(); |
143 | 14 | if (info.isMatch(contextFreeURI)) { |
144 | 10 | list.add(info.getFilter()); |
145 | } | |
146 | } | |
147 | 6 | Filter[] filters = new Filter[list.size()]; |
148 | 6 | list.toArray(filters); |
149 | 6 | return filters; |
150 | } | |
151 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |