KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 package javax.security.jacc;
25
26 import java.util.Arrays JavaDoc;
27
28 import javax.security.jacc.URLPattern JavaDoc;
29
30 /**
31  * This class extends the URLPattern class and is used to represent
32  * URLPatternSpec objects.
33  * URLPatternSpec objects occur withing WebResourcePermission and
34  * WebUserDataPermission objects.
35  *
36  * <P>
37  *
38  * @author Ron Monzillo
39  * @author Gary Ellison
40  */

41
42 class URLPatternSpec extends URLPattern JavaDoc
43 {
44
45     private static String JavaDoc DEFAULT_PATTERN = "/";
46
47     private static String JavaDoc EMPTY_STRING = "";
48
49     private transient int hashCodeValue = 0;
50
51     private String JavaDoc canonicalSpec = null;
52
53     private final String JavaDoc urlPatternList;
54     
55     private URLPattern JavaDoc[] urlPatternArray = null;
56
57     /**
58      * Creates a new URLPatternSpec that identifies the web
59      * resources to which a WebResourcePermission or WebUserDataPermission
60      * applies. The syntax of the name
61      * parameter is as follows:
62      * <P><Pre>
63      *
64      * URLPatternList ::= URLPattern | URLPatternList colon URLPattern
65      *
66      * URLPatternSpec ::= URLPattern | URLPattern colon URLPatternList
67      *
68      * name ::= URLPatternSpec
69      * </Pre><P>
70      * The first URLPattern in a URLPatternSpec may be any of the pattern
71      * types, exact, path-prefix, extension, or default as defined in the
72      * <i>Java Servlet Specification)</i>. When a URLPatternSpec includes
73      * a URLPatternList, the patterns of the URLPatternList identify the
74      * resources to which the permission does NOT apply and depend on the
75      * pattern type and value of the first pattern as follows: <p>
76      * <ul>
77      * <li> No pattern may exist in the URLPatternList that matches the
78      * first pattern.
79      * <li> If the first pattern is a path-prefix
80      * pattern, only exact patterns matched by the first pattern
81      * and path-prefix patterns matched by, but different from,
82      * the first pattern may occur in the URLPatternList.
83      * <li> If the first pattern is an extension
84      * pattern, only exact patterns that are matched by the first
85      * pattern and path-prefix patterns may occur in the URLPatternList.
86      * <li> If the first pattern is the default pattern, "/", any pattern
87      * except the default pattern may occur in the URLPatternList.
88      * <li> If the first pattern is an exact pattern a URLPatternList must not
89      * be present in the URLPatternSpec.
90      * </ul>
91      * <P>
92      * @param urlPatternSpec a String containing a URLPatternSpec
93      * that identifies the application
94      * specific web resources to which the permission pertains.
95      * All URLPatterns in the URLPatternSpec are relative to the context path
96      * of the deployed web application module, and the same URLPattern must not
97      * occur more than once in a URLPatternSpec.
98      */

99
100     public URLPatternSpec(String JavaDoc urlPatternSpec)
101     {
102     super(getFirstPattern(urlPatternSpec));
103     int colon = urlPatternSpec.indexOf(":");
104     if (colon >= 0) {
105         urlPatternList = urlPatternSpec.substring(colon+1);
106         setURLPatternArray();
107     }
108     else urlPatternList = null;
109     }
110
111    /**
112     * This method returns a String containing the first URLPattern in
113     * this URLPatternSpec.
114     */

115
116     public String JavaDoc getURLPattern()
117     {
118     return super.toString();
119     }
120     
121     /*
122      * Checks two URLPatternSpec objects for equality.
123      * A reference URLPatternSpec is equivalent to an argument URLPatternSpec
124      * if their first patterns are equivalent, and the patterns of its
125      * URLPatternList collectively match exactly the same set of
126      * patterns as are matched by the patterns of the URLPatternList of the
127      * argument URLPatternSpec.
128      */

129     public boolean equals(Object JavaDoc o)
130     {
131     if (o == null || ! (o instanceof URLPatternSpec JavaDoc)) return false;
132
133     URLPatternSpec JavaDoc that = (URLPatternSpec JavaDoc) o;
134
135     return this.toString().equals(that.toString());
136     }
137
138    /**
139     * Returns the hash code value for this URLPatternSpec
140     * properties of the returned hash code must be as follows: <p>
141     * <ul>
142     * <li> During the lifetime of a Java application, the hashCode method
143     * must return the same integer value, every time it is called on a
144     * URLPatternSpec object. The value returned by hashCode for a
145     * particular URlPatternSpec need not remain consistent from
146     * one execution of an application to another.
147     * <li> If two URLPatternSpec objects are equal according to the
148     * equals method, then calling the hashCode method on each of the two
149     * objects must produce the same integer result (within an
150     * application).
151     * </ul>
152     * <P>
153     * @return the integer hash code value for this object.
154     */

155
156     public int hashCode()
157     {
158     if (hashCodeValue == 0)
159         hashCodeValue = this.toString().hashCode();
160
161     return hashCodeValue;
162     }
163
164     /**
165      * Determines if the argument URLPatternSpec is "implied by" this
166      * URLPatternSpec. For this to be the case, all of the following must
167      * be true:<p>
168      * <ul>
169      * <li> The argument is an instanceof URLPatternSpec, and
170      * <li> The first Pattern in the argument URLPatternSpec
171      * is matched by the first URLPattern of this URLPatternSpec.
172      * <li> The first Pattern in the argument URLPatternSpec
173      * is NOT matched by any URLPattern in the URLPatternList
174      * of this URLPatternSpec.
175      * <li> If the first Pattern in the argument URLPatternSpec matches
176      * the first Pattern in this URLPatternSpec, then every URLPattern
177      * in the URLPatternList of this URLPatternSpec is matched by
178      * a URLPattern in the URLPatternList of the argument URLPatternSpec.
179      * </ul>
180      * <P>
181      * URLPattern matching is performed using the <i>Servlet matching
182      * rules</i> where two URL patterns match if they are related as follows:
183      * <p><ul>
184      * <li> their pattern values are String equivalent, or
185      * <li> this pattern is the path-prefix pattern "/*", or
186      * <li> this pattern is a path-prefix pattern (that is, it starts with
187      * "/" and ends with "/*") and the argument pattern starts with the
188      * substring of this pattern, minus its last 2 characters, and the
189      * next character of the argument pattern, if there is one, is "/", or
190      * <li> this pattern is an extension pattern (that is, it starts with
191      * "*.") and the argument pattern ends with this pattern, or
192      * <li> the reference pattern is the special default pattern, "/",
193      * which matches all argument patterns.
194      * </ul>
195      * <P>
196      * All of the comparisons described above are case sensitive.
197      * <P>
198      * @param that "this" URLPatternSpec is checked to see if
199      * it implies the argument URLPatternSpec.
200      * <P>
201      * @return true if the specified URLPatternSpec is implied by this
202      * URLPatternSpec, false if not.
203      */

204
205     public boolean implies(URLPatternSpec JavaDoc that)
206     {
207     if (that == null) return false;
208
209     if (!super.implies(that)) return false;
210        
211     for (int i=0; urlPatternArray != null && i<urlPatternArray.length; i++)
212
213         if (urlPatternArray[i] != null &&
214         urlPatternArray[i].implies(that)) return false;
215
216     if (urlPatternArray != null && ((URLPattern JavaDoc)that).implies(this)) {
217
218         if (that.urlPatternArray == null) return false;
219
220         boolean flags[] = new boolean[urlPatternArray.length];
221
222         for (int i=0; i<flags.length; i++) flags[i] = false;
223
224         int count = 0;
225         
226         for (int j=0; j<that.urlPatternArray.length; j++) {
227
228         for (int i=0; i<flags.length; i++) {
229
230             if (!flags[i])
231             if (urlPatternArray[i] == null ||
232                 (that.urlPatternArray[j] != null &&
233                  that.urlPatternArray[j].implies
234                  (urlPatternArray[i]))) {
235
236                 count += 1;
237                 flags[i] = true;
238                 if (count == flags.length) return true;
239             }
240         }
241         }
242
243         return (count == flags.length);
244     }
245
246     return true;
247     }
248
249
250     /*
251      * This method returns a canonical String representation of the
252      * URLPatternSpec. By this time, the patterns have already
253      * been sorted, and pruned by setURLPatternArray, such that
254      * all this method has to do is glue them together into a string.
255      */

256
257     public String JavaDoc toString()
258     {
259     if (canonicalSpec == null) {
260
261         if (urlPatternList == null)
262         canonicalSpec = new String JavaDoc(super.toString());
263
264         else {
265
266         StringBuffer JavaDoc s = null;
267
268         for (int i=0; i<urlPatternArray.length; i++) {
269             if (urlPatternArray[i] != null) {
270             if (s == null)
271                s = new StringBuffer JavaDoc(urlPatternArray[i].toString());
272             else s.append(":" + urlPatternArray[i].toString());
273             }
274         }
275
276         if (s == null) canonicalSpec = new String JavaDoc(super.toString());
277         else canonicalSpec =
278             new String JavaDoc(super.toString() + ":" + s.toString());
279         }
280     }
281
282     return canonicalSpec;
283     }
284
285     // ----------------- Private Methods ---------------------
286

287     private static String JavaDoc getFirstPattern(String JavaDoc urlPatternSpec)
288     {
289     if (urlPatternSpec == null)
290         throw new IllegalArgumentException JavaDoc("Invalid URLPatternSpec");
291     int colon = urlPatternSpec.indexOf(":");
292     if (colon < 0) return urlPatternSpec;
293     else if (colon > 0) return urlPatternSpec.substring(0,colon);
294     else if (colon == 0) return EMPTY_STRING;
295     throw new IllegalArgumentException JavaDoc("Invalid URLPatternSpec");
296     }
297
298     private void setURLPatternArray()
299     {
300     if (urlPatternArray == null && urlPatternList != null) {
301
302         String JavaDoc[] tokens = urlPatternList.split(":",-1);
303
304         int count = tokens.length;
305
306         if (count == 0)
307         throw new IllegalArgumentException JavaDoc
308             ("colon followed by empty URLPatternList");
309         urlPatternArray = new URLPattern JavaDoc[count];
310     
311         int firstType = this.patternType();
312
313         for (int i=0; i<count; i++) {
314
315         urlPatternArray[i] = new URLPattern JavaDoc(tokens[i]);
316
317         if (urlPatternArray[i].implies(this))
318             throw new IllegalArgumentException JavaDoc
319             ("pattern in URLPatternList implies first pattern");
320
321         switch(firstType) {
322         case URLPattern.PT_PREFIX:
323         case URLPattern.PT_EXTENSION:
324             switch (urlPatternArray[i].patternType()) {
325             case URLPattern.PT_PREFIX:
326             if (firstType == URLPattern.PT_PREFIX) {
327                 if (super.equals(urlPatternArray[i]) ||
328                 !super.implies(urlPatternArray[i]))
329                 throw new IllegalArgumentException JavaDoc
330                   ("Invalid prefix pattern in URLPatternList");
331             }
332             break;
333             case URLPattern.PT_EXACT:
334             if (!super.implies(urlPatternArray[i]))
335                 throw new IllegalArgumentException JavaDoc
336                 ("Invalid exact pattern in URLPatternList");
337             break;
338             default:
339             throw new IllegalArgumentException JavaDoc
340                 ("Invalid pattern type in URLPatternList");
341             }
342         case URLPattern.PT_DEFAULT:
343             if (super.equals(urlPatternArray[i]))
344             throw new IllegalArgumentException JavaDoc
345                 ("Invalid default pattern in URLPatternList");
346             break;
347         case URLPattern.PT_EXACT:
348             throw new IllegalArgumentException JavaDoc
349             ("invalid URLPatternSpec");
350         default:
351             throw new IllegalArgumentException JavaDoc
352             ("Invalid pattern type in URLPatternList");
353         }
354         }
355
356         Arrays.sort(urlPatternArray);
357
358         for (int i=0; i<urlPatternArray.length; i++) {
359         if (urlPatternArray[i] != null) {
360             switch(urlPatternArray[i].patternType()) {
361             case URLPattern.PT_PREFIX:
362             for (int j=i+1; j<urlPatternArray.length; j++)
363                 if (urlPatternArray[i].implies(urlPatternArray[j]))
364                 urlPatternArray[j] = null;
365             break;
366             default:
367             break;
368             }
369         }
370         }
371     }
372     }
373
374 }
375
376
377
378
Popular Tags