Home / Class/ AbstractRemoteAddressFilter Class — netty Architecture

AbstractRemoteAddressFilter Class — netty Architecture

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

Entity Profile

Dependency Diagram

graph TD
  ddea06e1_37ed_030d_4926_4c0a65889850["AbstractRemoteAddressFilter"]
  3c7cc28c_021a_3db9_35c5_b7fca90c32bf["AbstractRemoteAddressFilter.java"]
  ddea06e1_37ed_030d_4926_4c0a65889850 -->|defined in| 3c7cc28c_021a_3db9_35c5_b7fca90c32bf
  785c2b49_0de3_0c13_0be6_e595ab67c431["channelRegistered()"]
  ddea06e1_37ed_030d_4926_4c0a65889850 -->|method| 785c2b49_0de3_0c13_0be6_e595ab67c431
  2a1c9690_a411_fed1_15df_96d6297ea424["channelActive()"]
  ddea06e1_37ed_030d_4926_4c0a65889850 -->|method| 2a1c9690_a411_fed1_15df_96d6297ea424
  2cbb3691_64ac_6c04_d925_484365af8fa0["handleNewChannel()"]
  ddea06e1_37ed_030d_4926_4c0a65889850 -->|method| 2cbb3691_64ac_6c04_d925_484365af8fa0
  dbe1eae3_eb68_cf1a_d22c_dabf08b603d5["accept()"]
  ddea06e1_37ed_030d_4926_4c0a65889850 -->|method| dbe1eae3_eb68_cf1a_d22c_dabf08b603d5
  2be83267_e5ea_8355_96e0_ccdfd6570d96["channelAccepted()"]
  ddea06e1_37ed_030d_4926_4c0a65889850 -->|method| 2be83267_e5ea_8355_96e0_ccdfd6570d96
  26cde10a_acb9_39e8_07a5_69ccc9ff14b0["ChannelFuture()"]
  ddea06e1_37ed_030d_4926_4c0a65889850 -->|method| 26cde10a_acb9_39e8_07a5_69ccc9ff14b0

Relationship Graph

Source Code

handler/src/main/java/io/netty/handler/ipfilter/AbstractRemoteAddressFilter.java lines 38–109

public abstract class AbstractRemoteAddressFilter<T extends SocketAddress> extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        handleNewChannel(ctx);
        ctx.fireChannelRegistered();
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        if (!handleNewChannel(ctx)) {
            throw new IllegalStateException("cannot determine to accept or reject a channel: " + ctx.channel());
        } else {
            ctx.fireChannelActive();
        }
    }

    private boolean handleNewChannel(ChannelHandlerContext ctx) throws Exception {
        @SuppressWarnings("unchecked")
        T remoteAddress = (T) ctx.channel().remoteAddress();

        // If the remote address is not available yet, defer the decision.
        if (remoteAddress == null) {
            return false;
        }

        // No need to keep this handler in the pipeline anymore because the decision is going to be made now.
        // Also, this will prevent the subsequent events from being handled by this handler.
        ctx.pipeline().remove(this);

        if (accept(ctx, remoteAddress)) {
            channelAccepted(ctx, remoteAddress);
        } else {
            ChannelFuture rejectedFuture = channelRejected(ctx, remoteAddress);
            if (rejectedFuture != null) {
                rejectedFuture.addListener(ChannelFutureListener.CLOSE);
            } else {
                ctx.close();
            }
        }

        return true;
    }

    /**
     * This method is called immediately after a {@link io.netty.channel.Channel} gets registered.
     *
     * @return Return true if connections from this IP address and port should be accepted. False otherwise.
     */
    protected abstract boolean accept(ChannelHandlerContext ctx, T remoteAddress) throws Exception;

    /**
     * This method is called if {@code remoteAddress} gets accepted by
     * {@link #accept(ChannelHandlerContext, SocketAddress)}.  You should override it if you would like to handle
     * (e.g. respond to) accepted addresses.
     */
    @SuppressWarnings("UnusedParameters")
    protected void channelAccepted(ChannelHandlerContext ctx, T remoteAddress) { }

    /**
     * This method is called if {@code remoteAddress} gets rejected by
     * {@link #accept(ChannelHandlerContext, SocketAddress)}.  You should override it if you would like to handle
     * (e.g. respond to) rejected addresses.
     *
     * @return A {@link ChannelFuture} if you perform I/O operations, so that
     *         the {@link Channel} can be closed once it completes. Null otherwise.
     */
    @SuppressWarnings("UnusedParameters")
    protected ChannelFuture channelRejected(ChannelHandlerContext ctx, T remoteAddress) {
        return null;
    }
}

Frequently Asked Questions

What is the AbstractRemoteAddressFilter class?
AbstractRemoteAddressFilter is a class in the netty codebase, defined in handler/src/main/java/io/netty/handler/ipfilter/AbstractRemoteAddressFilter.java.
Where is AbstractRemoteAddressFilter defined?
AbstractRemoteAddressFilter is defined in handler/src/main/java/io/netty/handler/ipfilter/AbstractRemoteAddressFilter.java at line 38.

Analyze Your Own Codebase

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

Try Supermodel Free