Class ApplicationProtocolNegotiationHandler

  • All Implemented Interfaces:
    io.netty5.channel.ChannelHandler

    public abstract class ApplicationProtocolNegotiationHandler
    extends Object
    implements io.netty5.channel.ChannelHandler
    Configures a ChannelPipeline depending on the application-level protocol negotiation result of SslHandler. For example, you could configure your HTTP pipeline depending on the result of ALPN:
     public class MyInitializer extends ChannelInitializer<Channel> {
         private final SslContext sslCtx;
    
         public MyInitializer(SslContext sslCtx) {
             this.sslCtx = sslCtx;
         }
    
         protected void initChannel(Channel ch) {
             ChannelPipeline p = ch.pipeline();
             p.addLast(sslCtx.newHandler(...)); // Adds SslHandler
             p.addLast(new MyNegotiationHandler());
         }
     }
    
     public class MyNegotiationHandler extends ApplicationProtocolNegotiationHandler {
         public MyNegotiationHandler() {
             super(ApplicationProtocolNames.HTTP_1_1);
         }
    
         protected void configurePipeline(ChannelHandlerContext ctx, String protocol) {
             if (ApplicationProtocolNames.HTTP_2.equals(protocol) {
                 configureHttp2(ctx);
             } else if (ApplicationProtocolNames.HTTP_1_1.equals(protocol)) {
                 configureHttp1(ctx);
             } else {
                 throw new IllegalStateException("unknown protocol: " + protocol);
             }
         }
     }
     
    • Constructor Detail

      • ApplicationProtocolNegotiationHandler

        protected ApplicationProtocolNegotiationHandler​(String fallbackProtocol)
        Creates a new instance with the specified fallback protocol name.
        Parameters:
        fallbackProtocol - the name of the protocol to use when ALPN/NPN negotiation fails or the client does not support ALPN/NPN
    • Method Detail

      • handlerAdded

        public void handlerAdded​(io.netty5.channel.ChannelHandlerContext ctx)
                          throws Exception
        Specified by:
        handlerAdded in interface io.netty5.channel.ChannelHandler
        Throws:
        Exception
      • handlerRemoved

        public void handlerRemoved​(io.netty5.channel.ChannelHandlerContext ctx)
                            throws Exception
        Specified by:
        handlerRemoved in interface io.netty5.channel.ChannelHandler
        Throws:
        Exception
      • channelRead

        public void channelRead​(io.netty5.channel.ChannelHandlerContext ctx,
                                Object msg)
                         throws Exception
        Specified by:
        channelRead in interface io.netty5.channel.ChannelHandler
        Throws:
        Exception
      • channelInboundEvent

        public void channelInboundEvent​(io.netty5.channel.ChannelHandlerContext ctx,
                                        Object evt)
                                 throws Exception
        Specified by:
        channelInboundEvent in interface io.netty5.channel.ChannelHandler
        Throws:
        Exception
      • channelShutdown

        public void channelShutdown​(io.netty5.channel.ChannelHandlerContext ctx,
                                    io.netty5.channel.ChannelShutdownDirection direction)
        Specified by:
        channelShutdown in interface io.netty5.channel.ChannelHandler
      • channelInactive

        public void channelInactive​(io.netty5.channel.ChannelHandlerContext ctx)
                             throws Exception
        Specified by:
        channelInactive in interface io.netty5.channel.ChannelHandler
        Throws:
        Exception
      • configurePipeline

        protected abstract void configurePipeline​(io.netty5.channel.ChannelHandlerContext ctx,
                                                  String protocol)
                                           throws Exception
        Invoked on successful initial SSL/TLS handshake. Implement this method to configure your pipeline for the negotiated application-level protocol.
        Parameters:
        protocol - the name of the negotiated application-level protocol, or the fallback protocol name specified in the constructor call if negotiation failed or the client isn't aware of ALPN/NPN extension
        Throws:
        Exception
      • handshakeFailure

        protected void handshakeFailure​(io.netty5.channel.ChannelHandlerContext ctx,
                                        Throwable cause)
                                 throws Exception
        Invoked on failed initial SSL/TLS handshake.
        Throws:
        Exception
      • channelExceptionCaught

        public void channelExceptionCaught​(io.netty5.channel.ChannelHandlerContext ctx,
                                           Throwable cause)
                                    throws Exception
        Specified by:
        channelExceptionCaught in interface io.netty5.channel.ChannelHandler
        Throws:
        Exception