View Javadoc

1   package org.apache.maven.continuum.project.builder;
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 org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import java.security.KeyStore;
26  import java.security.KeyStoreException;
27  import java.security.NoSuchAlgorithmException;
28  import java.security.cert.CertificateException;
29  import java.security.cert.X509Certificate;
30  import javax.net.ssl.TrustManager;
31  import javax.net.ssl.TrustManagerFactory;
32  import javax.net.ssl.X509TrustManager;
33  
34  /**
35   * @author olamy
36   * @version $Id: EasyX509TrustManager.java 1372260 2012-08-13 04:29:09Z brett $
37   * @since 1.2.3
38   */
39  public class EasyX509TrustManager
40      implements X509TrustManager
41  {
42      private static final Logger log = LoggerFactory.getLogger( EasyX509TrustManager.class );
43  
44      private X509TrustManager standardTrustManager = null;
45  
46      /**
47       * Constructor for EasyX509TrustManager.
48       */
49      public EasyX509TrustManager( KeyStore keystore )
50          throws NoSuchAlgorithmException, KeyStoreException
51      {
52          super();
53          TrustManagerFactory factory = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm() );
54          factory.init( keystore );
55          TrustManager[] trustmanagers = factory.getTrustManagers();
56          if ( trustmanagers.length == 0 )
57          {
58              throw new NoSuchAlgorithmException( "no trust manager found" );
59          }
60          this.standardTrustManager = (X509TrustManager) trustmanagers[0];
61      }
62  
63      /**
64       * @see javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[], String authType)
65       */
66      public void checkClientTrusted( X509Certificate[] certificates, String authType )
67          throws CertificateException
68      {
69          standardTrustManager.checkClientTrusted( certificates, authType );
70      }
71  
72      /**
73       * @see javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[], String authType)
74       */
75      public void checkServerTrusted( X509Certificate[] certificates, String authType )
76          throws CertificateException
77      {
78          if ( ( certificates != null ) && log.isDebugEnabled() )
79          {
80              log.debug( "Server certificate chain:" );
81              for ( int i = 0; i < certificates.length; i++ )
82              {
83                  log.debug( "X509Certificate[" + i + "]=" + certificates[i] );
84              }
85          }
86          if ( ( certificates != null ) && ( certificates.length == 1 ) )
87          {
88              certificates[0].checkValidity();
89          }
90          else
91          {
92              standardTrustManager.checkServerTrusted( certificates, authType );
93          }
94      }
95  
96      /**
97       * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers()
98       */
99      public X509Certificate[] getAcceptedIssuers()
100     {
101         return this.standardTrustManager.getAcceptedIssuers();
102     }
103 
104 }