Home / Class/ HelloWorldHttp2Handler Class — netty Architecture

HelloWorldHttp2Handler Class — netty Architecture

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

Entity Profile

Dependency Diagram

graph TD
  c8a162e3_eede_4376_b493_eb1ed405d206["HelloWorldHttp2Handler"]
  9603c5db_d12f_2d48_3251_a7fdc428b3fe["HelloWorldHttp2Handler.java"]
  c8a162e3_eede_4376_b493_eb1ed405d206 -->|defined in| 9603c5db_d12f_2d48_3251_a7fdc428b3fe
  58d5d01f_a023_4881_2ccc_3b89c1591995["exceptionCaught()"]
  c8a162e3_eede_4376_b493_eb1ed405d206 -->|method| 58d5d01f_a023_4881_2ccc_3b89c1591995
  3e6fdf00_5772_8ac0_c003_2d5abd586226["channelRead()"]
  c8a162e3_eede_4376_b493_eb1ed405d206 -->|method| 3e6fdf00_5772_8ac0_c003_2d5abd586226
  d39879ae_8c73_a7bb_8660_76fc48c14747["channelReadComplete()"]
  c8a162e3_eede_4376_b493_eb1ed405d206 -->|method| d39879ae_8c73_a7bb_8660_76fc48c14747
  b678494b_8b69_9e9a_c231_26c69c0f78ce["onDataRead()"]
  c8a162e3_eede_4376_b493_eb1ed405d206 -->|method| b678494b_8b69_9e9a_c231_26c69c0f78ce
  9ffbc081_b11c_edec_7761_cee3b226f84a["onHeadersRead()"]
  c8a162e3_eede_4376_b493_eb1ed405d206 -->|method| 9ffbc081_b11c_edec_7761_cee3b226f84a
  585bb4b9_901b_e7dd_4a7b_0410fd16cb04["sendResponse()"]
  c8a162e3_eede_4376_b493_eb1ed405d206 -->|method| 585bb4b9_901b_e7dd_4a7b_0410fd16cb04

Relationship Graph

Source Code

example/src/main/java/io/netty/example/http2/helloworld/multiplex/server/HelloWorldHttp2Handler.java lines 37–100

@Sharable
public class HelloWorldHttp2Handler extends ChannelDuplexHandler {

    static final ByteBuf RESPONSE_BYTES = unreleasableBuffer(
            copiedBuffer("Hello World", CharsetUtil.UTF_8)).asReadOnly();

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof Http2HeadersFrame) {
            onHeadersRead(ctx, (Http2HeadersFrame) msg);
        } else if (msg instanceof Http2DataFrame) {
            onDataRead(ctx, (Http2DataFrame) msg);
        } else {
            super.channelRead(ctx, msg);
        }
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

    /**
     * If receive a frame with end-of-stream set, send a pre-canned response.
     */
    private static void onDataRead(ChannelHandlerContext ctx, Http2DataFrame data) throws Exception {
        if (data.isEndStream()) {
            sendResponse(ctx, data.content());
        } else {
            // We do not send back the response to the remote-peer, so we need to release it.
            data.release();
        }
    }

    /**
     * If receive a frame with end-of-stream set, send a pre-canned response.
     */
    private static void onHeadersRead(ChannelHandlerContext ctx, Http2HeadersFrame headers)
            throws Exception {
        if (headers.isEndStream()) {
            ByteBuf content = ctx.alloc().buffer();
            content.writeBytes(RESPONSE_BYTES.duplicate());
            ByteBufUtil.writeAscii(content, " - via HTTP/2");
            sendResponse(ctx, content);
        }
    }

    /**
     * Sends a "Hello World" DATA frame to the client.
     */
    private static void sendResponse(ChannelHandlerContext ctx, ByteBuf payload) {
        // Send a frame for the response status
        Http2Headers headers = new DefaultHttp2Headers().status(OK.codeAsText());
        ctx.write(new DefaultHttp2HeadersFrame(headers));
        ctx.write(new DefaultHttp2DataFrame(payload, true));
    }
}

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free