1 22 package org.jboss.metadata; 23 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 import java.util.Map ; 27 import java.util.Set ; 28 import java.util.ArrayList ; 29 import java.util.Collections ; 30 import java.util.TreeSet ; 31 32 import org.jboss.metadata.web.AuthConstraint; 33 34 39 public class WebSecurityMetaData 40 { 41 42 public static final Set ALL_HTTP_METHODS; 43 public static final String [] ALL_HTTP_METHOD_NAMES; 44 45 static 46 { 47 TreeSet tmp = new TreeSet (); 48 tmp.add("GET"); 49 tmp.add("POST"); 50 tmp.add("PUT"); 51 tmp.add("DELETE"); 52 tmp.add("HEAD"); 53 tmp.add("OPTIONS"); 54 tmp.add("TRACE"); 55 ALL_HTTP_METHODS = Collections.unmodifiableSortedSet(tmp); 56 ALL_HTTP_METHOD_NAMES = new String [ALL_HTTP_METHODS.size()]; 57 ALL_HTTP_METHODS.toArray(ALL_HTTP_METHOD_NAMES); 58 } 59 60 63 private HashMap <String , WebResourceCollection> webResources = 64 new HashMap <String , WebResourceCollection>(); 65 68 private Set roles = new HashSet (); 69 70 71 private String transportGuarantee; 72 74 private boolean unchecked = false; 75 78 private boolean excluded = false; 79 80 private AuthConstraint authConstraint; 81 82 public static String [] getMissingHttpMethods(HashSet httpMethods) 83 { 84 String [] methods = {}; 85 if( httpMethods.size() > 0 && httpMethods.containsAll(ALL_HTTP_METHODS) == false ) 86 { 87 HashSet missingMethods = new HashSet (ALL_HTTP_METHODS); 88 missingMethods.removeAll(httpMethods); 89 methods = new String [missingMethods.size()]; 90 missingMethods.toArray(methods); 91 } 92 return methods; 93 } 94 95 public WebResourceCollection addWebResource(String name) 96 { 97 WebResourceCollection webrc = new WebResourceCollection(name); 98 if( webResources.containsKey(name) == true ) 99 { 100 name = name + '@' + System.identityHashCode(webrc); 102 } 103 webResources.put(name, webrc); 104 return webrc; 105 } 106 public Map <String , WebResourceCollection> getWebResources() 107 { 108 return webResources; 109 } 110 public void setWebResources(WebResourceCollection collection) 111 { 112 113 } 114 115 public void addRole(String name) 116 { 117 roles.add(name); 118 } 119 124 public Set getRoles() 125 { 126 return roles; 127 } 128 129 134 public String getTransportGuarantee() 135 { 136 return transportGuarantee; 137 } 138 public void setTransportGuarantee(String transportGuarantee) 139 { 140 this.transportGuarantee = transportGuarantee; 141 } 142 143 public boolean isUnchecked() 144 { 145 return unchecked; 146 } 147 public void setUnchecked(boolean flag) 148 { 149 this.unchecked = flag; 150 } 151 152 public boolean isExcluded() 153 { 154 return excluded; 155 } 156 public void setExcluded(boolean flag) 157 { 158 this.excluded = flag; 159 } 160 161 public AuthConstraint getAuthConstraint() 162 { 163 return authConstraint; 164 } 165 public void setAuthConstraint(AuthConstraint authConstraint) 166 { 167 this.authConstraint = authConstraint; 168 if( authConstraint.getRoleNames().size() == 0 ) 169 setExcluded(true); 170 } 171 172 175 public static class WebResourceCollection 176 { 177 178 private String name; 179 180 private HashSet urlPatterns = new HashSet (); 181 182 private ArrayList httpMethods = new ArrayList (); 183 184 public WebResourceCollection() 185 { 186 this(null); 187 } 188 public WebResourceCollection(String name) 189 { 190 this.name = name; 191 } 192 193 public String getName() 194 { 195 return name; 196 } 197 public void setName(String name) 198 { 199 this.name = name; 200 } 201 public void addPattern(String pattern) 202 { 203 urlPatterns.add(pattern); 204 } 205 208 public String [] getUrlPatterns() 209 { 210 String [] patterns = {}; 211 patterns = new String [urlPatterns.size()]; 212 urlPatterns.toArray(patterns); 213 return patterns; 214 } 215 216 public void addHttpMethod(String method) 217 { 218 httpMethods.add(method); 219 } 220 224 public String [] getHttpMethods() 225 { 226 String [] methods = {}; 227 if( httpMethods.containsAll(ALL_HTTP_METHODS) == false ) 228 { 229 methods = new String [httpMethods.size()]; 230 httpMethods.toArray(methods); 231 } 232 return methods; 233 } 234 241 public String [] getMissingHttpMethods() 242 { 243 String [] methods = {}; 244 if( httpMethods.size() > 0 && httpMethods.containsAll(ALL_HTTP_METHODS) == false ) 245 { 246 HashSet missingMethods = new HashSet (ALL_HTTP_METHODS); 247 missingMethods.removeAll(httpMethods); 248 methods = new String [missingMethods.size()]; 249 missingMethods.toArray(methods); 250 } 251 return methods; 252 } 253 } 254 } 255 | Popular Tags |