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  
23  import org.apache.http.conn.HttpClientConnectionManager;
24  import org.eclipse.aether.RepositorySystemSession;
25  import org.eclipse.aether.repository.RemoteRepository;
26  
27  /**
28   * Container for HTTP-related state that can be shared across invocations of the transporter to optimize the
29   * communication with server.
30   */
31  final class LocalState implements Closeable {
32      private final GlobalState global;
33  
34      private final HttpClientConnectionManager connMgr;
35  
36      private final GlobalState.CompoundKey userTokenKey;
37  
38      private volatile Object userToken;
39  
40      private final GlobalState.CompoundKey expectContinueKey;
41  
42      private volatile Boolean expectContinue;
43  
44      private volatile Boolean webDav;
45  
46      LocalState(RepositorySystemSession session, RemoteRepository repo, ConnMgrConfig connMgrConfig) {
47          global = GlobalState.get(session);
48          userToken = this;
49          if (global == null) {
50              connMgr = GlobalState.newConnectionManager(connMgrConfig);
51              userTokenKey = null;
52              expectContinueKey = null;
53          } else {
54              connMgr = global.getConnectionManager(connMgrConfig);
55              userTokenKey =
56                      new GlobalState.CompoundKey(repo.getId(), repo.getUrl(), repo.getAuthentication(), repo.getProxy());
57              expectContinueKey = new GlobalState.CompoundKey(repo.getUrl(), repo.getProxy());
58          }
59      }
60  
61      public HttpClientConnectionManager getConnectionManager() {
62          return connMgr;
63      }
64  
65      public Object getUserToken() {
66          if (userToken == this) {
67              userToken = (global != null) ? global.getUserToken(userTokenKey) : null;
68          }
69          return userToken;
70      }
71  
72      public void setUserToken(Object userToken) {
73          this.userToken = userToken;
74          if (global != null) {
75              global.setUserToken(userTokenKey, userToken);
76          }
77      }
78  
79      public boolean isExpectContinue() {
80          if (expectContinue == null) {
81              expectContinue =
82                      !Boolean.FALSE.equals((global != null) ? global.getExpectContinue(expectContinueKey) : null);
83          }
84          return expectContinue;
85      }
86  
87      public void setExpectContinue(boolean enabled) {
88          expectContinue = enabled;
89          if (global != null) {
90              global.setExpectContinue(expectContinueKey, enabled);
91          }
92      }
93  
94      public Boolean getWebDav() {
95          return webDav;
96      }
97  
98      public void setWebDav(boolean webDav) {
99          this.webDav = webDav;
100     }
101 
102     @Override
103     public void close() {
104         if (global == null) {
105             connMgr.shutdown();
106         }
107     }
108 }