1 30 31 package org.apache.commons.httpclient.auth; 32 33 import org.apache.commons.httpclient.util.LangUtils; 34 35 45 public class AuthScope { 46 47 51 public static final String ANY_HOST = null; 52 53 56 public static final int ANY_PORT = -1; 57 58 61 public static final String ANY_REALM = null; 62 63 66 public static final String ANY_SCHEME = null; 67 68 73 public static final AuthScope ANY = new AuthScope(ANY_HOST, ANY_PORT, ANY_REALM, ANY_SCHEME); 74 75 76 private String scheme = null; 77 78 79 private String realm = null; 80 81 82 private String host = null; 83 84 85 private int port = -1; 86 87 106 public AuthScope(final String host, int port, 107 final String realm, final String scheme) 108 { 109 this.host = (host == null) ? ANY_HOST: host.toLowerCase(); 110 this.port = (port < 0) ? ANY_PORT: port; 111 this.realm = (realm == null) ? ANY_REALM: realm; 112 this.scheme = (scheme == null) ? ANY_SCHEME: scheme.toUpperCase();; 113 } 114 115 131 public AuthScope(final String host, int port, final String realm) { 132 this(host, port, realm, ANY_SCHEME); 133 } 134 135 148 public AuthScope(final String host, int port) { 149 this(host, port, ANY_REALM, ANY_SCHEME); 150 } 151 152 157 public AuthScope(final AuthScope authscope) { 158 super(); 159 if (authscope == null) { 160 throw new IllegalArgumentException ("Scope may not be null"); 161 } 162 this.host = authscope.getHost(); 163 this.port = authscope.getPort(); 164 this.realm = authscope.getRealm(); 165 this.scheme = authscope.getScheme(); 166 } 167 168 173 public String getHost() { 174 return this.host; 175 } 176 177 182 public int getPort() { 183 return this.port; 184 } 185 186 191 public String getRealm() { 192 return this.realm; 193 } 194 195 200 public String getScheme() { 201 return this.scheme; 202 } 203 204 210 private static boolean paramsEqual(final String p1, final String p2) { 211 if (p1 == null) { 212 return p1 == p2; 213 } else { 214 return p1.equals(p2); 215 } 216 } 217 218 224 private static boolean paramsEqual(int p1, int p2) { 225 return p1 == p2; 226 } 227 228 237 public int match(final AuthScope that) { 238 int factor = 0; 239 if (paramsEqual(this.scheme, that.scheme)) { 240 factor += 1; 241 } else { 242 if (this.scheme != ANY_SCHEME && that.scheme != ANY_SCHEME) { 243 return -1; 244 } 245 } 246 if (paramsEqual(this.realm, that.realm)) { 247 factor += 2; 248 } else { 249 if (this.realm != ANY_REALM && that.realm != ANY_REALM) { 250 return -1; 251 } 252 } 253 if (paramsEqual(this.port, that.port)) { 254 factor += 4; 255 } else { 256 if (this.port != ANY_PORT && that.port != ANY_PORT) { 257 return -1; 258 } 259 } 260 if (paramsEqual(this.host, that.host)) { 261 factor += 8; 262 } else { 263 if (this.host != ANY_HOST && that.host != ANY_HOST) { 264 return -1; 265 } 266 } 267 return factor; 268 } 269 270 273 public boolean equals(Object o) { 274 if (o == null) { 275 return false; 276 } 277 if (o == this) { 278 return true; 279 } 280 if (!(o instanceof AuthScope)) { 281 return super.equals(o); 282 } 283 AuthScope that = (AuthScope) o; 284 return 285 paramsEqual(this.host, that.host) 286 && paramsEqual(this.port, that.port) 287 && paramsEqual(this.realm, that.realm) 288 && paramsEqual(this.scheme, that.scheme); 289 } 290 291 294 public String toString() { 295 StringBuffer buffer = new StringBuffer (); 296 if (this.scheme != null) { 297 buffer.append(this.scheme.toUpperCase()); 298 buffer.append(' '); 299 } 300 if (this.realm != null) { 301 buffer.append('\''); 302 buffer.append(this.realm); 303 buffer.append('\''); 304 } else { 305 buffer.append("<any realm>"); 306 } 307 if (this.host != null) { 308 buffer.append('@'); 309 buffer.append(this.host); 310 if (this.port >= 0) { 311 buffer.append(':'); 312 buffer.append(this.port); 313 } 314 } 315 return buffer.toString(); 316 } 317 318 321 public int hashCode() { 322 int hash = LangUtils.HASH_SEED; 323 hash = LangUtils.hashCode(hash, this.host); 324 hash = LangUtils.hashCode(hash, this.port); 325 hash = LangUtils.hashCode(hash, this.realm); 326 hash = LangUtils.hashCode(hash, this.scheme); 327 return hash; 328 } 329 } 330 | Popular Tags |