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.HttpClientConnectionManager;
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      private final GlobalState global;
41  
42      private final HttpClientConnectionManager connMgr;
43  
44      private final CompoundKey userTokenKey;
45  
46      private volatile Object userToken;
47  
48      private final CompoundKey expectContinueKey;
49  
50      private volatile Boolean expectContinue;
51  
52      private volatile Boolean webDav;
53  
54      private final ConcurrentMap<HttpHost, AuthSchemePool> authSchemePools;
55  
56      LocalState( RepositorySystemSession session, RemoteRepository repo, SslConfig sslConfig )
57      {
58          global = GlobalState.get( session );
59          userToken = this;
60          if ( global == null )
61          {
62              connMgr = GlobalState.newConnectionManager( sslConfig );
63              userTokenKey = null;
64              expectContinueKey = null;
65              authSchemePools = new ConcurrentHashMap<>();
66          }
67          else
68          {
69              connMgr = global.getConnectionManager( sslConfig );
70              userTokenKey = new CompoundKey( repo.getId(), repo.getUrl(), repo.getAuthentication(), repo.getProxy() );
71              expectContinueKey = new CompoundKey( repo.getUrl(), repo.getProxy() );
72              authSchemePools = global.getAuthSchemePools();
73          }
74      }
75  
76      public HttpClientConnectionManager getConnectionManager()
77      {
78          return connMgr;
79      }
80  
81      public Object getUserToken()
82      {
83          if ( userToken == this )
84          {
85              userToken = ( global != null ) ? global.getUserToken( userTokenKey ) : null;
86          }
87          return userToken;
88      }
89  
90      public void setUserToken( Object userToken )
91      {
92          this.userToken = userToken;
93          if ( global != null )
94          {
95              global.setUserToken( userTokenKey, userToken );
96          }
97      }
98  
99      public boolean isExpectContinue()
100     {
101         if ( expectContinue == null )
102         {
103             expectContinue =
104                 !Boolean.FALSE.equals( ( global != null ) ? global.getExpectContinue( expectContinueKey ) : null );
105         }
106         return expectContinue;
107     }
108 
109     public void setExpectContinue( boolean enabled )
110     {
111         expectContinue = enabled;
112         if ( global != null )
113         {
114             global.setExpectContinue( expectContinueKey, enabled );
115         }
116     }
117 
118     public Boolean getWebDav()
119     {
120         return webDav;
121     }
122 
123     public void setWebDav( boolean webDav )
124     {
125         this.webDav = webDav;
126     }
127 
128     public AuthScheme getAuthScheme( HttpHost host )
129     {
130         AuthSchemePool pool = authSchemePools.get( host );
131         if ( pool != null )
132         {
133             return pool.get();
134         }
135         return null;
136     }
137 
138     public void setAuthScheme( HttpHost host, AuthScheme authScheme )
139     {
140         AuthSchemePool pool = authSchemePools.get( host );
141         if ( pool == null )
142         {
143             AuthSchemePool p = new AuthSchemePool();
144             pool = authSchemePools.putIfAbsent( host, p );
145             if ( pool == null )
146             {
147                 pool = p;
148             }
149         }
150         pool.put( authScheme );
151     }
152 
153     @Override
154     public void close()
155     {
156         if ( global == null )
157         {
158             connMgr.shutdown();
159         }
160     }
161 
162 }