1 26 27 package org.objectweb.jonas_web.deployment.api; 28 29 import java.security.Permission ; 30 import java.security.PermissionCollection ; 31 import java.security.Permissions ; 32 import java.util.ArrayList ; 33 import java.util.Collections ; 34 import java.util.Enumeration ; 35 import java.util.HashMap ; 36 import java.util.Iterator ; 37 import java.util.List ; 38 import java.util.Map ; 39 40 import org.objectweb.util.monolog.api.BasicLevel; 41 import org.objectweb.util.monolog.api.Logger; 42 43 import org.objectweb.jonas_lib.deployment.xml.SecurityRole; 44 45 import org.objectweb.jonas_web.deployment.xml.AuthConstraint; 46 import org.objectweb.jonas_web.deployment.xml.SecurityConstraint; 47 import org.objectweb.jonas_web.deployment.xml.UserDataConstraint; 48 import org.objectweb.jonas_web.deployment.xml.WebApp; 49 import org.objectweb.jonas_web.deployment.xml.WebResourceCollection; 50 51 import org.objectweb.common.TraceCore; 52 53 54 59 public class SecurityConstraintListDesc { 60 61 64 private static final String DEFAULT_PATTERN = "/"; 65 66 69 private WebApp webApp = null; 70 71 74 private Map mapPatterns = null; 75 76 79 private PermissionCollection excludedPermissions = null; 80 81 84 private PermissionCollection uncheckedPermissions = null; 85 86 87 90 private Map permissionsByRole = null; 91 92 93 96 private static Logger logger = null; 97 98 99 103 public SecurityConstraintListDesc(WebApp webApp) { 104 this.webApp = webApp; 105 106 logger = TraceCore.jacc; 108 109 mapPatterns = new HashMap (); 111 112 excludedPermissions = new Permissions (); 114 uncheckedPermissions = new Permissions (); 115 permissionsByRole = new HashMap (); 116 try { 117 initConstraints(); 119 120 qualifyPatterns(); 122 123 buildPermissions(); 125 } catch (Exception e) { 126 e.printStackTrace(); 127 } 128 } 129 130 131 136 private void initConstraints() { 137 138 String [] securityRoles = new String [webApp.getSecurityRoleList().size()]; 141 int r = 0; 142 for (Iterator itSecurityRoles = webApp.getSecurityRoleList().iterator(); itSecurityRoles.hasNext(); r++) { 143 securityRoles[r] = ((SecurityRole) itSecurityRoles.next()).getRoleName(); 144 } 145 146 SecurityConstraint securityConstraint = null; 147 for (Iterator it = webApp.getSecurityConstraintList().iterator(); it.hasNext();) { 148 149 securityConstraint = (SecurityConstraint) it.next(); 151 152 List webResourceCollectionList = securityConstraint.getWebResourceCollectionList(); 154 155 AuthConstraint authConstraint = securityConstraint.getAuthConstraint(); 157 158 UserDataConstraint userDataConstraint = securityConstraint.getUserDataConstraint(); 160 161 List rolesList = null; 163 boolean hasAuthConstraint = false; 164 boolean isExcludingAuthConstraint = false; 165 if (authConstraint != null) { 166 rolesList = authConstraint.getRoleNameList(); 167 hasAuthConstraint = true; 169 isExcludingAuthConstraint = (rolesList.size() == 0); 170 } 171 172 173 String transportGuarantee = null; 175 if (userDataConstraint != null) { 176 transportGuarantee = userDataConstraint.getTransportGuarantee(); 177 } 178 179 180 WebResourceCollection webRC = null; 182 183 for (Iterator itWebRC = webResourceCollectionList.iterator(); itWebRC.hasNext();) { 185 webRC = (WebResourceCollection) itWebRC.next(); 186 187 List methodList = webRC.getHttpMethodList(); 189 190 193 String urlPatternString = null; 195 for (Iterator itPattern = webRC.getUrlPatternList().iterator(); itPattern.hasNext();) { 196 urlPatternString = (String ) itPattern.next(); 197 198 PatternEntry patternEntry = (PatternEntry) mapPatterns.get(urlPatternString); 200 if (patternEntry == null) { 201 patternEntry = new PatternEntry(urlPatternString); 202 mapPatterns.put(urlPatternString, patternEntry); 203 } 204 String [] methods = null; 206 if (methodList.isEmpty()) { 207 methods = MethodsDesc.METHODS; 209 } else { 210 methods = (String []) methodList.toArray(new String [methodList.size()]); 211 } 212 if (hasAuthConstraint) { 213 if (isExcludingAuthConstraint) { 215 patternEntry.addExcludedMethods(methods, transportGuarantee); 216 } else { 217 for (Iterator itRole = rolesList.iterator(); itRole.hasNext();) { 219 String roleName = (String ) itRole.next(); 220 221 if (roleName.equals("*")) { 223 patternEntry.addMethodsOnRoles(methods, securityRoles, transportGuarantee); 224 } else { 225 patternEntry.addMethodsOnRole(methods, roleName, transportGuarantee); 226 } 227 } 228 } 229 } else { 230 patternEntry.addUncheckedMethods(methods, transportGuarantee); 232 } 233 } 234 } 235 } 236 } 237 238 239 243 private synchronized void qualifyPatterns() { 244 245 PatternEntry defaultPatternEntry = (PatternEntry) mapPatterns.get(DEFAULT_PATTERN); 247 if (defaultPatternEntry == null) { 248 defaultPatternEntry = new PatternEntry(DEFAULT_PATTERN); 249 defaultPatternEntry.setUncheckedLastEntry(); 251 mapPatterns.put(DEFAULT_PATTERN, defaultPatternEntry); 252 } 253 254 PatternEntry patternEntry = null; 256 Pattern otherPattern = null; 257 String patternString = null; 258 259 List patterns = new ArrayList (); 261 for (Iterator it = mapPatterns.keySet().iterator(); it.hasNext();) { 262 patternString = (String ) it.next(); 263 patterns.add(new Pattern(patternString)); 264 } 265 266 Collections.sort(patterns); 268 269 Pattern pattern = null; 270 for (Iterator it = mapPatterns.keySet().iterator(); it.hasNext();) { 271 patternString = (String ) it.next(); 272 pattern = new Pattern(patternString); 273 patternEntry = (PatternEntry) mapPatterns.get(patternString); 274 275 for (Iterator itOther = patterns.iterator(); itOther.hasNext();) { 277 otherPattern = (Pattern) itOther.next(); 278 279 if (pattern.isPathPrefix() && pattern.isMatching(otherPattern)) { 280 289 if (otherPattern.isPathPrefix() && !pattern.equals(otherPattern)) { 290 patternEntry.addQualifiedPattern(otherPattern); 291 } else if (otherPattern.isExactPattern()) { 292 patternEntry.addQualifiedPattern(otherPattern); 293 } 294 } else if (pattern.isExtensionPattern()) { 295 303 if (otherPattern.isPathPrefix() || (pattern.isMatching(otherPattern) && otherPattern.isExactPattern())) { 304 patternEntry.addQualifiedPattern(otherPattern); 305 } 306 } else if (pattern.isDefaultPattern()) { 307 313 if (!otherPattern.isDefaultPattern()) { 314 patternEntry.addQualifiedPattern(otherPattern); 315 } 316 324 } 325 } 326 } 327 } 328 329 330 334 private void buildPermissions() { 335 336 PatternEntry patternEntry = null; 337 for (Iterator it = mapPatterns.values().iterator(); it.hasNext();) { 339 patternEntry = (PatternEntry) it.next(); 340 if (!patternEntry.isIrrelevant()) { 342 if (patternEntry.isUncheckedLastEntry()) { 343 addUncheckedPermissions(patternEntry.getUncheckedPermissions()); 344 } else { 345 addExcludedPermissions(patternEntry.getExcludedPermissions()); 346 addUncheckedPermissions(patternEntry.getUncheckedPermissions()); 347 addRolePermissions(patternEntry.getRolesPermissionsMap()); 348 } 349 } 350 } 351 if (logger.isLoggable(BasicLevel.DEBUG)) { 352 logger.log(BasicLevel.DEBUG, "Excluded permissions = " + excludedPermissions); 353 logger.log(BasicLevel.DEBUG, "Unchecked permissions = " + uncheckedPermissions); 354 logger.log(BasicLevel.DEBUG, "Roles Permissions = "); 355 356 String roleName = null; 357 for (Iterator it = permissionsByRole.keySet().iterator(); it.hasNext();) { 358 roleName = (String ) it.next(); 359 logger.log(BasicLevel.DEBUG, "Permissions for role " + roleName + " are " 360 + permissionsByRole.get(roleName)); 361 } 362 } 363 364 } 365 366 367 372 private void addExcludedPermissions(PermissionCollection permissions) { 373 if (permissions == null) { 374 return; 375 } 376 377 for (Enumeration e = permissions.elements(); e.hasMoreElements();) { 378 excludedPermissions.add((Permission ) e.nextElement()); 379 } 380 } 381 382 387 private void addUncheckedPermissions(PermissionCollection permissions) { 388 if (permissions == null) { 389 return; 390 } 391 392 for (Enumeration e = permissions.elements(); e.hasMoreElements();) { 393 uncheckedPermissions.add((Permission ) e.nextElement()); 394 } 395 } 396 397 401 private void addRolePermissions(Map rolePermissionsMap) { 402 if (rolePermissionsMap == null) { 403 return; 404 } 405 406 String roleName = null; 408 PermissionCollection permissions = null; 409 PermissionCollection existingRolePermissions = null; 410 for (Iterator it = rolePermissionsMap.keySet().iterator(); it.hasNext();) { 411 roleName = (String ) it.next(); 412 permissions = (PermissionCollection ) rolePermissionsMap.get(roleName); 413 if (permissions != null) { 414 existingRolePermissions = (PermissionCollection ) permissionsByRole.get(roleName); 415 if (existingRolePermissions == null) { 416 existingRolePermissions = new Permissions (); 417 permissionsByRole.put(roleName, existingRolePermissions); 418 } 419 for (Enumeration e = permissions.elements(); e.hasMoreElements();) { 420 existingRolePermissions.add((Permission ) e.nextElement()); 421 } 422 } 423 } 424 } 425 426 427 431 public PermissionCollection getExcludedPermissions() { 432 return excludedPermissions; 433 } 434 435 436 440 public PermissionCollection getUncheckedPermissions() { 441 return uncheckedPermissions; 442 } 443 444 445 449 public Map getPermissionsByRole() { 450 return permissionsByRole; 451 } 452 453 } 454 455 456 457 458 459 | Popular Tags |