View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.transport.apache;
20  
21  import java.io.Closeable;
22  import java.util.concurrent.ConcurrentHashMap;
23  import java.util.concurrent.ConcurrentMap;
24  
25  import org.apache.http.HttpHost;
26  import org.apache.http.auth.AuthScheme;
27  import org.apache.http.conn.HttpClientConnectionManager;
28  import org.eclipse.aether.RepositorySystemSession;
29  import org.eclipse.aether.repository.RemoteRepository;
30  
31  /**
32   * Container for HTTP-related state that can be shared across invocations of the transporter to optimize the
33   * communication with server.
34   */
35  final class LocalState implements Closeable {
36      private final GlobalState global;
37  
38      private final HttpClientConnectionManager connMgr;
39  
40      private final GlobalState.CompoundKey userTokenKey;
41  
42      private volatile Object userToken;
43  
44      private final GlobalState.CompoundKey expectContinueKey;
45  
46      private volatile Boolean expectContinue;
47  
48      private volatile Boolean webDav;
49  
50      private final ConcurrentMap<HttpHost, AuthSchemePool> authSchemePools;
51  
52      LocalState(RepositorySystemSession session, RemoteRepository repo, ConnMgrConfig connMgrConfig) {
53          global = GlobalState.get(session);
54          userToken = this;
55          if (global == null) {
56              connMgr = GlobalState.newConnectionManager(connMgrConfig);
57              userTokenKey = null;
58              expectContinueKey = null;
59              authSchemePools = new ConcurrentHashMap<>();
60          } else {
61              connMgr = global.getConnectionManager(connMgrConfig);
62              userTokenKey =
63                      new GlobalState.CompoundKey(repo.getId(), repo.getUrl(), repo.getAuthentication(), repo.getProxy());
64              expectContinueKey = new GlobalState.CompoundKey(repo.getUrl(), repo.getProxy());
65              authSchemePools = global.getAuthSchemePools();
66          }
67      }
68  
69      public HttpClientConnectionManager getConnectionManager() {
70          return connMgr;
71      }
72  
73      public Object getUserToken() {
74          if (userToken == this) {
75              userToken = (global != null) ? global.getUserToken(userTokenKey) : null;
76          }
77          return userToken;
78      }
79  
80      public void setUserToken(Object userToken) {
81          this.userToken = userToken;
82          if (global != null) {
83              global.setUserToken(userTokenKey, userToken);
84          }
85      }
86  
87      public boolean isExpectContinue() {
88          if (expectContinue == null) {
89              expectContinue =
90                      !Boolean.FALSE.equals((global != null) ? global.getExpectContinue(expectContinueKey) : null);
91          }
92          return expectContinue;
93      }
94  
95      public void setExpectContinue(boolean enabled) {
96          expectContinue = enabled;
97          if (global != null) {
98              global.setExpectContinue(expectContinueKey, enabled);
99          }
100     }
101 
102     public Boolean getWebDav() {
103         return webDav;
104     }
105 
106     public void setWebDav(boolean webDav) {
107         this.webDav = webDav;
108     }
109 
110     public AuthScheme getAuthScheme(HttpHost host) {
111         AuthSchemePool pool = authSchemePools.get(host);
112         if (pool != null) {
113             return pool.get();
114         }
115         return null;
116     }
117 
118     public void setAuthScheme(HttpHost host, AuthScheme authScheme) {
119         AuthSchemePool pool = authSchemePools.get(host);
120         if (pool == null) {
121             AuthSchemePool p = new AuthSchemePool();
122             pool = authSchemePools.putIfAbsent(host, p);
123             if (pool == null) {
124                 pool = p;
125             }
126         }
127         pool.put(authScheme);
128     }
129 
130     @Override
131     public void close() {
132         if (global == null) {
133             connMgr.shutdown();
134         }
135     }
136 }