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