1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package org.apache.commons.httpclient.auth;
33
34 /*** The key used to look up authentication credentials.
35 *
36 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
37 * @author <a href="mailto:adrian@intencha.com">Adrian Sutton</a>
38 */
39 public class HttpAuthRealm {
40
41 /*** The realm the credentials apply to. */
42 private String realm = null;
43
44 /*** The domain the credentials apply to. */
45 private String domain = null;
46
47 /*** Creates a new HttpAuthRealm for the given <tt>domain</tt> and
48 * <tt>realm</tt>.
49 *
50 * @param domain the domain the credentials apply to. May be set
51 * to <tt>null</tt> if credenticals are applicable to
52 * any domain.
53 * @param realm the realm the credentials apply to. May be set
54 * to <tt>null</tt> if credenticals are applicable to
55 * any realm.
56 *
57 */
58 public HttpAuthRealm(final String domain, final String realm) {
59 this.domain = domain;
60 this.realm = realm;
61 }
62
63 /*** Determines if the given domains match. Note that <tt>null</tt> acts as a
64 * wildcard so if either of the domains are <tt>null</tt>, it is considered a match.
65 *
66 * @param d1 the domain
67 * @param d2 the other domain
68 * @return boolean true if the domains match, otherwise false.
69 */
70 private static boolean domainAttribMatch(final String d1, final String d2) {
71 return d1 == null || d2 == null || d1.equalsIgnoreCase(d2);
72 }
73
74 /*** Determines if the given realms match. Note that <tt>null</tt> acts as a
75 * wildcard so if either realm is <tt>null</tt>, this function will return <tt>true</tt>.
76 *
77 * @param r1 the realm
78 * @param r2 the other realm
79 * @return boolean true if the realms match, otherwise false.
80 */
81 private static boolean realmAttribMatch(final String r1, final String r2) {
82 return r1 == null || r2 == null || r1.equals(r2);
83 }
84
85
86 /***
87 * @see java.lang.Object#equals(Object)
88 */
89 public boolean equals(Object o) {
90 if (o == null) {
91 return false;
92 }
93 if (o == this) {
94 return true;
95 }
96 if (!(o instanceof HttpAuthRealm)) {
97 return super.equals(o);
98 }
99 HttpAuthRealm that = (HttpAuthRealm) o;
100 return
101 domainAttribMatch(this.domain, that.domain)
102 && realmAttribMatch(this.realm, that.realm);
103 }
104
105 /***
106 * @see java.lang.Object#toString()
107 */
108 public String toString() {
109 StringBuffer buffer = new StringBuffer();
110 buffer.append("Authentication domain: '");
111 buffer.append(this.domain);
112 buffer.append("', authentication realm: '");
113 buffer.append(this.realm);
114 buffer.append("'");
115 return buffer.toString();
116 }
117 /***
118 * @see java.lang.Object#hashCode()
119 */
120 public int hashCode() {
121 StringBuffer buffer = new StringBuffer();
122 buffer.append(this.domain);
123 buffer.append(this.realm);
124 return buffer.toString().hashCode();
125 }
126
127 }