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