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.http;
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  import org.eclipse.aether.transport.http.GlobalState.CompoundKey;
31  
32  /**
33   * Container for HTTP-related state that can be shared across invocations of the transporter to optimize the
34   * communication with server.
35   */
36  final class LocalState implements Closeable {
37      private final GlobalState global;
38  
39      private final HttpClientConnectionManager connMgr;
40  
41      private final CompoundKey userTokenKey;
42  
43      private volatile Object userToken;
44  
45      private final CompoundKey expectContinueKey;
46  
47      private volatile Boolean expectContinue;
48  
49      private volatile Boolean webDav;
50  
51      private final ConcurrentMap<HttpHost, AuthSchemePool> authSchemePools;
52  
53      LocalState(RepositorySystemSession session, RemoteRepository repo, ConnMgrConfig connMgrConfig) {
54          global = GlobalState.get(session);
55          userToken = this;
56          if (global == null) {
57              connMgr = GlobalState.newConnectionManager(connMgrConfig);
58              userTokenKey = null;
59              expectContinueKey = null;
60              authSchemePools = new ConcurrentHashMap<>();
61          } else {
62              connMgr = global.getConnectionManager(connMgrConfig);
63              userTokenKey = new CompoundKey(repo.getId(), repo.getUrl(), repo.getAuthentication(), repo.getProxy());
64              expectContinueKey = new 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 }