1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package io.netty.example.http2.helloworld.server;
16
17 import io.netty.channel.ChannelHandlerContext;
18 import io.netty.handler.codec.http.HttpObjectAggregator;
19 import io.netty.handler.codec.http.HttpServerCodec;
20 import io.netty.handler.ssl.ApplicationProtocolNames;
21 import io.netty.handler.ssl.ApplicationProtocolNegotiationHandler;
22
23
24
25
26
27 public class Http2OrHttpHandler extends ApplicationProtocolNegotiationHandler {
28
29 private static final int MAX_CONTENT_LENGTH = 1024 * 100;
30
31 protected Http2OrHttpHandler() {
32 super(ApplicationProtocolNames.HTTP_1_1);
33 }
34
35 @Override
36 protected void configurePipeline(ChannelHandlerContext ctx, String protocol) throws Exception {
37 if (ApplicationProtocolNames.HTTP_2.equals(protocol)) {
38 ctx.pipeline().addLast(new HelloWorldHttp2HandlerBuilder().build());
39 return;
40 }
41
42 if (ApplicationProtocolNames.HTTP_1_1.equals(protocol)) {
43 ctx.pipeline().addLast(new HttpServerCodec(),
44 new HttpObjectAggregator(MAX_CONTENT_LENGTH),
45 new HelloWorldHttp1Handler("ALPN Negotiation"));
46 return;
47 }
48
49 throw new IllegalStateException("unknown protocol: " + protocol);
50 }
51 }