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
  63df27d4_bc92_57a6_45eb_a37a451ae596["Http2ServerInitializer"]
  0cab5044_cbdd_1345_10ae_08f2d236c495["Http2ServerInitializer.java"]
  63df27d4_bc92_57a6_45eb_a37a451ae596 -->|defined in| 0cab5044_cbdd_1345_10ae_08f2d236c495
  13dee2bd_67ed_dd7a_9442_7c0cd3db56b6["Http2ServerInitializer()"]
  63df27d4_bc92_57a6_45eb_a37a451ae596 -->|method| 13dee2bd_67ed_dd7a_9442_7c0cd3db56b6
  10bc5522_88f6_71e4_54f2_a0853d202000["initChannel()"]
  63df27d4_bc92_57a6_45eb_a37a451ae596 -->|method| 10bc5522_88f6_71e4_54f2_a0853d202000
  ff5434ae_5e83_dcce_c429_4ca26fd0e5ff["configureClearText()"]
  63df27d4_bc92_57a6_45eb_a37a451ae596 -->|method| ff5434ae_5e83_dcce_c429_4ca26fd0e5ff

Relationship Graph

Source Code

testsuite-http2/src/main/java/io/netty/testsuite/http2/Http2ServerInitializer.java lines 43–110

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 int maxHttpContentLength;

    Http2ServerInitializer() {
        this(16 * 1024);
    }

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

    @Override
    public void initChannel(SocketChannel ch) {
        configureClearText(ch);
    }

    /**
     * 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();
                ChannelHandlerContext thisCtx = pipeline.context(this);
                pipeline.addAfter(thisCtx.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 testsuite-http2/src/main/java/io/netty/testsuite/http2/Http2ServerInitializer.java.
Where is Http2ServerInitializer defined?
Http2ServerInitializer is defined in testsuite-http2/src/main/java/io/netty/testsuite/http2/Http2ServerInitializer.java at line 43.

Analyze Your Own Codebase

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

Try Supermodel Free