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  package org.apache.hc.client5.http.examples;
28  
29  import java.util.concurrent.Future;
30  
31  import javax.net.ssl.SSLSession;
32  
33  import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
34  import org.apache.hc.client5.http.async.methods.SimpleHttpRequests;
35  import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
36  import org.apache.hc.client5.http.async.methods.SimpleRequestProducer;
37  import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer;
38  import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
39  import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
40  import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager;
41  import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
42  import org.apache.hc.client5.http.protocol.HttpClientContext;
43  import org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder;
44  import org.apache.hc.core5.concurrent.FutureCallback;
45  import org.apache.hc.core5.http.HttpHost;
46  import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
47  import org.apache.hc.core5.http2.HttpVersionPolicy;
48  import org.apache.hc.core5.io.CloseMode;
49  
50  /**
51   * This example demonstrates how to avoid the illegal reflective access operation warning
52   * when running with Oracle JRE 9 or newer.
53   */
54  public class AsyncClientTlsAlpn {
55  
56      public final static void main(final String[] args) throws Exception {
57          final TlsStrategy tlsStrategy = ClientTlsStrategyBuilder.create()
58                  .useSystemProperties()
59                  // IMPORTANT uncomment the following method when running Java 9 or older
60                  // in order for ALPN support to work and avoid the illegal reflective
61                  // access operation warning
62                  /*
63                  .setTlsDetailsFactory(new Factory<SSLEngine, TlsDetails>() {
64  
65                      @Override
66                      public TlsDetails create(final SSLEngine sslEngine) {
67                          return new TlsDetails(sslEngine.getSession(), sslEngine.getApplicationProtocol());
68                      }
69                  })
70                  */
71                  .build();
72          final PoolingAsyncClientConnectionManager cm = PoolingAsyncClientConnectionManagerBuilder.create()
73                  .setTlsStrategy(tlsStrategy)
74                  .build();
75          try (final CloseableHttpAsyncClient client = HttpAsyncClients.custom()
76                  .setVersionPolicy(HttpVersionPolicy.NEGOTIATE)
77                  .setConnectionManager(cm)
78                  .build()) {
79  
80              client.start();
81  
82              final HttpHost target = new HttpHost("https", "nghttp2.org", 443);
83              final String requestUri = "/httpbin/";
84              final HttpClientContext clientContext = HttpClientContext.create();
85  
86              final SimpleHttpRequest request = SimpleHttpRequests.get(target, requestUri);
87              final Future<SimpleHttpResponse> future = client.execute(
88                      SimpleRequestProducer.create(request),
89                      SimpleResponseConsumer.create(),
90                      clientContext,
91                      new FutureCallback<SimpleHttpResponse>() {
92  
93                          @Override
94                          public void completed(final SimpleHttpResponse response) {
95                              System.out.println(requestUri + "->" + response.getCode());
96                              System.out.println(response.getBody());
97                              final SSLSession sslSession = clientContext.getSSLSession();
98                              if (sslSession != null) {
99                                  System.out.println("SSL protocol " + sslSession.getProtocol());
100                                 System.out.println("SSL cipher suite " + sslSession.getCipherSuite());
101                             }
102                         }
103 
104                         @Override
105                         public void failed(final Exception ex) {
106                             System.out.println(requestUri + "->" + ex);
107                         }
108 
109                         @Override
110                         public void cancelled() {
111                             System.out.println(requestUri + " cancelled");
112                         }
113 
114                     });
115             future.get();
116 
117             System.out.println("Shutting down");
118             client.close(CloseMode.GRACEFUL);
119         }
120     }
121 
122 }