KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > security > jacc > URLPattern


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 /* The Apache Software License, Version 1.1
24  *
25  * Copyright (c) 1999 The Apache Software Foundation. All rights
26  * reserved.
27  *
28  * Redistribution and use in source and binary forms, with or without
29  * modification, are permitted provided that the following conditions
30  * are met:
31  *
32  * 1. Redistributions of source code must retain the above copyright
33  * notice, this list of conditions and the following disclaimer.
34  *
35  * 2. Redistributions in binary form must reproduce the above copyright
36  * notice, this list of conditions and the following disclaimer in
37  * the documentation and/or other materials provided with the
38  * distribution.
39  *
40  * 3. The end-user documentation included with the redistribution, if
41  * any, must include the following acknowlegement:
42  * "This product includes software developed by the
43  * Apache Software Foundation (http://www.apache.org/)."
44  * Alternately, this acknowlegement may appear in the software itself,
45  * if and wherever such third-party acknowlegements normally appear.
46  *
47  * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
48  * Foundation" must not be used to endorse or promote products derived
49  * from this software without prior written permission. For written
50  * permission, please contact apache@apache.org.
51  *
52  * 5. Products derived from this software may not be called "Apache"
53  * nor may "Apache" appear in their names without prior written
54  * permission of the Apache Group.
55  *
56  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
57  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
58  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
59  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
60  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
61  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
62  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
63  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
64  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
65  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
66  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67  * SUCH DAMAGE.
68  * ====================================================================
69  *
70  * This software consists of voluntary contributions made by many
71  * individuals on behalf of the Apache Software Foundation. For more
72  * information on the Apache Software Foundation, please see
73  * <http://www.apache.org/>.
74  *
75  * [Additional notices, if required by prior licensing conditions]
76  *
77  */

78
79 package javax.security.jacc;
80
81 /**
82  *
83  * @see
84  *
85  * @author Ron Monzillo
86  *
87  * @serial exclude
88  */

89
90
91 class URLPattern extends Object JavaDoc implements Comparable JavaDoc
92 {
93
94     private static String JavaDoc DEFAULT_PATTERN = "/";
95
96     private int patternType = -1;
97
98     private final String JavaDoc pattern;
99
100     public URLPattern ()
101     {
102         this.pattern = DEFAULT_PATTERN;
103         this.patternType = PT_DEFAULT;
104     }
105
106     // note tht the EMPTY_STRING is legitimte URL_PATTERN
107
public URLPattern (String JavaDoc p)
108     {
109         if (p == null) {
110         this.pattern = DEFAULT_PATTERN;
111         this.patternType = PT_DEFAULT;
112     }
113     else this.pattern = p;
114     }
115
116     /* changed to order default pattern / below extension */
117     public static final int PT_DEFAULT = 0;
118     public static final int PT_EXTENSION = 1;
119     public static final int PT_PREFIX = 2;
120     public static final int PT_EXACT = 3;
121
122     public int patternType() {
123     if (this.patternType < 0) {
124         if (this.pattern.startsWith("*."))
125         this.patternType = PT_EXTENSION;
126         else if (this.pattern.startsWith("/") &&
127              this.pattern.endsWith("/*")) this.patternType = PT_PREFIX;
128         else if (this.pattern.equals(DEFAULT_PATTERN))
129         this.patternType = PT_DEFAULT;
130         else this.patternType = PT_EXACT;
131     }
132         return this.patternType;
133     }
134
135     public int compareTo(Object JavaDoc o) {
136
137     if (!(o instanceof URLPattern JavaDoc))
138         throw new ClassCastException JavaDoc("argument must be URLPattern");
139     
140     URLPattern JavaDoc p = (URLPattern JavaDoc) o;
141         
142     if (p == null) p = new URLPattern JavaDoc(null);
143
144         int refPatternType = this.patternType();
145
146     /* The comparison yields increasing sort order
147      * by pattern type. That is, prefix patterns sort before exact
148      * patterns. Also shorter length patterns precede longer
149      * length patterns. This is important for the URLPatternList
150      * canonicalization done by URLPatternSpec.setURLPatternArray
151      */

152     int result = refPatternType - p.patternType();
153
154     if (result == 0) {
155
156             if (refPatternType == PT_PREFIX || refPatternType == PT_EXACT) {
157
158                 result = this.getPatternDepth() - p.getPatternDepth();
159
160                 if (result == 0) result = this.pattern.compareTo(p.pattern);
161
162             }
163
164             else result = this.pattern.compareTo(p.pattern);
165     }
166     
167     return (result > 0 ? 1 : (result < 0 ? -1 : 0));
168     }
169
170     /**
171      * Does this pattern imply (that is, match) the argument pattern?
172      * This method follows the same rules (in the same order) as those used
173      * for mapping requests to servlets.
174      *<P>
175      * Two URL patterns match if they are related as follows: <p>
176      * <ul>
177      * <li> their pattern values are String equivalent, or
178      * <li> this pattern is the path-prefix pattern "/*", or
179      * <li> this pattern is a path-prefix pattern (that is, it starts with
180      * "/" and ends with "/*") and the argument pattern starts with the
181      * substring of this pattern, minus its last 2 characters, and the
182      * next character of the argument pattern, if there is one, is "/", or
183      * <li> this pattern is an extension pattern (that is, it starts with
184      * "*.") and the argument pattern ends with this pattern, or
185      * <li> the reference pattern is the special default pattern, "/",
186      * which matches all argument patterns.
187      * </ul>
188
189      * @param p URLPattern to determine if implied by (matched by)
190      * this URLPattern to
191      */

192     public boolean implies(URLPattern JavaDoc p) {
193
194         // Normalize the argument
195
if (p == null) p = new URLPattern JavaDoc(null);
196
197         String JavaDoc path = p.pattern;
198         String JavaDoc pattern = this.pattern;
199
200         // Check for exact match
201
if (pattern.equals(path))
202             return (true);
203
204         // Check for path prefix matching
205
if (pattern.startsWith("/") && pattern.endsWith("/*")) {
206             pattern = pattern.substring(0, pattern.length() - 2);
207
208         int length = pattern.length();
209
210             if (length == 0) return (true); // "/*" is the same as the DEFAULT_PATTERN
211

212         return (path.startsWith(pattern) &&
213             (path.length() == length ||
214              path.substring(length).startsWith("/")));
215         }
216
217         // Check for suffix matching
218
if (pattern.startsWith("*.")) {
219             int slash = path.lastIndexOf('/');
220             int period = path.lastIndexOf('.');
221             if ((slash >= 0) && (period > slash) &&
222                 path.endsWith(pattern.substring(1))) {
223                 return (true);
224             }
225             return (false);
226         }
227
228         // Check for universal mapping
229
if (pattern.equals(DEFAULT_PATTERN))
230             return (true);
231
232         return (false);
233     }
234
235     public boolean equals(Object JavaDoc obj) {
236     if (! (obj instanceof URLPattern JavaDoc)) return false;
237     return this.pattern.equals(((URLPattern JavaDoc) obj).pattern);
238     }
239
240     public String JavaDoc toString() {
241         return this.pattern;
242     }
243
244     public int getPatternDepth() {
245
246     int i = 0;
247     int depth = 1;
248
249     while (i >= 0) {
250
251         i = this.pattern.indexOf("/",i);
252
253         if (i >= 0 ) {
254
255         if (i == 0 && depth != 1)
256             throw new IllegalArgumentException JavaDoc("// in pattern");
257
258         i += 1;
259         }
260     }
261
262     return depth;
263     }
264 }
265
266
267
268
269
270
271
272
273
274
Popular Tags