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