View Javadoc

1   package com.atlassian.sal.core.net;
2   
3   import org.apache.http.HttpHost;
4   import org.apache.http.auth.AuthScheme;
5   import org.apache.http.client.AuthCache;
6   
7   import java.util.Map;
8   import java.util.concurrent.ConcurrentHashMap;
9   
10  import static com.atlassian.sal.core.util.Assert.notNull;
11  
12  /**
13   * An AuthCache implementation allowing setting Auth for a host on all ports
14   *
15   * When getting a value for a host, we try first to find exact match and then
16   * a match for all ports
17   *
18   * @since v3.0
19   */
20  class AllPortsAuthCache implements AuthCache {
21      private final Map<HttpHost, AuthScheme> map = new ConcurrentHashMap<HttpHost, AuthScheme>();
22  
23      @Override
24      public AuthScheme get(final HttpHost host) {
25          notNull(host, "HTTP host");
26          // looking for exact match
27          AuthScheme authScheme = map.get(host);
28  
29          if (authScheme != null) {
30              return authScheme;
31          }
32  
33          // Exact match not found, looking for all ports match
34          return map.get(new HttpHost(host.getHostName()));
35      }
36  
37      @Override
38      public void put(final HttpHost host, final AuthScheme authScheme) {
39          notNull(host, "HTTP host");
40          this.map.put(host, authScheme);
41      }
42  
43      @Override
44      public void remove(final HttpHost host) {
45          notNull(host, "HTTP host");
46          this.map.remove(host);
47      }
48  
49      @Override
50      public void clear() {
51          this.map.clear();
52      }
53  
54      @Override
55      public String toString() {
56          return this.map.toString();
57      }
58  }