All Downloads are FREE. Search and download functionalities are using the official Maven repository.

javax.security.jacc.URLPattern Maven / Gradle / Ivy

The newest version!
package javax.security.jacc;

/**
 * 

* The representation of a URLPattern in the {@code WebResourcePermission} and {@code WebUserDataPermission} * URLPatternSpecs. *

* * @author Scott Stark * @author Stefan Guilhen * @see WebResourcePermission * @see WebUserDataPermission */ class URLPattern { private enum PatternType { /** the '/' pattern */ DEFAULT, /** the '/*' pattern */ THE_PATH_PREFIX, /** a '/.../*' pattern */ PATH_PREFIX, /** a '*.xxx' pattern */ EXTENSION, /** an exact pattern */ EXACT }; private String pattern; private String ext; private int length; private PatternType type; /** *

* Creates a {@code URLPattern} instance from the specified pattern {@code String}. *

* * @param pattern * the pattern {@code String}. */ URLPattern(String pattern) { this.pattern = pattern; length = pattern.length(); if (pattern.equals("/")) type = PatternType.DEFAULT; else if (pattern.startsWith("/*")) type = PatternType.THE_PATH_PREFIX; else if (length > 0 && pattern.charAt(0) == '/' && pattern.endsWith("/*")) type = PatternType.PATH_PREFIX; else if (pattern.startsWith("*.")) { type = PatternType.EXTENSION; ext = pattern.substring(1); } else type = PatternType.EXACT; } /** *

* Checks if this pattern matches the specified {@code URLPattern}. *

* *

* The matching rules from the {@code WebResourcePermission#implies}: *

    *
  1. their pattern values are {@code String} equivalent, or
  2. *
  3. this pattern is the path-prefix pattern "/*", or
  4. *
  5. this pattern is a path-prefix pattern (that is, it starts with "/" and ends with "/*") and the argument * pattern starts with the substring of this pattern, minus its last 2 characters, and the next character of the * argument pattern, if there is one, is "/", or
  6. *
  7. this pattern is an extension pattern (that is, it starts with "*.") and the argument pattern ends with this * pattern, or 5. the reference pattern is the special default pattern, "/", which matches all argument patterns.
  8. *
*

* * @param url * the {@code URLPattern} instance to which this pattern is to be matched. * @return {@code true} if this pattern matches the specified {@code URLPattern}; {@code false} otherwise. */ boolean matches(URLPattern url) { return matches(url.pattern); } /** *

* Checks if this pattern matches the specified pattern String. *

* *

* The matching rules from the {@code WebResourcePermission#implies}: *

    *
  1. their pattern values are {@code String} equivalent, or
  2. *
  3. this pattern is the path-prefix pattern "/*", or
  4. *
  5. this pattern is a path-prefix pattern (that is, it starts with "/" and ends with "/*") and the argument * pattern starts with the substring of this pattern, minus its last 2 characters, and the next character of the * argument pattern, if there is one, is "/", or
  6. *
  7. this pattern is an extension pattern (that is, it starts with "*.") and the argument pattern ends with this * pattern, or 5. the reference pattern is the special default pattern, "/", which matches all argument patterns.
  8. *
*

* * @param urlPattern * a {@code String} representing the pattern to which this pattern is to be matched. * @return {@code true} if this pattern matches the specified {@code URLPattern}; {@code false} otherwise. */ boolean matches(String urlPattern) { // 2 or 5 if (type == PatternType.DEFAULT || type == PatternType.THE_PATH_PREFIX) return true; // 4, extension pattern if (type == PatternType.EXTENSION && urlPattern.endsWith(ext)) return true; // 3. a path-prefix pattern if (type == PatternType.PATH_PREFIX) { if (urlPattern.regionMatches(0, pattern, 0, length - 2)) { int last = length - 2; if (urlPattern.length() > last && urlPattern.charAt(last) != '/') return false; return true; } return false; } // 1. pattern values are String equivalent for exact pattern if (pattern.equals(urlPattern)) return true; return false; } /** *

* Obtains the {@code String} representation of this pattern. *

* * @return this pattern's {@code String} representation. */ String getPattern() { return this.pattern; } /** *

* Checks if this pattern is a default (i.e. '/') pattern. *

* * @return {@code true} if this is a default pattern; {@code false} otherwise. */ boolean isDefault() { return this.type == PatternType.DEFAULT; } /** *

* Checks if this pattern is an exact pattern. *

* * @return {@code true} if this is an exact pattern; {@code false} otherwise. */ boolean isExact() { return this.type == PatternType.EXACT; } /** *

* Checks if this pattern is an extension (i.e. '*.xxx') pattern. *

* * @return {@code true} if this is an extension pattern; {@code false} otherwise. */ boolean isExtension() { return this.type == PatternType.EXTENSION; } /** *

* Checks if this pattern is a prefix (i.e. '/*' or '/.../*') pattern. *

* * @return {@code true} if this is a prefix pattern; {@code false} otherwise. */ boolean isPrefix() { return this.type == PatternType.THE_PATH_PREFIX || this.type == PatternType.PATH_PREFIX; } /* * (non-Javadoc) * * @see java.lang.Object#hashCode() */ @Override public int hashCode() { return this.pattern.hashCode(); } /* * (non-Javadoc) * * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object o) { if (o instanceof URLPattern == false) return false; URLPattern pattern = (URLPattern) o; boolean equals = this.type == pattern.type; if (equals) { equals = this.pattern.equals(pattern.pattern); } return equals; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy