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
  fa23b10d_ecd8_5787_14b8_20305e471f36["HelloWorldHttp2Handler"]
  d96abebc_cfae_138c_1d7f_0bb342d1d12a["HelloWorldHttp2Handler.java"]
  fa23b10d_ecd8_5787_14b8_20305e471f36 -->|defined in| d96abebc_cfae_138c_1d7f_0bb342d1d12a
  c7df94ad_7590_c92e_f69c_8ad68cb7f56a["exceptionCaught()"]
  fa23b10d_ecd8_5787_14b8_20305e471f36 -->|method| c7df94ad_7590_c92e_f69c_8ad68cb7f56a
  7c2c193e_a794_8fd3_2bb9_0ab2a904aa24["channelRead()"]
  fa23b10d_ecd8_5787_14b8_20305e471f36 -->|method| 7c2c193e_a794_8fd3_2bb9_0ab2a904aa24
  49709e8c_6ce2_e32d_040c_5f4ab841b0b5["channelReadComplete()"]
  fa23b10d_ecd8_5787_14b8_20305e471f36 -->|method| 49709e8c_6ce2_e32d_040c_5f4ab841b0b5
  ea67368a_00e9_4004_9c50_d0759434db20["onDataRead()"]
  fa23b10d_ecd8_5787_14b8_20305e471f36 -->|method| ea67368a_00e9_4004_9c50_d0759434db20
  6ee65ecb_5c8c_6923_e782_092b8dba7364["onHeadersRead()"]
  fa23b10d_ecd8_5787_14b8_20305e471f36 -->|method| 6ee65ecb_5c8c_6923_e782_092b8dba7364
  b13faf07_e0fe_ada8_4ade_42b8225c23ef["sendResponse()"]
  fa23b10d_ecd8_5787_14b8_20305e471f36 -->|method| b13faf07_e0fe_ada8_4ade_42b8225c23ef

Relationship Graph

Source Code

example/src/main/java/io/netty/example/http2/helloworld/frame/server/HelloWorldHttp2Handler.java lines 40–108

@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 {
        Http2FrameStream stream = data.stream();

        if (data.isEndStream()) {
            sendResponse(ctx, stream, data.content());
        } else {
            // We do not send back the response to the remote-peer, so we need to release it.
            data.release();
        }

        // Update the flowcontroller
        ctx.write(new DefaultHttp2WindowUpdateFrame(data.initialFlowControlledBytes()).stream(stream));
    }

    /**
     * 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, headers.stream(), content);
        }
    }

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

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/frame/server/HelloWorldHttp2Handler.java.
Where is HelloWorldHttp2Handler defined?
HelloWorldHttp2Handler is defined in example/src/main/java/io/netty/example/http2/helloworld/frame/server/HelloWorldHttp2Handler.java at line 40.

Analyze Your Own Codebase

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

Try Supermodel Free