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      public SharingAuthCache( LocalState state )
42      {
43          this.state = state;
44          authSchemes = new HashMap<HttpHost, AuthScheme>();
45      }
46  
47      private static HttpHost toKey( HttpHost host )
48      {
49          if ( host.getPort() <= 0 )
50          {
51              int port = host.getSchemeName().equalsIgnoreCase( "https" ) ? 443 : 80;
52              return new HttpHost( host.getHostName(), port, host.getSchemeName() );
53          }
54          return host;
55      }
56  
57      public AuthScheme get( HttpHost host )
58      {
59          host = toKey( host );
60          AuthScheme authScheme = authSchemes.get( host );
61          if ( authScheme == null )
62          {
63              authScheme = state.getAuthScheme( host );
64              authSchemes.put( host, authScheme );
65          }
66          return authScheme;
67      }
68  
69      public void put( HttpHost host, AuthScheme authScheme )
70      {
71          if ( authScheme != null )
72          {
73              authSchemes.put( toKey( host ), authScheme );
74          }
75          else
76          {
77              remove( host );
78          }
79      }
80  
81      public void remove( HttpHost host )
82      {
83          authSchemes.remove( toKey( host ) );
84      }
85  
86      public void clear()
87      {
88          share();
89          authSchemes.clear();
90      }
91  
92      private void share()
93      {
94          for ( Map.Entry<HttpHost, AuthScheme> entry : authSchemes.entrySet() )
95          {
96              state.setAuthScheme( entry.getKey(), entry.getValue() );
97          }
98      }
99  
100     @Override
101     public String toString()
102     {
103         return authSchemes.toString();
104     }
105 
106 }