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   * SSL-related configuration and cache key for connection pools (whose scheme registries are derived from this config).
33   */
34  final class SslConfig {
35  
36      private static final String CIPHER_SUITES = "https.cipherSuites";
37  
38      private static final String PROTOCOLS = "https.protocols";
39  
40      final SSLContext context;
41  
42      final HostnameVerifier verifier;
43  
44      final String[] cipherSuites;
45  
46      final String[] protocols;
47  
48      final String httpsSecurityMode;
49  
50      SslConfig(RepositorySystemSession session, AuthenticationContext authContext, String httpsSecurityMode) {
51          context = (authContext != null) ? authContext.get(AuthenticationContext.SSL_CONTEXT, SSLContext.class) : null;
52          verifier = (authContext != null)
53                  ? authContext.get(AuthenticationContext.SSL_HOSTNAME_VERIFIER, HostnameVerifier.class)
54                  : null;
55  
56          cipherSuites = split(get(session, CIPHER_SUITES));
57          protocols = split(get(session, PROTOCOLS));
58          this.httpsSecurityMode = httpsSecurityMode;
59      }
60  
61      private static String get(RepositorySystemSession session, String key) {
62          String value = ConfigUtils.getString(session, null, "aether.connector." + key, key);
63          if (value == null) {
64              value = System.getProperty(key);
65          }
66          return value;
67      }
68  
69      private static String[] split(String value) {
70          if (value == null || value.isEmpty()) {
71              return null;
72          }
73          return value.split(",+");
74      }
75  
76      @Override
77      public boolean equals(Object obj) {
78          if (this == obj) {
79              return true;
80          }
81          if (obj == null || !getClass().equals(obj.getClass())) {
82              return false;
83          }
84          SslConfig that = (SslConfig) obj;
85          return Objects.equals(context, that.context)
86                  && Objects.equals(verifier, that.verifier)
87                  && Arrays.equals(cipherSuites, that.cipherSuites)
88                  && Arrays.equals(protocols, that.protocols);
89      }
90  
91      @Override
92      public int hashCode() {
93          int hash = 17;
94          hash = hash * 31 + hash(context);
95          hash = hash * 31 + hash(verifier);
96          hash = hash * 31 + Arrays.hashCode(cipherSuites);
97          hash = hash * 31 + Arrays.hashCode(protocols);
98          return hash;
99      }
100 
101     private static int hash(Object obj) {
102         return obj != null ? obj.hashCode() : 0;
103     }
104 }