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      @Override
59      public AuthScheme get( HttpHost host )
60      {
61          host = toKey( host );
62          AuthScheme authScheme = authSchemes.get( host );
63          if ( authScheme == null )
64          {
65              authScheme = state.getAuthScheme( host );
66              authSchemes.put( host, authScheme );
67          }
68          return authScheme;
69      }
70  
71      @Override
72      public void put( HttpHost host, AuthScheme authScheme )
73      {
74          if ( authScheme != null )
75          {
76              authSchemes.put( toKey( host ), authScheme );
77          }
78          else
79          {
80              remove( host );
81          }
82      }
83  
84      @Override
85      public void remove( HttpHost host )
86      {
87          authSchemes.remove( toKey( host ) );
88      }
89  
90      @Override
91      public void clear()
92      {
93          share();
94          authSchemes.clear();
95      }
96  
97      private void share()
98      {
99          for ( Map.Entry<HttpHost, AuthScheme> entry : authSchemes.entrySet() )
100         {
101             state.setAuthScheme( entry.getKey(), entry.getValue() );
102         }
103     }
104 
105     @Override
106     public String toString()
107     {
108         return authSchemes.toString();
109     }
110 
111 }