View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.conn.ssl;
29  
30  import java.security.KeyManagementException;
31  import java.security.NoSuchAlgorithmException;
32  
33  import javax.net.ssl.SSLContext;
34  
35  import org.apache.http.annotation.Immutable;
36  
37  /**
38   * {@link SSLContext} factory methods.
39   *
40   * @since 4.3
41   *
42   * @deprecated (4.4) use {@link org.apache.http.ssl.SSLContexts}.
43   */
44  @Immutable
45  @Deprecated
46  public class SSLContexts {
47  
48      /**
49       * Creates default factory based on the standard JSSE trust material
50       * ({@code cacerts} file in the security properties directory). System properties
51       * are not taken into consideration.
52       *
53       * @return the default SSL socket factory
54       */
55      public static SSLContext createDefault() throws SSLInitializationException {
56          try {
57              final SSLContext sslcontext = SSLContext.getInstance(SSLContextBuilder.TLS);
58              sslcontext.init(null, null, null);
59              return sslcontext;
60          } catch (final NoSuchAlgorithmException ex) {
61              throw new SSLInitializationException(ex.getMessage(), ex);
62          } catch (final KeyManagementException ex) {
63              throw new SSLInitializationException(ex.getMessage(), ex);
64          }
65      }
66  
67      /**
68       * Creates default SSL context based on system properties. This method obtains
69       * default SSL context by calling {@code SSLContext.getInstance("Default")}.
70       * Please note that {@code Default} algorithm is supported as of Java 6.
71       * This method will fall back onto {@link #createDefault()} when
72       * {@code Default} algorithm is not available.
73       *
74       * @return default system SSL context
75       */
76      public static SSLContext createSystemDefault() throws SSLInitializationException {
77          try {
78              return SSLContext.getDefault();
79          } catch (final NoSuchAlgorithmException ex) {
80              return createDefault();
81          }
82      }
83  
84      /**
85       * Creates custom SSL context.
86       *
87       * @return default system SSL context
88       */
89      public static SSLContextBuilder custom() {
90          return new SSLContextBuilder();
91      }
92  
93  }