1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.transport.apache;
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 @SuppressWarnings("checkstyle:magicnumber")
44 private static HttpHost toKey(HttpHost host) {
45 if (host.getPort() <= 0) {
46 int port = host.getSchemeName().equalsIgnoreCase("https") ? 443 : 80;
47 return new HttpHost(host.getHostName(), port, host.getSchemeName());
48 }
49 return host;
50 }
51
52 @Override
53 public AuthScheme get(HttpHost host) {
54 host = toKey(host);
55 AuthScheme authScheme = authSchemes.get(host);
56 if (authScheme == null) {
57 authScheme = state.getAuthScheme(host);
58 authSchemes.put(host, authScheme);
59 }
60 return authScheme;
61 }
62
63 @Override
64 public void put(HttpHost host, AuthScheme authScheme) {
65 if (authScheme != null) {
66 authSchemes.put(toKey(host), authScheme);
67 } else {
68 remove(host);
69 }
70 }
71
72 @Override
73 public void remove(HttpHost host) {
74 authSchemes.remove(toKey(host));
75 }
76
77 @Override
78 public void clear() {
79 share();
80 authSchemes.clear();
81 }
82
83 private void share() {
84 for (Map.Entry<HttpHost, AuthScheme> entry : authSchemes.entrySet()) {
85 state.setAuthScheme(entry.getKey(), entry.getValue());
86 }
87 }
88
89 @Override
90 public String toString() {
91 return authSchemes.toString();
92 }
93 }