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.examples;
29  
30  import java.net.InetAddress;
31  import java.util.concurrent.TimeUnit;
32  
33  import javax.net.ssl.SSLSession;
34  
35  import org.apache.hc.core5.http.ClassicHttpRequest;
36  import org.apache.hc.core5.http.ClassicHttpResponse;
37  import org.apache.hc.core5.http.HttpConnection;
38  import org.apache.hc.core5.http.HttpHost;
39  import org.apache.hc.core5.http.HttpRequest;
40  import org.apache.hc.core5.http.HttpResponse;
41  import org.apache.hc.core5.http.impl.Http1StreamListener;
42  import org.apache.hc.core5.http.impl.bootstrap.HttpRequester;
43  import org.apache.hc.core5.http.impl.bootstrap.RequesterBootstrap;
44  import org.apache.hc.core5.http.io.SocketConfig;
45  import org.apache.hc.core5.http.io.support.ClassicRequestBuilder;
46  import org.apache.hc.core5.http.message.RequestLine;
47  import org.apache.hc.core5.http.message.StatusLine;
48  import org.apache.hc.core5.http.protocol.HttpCoreContext;
49  import org.apache.hc.core5.util.Timeout;
50  
51  /**
52   * Example of SNI (Server Name Identification) usage with classic I/O.
53   */
54  public class ClassicClientSNIExample {
55  
56      public static void main(final String[] args) throws Exception {
57          final HttpRequester httpRequester = RequesterBootstrap.bootstrap()
58                  .setStreamListener(new Http1StreamListener() {
59  
60                      @Override
61                      public void onRequestHead(final HttpConnection connection, final HttpRequest request) {
62                          System.out.println(connection.getRemoteAddress() + " " + new RequestLine(request));
63  
64                      }
65  
66                      @Override
67                      public void onResponseHead(final HttpConnection connection, final HttpResponse response) {
68                          System.out.println(connection.getRemoteAddress() + " " + new StatusLine(response));
69                      }
70  
71                      @Override
72                      public void onExchangeComplete(final HttpConnection connection, final boolean keepAlive) {
73                          if (keepAlive) {
74                              System.out.println(connection.getRemoteAddress() + " exchange completed (connection kept alive)");
75                          } else {
76                              System.out.println(connection.getRemoteAddress() + " exchange completed (connection closed)");
77                          }
78                      }
79  
80                  })
81                  .setSocketConfig(SocketConfig.custom()
82                          .setSoTimeout(5, TimeUnit.SECONDS)
83                          .build())
84                  .create();
85  
86          // Physical endpoint (google.com)
87          final InetAddress targetAddress = InetAddress.getByName("www.google.com");
88          // Target host (google.ch)
89          final HttpHost target = new HttpHost("https", targetAddress, "www.google.ch", 443);
90          final HttpCoreContext coreContext = HttpCoreContext.create();
91  
92          final ClassicHttpRequest request = ClassicRequestBuilder.get()
93                  .setPath("/")
94                  .build();
95  
96          try (ClassicHttpResponse response = httpRequester.execute(target, request, Timeout.ofSeconds(5), coreContext)) {
97              System.out.println(request.getUri() + "->" + response.getCode());
98  
99              final SSLSession sslSession = coreContext.getSSLSession();
100             if (sslSession != null) {
101                 System.out.println("Peer: " + sslSession.getPeerPrincipal());
102                 System.out.println("TLS protocol: " + sslSession.getProtocol());
103                 System.out.println("TLS cipher suite: " + sslSession.getCipherSuite());
104             }
105 
106             System.out.println("==============");
107         }
108     }
109 
110 }