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 package org.apache.http.auth;
28
29 import java.util.Locale;
30
31 import org.apache.http.HttpHost;
32 import org.apache.http.annotation.Contract;
33 import org.apache.http.annotation.ThreadingBehavior;
34 import org.apache.http.util.Args;
35 import org.apache.http.util.LangUtils;
36
37
38
39
40
41
42
43
44
45
46 @Contract(threading = ThreadingBehavior.IMMUTABLE)
47 public class AuthScope {
48
49
50
51
52
53 public static final String ANY_HOST = null;
54
55
56
57
58 public static final int ANY_PORT = -1;
59
60
61
62
63 public static final String ANY_REALM = null;
64
65
66
67
68 public static final String ANY_SCHEME = null;
69
70
71
72
73
74
75 public static final AuthScopepe.html#AuthScope">AuthScope ANY = new AuthScope(ANY_HOST, ANY_PORT, ANY_REALM, ANY_SCHEME);
76
77
78 private final String scheme;
79
80
81 private final String realm;
82
83
84 private final String host;
85
86
87 private final int port;
88
89
90 private final HttpHost origin;
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 public AuthScope(
106 final String host,
107 final int port,
108 final String realm,
109 final String schemeName) {
110 this.host = host == null ? ANY_HOST: host.toLowerCase(Locale.ROOT);
111 this.port = port < 0 ? ANY_PORT : port;
112 this.realm = realm == null ? ANY_REALM : realm;
113 this.scheme = schemeName == null ? ANY_SCHEME : schemeName.toUpperCase(Locale.ROOT);
114 this.origin = null;
115 }
116
117
118
119
120
121
122
123
124
125
126
127
128 public AuthScope(
129 final HttpHost origin,
130 final String realm,
131 final String schemeName) {
132 Args.notNull(origin, "Host");
133 this.host = origin.getHostName().toLowerCase(Locale.ROOT);
134 this.port = origin.getPort() < 0 ? ANY_PORT : origin.getPort();
135 this.realm = realm == null ? ANY_REALM : realm;
136 this.scheme = schemeName == null ? ANY_SCHEME : schemeName.toUpperCase(Locale.ROOT);
137 this.origin = origin;
138 }
139
140
141
142
143
144
145
146
147 public AuthScope(final HttpHost origin) {
148 this(origin, ANY_REALM, ANY_SCHEME);
149 }
150
151
152
153
154
155
156
157
158
159
160
161 public AuthScope(final String host, final int port, final String realm) {
162 this(host, port, realm, ANY_SCHEME);
163 }
164
165
166
167
168
169
170
171
172
173 public AuthScope(final String host, final int port) {
174 this(host, port, ANY_REALM, ANY_SCHEME);
175 }
176
177
178
179
180 public AuthScopehScope.html#AuthScope">AuthScope(final AuthScope authscope) {
181 super();
182 Args.notNull(authscope, "Scope");
183 this.host = authscope.getHost();
184 this.port = authscope.getPort();
185 this.realm = authscope.getRealm();
186 this.scheme = authscope.getScheme();
187 this.origin = authscope.getOrigin();
188 }
189
190
191
192
193
194
195 public HttpHost getOrigin() {
196 return this.origin;
197 }
198
199
200
201
202 public String getHost() {
203 return this.host;
204 }
205
206
207
208
209 public int getPort() {
210 return this.port;
211 }
212
213
214
215
216 public String getRealm() {
217 return this.realm;
218 }
219
220
221
222
223 public String getScheme() {
224 return this.scheme;
225 }
226
227
228
229
230
231
232
233
234 public int match(final AuthScope that) {
235 int factor = 0;
236 if (LangUtils.equals(this.scheme, that.scheme)) {
237 factor += 1;
238 } else {
239 if (this.scheme != ANY_SCHEME && that.scheme != ANY_SCHEME) {
240 return -1;
241 }
242 }
243 if (LangUtils.equals(this.realm, that.realm)) {
244 factor += 2;
245 } else {
246 if (this.realm != ANY_REALM && that.realm != ANY_REALM) {
247 return -1;
248 }
249 }
250 if (this.port == that.port) {
251 factor += 4;
252 } else {
253 if (this.port != ANY_PORT && that.port != ANY_PORT) {
254 return -1;
255 }
256 }
257 if (LangUtils.equals(this.host, that.host)) {
258 factor += 8;
259 } else {
260 if (this.host != ANY_HOST && that.host != ANY_HOST) {
261 return -1;
262 }
263 }
264 return factor;
265 }
266
267
268
269
270 @Override
271 public boolean equals(final Object o) {
272 if (o == null) {
273 return false;
274 }
275 if (o == this) {
276 return true;
277 }
278 if (!(o instanceof AuthScope)) {
279 return super.equals(o);
280 }
281 final AuthScope./../../../org/apache/http/auth/AuthScope.html#AuthScope">AuthScope that = (AuthScope) o;
282 return
283 LangUtils.equals(this.host, that.host)
284 && this.port == that.port
285 && LangUtils.equals(this.realm, that.realm)
286 && LangUtils.equals(this.scheme, that.scheme);
287 }
288
289
290
291
292 @Override
293 public String toString() {
294 final StringBuilder buffer = new StringBuilder();
295 if (this.scheme != null) {
296 buffer.append(this.scheme.toUpperCase(Locale.ROOT));
297 buffer.append(' ');
298 }
299 if (this.realm != null) {
300 buffer.append('\'');
301 buffer.append(this.realm);
302 buffer.append('\'');
303 } else {
304 buffer.append("<any realm>");
305 }
306 if (this.host != null) {
307 buffer.append('@');
308 buffer.append(this.host);
309 if (this.port >= 0) {
310 buffer.append(':');
311 buffer.append(this.port);
312 }
313 }
314 return buffer.toString();
315 }
316
317
318
319
320 @Override
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 }