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.core5.http2.examples;
28  
29  import java.io.IOException;
30  import java.net.InetSocketAddress;
31  import java.time.Instant;
32  import java.util.List;
33  import java.util.concurrent.ExecutionException;
34  import java.util.concurrent.Future;
35  
36  import org.apache.hc.core5.http.ContentType;
37  import org.apache.hc.core5.http.EndpointDetails;
38  import org.apache.hc.core5.http.EntityDetails;
39  import org.apache.hc.core5.http.Header;
40  import org.apache.hc.core5.http.HttpException;
41  import org.apache.hc.core5.http.HttpHeaders;
42  import org.apache.hc.core5.http.HttpRequest;
43  import org.apache.hc.core5.http.HttpResponse;
44  import org.apache.hc.core5.http.Message;
45  import org.apache.hc.core5.http.NameValuePair;
46  import org.apache.hc.core5.http.URIScheme;
47  import org.apache.hc.core5.http.impl.bootstrap.HttpAsyncServer;
48  import org.apache.hc.core5.http.message.BasicHttpResponse;
49  import org.apache.hc.core5.http.nio.AsyncEntityConsumer;
50  import org.apache.hc.core5.http.nio.AsyncRequestConsumer;
51  import org.apache.hc.core5.http.nio.AsyncServerRequestHandler;
52  import org.apache.hc.core5.http.nio.entity.AsyncEntityProducers;
53  import org.apache.hc.core5.http.nio.entity.DiscardingEntityConsumer;
54  import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer;
55  import org.apache.hc.core5.http.nio.support.AbstractServerExchangeHandler;
56  import org.apache.hc.core5.http.nio.support.BasicRequestConsumer;
57  import org.apache.hc.core5.http.nio.support.BasicResponseProducer;
58  import org.apache.hc.core5.http.protocol.HttpContext;
59  import org.apache.hc.core5.http.protocol.HttpCoreContext;
60  import org.apache.hc.core5.http2.HttpVersionPolicy;
61  import org.apache.hc.core5.http2.config.H2Config;
62  import org.apache.hc.core5.http2.impl.nio.bootstrap.H2ServerBootstrap;
63  import org.apache.hc.core5.io.CloseMode;
64  import org.apache.hc.core5.net.WWWFormCodec;
65  import org.apache.hc.core5.reactor.IOReactorConfig;
66  import org.apache.hc.core5.reactor.ListenerEndpoint;
67  import org.apache.hc.core5.util.TimeValue;
68  
69  /**
70   * Example HTTP2 server that reads an entity body and responds back with a greeting.
71   *
72   * <pre>
73   * {@code
74   * $ curl  -id name=bob localhost:8080
75   * HTTP/1.1 200 OK
76   * Date: Sat, 25 May 2019 03:44:49 GMT
77   * Server: Apache-HttpCore/5.0-beta8-SNAPSHOT (Java/1.8.0_202)
78   * Transfer-Encoding: chunked
79   * Content-Type: text/plain; charset=ISO-8859-1
80   *
81   * Hello bob
82   * }</pre>
83   * <p>
84   * This examples uses a {@link AbstractServerExchangeHandler} for the basic request / response processing cycle.
85   */
86  public class H2GreetingServer {
87      public static void main(final String[] args) throws ExecutionException, InterruptedException {
88          int port = 8080;
89          if (args.length >= 1) {
90              port = Integer.parseInt(args[0]);
91          }
92  
93          final HttpAsyncServer server = H2ServerBootstrap.bootstrap()
94                  .setH2Config(H2Config.DEFAULT)
95                  .setIOReactorConfig(IOReactorConfig.DEFAULT)
96                  .setVersionPolicy(HttpVersionPolicy.NEGOTIATE) // fallback to HTTP/1 as needed
97  
98                  // wildcard path matcher:
99                  .register("*", CustomServerExchangeHandler::new)
100                 .create();
101 
102 
103         Runtime.getRuntime().addShutdownHook(new Thread(() -> {
104             System.out.println("HTTP server shutting down");
105             server.close(CloseMode.GRACEFUL);
106         }));
107 
108         server.start();
109         final Future<ListenerEndpoint> future = server.listen(new InetSocketAddress(port), URIScheme.HTTP);
110         final ListenerEndpoint listenerEndpoint = future.get();
111         System.out.println("Listening on " + listenerEndpoint.getAddress());
112         server.awaitShutdown(TimeValue.ofDays(Long.MAX_VALUE));
113     }
114 
115     static class CustomServerExchangeHandler extends AbstractServerExchangeHandler<Message<HttpRequest, String>> {
116 
117 
118         @Override
119         protected AsyncRequestConsumer<Message<HttpRequest, String>> supplyConsumer(
120                 final HttpRequest request,
121                 final EntityDetails entityDetails,
122                 final HttpContext context) {
123             // if there's no body don't try to parse entity:
124             AsyncEntityConsumer<String> entityConsumer = new DiscardingEntityConsumer<>();
125 
126             if (entityDetails != null) {
127                 entityConsumer = new StringAsyncEntityConsumer();
128             }
129             //noinspection unchecked
130             return new BasicRequestConsumer<>(entityConsumer);
131 
132         }
133 
134         @Override
135         protected void handle(final Message<HttpRequest, String> requestMessage,
136                               final AsyncServerRequestHandler.ResponseTrigger responseTrigger,
137                               final HttpContext context) throws HttpException, IOException {
138 
139             final HttpCoreContext coreContext = HttpCoreContext.adapt(context);
140             final EndpointDetails endpoint = coreContext.getEndpointDetails();
141             final HttpRequest req = requestMessage.getHead();
142             final String httpEntity = requestMessage.getBody();
143 
144             // generic success response:
145             final HttpResponse resp = new BasicHttpResponse(200);
146 
147             // recording the request
148             System.out.printf("[%s] %s %s %s%n", Instant.now(),
149                     endpoint.getRemoteAddress(),
150                     req.getMethod(),
151                     req.getPath());
152 
153             // Request without an entity - GET/HEAD/DELETE
154             if (httpEntity == null) {
155                 responseTrigger.submitResponse(
156                         new BasicResponseProducer(resp), context);
157                 return;
158             }
159 
160             // Request with an entity - POST/PUT
161             final Header cth = req.getHeader(HttpHeaders.CONTENT_TYPE);
162             final ContentType contentType = cth != null ? ContentType.parse(cth.getValue()) : null;
163             String name = "stranger";
164             if (contentType != null && contentType.isSameMimeType(ContentType.APPLICATION_FORM_URLENCODED)) {
165 
166                 // decoding the form entity into key/value pairs:
167                 final List<NameValuePair> args = WWWFormCodec.parse(httpEntity, contentType.getCharset());
168                 if (!args.isEmpty()) {
169                     name = args.get(0).getValue();
170                 }
171             }
172 
173             // composing greeting:
174             final String greeting = String.format("Hello %s\n", name);
175             responseTrigger.submitResponse(
176                     new BasicResponseProducer(resp, AsyncEntityProducers.create(greeting)), context);
177         }
178     }
179 
180 }
181