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.io.Closeable;
23  import java.util.concurrent.ConcurrentHashMap;
24  import java.util.concurrent.ConcurrentMap;
25  
26  import org.apache.http.HttpHost;
27  import org.apache.http.auth.AuthScheme;
28  import org.apache.http.conn.ClientConnectionManager;
29  import org.eclipse.aether.RepositorySystemSession;
30  import org.eclipse.aether.repository.RemoteRepository;
31  import org.eclipse.aether.transport.http.GlobalState.CompoundKey;
32  
33  /**
34   * Container for HTTP-related state that can be shared across invocations of the transporter to optimize the
35   * communication with server.
36   */
37  final class LocalState
38      implements Closeable
39  {
40  
41      private final GlobalState global;
42  
43      private final ClientConnectionManager connMgr;
44  
45      private final CompoundKey userTokenKey;
46  
47      private volatile Object userToken;
48  
49      private final CompoundKey expectContinueKey;
50  
51      private volatile Boolean expectContinue;
52  
53      private volatile Boolean webDav;
54  
55      private final ConcurrentMap<HttpHost, AuthSchemePool> authSchemePools;
56  
57      LocalState( RepositorySystemSession session, RemoteRepository repo, SslConfig sslConfig )
58      {
59          global = GlobalState.get( session );
60          userToken = this;
61          if ( global == null )
62          {
63              connMgr = GlobalState.newConnectionManager( sslConfig );
64              userTokenKey = null;
65              expectContinueKey = null;
66              authSchemePools = new ConcurrentHashMap<>();
67          }
68          else
69          {
70              connMgr = global.getConnectionManager( sslConfig );
71              userTokenKey = new CompoundKey( repo.getId(), repo.getUrl(), repo.getAuthentication(), repo.getProxy() );
72              expectContinueKey = new CompoundKey( repo.getUrl(), repo.getProxy() );
73              authSchemePools = global.getAuthSchemePools();
74          }
75      }
76  
77      public ClientConnectionManager getConnectionManager()
78      {
79          return connMgr;
80      }
81  
82      public Object getUserToken()
83      {
84          if ( userToken == this )
85          {
86              userToken = ( global != null ) ? global.getUserToken( userTokenKey ) : null;
87          }
88          return userToken;
89      }
90  
91      public void setUserToken( Object userToken )
92      {
93          this.userToken = userToken;
94          if ( global != null )
95          {
96              global.setUserToken( userTokenKey, userToken );
97          }
98      }
99  
100     public boolean isExpectContinue()
101     {
102         if ( expectContinue == null )
103         {
104             expectContinue =
105                 !Boolean.FALSE.equals( ( global != null ) ? global.getExpectContinue( expectContinueKey ) : null );
106         }
107         return expectContinue;
108     }
109 
110     public void setExpectContinue( boolean enabled )
111     {
112         expectContinue = enabled;
113         if ( global != null )
114         {
115             global.setExpectContinue( expectContinueKey, enabled );
116         }
117     }
118 
119     public Boolean getWebDav()
120     {
121         return webDav;
122     }
123 
124     public void setWebDav( boolean webDav )
125     {
126         this.webDav = webDav;
127     }
128 
129     public AuthScheme getAuthScheme( HttpHost host )
130     {
131         AuthSchemePool pool = authSchemePools.get( host );
132         if ( pool != null )
133         {
134             return pool.get();
135         }
136         return null;
137     }
138 
139     public void setAuthScheme( HttpHost host, AuthScheme authScheme )
140     {
141         AuthSchemePool pool = authSchemePools.get( host );
142         if ( pool == null )
143         {
144             AuthSchemePool p = new AuthSchemePool();
145             pool = authSchemePools.putIfAbsent( host, p );
146             if ( pool == null )
147             {
148                 pool = p;
149             }
150         }
151         pool.put( authScheme );
152     }
153 
154     public void close()
155     {
156         if ( global == null )
157         {
158             connMgr.shutdown();
159         }
160     }
161 
162 }