1 17 18 19 package org.apache.catalina.deploy; 20 21 import java.io.Serializable ; 22 23 24 38 39 public class SecurityConstraint implements Serializable { 40 41 42 44 45 48 public SecurityConstraint() { 49 50 super(); 51 52 } 53 54 55 57 58 62 private boolean allRoles = false; 63 64 65 71 private boolean authConstraint = false; 72 73 74 78 private String authRoles[] = new String [0]; 79 80 81 85 private SecurityCollection collections[] = new SecurityCollection[0]; 86 87 88 91 private String displayName = null; 92 93 94 98 private String userConstraint = "NONE"; 99 100 101 103 104 108 public boolean getAllRoles() { 109 110 return (this.allRoles); 111 112 } 113 114 115 119 public boolean getAuthConstraint() { 120 121 return (this.authConstraint); 122 123 } 124 125 126 130 public void setAuthConstraint(boolean authConstraint) { 131 132 this.authConstraint = authConstraint; 133 134 } 135 136 137 140 public String getDisplayName() { 141 142 return (this.displayName); 143 144 } 145 146 147 150 public void setDisplayName(String displayName) { 151 152 this.displayName = displayName; 153 154 } 155 156 157 160 public String getUserConstraint() { 161 162 return (userConstraint); 163 164 } 165 166 167 172 public void setUserConstraint(String userConstraint) { 173 174 if (userConstraint != null) 175 this.userConstraint = userConstraint; 176 177 } 178 179 180 182 183 189 public void addAuthRole(String authRole) { 190 191 if (authRole == null) 192 return; 193 if ("*".equals(authRole)) { 194 allRoles = true; 195 return; 196 } 197 String results[] = new String [authRoles.length + 1]; 198 for (int i = 0; i < authRoles.length; i++) 199 results[i] = authRoles[i]; 200 results[authRoles.length] = authRole; 201 authRoles = results; 202 authConstraint = true; 203 204 } 205 206 207 213 public void addCollection(SecurityCollection collection) { 214 215 if (collection == null) 216 return; 217 SecurityCollection results[] = 218 new SecurityCollection[collections.length + 1]; 219 for (int i = 0; i < collections.length; i++) 220 results[i] = collections[i]; 221 results[collections.length] = collection; 222 collections = results; 223 224 } 225 226 227 233 public boolean findAuthRole(String role) { 234 235 if (role == null) 236 return (false); 237 for (int i = 0; i < authRoles.length; i++) { 238 if (role.equals(authRoles[i])) 239 return (true); 240 } 241 return (false); 242 243 } 244 245 246 252 public String [] findAuthRoles() { 253 254 return (authRoles); 255 256 } 257 258 259 265 public SecurityCollection findCollection(String name) { 266 267 if (name == null) 268 return (null); 269 for (int i = 0; i < collections.length; i++) { 270 if (name.equals(collections[i].getName())) 271 return (collections[i]); 272 } 273 return (null); 274 275 } 276 277 278 283 public SecurityCollection[] findCollections() { 284 285 return (collections); 286 287 } 288 289 290 297 public boolean included(String uri, String method) { 298 299 if (method == null) 301 return (false); 302 303 for (int i = 0; i < collections.length; i++) { 305 if (!collections[i].findMethod(method)) 306 continue; 307 String patterns[] = collections[i].findPatterns(); 308 for (int j = 0; j < patterns.length; j++) { 309 if (matchPattern(uri, patterns[j])) 310 return (true); 311 } 312 } 313 314 return (false); 316 317 } 318 319 320 326 public void removeAuthRole(String authRole) { 327 328 if (authRole == null) 329 return; 330 int n = -1; 331 for (int i = 0; i < authRoles.length; i++) { 332 if (authRoles[i].equals(authRole)) { 333 n = i; 334 break; 335 } 336 } 337 if (n >= 0) { 338 int j = 0; 339 String results[] = new String [authRoles.length - 1]; 340 for (int i = 0; i < authRoles.length; i++) { 341 if (i != n) 342 results[j++] = authRoles[i]; 343 } 344 authRoles = results; 345 } 346 347 } 348 349 350 356 public void removeCollection(SecurityCollection collection) { 357 358 if (collection == null) 359 return; 360 int n = -1; 361 for (int i = 0; i < collections.length; i++) { 362 if (collections[i].equals(collection)) { 363 n = i; 364 break; 365 } 366 } 367 if (n >= 0) { 368 int j = 0; 369 SecurityCollection results[] = 370 new SecurityCollection[collections.length - 1]; 371 for (int i = 0; i < collections.length; i++) { 372 if (i != n) 373 results[j++] = collections[i]; 374 } 375 collections = results; 376 } 377 378 } 379 380 381 384 public String toString() { 385 386 StringBuffer sb = new StringBuffer ("SecurityConstraint["); 387 for (int i = 0; i < collections.length; i++) { 388 if (i > 0) 389 sb.append(", "); 390 sb.append(collections[i].getName()); 391 } 392 sb.append("]"); 393 return (sb.toString()); 394 395 } 396 397 398 400 401 410 private boolean matchPattern(String path, String pattern) { 411 412 if ((path == null) || (path.length() == 0)) 414 path = "/"; 415 if ((pattern == null) || (pattern.length() == 0)) 416 pattern = "/"; 417 418 if (path.equals(pattern)) 420 return (true); 421 422 if (pattern.startsWith("/") && pattern.endsWith("/*")) { 424 pattern = pattern.substring(0, pattern.length() - 2); 425 if (pattern.length() == 0) 426 return (true); if (path.endsWith("/")) 428 path = path.substring(0, path.length() - 1); 429 while (true) { 430 if (pattern.equals(path)) 431 return (true); 432 int slash = path.lastIndexOf('/'); 433 if (slash <= 0) 434 break; 435 path = path.substring(0, slash); 436 } 437 return (false); 438 } 439 440 if (pattern.startsWith("*.")) { 442 int slash = path.lastIndexOf('/'); 443 int period = path.lastIndexOf('.'); 444 if ((slash >= 0) && (period > slash) && 445 path.endsWith(pattern.substring(1))) { 446 return (true); 447 } 448 return (false); 449 } 450 451 if (pattern.equals("/")) 453 return (true); 454 455 return (false); 456 457 } 458 459 460 } 461 | Popular Tags |