| 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.InputStream; | |
| 36 | import java.util.Collection; | |
| 37 | import java.util.HashMap; | |
| 38 | import java.util.Iterator; | |
| 39 | import java.util.LinkedList; | |
| 40 | import java.util.List; | |
| 41 | import java.util.Map; | |
| 42 | ||
| 43 | import javax.servlet.ServletContext; | |
| 44 | import javax.servlet.ServletException; | |
| 45 | import javax.xml.parsers.DocumentBuilderFactory; | |
| 46 | ||
| 47 | import org.chwf.util.SequencedHashMap; | |
| 48 | import org.w3c.dom.Document; | |
| 49 | import org.w3c.dom.Element; | |
| 50 | import org.w3c.dom.Node; | |
| 51 | import org.w3c.dom.NodeList; | |
| 52 | import org.xml.sax.InputSource; | |
| 53 | ||
| 54 | /** | |
| 55 | * Class with configuration data for an entire set of filters. It retrieves | |
| 56 | * filter configuration from the specified XML file. | |
| 57 | * | |
| 58 | * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a> | |
| 59 | */ | |
| 60 | public class AllFilterConfig { | |
| 61 | ||
| 62 | /** The servlet context. */ | |
| 63 | private final ServletContext context; | |
| 64 | ||
| 65 | /** Filter classes. An ordered map. */ | |
| 66 | 60 | private final Map filterClassMap = new SequencedHashMap(); |
| 67 | ||
| 68 | /** Init parameters. */ | |
| 69 | 60 | private final Map initParamMap = new HashMap(); |
| 70 | ||
| 71 | /** URI Patterns. */ | |
| 72 | 60 | private final Map uriPatternsMap = new HashMap(); |
| 73 | ||
| 74 | /** URI Patterns. */ | |
| 75 | 60 | private final Map resourceMapping = new HashMap(); |
| 76 | ||
| 77 | /** | |
| 78 | * Constructor. | |
| 79 | * | |
| 80 | * @param context The servlet context. | |
| 81 | * @param xml The XML input source. | |
| 82 | * @throws ServletException If there is a flaw in the configuration file. | |
| 83 | */ | |
| 84 | public AllFilterConfig(ServletContext context, InputSource xml) | |
| 85 | 60 | throws ServletException { |
| 86 | try { | |
| 87 | 60 | this.context = context; |
| 88 | 60 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
| 89 | 60 | Document doc = dbf.newDocumentBuilder().parse(xml); |
| 90 | 59 | NodeList filterNodes = doc.getElementsByTagName("filter"); |
| 91 | 186 | for (int i = 0; i < filterNodes.getLength(); i++) { |
| 92 | 127 | Element filterNode = (Element) filterNodes.item(i); |
| 93 | 127 | String name = getChildText(filterNode, "filter-name"); |
| 94 | 127 | initFilterClass(filterNode, name); |
| 95 | 127 | initParamMap(filterNode, name); |
| 96 | 127 | initURIPatterns(filterNode, name); |
| 97 | } | |
| 98 | 59 | NodeList resourceNodes = doc.getElementsByTagName("resource-mapping"); |
| 99 | 82 | for (int i = 0; i < resourceNodes.getLength(); i++) { |
| 100 | 23 | Element resourceNode = (Element) resourceNodes.item(i); |
| 101 | 23 | String mapped = deriveExtension(resourceNode, "mapped-extension"); |
| 102 | 23 | String resource = deriveExtension(resourceNode, "resource-extension"); |
| 103 | 23 | this.resourceMapping.put(mapped, resource); |
| 104 | } | |
| 105 | 59 | } catch (Exception ex) { |
| 106 | 1 | String msg = "Flaw in filter configuration [" + ex.getMessage() + "]"; |
| 107 | 1 | context.log(msg, ex); |
| 108 | 1 | throw new ServletException(msg, ex); |
| 109 | } | |
| 110 | 59 | } |
| 111 | ||
| 112 | /** | |
| 113 | * The servlet context. | |
| 114 | * | |
| 115 | * @return The servlet context. | |
| 116 | */ | |
| 117 | public ServletContext getContext() { | |
| 118 | 95 | return context; |
| 119 | } | |
| 120 | ||
| 121 | /** | |
| 122 | * The list of filter names, in the order that the filters appear in the | |
| 123 | * configuration file. | |
| 124 | * | |
| 125 | * @return The list of filter names. | |
| 126 | */ | |
| 127 | public Iterator getFilterNames() { | |
| 128 | 34 | return this.filterClassMap.keySet().iterator(); |
| 129 | } | |
| 130 | ||
| 131 | /** | |
| 132 | * A map containing init parameters for the named filter. | |
| 133 | * | |
| 134 | * @param name The filter name. | |
| 135 | * @return The init parameters. | |
| 136 | */ | |
| 137 | public Map getInitParameters(String name) { | |
| 138 | 95 | return (Map) this.initParamMap.get(name); |
| 139 | } | |
| 140 | ||
| 141 | /** | |
| 142 | * An array of URI patterns for the named filter. | |
| 143 | * | |
| 144 | * @param name The filter name. | |
| 145 | * @return The URI patterns. | |
| 146 | */ | |
| 147 | public String[] getURIPatterns(String name) { | |
| 148 | 95 | return (String[]) this.uriPatternsMap.get(name); |
| 149 | } | |
| 150 | ||
| 151 | /** | |
| 152 | * The filter class for the named filter. | |
| 153 | * | |
| 154 | * @param name The filter name. | |
| 155 | * @return The URI patterns. | |
| 156 | */ | |
| 157 | public String getFilterClass(String name) { | |
| 158 | 95 | return (String) this.filterClassMap.get(name); |
| 159 | } | |
| 160 | ||
| 161 | /** | |
| 162 | * Get the file-extension mapping for resources. | |
| 163 | * | |
| 164 | * @return The resource mapping. | |
| 165 | */ | |
| 166 | public Map getResourceMapping() { | |
| 167 | 8 | return resourceMapping; |
| 168 | } | |
| 169 | ||
| 170 | /** | |
| 171 | * Init the filter class. | |
| 172 | * | |
| 173 | * @param filterNode The filter node. | |
| 174 | * @param name The filter name. | |
| 175 | */ | |
| 176 | private void initFilterClass(Element filterNode, String name) { | |
| 177 | 127 | String className = getChildText(filterNode, "filter-class"); |
| 178 | 127 | this.filterClassMap.put(name, className); |
| 179 | 127 | } |
| 180 | ||
| 181 | /** | |
| 182 | * Init the init parameters. | |
| 183 | * | |
| 184 | * @param filterNode The filter node. | |
| 185 | * @param name The filter name. | |
| 186 | */ | |
| 187 | private void initParamMap(Element filterNode, String name) { | |
| 188 | 127 | HashMap initParam = new HashMap(); |
| 189 | 127 | NodeList paramNodes = filterNode.getElementsByTagName("init-param"); |
| 190 | 169 | for (int j = 0; j < paramNodes.getLength(); j++) { |
| 191 | 42 | Element paramNode = (Element) paramNodes.item(j); |
| 192 | 42 | String paramName = getChildText(paramNode, "param-name"); |
| 193 | 42 | String paramValue = getChildText(paramNode, "param-value"); |
| 194 | 42 | initParam.put(paramName, paramValue); |
| 195 | } | |
| 196 | 127 | this.initParamMap.put(name, initParam); |
| 197 | 127 | } |
| 198 | ||
| 199 | /** | |
| 200 | * Init the uri patterns. | |
| 201 | * | |
| 202 | * @param filterNode The filter node. | |
| 203 | * @param name The filter name. | |
| 204 | */ | |
| 205 | private void initURIPatterns(Element filterNode, String name) { | |
| 206 | 127 | List patternsList = new LinkedList(); |
| 207 | 127 | NodeList patternNodes = filterNode.getElementsByTagName("url-pattern"); |
| 208 | 217 | for (int j = 0; j < patternNodes.getLength(); j++) { |
| 209 | 90 | Element patternNode = (Element) patternNodes.item(j); |
| 210 | 90 | Node child = patternNode.getFirstChild(); |
| 211 | 90 | String pattern = child.getNodeValue(); |
| 212 | 90 | patternsList.add(pattern); |
| 213 | } | |
| 214 | 127 | this.uriPatternsMap.put(name, toStringArray(patternsList)); |
| 215 | 127 | } |
| 216 | ||
| 217 | /** | |
| 218 | * Convert list to string array. List must contain only strings. | |
| 219 | * | |
| 220 | * @param list The list. | |
| 221 | * @return The string array. | |
| 222 | */ | |
| 223 | private static String[] toStringArray(Collection list) { | |
| 224 | 127 | String[] array = new String[list.size()]; |
| 225 | 127 | list.toArray(array); |
| 226 | 127 | return array; |
| 227 | } | |
| 228 | ||
| 229 | /** | |
| 230 | * Get the text of the child of the specified node. | |
| 231 | * | |
| 232 | * @param node The parent node. | |
| 233 | * @param tag The child node name. | |
| 234 | * @return The text. | |
| 235 | */ | |
| 236 | private String getChildText(Element node, String tag) { | |
| 237 | 384 | Node child = node.getElementsByTagName(tag).item(0); |
| 238 | 384 | return child.getFirstChild().getNodeValue(); |
| 239 | } | |
| 240 | ||
| 241 | /** | |
| 242 | * Derive extension (stripping any leading "*". | |
| 243 | * | |
| 244 | * @param resourceNode The resource name. | |
| 245 | * @param tagName The child tag name. | |
| 246 | * @return The extension. | |
| 247 | */ | |
| 248 | private String deriveExtension(Element resourceNode, String tagName) { | |
| 249 | 46 | String mapped = getChildText(resourceNode, tagName); |
| 250 | 46 | if (mapped.startsWith("*")) { |
| 251 | 46 | mapped = mapped.substring(1); |
| 252 | } | |
| 253 | 46 | return mapped; |
| 254 | } | |
| 255 | ||
| 256 | /** | |
| 257 | * Get the AllFilterConfig from the "WEB-INF/filter.xml" file, using the | |
| 258 | * resource retrieval mechanism of the servlet context. | |
| 259 | * | |
| 260 | * @param context The servlet context. | |
| 261 | * @return The filter config. | |
| 262 | * @throws ServletException If the config cannot be initialized. | |
| 263 | */ | |
| 264 | static AllFilterConfig getConfig(ServletContext context) | |
| 265 | throws ServletException { | |
| 266 | 14 | InputStream stream = context.getResourceAsStream("/WEB-INF/filter.xml"); |
| 267 | 14 | InputSource xml = new InputSource(stream); |
| 268 | 14 | return new AllFilterConfig(context, xml); |
| 269 | } | |
| 270 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |