View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package io.netty.example.http2.server;
18  
19  import io.netty.channel.ChannelHandlerAdapter;
20  import io.netty.channel.ChannelHandlerContext;
21  import io.netty.channel.ChannelInitializer;
22  import io.netty.channel.socket.SocketChannel;
23  import io.netty.handler.codec.http.HttpServerCodec;
24  import io.netty.handler.codec.http.HttpServerUpgradeHandler;
25  import io.netty.handler.codec.http2.Http2ServerUpgradeCodec;
26  import io.netty.handler.ssl.SslContext;
27  
28  import java.util.Collections;
29  
30  /**
31   * Sets up the Netty pipeline for the example server. Depending on the endpoint config, sets up the
32   * pipeline for NPN or cleartext HTTP upgrade to HTTP/2.
33   */
34  public class Http2ServerInitializer extends ChannelInitializer<SocketChannel> {
35      private final SslContext sslCtx;
36  
37      public Http2ServerInitializer(SslContext sslCtx) {
38          this.sslCtx = sslCtx;
39      }
40  
41      @Override
42      public void initChannel(SocketChannel ch) {
43          if (sslCtx != null) {
44              configureSsl(ch);
45          } else {
46              configureClearText(ch);
47          }
48      }
49  
50      /**
51       * Configure the pipeline for TLS NPN negotiation to HTTP/2.
52       */
53      private void configureSsl(SocketChannel ch) {
54          ch.pipeline().addLast(sslCtx.newHandler(ch.alloc()), new Http2OrHttpHandler());
55      }
56  
57      /**
58       * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.
59       */
60      private static void configureClearText(SocketChannel ch) {
61          HttpServerCodec sourceCodec = new HttpServerCodec();
62          HttpServerUpgradeHandler.UpgradeCodec upgradeCodec =
63                  new Http2ServerUpgradeCodec(new HelloWorldHttp2Handler());
64          HttpServerUpgradeHandler upgradeHandler =
65                  new HttpServerUpgradeHandler(sourceCodec, Collections.singletonList(upgradeCodec), 65536);
66  
67          ch.pipeline().addLast(sourceCodec);
68          ch.pipeline().addLast(upgradeHandler);
69          ch.pipeline().addLast(new UserEventLogger());
70      }
71  
72      /**
73       * Class that logs any User Events triggered on this channel.
74       */
75      private static class UserEventLogger extends ChannelHandlerAdapter {
76          @Override
77          public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
78              System.out.println("User Event Triggered: " + evt);
79              ctx.fireUserEventTriggered(evt);
80          }
81      }
82  }