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.hc.core5.http.nio.ssl;
29  
30  import java.net.SocketAddress;
31  
32  import javax.net.ssl.SSLContext;
33  
34  import org.apache.hc.core5.http.HttpHost;
35  import org.apache.hc.core5.http.URIScheme;
36  import org.apache.hc.core5.reactor.ssl.SSLBufferMode;
37  import org.apache.hc.core5.reactor.ssl.SSLSessionInitializer;
38  import org.apache.hc.core5.reactor.ssl.SSLSessionVerifier;
39  import org.apache.hc.core5.reactor.ssl.TransportSecurityLayer;
40  import org.apache.hc.core5.ssl.SSLContexts;
41  import org.apache.hc.core5.util.Args;
42  
43  /**
44   * Basic client-side implementation of {@link TlsStrategy} that upgrades to TLS for all endpoints
45   * with {@code HTTPS} scheme.
46   *
47   * @since 5.0
48   */
49  public class BasicClientTlsStrategy implements TlsStrategy {
50  
51      private final SSLContext sslContext;
52      private final SSLBufferMode sslBufferMode;
53      private final SSLSessionInitializer initializer;
54      private final SSLSessionVerifier verifier;
55  
56      public BasicClientTlsStrategy(
57              final SSLContext sslContext,
58              final SSLBufferMode sslBufferMode,
59              final SSLSessionInitializer initializer,
60              final SSLSessionVerifier verifier) {
61          this.sslContext = Args.notNull(sslContext, "SSL context");
62          this.sslBufferMode = sslBufferMode;
63          this.initializer = initializer;
64          this.verifier = verifier;
65      }
66  
67      public BasicClientTlsStrategy(
68              final SSLContext sslContext,
69              final SSLSessionInitializer initializer,
70              final SSLSessionVerifier verifier) {
71          this(sslContext, null, initializer, verifier);
72      }
73  
74      public BasicClientTlsStrategy(
75              final SSLContext sslContext,
76              final SSLSessionVerifier verifier) {
77          this(sslContext, null, null, verifier);
78      }
79  
80      public BasicClientTlsStrategy(final SSLContext sslContext) {
81          this(sslContext, null, null, null);
82      }
83  
84      public BasicClientTlsStrategy() {
85          this(SSLContexts.createSystemDefault());
86      }
87  
88      @Override
89      public boolean upgrade(
90              final TransportSecurityLayer tlsSession,
91              final HttpHost host,
92              final SocketAddress localAddress,
93              final SocketAddress remoteAddress,
94              final Object attachment) {
95          final String scheme = host != null ? host.getSchemeName() : null;
96          if (URIScheme.HTTPS.same(scheme)) {
97              tlsSession.startTls(sslContext, host, sslBufferMode, initializer, verifier);
98              return true;
99          }
100         return false;
101     }
102 
103 }