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.io.IOException;
23  
24  import javax.net.ssl.HostnameVerifier;
25  import javax.net.ssl.SSLContext;
26  import javax.net.ssl.SSLSocket;
27  import javax.net.ssl.SSLSocketFactory;
28  
29  import org.apache.http.conn.ssl.X509HostnameVerifier;
30  
31  /**
32   * Specialized SSL socket factory to more closely resemble the JRE's HttpsClient and respect well-known SSL-related
33   * configuration properties.
34   * 
35   * @see <a href="http://docs.oracle.com/javase/1.5.0/docs/guide/security/jsse/JSSERefGuide.html#Customization">JSSE
36   *      Reference Guide, Customization</a>
37   */
38  final class SslSocketFactory
39      extends org.apache.http.conn.ssl.SSLSocketFactory
40  {
41  
42      private final String[] cipherSuites;
43  
44      private final String[] protocols;
45  
46      public SslSocketFactory( SslConfig config )
47      {
48          this( getSocketFactory( config.context ), getHostnameVerifier( config.verifier ), config.cipherSuites,
49                config.protocols );
50      }
51  
52      private static SSLSocketFactory getSocketFactory( SSLContext context )
53      {
54          return ( context != null ) ? context.getSocketFactory() : (SSLSocketFactory) SSLSocketFactory.getDefault();
55      }
56  
57      private static X509HostnameVerifier getHostnameVerifier( HostnameVerifier verifier )
58      {
59          return ( verifier != null ) ? X509HostnameVerifierAdapter.adapt( verifier )
60                          : org.apache.http.conn.ssl.SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER;
61      }
62  
63      private SslSocketFactory( SSLSocketFactory socketfactory, X509HostnameVerifier hostnameVerifier,
64                                String[] cipherSuites, String[] protocols )
65      {
66          super( socketfactory, hostnameVerifier );
67  
68          this.cipherSuites = cipherSuites;
69          this.protocols = protocols;
70      }
71  
72      @Override
73      protected void prepareSocket( SSLSocket socket )
74          throws IOException
75      {
76          super.prepareSocket( socket );
77          if ( cipherSuites != null )
78          {
79              socket.setEnabledCipherSuites( cipherSuites );
80          }
81          if ( protocols != null )
82          {
83              socket.setEnabledProtocols( protocols );
84          }
85      }
86  
87  }