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 javax.net.ssl.HostnameVerifier;
22  import javax.net.ssl.SSLContext;
23  
24  import java.util.Arrays;
25  import java.util.Objects;
26  
27  import org.eclipse.aether.RepositorySystemSession;
28  import org.eclipse.aether.repository.AuthenticationContext;
29  import org.eclipse.aether.util.ConfigUtils;
30  
31  /**
32   * Connection manager config: among other SSL-related configuration and cache key for connection pools (whose scheme
33   * registries are derived from this config).
34   */
35  final class ConnMgrConfig {
36  
37      private static final String CIPHER_SUITES = "https.cipherSuites";
38  
39      private static final String PROTOCOLS = "https.protocols";
40  
41      final SSLContext context;
42  
43      final HostnameVerifier verifier;
44  
45      final String[] cipherSuites;
46  
47      final String[] protocols;
48  
49      final String httpsSecurityMode;
50  
51      final int connectionMaxTtlSeconds;
52  
53      final int maxConnectionsPerRoute;
54  
55      ConnMgrConfig(
56              RepositorySystemSession session,
57              AuthenticationContext authContext,
58              String httpsSecurityMode,
59              int connectionMaxTtlSeconds,
60              int maxConnectionsPerRoute) {
61          context = (authContext != null) ? authContext.get(AuthenticationContext.SSL_CONTEXT, SSLContext.class) : null;
62          verifier = (authContext != null)
63                  ? authContext.get(AuthenticationContext.SSL_HOSTNAME_VERIFIER, HostnameVerifier.class)
64                  : null;
65  
66          cipherSuites = split(get(session, CIPHER_SUITES));
67          protocols = split(get(session, PROTOCOLS));
68          this.httpsSecurityMode = httpsSecurityMode;
69          this.connectionMaxTtlSeconds = connectionMaxTtlSeconds;
70          this.maxConnectionsPerRoute = maxConnectionsPerRoute;
71      }
72  
73      private static String get(RepositorySystemSession session, String key) {
74          String value = ConfigUtils.getString(session, null, "aether.connector." + key, key);
75          if (value == null) {
76              value = System.getProperty(key);
77          }
78          return value;
79      }
80  
81      private static String[] split(String value) {
82          if (value == null || value.isEmpty()) {
83              return null;
84          }
85          return value.split(",+");
86      }
87  
88      @Override
89      public boolean equals(Object obj) {
90          if (this == obj) {
91              return true;
92          }
93          if (obj == null || !getClass().equals(obj.getClass())) {
94              return false;
95          }
96          ConnMgrConfig that = (ConnMgrConfig) obj;
97          return Objects.equals(context, that.context)
98                  && Objects.equals(verifier, that.verifier)
99                  && Arrays.equals(cipherSuites, that.cipherSuites)
100                 && Arrays.equals(protocols, that.protocols)
101                 && Objects.equals(httpsSecurityMode, that.httpsSecurityMode)
102                 && connectionMaxTtlSeconds == that.connectionMaxTtlSeconds
103                 && maxConnectionsPerRoute == that.maxConnectionsPerRoute;
104     }
105 
106     @Override
107     public int hashCode() {
108         int hash = 17;
109         hash = hash * 31 + hash(context);
110         hash = hash * 31 + hash(verifier);
111         hash = hash * 31 + Arrays.hashCode(cipherSuites);
112         hash = hash * 31 + Arrays.hashCode(protocols);
113         hash = hash * 31 + hash(httpsSecurityMode);
114         hash = hash * 31 + hash(connectionMaxTtlSeconds);
115         hash = hash * 31 + hash(maxConnectionsPerRoute);
116         return hash;
117     }
118 
119     private static int hash(Object obj) {
120         return obj != null ? obj.hashCode() : 0;
121     }
122 }