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