1 23 24 package javax.security.jacc; 25 26 import java.util.HashMap ; 27 28 import java.io.IOException ; 29 import java.io.ObjectStreamField ; 30 31 import java.security.*; 32 import javax.security.jacc.URLPatternSpec ; 33 import javax.security.jacc.HttpMethodSpec ; 34 35 import javax.servlet.http.HttpServletRequest ; 36 37 50 51 public final class WebUserDataPermission extends Permission 52 implements java.io.Serializable 53 { 54 55 private static String transportKeys[] = { 56 "NONE", 57 "INTEGRAL", 58 "CONFIDENTIAL", 59 }; 60 61 private static HashMap transportHash = new HashMap (); 62 static { 63 for (int i=0; i<transportKeys.length; i++) 64 transportHash.put(transportKeys[i], new Integer (i)); 65 }; 66 67 private static int TT_NONE = 68 ((Integer ) transportHash.get("NONE")).intValue(); 69 private static int TT_CONFIDENTIAL = 70 ((Integer ) transportHash.get("CONFIDENTIAL")).intValue(); 71 72 private transient URLPatternSpec urlPatternSpec = null; 73 74 private transient HttpMethodSpec methodSpec; 75 76 private transient int transportType; 77 78 private transient int hashCodeValue = 0; 79 80 private transient static final String EMPTY_STRING = ""; 81 82 private static final long serialVersionUID = 1L; 83 84 91 private static final ObjectStreamField [] serialPersistentFields = { 92 new ObjectStreamField ("actions", java.lang.String .class) 93 }; 94 95 187 188 public WebUserDataPermission(String name, String actions) 189 { 190 super(name); 191 this.urlPatternSpec = new URLPatternSpec (name); 192 parseActions(actions); 193 } 194 195 216 217 public WebUserDataPermission(String urlPatternSpec, String [] HTTPMethods, 218 String transportType) 219 { 220 super(urlPatternSpec); 221 this.urlPatternSpec = new URLPatternSpec (urlPatternSpec); 222 223 this.transportType = TT_NONE; 224 225 if (transportType != null) { 226 Integer bit = (Integer ) transportHash.get(transportType); 227 if (bit == null) 228 throw new IllegalArgumentException ("illegal transport value"); 229 this.transportType = bit.intValue(); 230 } 231 232 this.methodSpec = HttpMethodSpec.getSpec(HTTPMethods); 233 } 234 235 250 251 public WebUserDataPermission(HttpServletRequest request) 252 { 253 super(getUriMinusContextPath(request)); 254 this.urlPatternSpec = new URLPatternSpec (super.getName()); 255 this.transportType = request.isSecure() ? TT_CONFIDENTIAL : TT_NONE; 256 this.methodSpec = HttpMethodSpec.getSpec(request.getMethod()); 257 } 258 259 279 280 public boolean equals(Object o) { 281 if (o == null || ! (o instanceof WebUserDataPermission )) return false; 282 283 WebUserDataPermission that = (WebUserDataPermission ) o; 284 285 if (this.transportType != that.transportType) return false; 286 287 if (!this.methodSpec.equals(that.methodSpec)) return false; 288 289 return this.urlPatternSpec.equals(that.urlPatternSpec); 290 } 291 292 338 339 public String getActions() 340 { 341 String result; 342 String hActions = this.methodSpec.getActions(); 343 if (this.transportType == TT_NONE && hActions == null) result = null; 344 else if (this.transportType == TT_NONE) result = hActions; 345 else if (hActions == null) 346 result = ":" + transportKeys[this.transportType]; 347 else result = hActions + ":" + transportKeys[this.transportType]; 348 return result; 349 } 350 351 368 369 public int hashCode() { 370 if (this.hashCodeValue == 0) { 371 String hashInput = new String (this.urlPatternSpec.toString() + 372 " " + this.methodSpec.hashCode() + 373 ":" + this.transportType); 374 375 this.hashCodeValue = hashInput.hashCode(); 376 } 377 return this.hashCodeValue; 378 } 379 380 427 428 public boolean implies(Permission permission) { 429 if (permission == null || 430 ! (permission instanceof WebUserDataPermission )) return false; 431 432 WebUserDataPermission that = (WebUserDataPermission ) permission; 433 434 if (this.transportType != TT_NONE && 435 this.transportType != that.transportType) return false; 436 437 if (!this.methodSpec.implies(that.methodSpec)) return false; 438 439 return this.urlPatternSpec.implies(that.urlPatternSpec); 440 } 441 442 444 450 private static String getUriMinusContextPath(HttpServletRequest request) { 451 String uri = request.getRequestURI(); 452 if (uri != null) { 453 String contextPath = request.getContextPath(); 454 int contextLength = contextPath == null ? 0 : contextPath.length(); 455 if (contextLength > 0) { 456 uri = uri.substring(contextLength); 457 } 458 if (uri.equals("/")) { 459 uri = EMPTY_STRING; 460 } 461 } else { 462 uri = EMPTY_STRING; 463 } 464 return uri; 465 } 466 467 private void parseActions(String actions) 468 { 469 this.transportType = TT_NONE; 470 471 if (actions == null || actions.equals("")) { 472 this.methodSpec = HttpMethodSpec.getSpec((String ) null); 473 } else { 474 int colon = actions.indexOf(':'); 475 if (colon < 0) { 476 this.methodSpec = HttpMethodSpec.getSpec(actions); 477 } else { 478 if (colon == 0) { 479 this.methodSpec = HttpMethodSpec.getSpec((String ) null); 480 } else { 481 this.methodSpec = HttpMethodSpec.getSpec 482 (actions.substring(0,colon)); 483 } 484 Integer bit = (Integer ) 485 transportHash.get(actions.substring(colon+1)); 486 if (bit == null) 487 throw new IllegalArgumentException 488 ("illegal transport value"); 489 490 this.transportType = bit.intValue(); 491 } 492 } 493 } 494 495 502 private synchronized void readObject(java.io.ObjectInputStream s) 503 throws IOException ,ClassNotFoundException 504 { 505 parseActions((String ) s.readFields().get("actions",null)); 506 this.urlPatternSpec = new URLPatternSpec (super.getName()); 507 } 508 509 517 private synchronized void writeObject(java.io.ObjectOutputStream s) 518 throws IOException 519 { 520 s.putFields().put("actions",this.getActions()); 521 s.writeFields(); 522 } 523 524 } 525 526 527 528 529 530 531 532 | Popular Tags |