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

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.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)
7894    throws ServletException {
79     try {
8094      this.filterName = name;
8194      this.context = allFilterConfig.getContext();
8294      this.initParameters = allFilterConfig.getInitParameters(name);
8394      this.uriPatterns = allFilterConfig.getURIPatterns(name);
8494      initMatchAllPatterns();
8594      String filterClass = allFilterConfig.getFilterClass(name);
8694      this.filter = (Filter) Class.forName(filterClass).newInstance();
8794      this.filter.init(this);
8893    } catch (Exception ex) {
891      String msg = "Filter '" + name + "' object could not be initialized";
901      throw new ServletException(msg, ex);
91     }
9293  }
93  
94   /**
95    * The filter object.
96    *
97    * @return The filter object.
98    */
99   public Filter getFilter() {
10076    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() {
1101    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) {
1211    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() {
13015    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() {
1421    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) {
15215    String[] patterns = getURIPatterns();
15315    if ((patterns.length > 0) && (patterns[0] == WILDCARD)) {
1546      return true;
155     }
15613    for (int i = 0; i < patterns.length; i++) {
1579      String pattern = patterns[i];
1589      if (pattern.startsWith(WILDCARD)) {
1594        pattern = pattern.substring(1);
1604        if (contextFreeURI.endsWith(pattern)) {
1612          return true;
162         }
1635      } else if (pattern.endsWith(WILDCARD)) {
1644        pattern = pattern.substring(0, pattern.length() - 1);
1654        if (contextFreeURI.startsWith(pattern)) {
1662          return true;
167         }
1681      } else if (pattern.equals(contextFreeURI)) {
1691        return true;
170       }
171     }
1724    return false;
173   }
174  
175   /** Initialize patterns matching all URI. */
176   private void initMatchAllPatterns() {
177133    for (int i = 0; i < this.uriPatterns.length; i++) {
17839      String pattern = this.uriPatterns[i];
17939      if (pattern.equals(WILDCARD) || pattern.equals("/*")) {
18014        this.uriPatterns[0] = WILDCARD;
181       }
182     }
18394  }
184  
185   /**
186    * The filter's URI patterns.
187    *
188    * @return The filter's URI patterns.
189    */
190   private String[] getURIPatterns() {
19115    return this.uriPatterns;
192   }
193 }

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.