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.io.IOException;
30  import java.nio.ByteBuffer;
31  import java.nio.CharBuffer;
32  import java.util.concurrent.Future;
33  
34  import org.apache.hc.client5.http.async.methods.AbstractBinPushConsumer;
35  import org.apache.hc.client5.http.async.methods.AbstractCharResponseConsumer;
36  import org.apache.hc.client5.http.config.TlsConfig;
37  import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
38  import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
39  import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
40  import org.apache.hc.core5.http.ContentType;
41  import org.apache.hc.core5.http.HttpException;
42  import org.apache.hc.core5.http.HttpRequest;
43  import org.apache.hc.core5.http.HttpResponse;
44  import org.apache.hc.core5.http.message.BasicHttpRequest;
45  import org.apache.hc.core5.http.message.StatusLine;
46  import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
47  import org.apache.hc.core5.http.support.BasicRequestBuilder;
48  import org.apache.hc.core5.http2.HttpVersionPolicy;
49  import org.apache.hc.core5.http2.config.H2Config;
50  import org.apache.hc.core5.io.CloseMode;
51  
52  /**
53   * This example demonstrates handling of HTTP/2 message exchanges pushed by the server.
54   */
55  public class AsyncClientH2ServerPush {
56  
57      public static void main(final String[] args) throws Exception {
58  
59          final CloseableHttpAsyncClient client = HttpAsyncClients.custom()
60                  .setH2Config(H2Config.custom()
61                          .setPushEnabled(true)
62                          .build())
63                  .setConnectionManager(PoolingAsyncClientConnectionManagerBuilder.create()
64                          .setDefaultTlsConfig(TlsConfig.custom()
65                                  .setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_2)
66                                  .build())
67                          .build())
68                  .build();
69  
70          client.start();
71  
72          client.register("*", () -> new AbstractBinPushConsumer() {
73  
74              @Override
75              protected void start(
76                      final HttpRequest promise,
77                      final HttpResponse response,
78                      final ContentType contentType) throws HttpException, IOException {
79                  System.out.println(promise.getPath() + " (push)->" + new StatusLine(response));
80              }
81  
82              @Override
83              protected int capacityIncrement() {
84                  return Integer.MAX_VALUE;
85              }
86  
87              @Override
88              protected void data(final ByteBuffer data, final boolean endOfStream) throws IOException {
89              }
90  
91              @Override
92              protected void completed() {
93              }
94  
95              @Override
96              public void failed(final Exception cause) {
97                  System.out.println("(push)->" + cause);
98              }
99  
100             @Override
101             public void releaseResources() {
102             }
103 
104         });
105 
106         final BasicHttpRequest request = BasicRequestBuilder.get("https://nghttp2.org/httpbin/").build();
107 
108         System.out.println("Executing request " + request);
109         final Future<Void> future = client.execute(
110                 new BasicRequestProducer(request, null),
111                 new AbstractCharResponseConsumer<Void>() {
112 
113                     @Override
114                     protected void start(
115                             final HttpResponse response,
116                             final ContentType contentType) throws HttpException, IOException {
117                         System.out.println(request + "->" + new StatusLine(response));
118                     }
119 
120                     @Override
121                     protected int capacityIncrement() {
122                         return Integer.MAX_VALUE;
123                     }
124 
125                     @Override
126                     protected void data(final CharBuffer data, final boolean endOfStream) throws IOException {
127                     }
128 
129                     @Override
130                     protected Void buildResult() throws IOException {
131                         return null;
132                     }
133 
134                     @Override
135                     public void failed(final Exception cause) {
136                         System.out.println(request + "->" + cause);
137                     }
138 
139                     @Override
140                     public void releaseResources() {
141                     }
142 
143                 }, null);
144         future.get();
145 
146         System.out.println("Shutting down");
147         client.close(CloseMode.GRACEFUL);
148     }
149 
150 }