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
  dde7fab7_ab37_3918_6df2_b3e8b994940f["Http2ServerInitializer"]
  85fce4ca_998d_7398_c84a_bb3428d7f083["Http2ServerInitializer.java"]
  dde7fab7_ab37_3918_6df2_b3e8b994940f -->|defined in| 85fce4ca_998d_7398_c84a_bb3428d7f083
  40321cce_749a_c874_2b94_2bbd1d1dc3a6["Http2ServerInitializer()"]
  dde7fab7_ab37_3918_6df2_b3e8b994940f -->|method| 40321cce_749a_c874_2b94_2bbd1d1dc3a6
  f7d3c227_1ff7_d3c7_fcd4_550f4b8bdb74["initChannel()"]
  dde7fab7_ab37_3918_6df2_b3e8b994940f -->|method| f7d3c227_1ff7_d3c7_fcd4_550f4b8bdb74
  78ee8321_7268_8c2b_236d_1bef32ac90bb["configureSsl()"]
  dde7fab7_ab37_3918_6df2_b3e8b994940f -->|method| 78ee8321_7268_8c2b_236d_1bef32ac90bb
  33997779_95aa_b421_dbbd_fcc7afd3acaf["configureClearText()"]
  dde7fab7_ab37_3918_6df2_b3e8b994940f -->|method| 33997779_95aa_b421_dbbd_fcc7afd3acaf

Relationship Graph

Source Code

example/src/main/java/io/netty/example/http2/helloworld/frame/server/Http2ServerInitializer.java lines 45–121

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(
                        Http2FrameCodecBuilder.forServer().build(), new HelloWorldHttp2Handler());
            } 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();

        p.addLast(sourceCodec);
        p.addLast(new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory));
        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/frame/server/Http2ServerInitializer.java.
Where is Http2ServerInitializer defined?
Http2ServerInitializer is defined in example/src/main/java/io/netty/example/http2/helloworld/frame/server/Http2ServerInitializer.java at line 45.

Analyze Your Own Codebase

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

Try Supermodel Free