View Javadoc
1   package org.eclipse.aether.transport.http;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Auth scheme cache that upon clearing releases all cached schemes into a pool for future reuse by other requests,
31   * thereby reducing challenge-response roundtrips.
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 }