1 28 package net.sf.jguard.core.authorization.permissions; 29 30 import java.io.Serializable ; 31 import java.net.URI ; 32 import java.util.Arrays ; 33 import java.util.HashSet ; 34 import java.util.Iterator ; 35 import java.util.List ; 36 import java.util.Set ; 37 import java.util.regex.Pattern ; 38 39 import org.apache.commons.lang.StringEscapeUtils; 40 41 46 public class URLParameter implements Serializable ,Cloneable { 47 48 51 private static final long serialVersionUID = 3835156176932384821L; 52 private String key = null; 53 54 59 private String [] value = null; 60 private String permissionName = null; 61 62 65 protected URLParameter(){ 66 67 } 68 69 73 public Object clone()throws CloneNotSupportedException { 74 URLParameter param = new URLParameter(); 75 param.setKey(new String (this.key)); 76 param.setValue(this.value); 77 param.setPermissionName(new String (this.permissionName)); 78 return param; 79 } 80 81 84 protected String getKey() { 85 return key; 86 } 87 88 91 protected String [] getValue() { 92 return value; 93 } 94 95 99 protected String getValuesAsString(){ 100 String [] valuesArray = this.value; 101 StringBuffer concatValues=new StringBuffer (); 102 for(int i=0;i<valuesArray.length;i++){ 103 if(i!=0){ 104 concatValues.append(";"); 105 } 106 concatValues.append(valuesArray[i]); 107 } 108 return concatValues.toString(); 109 } 110 113 protected void setKey(String string) { 114 key = string; 115 } 116 117 120 protected void setValue(String [] string) { 121 value = string; 122 } 123 124 128 public int hashCode(){ 129 StringBuffer sb = new StringBuffer (this.key); 130 sb.append(this.getValuesAsString()); 131 return sb.toString().hashCode(); 132 } 133 134 135 138 public boolean equals(Object obj){ 139 if(!(obj instanceof URLParameter)){ 140 return false; 141 } 142 URLParameter urlp = (URLParameter)obj; 143 144 145 if(urlp.getKey().equals(this.key)==false){ 146 return false; 147 } 148 149 String [] targetArray = urlp.getValue(); 150 String [] myArray = this.value; 151 152 if(myArray.length !=targetArray.length){ 153 return false; 154 } 155 Arrays.sort(targetArray); 157 Arrays.sort(myArray); 158 for(int i=0;i< myArray.length;i++){ 159 if(!(myArray[i]).equals(targetArray[i])){ 160 return false; 161 } 162 } 163 164 return true; 165 } 166 167 168 169 170 171 174 protected String getPermissionName() { 175 return permissionName; 176 } 177 178 181 protected void setPermissionName(String string) { 182 permissionName = string; 183 } 184 185 190 protected static Set getURLParameters(URI uri){ 191 192 Set parameters = new HashSet (); 193 String query = uri.getQuery(); 194 query = StringEscapeUtils.unescapeHtml(query); 195 if(query!=null && query!=""){ 196 List tokens = Arrays.asList(query.split("&")); 197 Iterator itTokens = tokens.iterator(); 198 while(itTokens.hasNext()){ 199 String token = (String )itTokens.next(); 200 String [] parts = token.split("="); 201 if(parts.length==2){ 202 URLParameter param = new URLParameter(); 203 param.setKey(parts[0]); 204 String [] values = parts[1].split(";"); 205 param.setValue(values); 206 parameters.add(param); 207 } 208 } 209 210 } 211 return parameters; 212 } 213 214 public boolean impliesKey(String key) { 215 String regexpKey = this.key.replaceAll("\\*","\\.\\*"); 216 if(!key.equals(this.key) && !Pattern.matches(regexpKey, key)){ 217 return false; 218 } 219 return true; 220 } 221 222 public boolean impliesValues(String [] value) { 223 if(value.length != this.value.length){ 224 return false; 225 } 226 Arrays.sort(value); 228 Arrays.sort(this.value); 229 for(int i=0;i< value.length;i++){ 230 String regexpValue = this.value[i].replaceAll("\\*","\\.\\*"); 231 if(!(value[i]).equals(this.value[i]) && !Pattern.matches(regexpValue, value[i])){ 232 return false; 233 } 234 } 235 return true; 236 } 237 } 238 | Popular Tags |