View Javadoc
1   package org.eclipse.aether.transport.http;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Arrays;
23  
24  import javax.net.ssl.HostnameVerifier;
25  import javax.net.ssl.SSLContext;
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  
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      public SslConfig( RepositorySystemSession session, AuthenticationContext authContext )
50      {
51          context =
52              ( authContext != null ) ? authContext.get( AuthenticationContext.SSL_CONTEXT, SSLContext.class ) : null;
53          verifier =
54              ( authContext != null ) ? authContext.get( AuthenticationContext.SSL_HOSTNAME_VERIFIER,
55                                                         HostnameVerifier.class ) : null;
56  
57          cipherSuites = split( get( session, CIPHER_SUITES ) );
58          protocols = split( get( session, PROTOCOLS ) );
59      }
60  
61      private static String get( RepositorySystemSession session, String key )
62      {
63          String value = ConfigUtils.getString( session, null, "aether.connector." + key, key );
64          if ( value == null )
65          {
66              value = System.getProperty( key );
67          }
68          return value;
69      }
70  
71      private static String[] split( String value )
72      {
73          if ( value == null || value.length() <= 0 )
74          {
75              return null;
76          }
77          return value.split( ",+" );
78      }
79  
80      @Override
81      public boolean equals( Object obj )
82      {
83          if ( this == obj )
84          {
85              return true;
86          }
87          if ( obj == null || !getClass().equals( obj.getClass() ) )
88          {
89              return false;
90          }
91          SslConfig that = (SslConfig) obj;
92          return eq( context, that.context ) && eq( verifier, that.verifier )
93              && Arrays.equals( cipherSuites, that.cipherSuites ) && Arrays.equals( protocols, that.protocols );
94      }
95  
96      private static <T> boolean eq( T s1, T s2 )
97      {
98          return s1 != null ? s1.equals( s2 ) : s2 == null;
99      }
100 
101     @Override
102     public int hashCode()
103     {
104         int hash = 17;
105         hash = hash * 31 + hash( context );
106         hash = hash * 31 + hash( verifier );
107         hash = hash * 31 + Arrays.hashCode( cipherSuites );
108         hash = hash * 31 + Arrays.hashCode( protocols );
109         return hash;
110     }
111 
112     private static int hash( Object obj )
113     {
114         return obj != null ? obj.hashCode() : 0;
115     }
116 
117 }