Coverage details for org.chwf.servlet.filter.AllFilterConfig

LineHitsSource
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. */
6660  private final Map filterClassMap = new SequencedHashMap();
67  
68   /** Init parameters. */
6960  private final Map initParamMap = new HashMap();
70  
71   /** URI Patterns. */
7260  private final Map uriPatternsMap = new HashMap();
73  
74   /** URI Patterns. */
7560  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)
8560    throws ServletException {
86     try {
8760      this.context = context;
8860      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
8960      Document doc = dbf.newDocumentBuilder().parse(xml);
9059      NodeList filterNodes = doc.getElementsByTagName("filter");
91186      for (int i = 0; i < filterNodes.getLength(); i++) {
92127        Element filterNode = (Element) filterNodes.item(i);
93127        String name = getChildText(filterNode, "filter-name");
94127        initFilterClass(filterNode, name);
95127        initParamMap(filterNode, name);
96127        initURIPatterns(filterNode, name);
97       }
9859      NodeList resourceNodes = doc.getElementsByTagName("resource-mapping");
9982      for (int i = 0; i < resourceNodes.getLength(); i++) {
10023        Element resourceNode = (Element) resourceNodes.item(i);
10123        String mapped = deriveExtension(resourceNode, "mapped-extension");
10223        String resource = deriveExtension(resourceNode, "resource-extension");
10323        this.resourceMapping.put(mapped, resource);
104       }
10559    } catch (Exception ex) {
1061      String msg = "Flaw in filter configuration [" + ex.getMessage() + "]";
1071      context.log(msg, ex);
1081      throw new ServletException(msg, ex);
109     }
11059  }
111   
112   /**
113    * The servlet context.
114    *
115    * @return The servlet context.
116    */
117   public ServletContext getContext() {
11895    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() {
12834    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) {
13895    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) {
14895    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) {
15895    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() {
1678    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) {
177127    String className = getChildText(filterNode, "filter-class");
178127    this.filterClassMap.put(name, className);
179127  }
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) {
188127    HashMap initParam = new HashMap();
189127    NodeList paramNodes = filterNode.getElementsByTagName("init-param");
190169    for (int j = 0; j < paramNodes.getLength(); j++) {
19142      Element paramNode = (Element) paramNodes.item(j);
19242      String paramName = getChildText(paramNode, "param-name");
19342      String paramValue = getChildText(paramNode, "param-value");
19442      initParam.put(paramName, paramValue);
195     }
196127    this.initParamMap.put(name, initParam);
197127  }
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) {
206127    List patternsList = new LinkedList();
207127    NodeList patternNodes = filterNode.getElementsByTagName("url-pattern");
208217    for (int j = 0; j < patternNodes.getLength(); j++) {
20990      Element patternNode = (Element) patternNodes.item(j);
21090      Node child = patternNode.getFirstChild();
21190      String pattern = child.getNodeValue();
21290      patternsList.add(pattern);
213     }
214127    this.uriPatternsMap.put(name, toStringArray(patternsList));
215127  }
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) {
224127    String[] array = new String[list.size()];
225127    list.toArray(array);
226127    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) {
237384    Node child = node.getElementsByTagName(tag).item(0);
238384    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) {
24946    String mapped = getChildText(resourceNode, tagName);
25046    if (mapped.startsWith("*")) {
25146      mapped = mapped.substring(1);
252     }
25346    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 {
26614    InputStream stream = context.getResourceAsStream("/WEB-INF/filter.xml");
26714    InputSource xml = new InputSource(stream);
26814    return new AllFilterConfig(context, xml);
269   }
270 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.