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  import java.security.cert.X509Certificate;
24  
25  import javax.net.ssl.HostnameVerifier;
26  import javax.net.ssl.SSLException;
27  import javax.net.ssl.SSLSession;
28  import javax.net.ssl.SSLSocket;
29  
30  import org.apache.http.conn.ssl.X509HostnameVerifier;
31  
32  /**
33   * Makes a standard hostname verifier compatible with Apache HttpClient's API.
34   */
35  final class X509HostnameVerifierAdapter
36      implements X509HostnameVerifier
37  {
38  
39      private final HostnameVerifier verifier;
40  
41      public static X509HostnameVerifier adapt( HostnameVerifier verifier )
42      {
43          if ( verifier instanceof X509HostnameVerifier )
44          {
45              return (X509HostnameVerifier) verifier;
46          }
47          return new X509HostnameVerifierAdapter( verifier );
48      }
49  
50      private X509HostnameVerifierAdapter( HostnameVerifier verifier )
51      {
52          this.verifier = verifier;
53      }
54  
55      public boolean verify( String hostname, SSLSession session )
56      {
57          return verifier.verify( hostname, session );
58      }
59  
60      public void verify( String host, SSLSocket socket )
61          throws IOException
62      {
63          if ( !verify( host, socket.getSession() ) )
64          {
65              throw new SSLException( "<" + host + "> does not pass hostname verification" );
66          }
67      }
68  
69      public void verify( String host, X509Certificate cert )
70          throws SSLException
71      {
72          throw new UnsupportedOperationException();
73      }
74  
75      public void verify( String host, String[] cns, String[] subjectAlts )
76          throws SSLException
77      {
78          throw new UnsupportedOperationException();
79      }
80  
81  }