1 23 24 package javax.security.jacc; 25 26 import java.util.Arrays ; 27 28 import javax.security.jacc.URLPattern ; 29 30 41 42 class URLPatternSpec extends URLPattern 43 { 44 45 private static String DEFAULT_PATTERN = "/"; 46 47 private static String EMPTY_STRING = ""; 48 49 private transient int hashCodeValue = 0; 50 51 private String canonicalSpec = null; 52 53 private final String urlPatternList; 54 55 private URLPattern [] urlPatternArray = null; 56 57 99 100 public URLPatternSpec(String 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 115 116 public String getURLPattern() 117 { 118 return super.toString(); 119 } 120 121 129 public boolean equals(Object o) 130 { 131 if (o == null || ! (o instanceof URLPatternSpec )) return false; 132 133 URLPatternSpec that = (URLPatternSpec ) o; 134 135 return this.toString().equals(that.toString()); 136 } 137 138 155 156 public int hashCode() 157 { 158 if (hashCodeValue == 0) 159 hashCodeValue = this.toString().hashCode(); 160 161 return hashCodeValue; 162 } 163 164 204 205 public boolean implies(URLPatternSpec 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 )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 256 257 public String toString() 258 { 259 if (canonicalSpec == null) { 260 261 if (urlPatternList == null) 262 canonicalSpec = new String (super.toString()); 263 264 else { 265 266 StringBuffer 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 (urlPatternArray[i].toString()); 272 else s.append(":" + urlPatternArray[i].toString()); 273 } 274 } 275 276 if (s == null) canonicalSpec = new String (super.toString()); 277 else canonicalSpec = 278 new String (super.toString() + ":" + s.toString()); 279 } 280 } 281 282 return canonicalSpec; 283 } 284 285 287 private static String getFirstPattern(String urlPatternSpec) 288 { 289 if (urlPatternSpec == null) 290 throw new IllegalArgumentException ("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 ("Invalid URLPatternSpec"); 296 } 297 298 private void setURLPatternArray() 299 { 300 if (urlPatternArray == null && urlPatternList != null) { 301 302 String [] tokens = urlPatternList.split(":",-1); 303 304 int count = tokens.length; 305 306 if (count == 0) 307 throw new IllegalArgumentException 308 ("colon followed by empty URLPatternList"); 309 urlPatternArray = new URLPattern [count]; 310 311 int firstType = this.patternType(); 312 313 for (int i=0; i<count; i++) { 314 315 urlPatternArray[i] = new URLPattern (tokens[i]); 316 317 if (urlPatternArray[i].implies(this)) 318 throw new IllegalArgumentException 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 330 ("Invalid prefix pattern in URLPatternList"); 331 } 332 break; 333 case URLPattern.PT_EXACT: 334 if (!super.implies(urlPatternArray[i])) 335 throw new IllegalArgumentException 336 ("Invalid exact pattern in URLPatternList"); 337 break; 338 default: 339 throw new IllegalArgumentException 340 ("Invalid pattern type in URLPatternList"); 341 } 342 case URLPattern.PT_DEFAULT: 343 if (super.equals(urlPatternArray[i])) 344 throw new IllegalArgumentException 345 ("Invalid default pattern in URLPatternList"); 346 break; 347 case URLPattern.PT_EXACT: 348 throw new IllegalArgumentException 349 ("invalid URLPatternSpec"); 350 default: 351 throw new IllegalArgumentException 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 |