/*
- * $Header: /cvsroot/securityfilter/securityfilter/src/share/org/securityfilter/filter/URLPattern.java,v 1.2 2003/01/02 19:21:28 maxcooper Exp $
- * $Revision: 1.2 $
- * $Date: 2003/01/02 19:21:28 $
+ * $Header: /cvsroot/securityfilter/securityfilter/src/share/org/securityfilter/filter/URLPattern.java,v 1.3 2003/01/05 09:23:53 maxcooper Exp $
+ * $Revision: 1.3 $
+ * $Date: 2003/01/05 09:23:53 $
*
* ====================================================================
* The SecurityFilter Software License, Version 1.1
* Also supports sorting according to the Servlet Spec v2.3.
*
* @author Max Cooper (max@maxcooper.com)
- * @version $Revision: 1.2 $ $Date: 2003/01/02 19:21:28 $
+ * @version $Revision: 1.3 $ $Date: 2003/01/05 09:23:53 $
*/
public class URLPattern implements Comparable {
/**
- * Pattern type for patterns that do not meet the PATH_TYPE
- * or EXTENSION_TYPE pattern type specifications.
+ * 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 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;
* Initialize the patternType protected member.
*/
protected void initPatternType() {
- if (pattern.startsWith("*.")) {
- patternType = URLPattern.EXTENSION_TYPE;
+ if ("/".equals(pattern)) {
+ patternType = DEFAULT_TYPE;
+ } else if (pattern.startsWith("*.")) {
+ patternType = EXTENSION_TYPE;
} else if (pattern.startsWith("/") && pattern.endsWith("/*")) {
- patternType = URLPattern.PATH_TYPE;
+ patternType = PATH_TYPE;
} else {
- patternType = URLPattern.EXACT_TYPE;
+ patternType = EXACT_TYPE;
}
}
* Initialize the convertedPattern protected member.
*/
protected void initConvertedPattern() {
- StringBuffer buf = new StringBuffer(pattern);
- int pos;
- // escape '.' characters
- pos = buf.toString().indexOf('.');
- while (pos != -1) {
- buf.insert(pos, "\\");
- pos = buf.toString().indexOf('.', pos + 2);
- }
- // replace '*' chars in the compiledPattern with '.*'
- pos = buf.toString().indexOf('*');
- while (pos != -1) {
- buf.replace(pos, pos + 1, ".*");
- pos = buf.toString().indexOf('*', pos + 2);
- }
- // replace '/' chars with '/+' to match one or more consecutive slashes
- pos = buf.toString().indexOf('/');
- while (pos != -1) {
- buf.replace(pos, pos + 1, "/+");
- pos = buf.toString().indexOf('/', pos + 2);
- }
- // adjustments for the different expression types
- switch (patternType) {
- case PATH_TYPE:
- // make sure it matches from the start of the string
- buf.insert(0, '^');
- // make sure /foo/* matches /foo and /foo/morestuff, but not /foobar
- buf.insert(buf.length()-4, "(");
- buf.append(")?$");
- break;
- case EXTENSION_TYPE:
- buf.append('$');
- break;
- case EXACT_TYPE:
- buf.insert(0, '^');
- buf.append('$');
- break;
+ if (patternType == DEFAULT_TYPE) {
+ // match anything for default pattern
+ convertedPattern = ".*";
+ } else {
+ StringBuffer buf = new StringBuffer(pattern);
+ int pos;
+ // escape '.' characters
+ pos = buf.toString().indexOf('.');
+ while (pos != -1) {
+ buf.insert(pos, "\\");
+ pos = buf.toString().indexOf('.', pos + 2);
+ }
+ // replace '*' chars in the compiledPattern with '.*'
+ pos = buf.toString().indexOf('*');
+ while (pos != -1) {
+ buf.replace(pos, pos + 1, ".*");
+ pos = buf.toString().indexOf('*', pos + 2);
+ }
+ // replace '/' chars with '/+' to match one or more consecutive slashes
+ // the spec hints that containers are supposed to normalize the extra slashes out,
+ // but testing revealed that sometimes the extra slashes are not normalized out
+ pos = buf.toString().indexOf('/');
+ while (pos != -1) {
+ buf.replace(pos, pos + 1, "/+");
+ pos = buf.toString().indexOf('/', pos + 2);
+ }
+ // adjustments for the different expression types
+ switch (patternType) {
+ case PATH_TYPE:
+ // make sure it matches from the start of the string
+ buf.insert(0, '^');
+ // make sure /foo/* matches /foo and /foo/morestuff, but not /foobar
+ buf.insert(buf.length()-4, "(");
+ buf.append(")?$");
+ break;
+ case EXTENSION_TYPE:
+ buf.append('$');
+ break;
+ case EXACT_TYPE:
+ buf.insert(0, '^');
+ buf.append('$');
+ break;
+ }
+ convertedPattern = buf.toString();
}
- convertedPattern = buf.toString();
}
/**