Class AbstractNettyWebSocketHandler

  • All Implemented Interfaces:
    io.netty.channel.ChannelHandler, io.netty.channel.ChannelInboundHandler

    @Internal
    public abstract class AbstractNettyWebSocketHandler
    extends io.netty.channel.SimpleChannelInboundHandler<java.lang.Object>
    Abstract implementation that handles WebSocket frames.
    Since:
    1.0
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler

        io.netty.channel.ChannelHandler.Sharable
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String ID
      The id of the handler used when adding it to the Netty pipeline.
      protected org.slf4j.Logger LOG  
      protected io.micronaut.http.codec.MediaTypeCodecRegistry mediaTypeCodecRegistry  
      protected io.micronaut.inject.MethodExecutionHandle<?,​?> messageHandler  
      protected io.micronaut.http.HttpRequest<?> originatingRequest  
      protected io.micronaut.inject.MethodExecutionHandle<?,​?> pongHandler  
      protected java.lang.String subProtocol  
      protected java.util.Map<java.lang.String,​java.lang.Object> uriVariables  
      protected io.micronaut.websocket.context.WebSocketBean<?> webSocketBean  
      protected io.micronaut.core.bind.ArgumentBinderRegistry<io.micronaut.websocket.bind.WebSocketState> webSocketBinder  
      protected WebSocketSessionRepository webSocketSessionRepository  
      protected io.netty.handler.codec.http.websocketx.WebSocketVersion webSocketVersion  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected AbstractNettyWebSocketHandler​(io.netty.channel.ChannelHandlerContext ctx, io.micronaut.http.bind.RequestBinderRegistry binderRegistry, io.micronaut.http.codec.MediaTypeCodecRegistry mediaTypeCodecRegistry, io.micronaut.websocket.context.WebSocketBean<?> webSocketBean, io.micronaut.http.HttpRequest<?> request, java.util.Map<java.lang.String,​java.lang.Object> uriVariables, io.netty.handler.codec.http.websocketx.WebSocketVersion version, java.lang.String subProtocol, WebSocketSessionRepository webSocketSessionRepository)
      Default constructor.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      protected org.reactivestreams.Publisher<?> callOpenMethod​(io.netty.channel.ChannelHandlerContext ctx)
      Calls the open method of the websocket bean.
      protected void channelRead0​(io.netty.channel.ChannelHandlerContext ctx, java.lang.Object msg)  
      protected abstract NettyWebSocketSession createWebSocketSession​(io.netty.channel.ChannelHandlerContext ctx)
      Subclasses should implement to create the actual NettyWebSocketSession.
      void exceptionCaught​(io.netty.channel.ChannelHandlerContext ctx, java.lang.Throwable cause)  
      protected void forwardErrorToUser​(io.netty.channel.ChannelHandlerContext ctx, java.util.function.Consumer<java.lang.Throwable> fallback, java.lang.Throwable cause)  
      abstract io.micronaut.core.type.Argument<?> getBodyArgument()  
      abstract io.micronaut.core.type.Argument<?> getPongArgument()  
      abstract NettyWebSocketSession getSession()  
      protected void handleCloseReason​(io.netty.channel.ChannelHandlerContext ctx, io.micronaut.websocket.CloseReason cr, boolean writeCloseReason)
      Used to close the session with a given reason.
      void handlerRemoved​(io.netty.channel.ChannelHandlerContext ctx)  
      protected void handleWebSocketFrame​(io.netty.channel.ChannelHandlerContext ctx, io.netty.handler.codec.http.websocketx.WebSocketFrame msg)
      Handles WebSocket frame request.
      protected org.reactivestreams.Publisher<?> instrumentPublisher​(io.netty.channel.ChannelHandlerContext ctx, java.lang.Object result)
      Subclasses can override to customize publishers returned from message handlers.
      protected java.lang.Object invokeExecutable​(io.micronaut.core.bind.BoundExecutable boundExecutable, io.micronaut.inject.MethodExecutionHandle<?,​?> messageHandler)
      Invokes the given executable.
      protected void messageHandled​(io.netty.channel.ChannelHandlerContext ctx, java.lang.Object message)
      Method called once a message has been handled by the handler.
      protected void writeCloseFrameAndTerminate​(io.netty.channel.ChannelHandlerContext ctx, io.micronaut.websocket.CloseReason closeReason)
      Writes the give close reason and terminates the session.
      • Methods inherited from class io.netty.channel.SimpleChannelInboundHandler

        acceptInboundMessage, channelRead
      • Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter

        channelActive, channelInactive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, userEventTriggered
      • Methods inherited from class io.netty.channel.ChannelHandlerAdapter

        ensureNotSharable, handlerAdded, isSharable
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface io.netty.channel.ChannelHandler

        handlerAdded
    • Field Detail

      • ID

        public static final java.lang.String ID
        The id of the handler used when adding it to the Netty pipeline.
        See Also:
        Constant Field Values
      • LOG

        protected final org.slf4j.Logger LOG
      • webSocketBinder

        protected final io.micronaut.core.bind.ArgumentBinderRegistry<io.micronaut.websocket.bind.WebSocketState> webSocketBinder
      • uriVariables

        protected final java.util.Map<java.lang.String,​java.lang.Object> uriVariables
      • webSocketBean

        protected final io.micronaut.websocket.context.WebSocketBean<?> webSocketBean
      • originatingRequest

        protected final io.micronaut.http.HttpRequest<?> originatingRequest
      • messageHandler

        protected final io.micronaut.inject.MethodExecutionHandle<?,​?> messageHandler
      • pongHandler

        protected final io.micronaut.inject.MethodExecutionHandle<?,​?> pongHandler
      • mediaTypeCodecRegistry

        protected final io.micronaut.http.codec.MediaTypeCodecRegistry mediaTypeCodecRegistry
      • webSocketVersion

        protected final io.netty.handler.codec.http.websocketx.WebSocketVersion webSocketVersion
      • subProtocol

        protected final java.lang.String subProtocol
    • Constructor Detail

      • AbstractNettyWebSocketHandler

        protected AbstractNettyWebSocketHandler​(io.netty.channel.ChannelHandlerContext ctx,
                                                io.micronaut.http.bind.RequestBinderRegistry binderRegistry,
                                                io.micronaut.http.codec.MediaTypeCodecRegistry mediaTypeCodecRegistry,
                                                io.micronaut.websocket.context.WebSocketBean<?> webSocketBean,
                                                io.micronaut.http.HttpRequest<?> request,
                                                java.util.Map<java.lang.String,​java.lang.Object> uriVariables,
                                                io.netty.handler.codec.http.websocketx.WebSocketVersion version,
                                                java.lang.String subProtocol,
                                                WebSocketSessionRepository webSocketSessionRepository)
        Default constructor.
        Parameters:
        ctx - The channel handler context
        binderRegistry - The request binder registry
        mediaTypeCodecRegistry - The codec registry
        webSocketBean - The websocket bean
        request - The originating request
        uriVariables - The URI variables
        version - The websocket version being used
        subProtocol - The handler sub-protocol
        webSocketSessionRepository - The web socket repository if they are supported (like on the server), null otherwise
    • Method Detail

      • callOpenMethod

        protected org.reactivestreams.Publisher<?> callOpenMethod​(io.netty.channel.ChannelHandlerContext ctx)
        Calls the open method of the websocket bean.
        Parameters:
        ctx - The handler context
        Returns:
        Publisher for any errors, or the result of the open method
      • getBodyArgument

        public abstract io.micronaut.core.type.Argument<?> getBodyArgument()
        Returns:
        The body argument for the message handler
      • getPongArgument

        public abstract io.micronaut.core.type.Argument<?> getPongArgument()
        Returns:
        The pong argument for the pong handler
      • exceptionCaught

        public void exceptionCaught​(io.netty.channel.ChannelHandlerContext ctx,
                                    java.lang.Throwable cause)
        Specified by:
        exceptionCaught in interface io.netty.channel.ChannelHandler
        Specified by:
        exceptionCaught in interface io.netty.channel.ChannelInboundHandler
        Overrides:
        exceptionCaught in class io.netty.channel.ChannelInboundHandlerAdapter
      • forwardErrorToUser

        protected final void forwardErrorToUser​(io.netty.channel.ChannelHandlerContext ctx,
                                                java.util.function.Consumer<java.lang.Throwable> fallback,
                                                java.lang.Throwable cause)
      • handlerRemoved

        public void handlerRemoved​(io.netty.channel.ChannelHandlerContext ctx)
                            throws java.lang.Exception
        Specified by:
        handlerRemoved in interface io.netty.channel.ChannelHandler
        Overrides:
        handlerRemoved in class io.netty.channel.ChannelHandlerAdapter
        Throws:
        java.lang.Exception
      • createWebSocketSession

        protected abstract NettyWebSocketSession createWebSocketSession​(io.netty.channel.ChannelHandlerContext ctx)
        Subclasses should implement to create the actual NettyWebSocketSession.
        Parameters:
        ctx - The context
        Returns:
        The session
      • instrumentPublisher

        protected org.reactivestreams.Publisher<?> instrumentPublisher​(io.netty.channel.ChannelHandlerContext ctx,
                                                                       java.lang.Object result)
        Subclasses can override to customize publishers returned from message handlers.
        Parameters:
        ctx - The context
        result - The result
        Returns:
        The flowable
      • invokeExecutable

        protected java.lang.Object invokeExecutable​(io.micronaut.core.bind.BoundExecutable boundExecutable,
                                                    io.micronaut.inject.MethodExecutionHandle<?,​?> messageHandler)
        Invokes the given executable.
        Parameters:
        boundExecutable - The bound executable
        messageHandler - The message handler
        Returns:
        The result
      • channelRead0

        protected void channelRead0​(io.netty.channel.ChannelHandlerContext ctx,
                                    java.lang.Object msg)
        Specified by:
        channelRead0 in class io.netty.channel.SimpleChannelInboundHandler<java.lang.Object>
      • handleWebSocketFrame

        protected void handleWebSocketFrame​(io.netty.channel.ChannelHandlerContext ctx,
                                            io.netty.handler.codec.http.websocketx.WebSocketFrame msg)
        Handles WebSocket frame request.
        Parameters:
        ctx - The context
        msg - The frame
      • messageHandled

        protected void messageHandled​(io.netty.channel.ChannelHandlerContext ctx,
                                      java.lang.Object message)
        Method called once a message has been handled by the handler.
        Parameters:
        ctx - The channel handler context
        message - The message that was handled
      • writeCloseFrameAndTerminate

        protected void writeCloseFrameAndTerminate​(io.netty.channel.ChannelHandlerContext ctx,
                                                   io.micronaut.websocket.CloseReason closeReason)
        Writes the give close reason and terminates the session.
        Parameters:
        ctx - The context
        closeReason - The reason
      • handleCloseReason

        protected void handleCloseReason​(io.netty.channel.ChannelHandlerContext ctx,
                                         io.micronaut.websocket.CloseReason cr,
                                         boolean writeCloseReason)
        Used to close the session with a given reason.
        Parameters:
        ctx - The context
        cr - The reason
        writeCloseReason - Whether to allow writing the close reason to the remote