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
  8d5f7d30_172c_30af_5e36_167b7fbb8b95["Http2ServerInitializer"]
  b7d48de3_e6f8_0109_e5e6_e35cfdd72e62["Http2ServerInitializer.java"]
  8d5f7d30_172c_30af_5e36_167b7fbb8b95 -->|defined in| b7d48de3_e6f8_0109_e5e6_e35cfdd72e62
  3461593a_6e19_74e8_aae5_fe30f4754314["Http2ServerInitializer()"]
  8d5f7d30_172c_30af_5e36_167b7fbb8b95 -->|method| 3461593a_6e19_74e8_aae5_fe30f4754314
  5ff560df_d056_7021_bf34_918c98dea01e["initChannel()"]
  8d5f7d30_172c_30af_5e36_167b7fbb8b95 -->|method| 5ff560df_d056_7021_bf34_918c98dea01e
  5fff02e8_b315_8874_95e0_62b4ec355cae["configureSsl()"]
  8d5f7d30_172c_30af_5e36_167b7fbb8b95 -->|method| 5fff02e8_b315_8874_95e0_62b4ec355cae
  b65bc168_5b75_5a15_71ca_7c2a8919c815["configureClearText()"]
  8d5f7d30_172c_30af_5e36_167b7fbb8b95 -->|method| b65bc168_5b75_5a15_71ca_7c2a8919c815

Relationship Graph

Source Code

example/src/main/java/io/netty/example/http2/helloworld/multiplex/server/Http2ServerInitializer.java lines 46–123

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 Http2MultiplexHandler(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/multiplex/server/Http2ServerInitializer.java.
Where is Http2ServerInitializer defined?
Http2ServerInitializer is defined in example/src/main/java/io/netty/example/http2/helloworld/multiplex/server/Http2ServerInitializer.java at line 46.

Analyze Your Own Codebase

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

Try Supermodel Free