1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.transport.http;
20
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import org.apache.http.HttpHost;
25 import org.apache.http.auth.AuthScheme;
26 import org.apache.http.client.AuthCache;
27
28
29
30
31
32 final class SharingAuthCache implements AuthCache {
33
34 private final LocalState state;
35
36 private final Map<HttpHost, AuthScheme> authSchemes;
37
38 SharingAuthCache(LocalState state) {
39 this.state = state;
40 authSchemes = new HashMap<>();
41 }
42
43 private static HttpHost toKey(HttpHost host) {
44 if (host.getPort() <= 0) {
45 int port = host.getSchemeName().equalsIgnoreCase("https") ? 443 : 80;
46 return new HttpHost(host.getHostName(), port, host.getSchemeName());
47 }
48 return host;
49 }
50
51 @Override
52 public AuthScheme get(HttpHost host) {
53 host = toKey(host);
54 AuthScheme authScheme = authSchemes.get(host);
55 if (authScheme == null) {
56 authScheme = state.getAuthScheme(host);
57 authSchemes.put(host, authScheme);
58 }
59 return authScheme;
60 }
61
62 @Override
63 public void put(HttpHost host, AuthScheme authScheme) {
64 if (authScheme != null) {
65 authSchemes.put(toKey(host), authScheme);
66 } else {
67 remove(host);
68 }
69 }
70
71 @Override
72 public void remove(HttpHost host) {
73 authSchemes.remove(toKey(host));
74 }
75
76 @Override
77 public void clear() {
78 share();
79 authSchemes.clear();
80 }
81
82 private void share() {
83 for (Map.Entry<HttpHost, AuthScheme> entry : authSchemes.entrySet()) {
84 state.setAuthScheme(entry.getKey(), entry.getValue());
85 }
86 }
87
88 @Override
89 public String toString() {
90 return authSchemes.toString();
91 }
92 }