Home / Class/ Http2ServerInitializer Class — netty Architecture

Http2ServerInitializer Class — netty Architecture

Architecture documentation for the Http2ServerInitializer class in Http2ServerInitializer.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  70b64b44_33a9_847d_400c_ed70c3a9ba07["Http2ServerInitializer"]
  16ecb9ab_e3c3_32b3_7b77_82cd5162d7b4["Http2ServerInitializer.java"]
  70b64b44_33a9_847d_400c_ed70c3a9ba07 -->|defined in| 16ecb9ab_e3c3_32b3_7b77_82cd5162d7b4
  211aa8b6_5751_5755_cdd9_7ce86f81b123["Http2ServerInitializer()"]
  70b64b44_33a9_847d_400c_ed70c3a9ba07 -->|method| 211aa8b6_5751_5755_cdd9_7ce86f81b123
  c6ddeb0c_d799_467c_8a18_2c2fc208b057["initChannel()"]
  70b64b44_33a9_847d_400c_ed70c3a9ba07 -->|method| c6ddeb0c_d799_467c_8a18_2c2fc208b057
  cbcbecdf_bf82_2f49_2d49_b49084def697["configureSsl()"]
  70b64b44_33a9_847d_400c_ed70c3a9ba07 -->|method| cbcbecdf_bf82_2f49_2d49_b49084def697
  24b7c385_d097_2b38_a664_cac56e3b33b9["configureClearText()"]
  70b64b44_33a9_847d_400c_ed70c3a9ba07 -->|method| 24b7c385_d097_2b38_a664_cac56e3b33b9

Relationship Graph

Source Code

example/src/main/java/io/netty/example/http2/helloworld/server/Http2ServerInitializer.java lines 44–122

public class Http2ServerInitializer extends ChannelInitializer<SocketChannel> {

    private static final UpgradeCodecFactory upgradeCodecFactory = new UpgradeCodecFactory() {
        @Override
        public UpgradeCodec newUpgradeCodec(CharSequence protocol) {
            if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) {
                return new Http2ServerUpgradeCodec(new HelloWorldHttp2HandlerBuilder().build());
            } else {
                return null;
            }
        }
    };

    private final SslContext sslCtx;
    private final int maxHttpContentLength;

    public Http2ServerInitializer(SslContext sslCtx) {
        this(sslCtx, 16 * 1024);
    }

    public Http2ServerInitializer(SslContext sslCtx, int maxHttpContentLength) {
        this.sslCtx = sslCtx;
        this.maxHttpContentLength = checkPositiveOrZero(maxHttpContentLength, "maxHttpContentLength");
    }

    @Override
    public void initChannel(SocketChannel ch) {
        if (sslCtx != null) {
            configureSsl(ch);
        } else {
            configureClearText(ch);
        }
    }

    /**
     * Configure the pipeline for TLS NPN negotiation to HTTP/2.
     */
    private void configureSsl(SocketChannel ch) {
        ch.pipeline().addLast(sslCtx.newHandler(ch.alloc()), new Http2OrHttpHandler());
    }

    /**
     * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.0
     */
    private void configureClearText(SocketChannel ch) {
        final ChannelPipeline p = ch.pipeline();
        final HttpServerCodec sourceCodec = new HttpServerCodec();
        final HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory);
        final CleartextHttp2ServerUpgradeHandler cleartextHttp2ServerUpgradeHandler =
                new CleartextHttp2ServerUpgradeHandler(sourceCodec, upgradeHandler,
                                                       new HelloWorldHttp2HandlerBuilder().build());

        p.addLast(cleartextHttp2ServerUpgradeHandler);
        p.addLast(new SimpleChannelInboundHandler<HttpMessage>() {
            @Override
            protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception {
                // If this handler is hit then no upgrade has been attempted and the client is just talking HTTP.
                System.err.println("Directly talking: " + msg.protocolVersion() + " (no upgrade was attempted)");
                ChannelPipeline pipeline = ctx.pipeline();
                pipeline.addAfter(ctx.name(), null, new HelloWorldHttp1Handler("Direct. No Upgrade Attempted."));
                pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength));
                ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
            }
        });

        p.addLast(new UserEventLogger());
    }

    /**
     * Class that logs any User Events triggered on this channel.
     */
    private static class UserEventLogger extends ChannelInboundHandlerAdapter {
        @Override
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
            System.out.println("User Event Triggered: " + evt);
            ctx.fireUserEventTriggered(evt);
        }
    }
}

Frequently Asked Questions

What is the Http2ServerInitializer class?
Http2ServerInitializer is a class in the netty codebase, defined in example/src/main/java/io/netty/example/http2/helloworld/server/Http2ServerInitializer.java.
Where is Http2ServerInitializer defined?
Http2ServerInitializer is defined in example/src/main/java/io/netty/example/http2/helloworld/server/Http2ServerInitializer.java at line 44.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free