001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package javax.security.jacc;
023:
024: /** The representation of a URLPattern in the WebResourcePermission and
025: * WebUserDataPermission URLPatternSpecs.
026: *
027: * @author Scott.Stark@jboss.org
028: * @version $Revison:$
029: */
030: class URLPattern {
031: /** the '/' pattern */
032: static final int DEFAULT = 0;
033: /** the '/*' pattern */
034: static final int THE_PATH_PREFIX = 1;
035: /** a '/.../*' pattern */
036: static final int PATH_PREFIX = 2;
037: /** a '*.xxx' pattern */
038: static final int EXTENSION = 3;
039: /** an exact pattern */
040: static final int EXACT = 4;
041:
042: private String pattern;
043: private String ext;
044: private int length;
045: private int type = -1;
046:
047: URLPattern(String pattern) {
048: this .pattern = pattern;
049: length = pattern.length();
050: if (pattern.equals("/"))
051: type = DEFAULT;
052: else if (pattern.startsWith("/*"))
053: type = THE_PATH_PREFIX;
054: else if (length > 0 && pattern.charAt(0) == '/'
055: && pattern.endsWith("/*"))
056: type = PATH_PREFIX;
057: else if (pattern.startsWith("*.")) {
058: type = EXTENSION;
059: ext = pattern.substring(1);
060: } else
061: type = EXACT;
062: }
063:
064: /** The matching rules from the WebResourcePermission implies:
065:
066: 1. their pattern values are String equivalent, or
067: 2. this pattern is the path-prefix pattern "/*", or
068: 3. this pattern is a path-prefix pattern (that is, it starts with "/" and ends
069: with "/*") and the argument pattern starts with the substring of this
070: pattern, minus its last 2 characters, and the next character of the
071: argument pattern, if there is one, is "/", or
072: 4. this pattern is an extension pattern (that is, it starts with "*.") and the
073: argument pattern ends with this pattern, or
074: 5. the reference pattern is the special default pattern, "/", which matches all
075: argument patterns.
076: */
077: boolean matches(URLPattern url) {
078: return matches(url.pattern);
079: }
080:
081: boolean matches(String urlPattern) {
082: // 2 or 5
083: if (type == DEFAULT || type == THE_PATH_PREFIX)
084: return true;
085:
086: // 4, extension pattern
087: if (type == EXTENSION && urlPattern.endsWith(ext))
088: return true;
089:
090: // 3. a path-prefix pattern
091: if (type == PATH_PREFIX) {
092: if (urlPattern.regionMatches(0, pattern, 0, length - 2)) {
093: int last = length - 2;
094: if (urlPattern.length() > last
095: && urlPattern.charAt(last) != '/')
096: return false;
097: return true;
098: }
099: return false;
100: }
101:
102: // 1. pattern values are String equivalent for exact pattern
103: if (pattern.equals(urlPattern))
104: return true;
105:
106: return false;
107: }
108:
109: String getPattern() {
110: return pattern;
111: }
112:
113: boolean isDefault() {
114: return type == DEFAULT;
115: }
116:
117: boolean isExact() {
118: return type == EXACT;
119: }
120:
121: boolean isExtension() {
122: return type == EXTENSION;
123: }
124:
125: boolean isPrefix() {
126: return type == THE_PATH_PREFIX || type == PATH_PREFIX;
127: }
128:
129: public int hashCode() {
130: return pattern.hashCode();
131: }
132:
133: boolean equals(URLPattern p) {
134: boolean equals = type == p.type;
135: if (equals) {
136: equals = pattern.equals(p.pattern);
137: }
138: return equals;
139: }
140:
141: }
|