Home / Class/ IntermediaryHandler Class — netty Architecture

IntermediaryHandler Class — netty Architecture

Architecture documentation for the IntermediaryHandler class in ProxyServer.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  2b6c4159_d6d3_1ea1_7d2a_b3ecbc771341["IntermediaryHandler"]
  db43fe47_37b6_43cd_123c_8899d4fb6e75["ProxyServer.java"]
  2b6c4159_d6d3_1ea1_7d2a_b3ecbc771341 -->|defined in| db43fe47_37b6_43cd_123c_8899d4fb6e75
  c4b3f8a3_81ad_bb3f_ed26_42280222f3f8["channelRead0()"]
  2b6c4159_d6d3_1ea1_7d2a_b3ecbc771341 -->|method| c4b3f8a3_81ad_bb3f_ed26_42280222f3f8
  a9f2f8a5_cde6_ed4b_c0f0_4f3ecb59b774["flush()"]
  2b6c4159_d6d3_1ea1_7d2a_b3ecbc771341 -->|method| a9f2f8a5_cde6_ed4b_c0f0_4f3ecb59b774
  184b2fe6_cb56_df45_eecf_a591387f9a36["handleProxyProtocol()"]
  2b6c4159_d6d3_1ea1_7d2a_b3ecbc771341 -->|method| 184b2fe6_cb56_df45_eecf_a591387f9a36
  d7842002_7e5b_3055_8d88_ee0af0273d4f["SocketAddress()"]
  2b6c4159_d6d3_1ea1_7d2a_b3ecbc771341 -->|method| d7842002_7e5b_3055_8d88_ee0af0273d4f
  16434b40_7949_ebac_e915_2a1845904824["ChannelFuture()"]
  2b6c4159_d6d3_1ea1_7d2a_b3ecbc771341 -->|method| 16434b40_7949_ebac_e915_2a1845904824
  48032e34_6f80_a5ec_df8e_42cebbf5c081["channelReadComplete()"]
  2b6c4159_d6d3_1ea1_7d2a_b3ecbc771341 -->|method| 48032e34_6f80_a5ec_df8e_42cebbf5c081
  dc2b6f53_809e_867e_de8d_17dd601f8f8f["channelInactive()"]
  2b6c4159_d6d3_1ea1_7d2a_b3ecbc771341 -->|method| dc2b6f53_809e_867e_de8d_17dd601f8f8f
  67bc2669_3b8c_a6c6_19df_d43b3b7a12ec["exceptionCaught()"]
  2b6c4159_d6d3_1ea1_7d2a_b3ecbc771341 -->|method| 67bc2669_3b8c_a6c6_19df_d43b3b7a12ec

Relationship Graph

Source Code

handler-proxy/src/test/java/io/netty/handler/proxy/ProxyServer.java lines 153–261

    protected abstract class IntermediaryHandler extends SimpleChannelInboundHandler<Object> {

        private final Queue<Object> received = new ArrayDeque<Object>();

        private boolean finished;
        private Channel backend;

        @Override
        protected final void channelRead0(final ChannelHandlerContext ctx, Object msg) throws Exception {
            if (finished) {
                received.add(ReferenceCountUtil.retain(msg));
                flush();
                return;
            }

            boolean finished = handleProxyProtocol(ctx, msg);
            if (finished) {
                this.finished = true;
                ChannelFuture f = connectToDestination(ctx.channel().eventLoop(), new BackendHandler(ctx));
                f.addListener((ChannelFutureListener) future -> {
                    if (!future.isSuccess()) {
                        recordException(future.cause());
                        ctx.close();
                    } else {
                        backend = future.channel();
                        flush();
                    }
                });
            }
        }

        private void flush() {
            if (backend != null) {
                boolean wrote = false;
                for (;;) {
                    Object msg = received.poll();
                    if (msg == null) {
                        break;
                    }
                    backend.write(msg);
                    wrote = true;
                }

                if (wrote) {
                    backend.flush();
                }
            }
        }

        protected abstract boolean handleProxyProtocol(ChannelHandlerContext ctx, Object msg) throws Exception;

        protected abstract SocketAddress intermediaryDestination();

        private ChannelFuture connectToDestination(EventLoop loop, ChannelHandler handler) {
            Bootstrap b = new Bootstrap();
            b.channel(NioSocketChannel.class);
            b.group(loop);
            b.handler(handler);
            return b.connect(intermediaryDestination());
        }

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

        @Override
        public void channelInactive(ChannelHandlerContext ctx) throws Exception {
            if (backend != null) {
                backend.close();
            }
        }

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

        private final class BackendHandler extends ChannelInboundHandlerAdapter {

Frequently Asked Questions

What is the IntermediaryHandler class?
IntermediaryHandler is a class in the netty codebase, defined in handler-proxy/src/test/java/io/netty/handler/proxy/ProxyServer.java.
Where is IntermediaryHandler defined?
IntermediaryHandler is defined in handler-proxy/src/test/java/io/netty/handler/proxy/ProxyServer.java at line 153.

Analyze Your Own Codebase

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

Try Supermodel Free