View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.transport.apache;
20  
21  import java.util.Map;
22  import java.util.concurrent.ConcurrentHashMap;
23  
24  import org.apache.http.HttpHost;
25  import org.apache.http.auth.AuthScheme;
26  import org.apache.http.client.AuthCache;
27  
28  /**
29   * Auth scheme cache that upon clearing releases all cached schemes into a pool for future reuse by other requests,
30   * thereby reducing challenge-response roundtrips.
31   */
32  final class SharingAuthCache implements AuthCache {
33  
34      private final LocalState state;
35  
36      private final ConcurrentHashMap<HttpHost, AuthScheme> authSchemes;
37  
38      SharingAuthCache(LocalState state) {
39          this.state = state;
40          authSchemes = new ConcurrentHashMap<>();
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              if (authScheme != null) {
58                  authSchemes.put(host, authScheme);
59              }
60          }
61          return authScheme;
62      }
63  
64      @Override
65      public void put(HttpHost host, AuthScheme authScheme) {
66          host = toKey(host);
67          if (authScheme != null) {
68              authSchemes.put(host, authScheme);
69          } else {
70              authSchemes.remove(host);
71          }
72      }
73  
74      @Override
75      public void remove(HttpHost host) {
76          authSchemes.remove(toKey(host));
77      }
78  
79      @Override
80      public void clear() {
81          share();
82          authSchemes.clear();
83      }
84  
85      private void share() {
86          for (Map.Entry<HttpHost, AuthScheme> entry : authSchemes.entrySet()) {
87              state.setAuthScheme(entry.getKey(), entry.getValue());
88          }
89      }
90  
91      @Override
92      public String toString() {
93          return authSchemes.toString();
94      }
95  }