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.Enumeration ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.Map ; 38 39 import javax.security.jacc.WebResourcePermission ; 40 import javax.security.jacc.WebUserDataPermission ; 41 42 43 47 public class PatternEntry { 48 49 52 private Pattern pattern = null; 53 54 55 58 private boolean uncheckedLastEntry = false; 59 60 63 private MethodsDesc methods = null; 64 65 68 private boolean irrelevant = false; 69 70 71 74 private StringBuffer qualified = null; 75 76 80 public PatternEntry(String pattern) { 81 this.pattern = new Pattern(pattern); 82 methods = new MethodsDesc(); 83 qualified = new StringBuffer (pattern); 84 } 85 86 87 93 public void addMethods(String [] methods, String transportGuarantee, boolean isExcluded) { 94 this.methods.addMethods(methods, transportGuarantee, isExcluded); 95 } 96 97 102 public void addExcludedMethods(String [] methods, String transportGuarantee) { 103 addMethods(methods, transportGuarantee, true); 104 } 105 106 111 public void addUncheckedMethods(String [] methods, String transportGuarantee) { 112 addMethods(methods, transportGuarantee, false); 113 } 114 115 116 122 public void addMethodsOnRoles(String [] methods, String [] roles, String transportGuarantee) { 123 for (int r = 0; r < roles.length; r++) { 124 addMethodsOnRole(methods, roles[r], transportGuarantee); 125 } 126 } 127 128 129 135 public void addMethodsOnRole(String [] methods, String role, String transportGuarantee) { 136 this.methods.addMethodsOnRole(methods, role, transportGuarantee); 137 } 138 139 143 public void setUncheckedLastEntry() { 144 uncheckedLastEntry = true; 145 } 146 147 152 public boolean isUncheckedLastEntry() { 153 return uncheckedLastEntry; 154 } 155 156 157 158 159 164 public void addQualifiedPattern(Pattern otherPattern) { 165 169 if (otherPattern.isMatching(pattern)) { 170 irrelevant = true; 171 } else { 172 qualified.append(":"); 173 qualified.append(otherPattern); 174 } 175 176 } 177 178 183 public Map getRolesPermissionsMap() { 184 Map roleMapActions = methods.getRoleMapActions(); 185 String roleName = null; 186 String actions = null; 187 Map rolesPermissionsMap = new HashMap (); 188 189 for (Iterator it = roleMapActions.keySet().iterator(); it.hasNext();) { 191 roleName = (String ) it.next(); 192 actions = (String ) roleMapActions.get(roleName); 193 if (actions != null) { 194 PermissionCollection pc = new Permissions (); 195 pc.add(new WebResourcePermission (getQualifiedPattern(), actions)); 196 rolesPermissionsMap.put(roleName, pc); 197 } 198 } 199 return rolesPermissionsMap; 200 } 201 202 203 207 public PermissionCollection getExcludedPermissions() { 208 PermissionCollection pc = new Permissions (); 210 String actions = methods.getExcludedActions(); 211 if (!actions.equals("")) { 212 pc.add(new WebResourcePermission (getQualifiedPattern(), actions)); 213 pc.add(new WebUserDataPermission (getQualifiedPattern(), actions)); 214 } 215 return pc; 216 } 217 218 219 223 public PermissionCollection getUncheckedPermissions() { 224 String actions = null; 227 List permissions = new ArrayList (); 228 229 actions = methods.getUncheckedActions(); 230 if (actions == null || (!actions.equals(""))) { 231 permissions.add(new WebResourcePermission (getQualifiedPattern(), actions)); 232 permissions.add(new WebUserDataPermission (getQualifiedPattern(), actions)); 233 } 234 235 List actionsList = methods.getUncheckedWebUserDataActionsRoleList(); 237 for (Iterator it = actionsList.iterator(); it.hasNext();) { 238 actions = (String ) it.next(); 239 permissions.add(new WebUserDataPermission (getQualifiedPattern(), actions)); 240 } 241 242 PermissionCollection pc = new Permissions (); 244 for (Iterator it = permissions.iterator(); it.hasNext();) { 245 Permission p = (Permission ) it.next(); 246 if (p instanceof WebUserDataPermission ) { 247 WebUserDataPermission wdp = (WebUserDataPermission ) p; 248 String wdpName = wdp.getName(); 250 String wdpActions = wdp.getActions(); 251 if (wdpActions == null) { 252 pc.add(p); 254 continue; 255 } 256 boolean wasMerged = false; 257 for (Iterator itLoop = permissions.iterator(); itLoop.hasNext();) { 259 Permission loopPerm = (Permission ) itLoop.next(); 260 if (loopPerm instanceof WebUserDataPermission ) { 261 WebUserDataPermission loopWdp = (WebUserDataPermission ) loopPerm; 262 if (loopWdp.equals(wdp)) { 264 continue; 265 } 266 String loopWdpName = loopWdp.getName(); 267 String loopWdpActions = loopWdp.getActions(); 268 if (loopWdpActions == null) { 269 continue; 270 } 271 boolean wNoTransport = (wdpActions.indexOf(":") == -1); 272 boolean loopNoWTransport = (loopWdpActions.indexOf(":") == -1); 273 274 if (wdpName.equals(loopWdpName) && wNoTransport && loopNoWTransport) { 276 String newActions = wdpActions + "," + loopWdpActions; 278 279 Enumeration existingPermissions = pc.elements(); 281 boolean exist = false; 282 Permission permissionToAdd = new WebUserDataPermission (wdpName, newActions); 283 while (existingPermissions.hasMoreElements()) { 284 Permission perm = (Permission ) existingPermissions.nextElement(); 285 if (perm.equals(permissionToAdd)) { 286 exist = true; 287 } 288 } 289 if (!exist) { 290 wasMerged = true; 291 pc.add(permissionToAdd); 292 } 293 } 294 } 295 } 296 if (!wasMerged) { 298 pc.add(p); 299 } 300 301 } else { 302 pc.add(p); 304 } 305 } 306 307 308 return pc; 309 } 310 311 312 316 public boolean isIrrelevant() { 317 return irrelevant; 318 } 319 320 324 public String getQualifiedPattern() { 325 return qualified.toString(); 326 } 327 328 332 public String toString() { 333 StringBuffer sb = new StringBuffer (); 334 sb.append("PatternEntry[pattern="); 335 sb.append(pattern); 336 sb.append(";qualified="); 337 sb.append(getQualifiedPattern()); 338 sb.append(";irrelevant="); 339 sb.append(irrelevant); 340 sb.append("]"); 341 return sb.toString(); 342 } 343 } 344 | Popular Tags |