www.pudn.com > SecurityFilter.rar > URLPattern.java
package dev.trade.common.securityfilter.filter;
import org.apache.oro.text.regex.*;
import dev.trade.common.securityfilter.config.*;
/**
* URLPattern - Contains matchable URL pattern and the associated
* SecurityConstraint and WebResourceCollection objects for the pattern.
* Also supports sorting according to the Servlet Spec v2.3.
*/
public class URLPattern implements Comparable {
/**
* Pattern type for patterns that do not meet the specifications for the
* other pattern types.
*/
public static final int EXACT_TYPE = 1;
/**
* Pattern type for PATH_TYPE mappings. Starts with '/' and ends with '/*'.
*/
public static final int PATH_TYPE = 2;
/**
* Pattern type for EXTENSION_TYPE mappings. Starts with '*.'
*/
public static final int EXTENSION_TYPE = 3;
/**
* Pattern type for EXTENSION_TYPE mappings. Starts with '*.'
*/
public static final int DEFAULT_TYPE = 4;
protected String pattern;
protected String convertedPattern;
protected Pattern compiledPattern;
protected SecurityConstraint constraint;
protected WebResourceCollection resourceCollection;
protected int order;
protected int patternType;
protected int pathLength;
/**
* Construct a new URLPattern object.
*
* @param pattern the url pattern to match
* @param constraint the SecurityConstraint associated with this pattern
* @param resourceCollection the WebResourceCollection associated with this pattern
* @param order the order in which this pattern occurred in the configuration file
* @param compiler a PatternCompiler to use to compile this url pattern
*
* @see URLPatternFactory
*/
public URLPattern(
String pattern,
SecurityConstraint constraint,
WebResourceCollection resourceCollection,
int order,
PatternCompiler compiler
) throws Exception {
this.pattern = pattern;
this.constraint = constraint;
this.resourceCollection = resourceCollection;
this.order = order;
initPatternType();
initPathLength();
initConvertedPattern();
initCompiledPattern(compiler);
}
/**
* Get the url pattern to match.
*/
public String getPattern() {
return pattern;
}
/**
* Get the compiled version of this pattern.
*
* @return compiled version of this pattern
*/
public Pattern getCompiledPattern() {
return compiledPattern;
}
/**
* Get the pattern type. The pattern type will be determined on the first call to this method.
*
* @return EXACT, PATH, or EXTENSION
*/
public int getPatternType() {
return patternType;
}
/**
* Get the path length of the pattern. This is only valid when getPatternType() = PATH.
* Examples:
*
*
* The sort order is dictated by the servlet spec. The ordering by type is:
* EXACT_TYPE
* PATH_TYPE
* EXTENTION_TYPE
* DEFAULT_TYPE
* Ordering among PATH_TYPE patterns is determined by path length, with the
* longer path coming first. If the path lengths are the same, or both patterns
* are of the same type other than PATH_TYPE, ordering is determined by the order
* in which the pattern appeared in the config file.
*
* Thanks to Chris Nokleberg for contributing code for this method.
*
* @param obj another URLPattern to compare to
*
* @return a negative integer, zero, or a positive integer as this object is
* less than, equal to, or greater than the specified object.
*
* @exception ClassCastException thrown if obj is not a URLPattern instance
*/
public int compareTo(Object obj) throws ClassCastException {
URLPattern other = (URLPattern) obj;
// return 0 if the other pattern is equivalent to this one
if (this.equals(other)) {
return 0;
}
int c = patternType - other.patternType;
if (c == 0) {
switch (patternType) {
case PATH_TYPE:
c = other.pathLength - pathLength;
if (c != 0) {
break;
}
/* fall through */
case EXACT_TYPE:
/* fall through */
case EXTENSION_TYPE:
/* fall through */
case DEFAULT_TYPE:
c = order - other.order;
}
}
return c;
}
}
// ----------------------------------------------------------------------------
// EOF