Class EspressoMessageAggregator<I,​S,​C extends io.netty.buffer.ByteBufHolder,​O extends io.netty.buffer.ByteBufHolder>

  • Type Parameters:
    I - the type that covers both start message and content message
    S - the type of the start message
    C - the type of the content message (must be a subtype of ByteBufHolder)
    O - the type of the aggregated message (must be a subtype of S and ByteBufHolder)
    All Implemented Interfaces:
    io.netty.channel.ChannelHandler, io.netty.channel.ChannelInboundHandler
    Direct Known Subclasses:
    EspressoHttpObjectAggregator

    public abstract class EspressoMessageAggregator<I,​S,​C extends io.netty.buffer.ByteBufHolder,​O extends io.netty.buffer.ByteBufHolder>
    extends EspressoMessageToMessageDecoder<I>
    Forked from Netty 4.1.42.Final. Main change is to avoid creating TypeParameterMatcher. An abstract ChannelHandler that aggregates a series of message objects into a single aggregated message.

    'A series of messages' is composed of the following:

    • a single start message which optionally contains the first part of the content, and
    • 1 or more content messages.
    The content of the aggregated message will be the merged content of the start message and its following content messages. If this aggregator encounters a content message where isLastContentMessage(ByteBufHolder) return true for, the aggregator will finish the aggregation and produce the aggregated message and expect another start message.

    • Nested Class Summary

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

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

      Constructors 
      Modifier Constructor Description
      protected EspressoMessageAggregator​(int maxContentLength)
      Creates a new instance.
      protected EspressoMessageAggregator​(int maxContentLength, java.lang.Class<? extends I> inboundMessageType)  
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      boolean acceptInboundMessage​(java.lang.Object msg)
      Returns true if the given message should be handled.
      protected void aggregate​(O aggregated, C content)
      Transfers the information provided by the specified content message to the specified aggregated message.
      protected abstract O beginAggregation​(S start, io.netty.buffer.ByteBuf content)
      Creates a new aggregated message from the specified start message and the specified content.
      void channelInactive​(io.netty.channel.ChannelHandlerContext ctx)  
      void channelReadComplete​(io.netty.channel.ChannelHandlerContext ctx)  
      protected abstract boolean closeAfterContinueResponse​(java.lang.Object msg)
      Determine if the channel should be closed after the result of newContinueResponse(Object, int, ChannelPipeline) is written.
      protected io.netty.channel.ChannelHandlerContext ctx()  
      protected void decode​(io.netty.channel.ChannelHandlerContext ctx, I msg, java.util.List<java.lang.Object> out)
      Decode from one message to an other.
      protected void finishAggregation​(O aggregated)
      Invoked when the specified aggregated message is about to be passed to the next handler in the pipeline.
      protected void handleOversizedMessage​(io.netty.channel.ChannelHandlerContext ctx, S oversized)
      Invoked when an incoming request exceeds the maximum content length.
      void handlerAdded​(io.netty.channel.ChannelHandlerContext ctx)  
      void handlerRemoved​(io.netty.channel.ChannelHandlerContext ctx)  
      protected abstract boolean ignoreContentAfterContinueResponse​(java.lang.Object msg)
      Determine if all objects for the current request/response should be ignored or not.
      protected abstract boolean isAggregated​(I msg)
      Returns true if and only if the specified message is already aggregated.
      protected abstract boolean isContentLengthInvalid​(S start, int maxContentLength)
      Determine if the message start's content length is known, and if it greater than maxContentLength.
      protected abstract boolean isContentMessage​(I msg)
      Returns true if and only if the specified message is a content message.
      boolean isHandlingOversizedMessage()
      Deprecated.
      This method will be removed in future releases.
      protected abstract boolean isLastContentMessage​(C msg)
      Returns true if and only if the specified message is the last content message.
      protected abstract boolean isStartMessage​(I msg)
      Returns true if and only if the specified message is a start message.
      int maxContentLength()
      Returns the maximum allowed length of the aggregated message in bytes.
      int maxCumulationBufferComponents()
      Returns the maximum number of components in the cumulation buffer.
      protected abstract java.lang.Object newContinueResponse​(S start, int maxContentLength, io.netty.channel.ChannelPipeline pipeline)
      Returns the 'continue response' for the specified start message if necessary.
      void setMaxCumulationBufferComponents​(int maxCumulationBufferComponents)
      Sets the maximum number of components in the cumulation buffer.
      • Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter

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

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

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • EspressoMessageAggregator

        protected EspressoMessageAggregator​(int maxContentLength)
        Creates a new instance.
        Parameters:
        maxContentLength - the maximum length of the aggregated content. If the length of the aggregated content exceeds this value, handleOversizedMessage(ChannelHandlerContext, Object) will be called.
      • EspressoMessageAggregator

        protected EspressoMessageAggregator​(int maxContentLength,
                                            java.lang.Class<? extends I> inboundMessageType)
    • Method Detail

      • acceptInboundMessage

        public boolean acceptInboundMessage​(java.lang.Object msg)
                                     throws java.lang.Exception
        Description copied from class: EspressoMessageToMessageDecoder
        Returns true if the given message should be handled. If false it will be passed to the next ChannelInboundHandler in the ChannelPipeline.
        Overrides:
        acceptInboundMessage in class EspressoMessageToMessageDecoder<I>
        Throws:
        java.lang.Exception
      • isStartMessage

        protected abstract boolean isStartMessage​(I msg)
                                           throws java.lang.Exception
        Returns true if and only if the specified message is a start message. Typically, this method is implemented as a single return statement with instanceof:
         return msg instanceof MyStartMessage;
         
        Throws:
        java.lang.Exception
      • isContentMessage

        protected abstract boolean isContentMessage​(I msg)
                                             throws java.lang.Exception
        Returns true if and only if the specified message is a content message. Typically, this method is implemented as a single return statement with instanceof:
         return msg instanceof MyContentMessage;
         
        Throws:
        java.lang.Exception
      • isLastContentMessage

        protected abstract boolean isLastContentMessage​(C msg)
                                                 throws java.lang.Exception
        Returns true if and only if the specified message is the last content message. Typically, this method is implemented as a single return statement with instanceof:
         return msg instanceof MyLastContentMessage;
         
        or with instanceof and boolean field check:
         return msg instanceof MyContentMessage && msg.isLastFragment();
         
        Throws:
        java.lang.Exception
      • isAggregated

        protected abstract boolean isAggregated​(I msg)
                                         throws java.lang.Exception
        Returns true if and only if the specified message is already aggregated. If this method returns true, this handler will simply forward the message to the next handler as-is.
        Throws:
        java.lang.Exception
      • maxContentLength

        public final int maxContentLength()
        Returns the maximum allowed length of the aggregated message in bytes.
      • maxCumulationBufferComponents

        public final int maxCumulationBufferComponents()
        Returns the maximum number of components in the cumulation buffer. If the number of the components in the cumulation buffer exceeds this value, the components of the cumulation buffer are consolidated into a single component, involving memory copies. The default value of this property is 1024.
      • setMaxCumulationBufferComponents

        public final void setMaxCumulationBufferComponents​(int maxCumulationBufferComponents)
        Sets the maximum number of components in the cumulation buffer. If the number of the components in the cumulation buffer exceeds this value, the components of the cumulation buffer are consolidated into a single component, involving memory copies. The default value of this property is 1024 and its minimum allowed value is 2.
      • isHandlingOversizedMessage

        @Deprecated
        public final boolean isHandlingOversizedMessage()
        Deprecated.
        This method will be removed in future releases.
      • ctx

        protected final io.netty.channel.ChannelHandlerContext ctx()
      • decode

        protected void decode​(io.netty.channel.ChannelHandlerContext ctx,
                              I msg,
                              java.util.List<java.lang.Object> out)
                       throws java.lang.Exception
        Description copied from class: EspressoMessageToMessageDecoder
        Decode from one message to an other. This method will be called for each written message that can be handled by this decoder.
        Specified by:
        decode in class EspressoMessageToMessageDecoder<I>
        Parameters:
        ctx - the ChannelHandlerContext which this MessageToMessageDecoder belongs to
        msg - the message to decode to an other one
        out - the List to which decoded messages should be added
        Throws:
        java.lang.Exception - is thrown if an error occurs
      • isContentLengthInvalid

        protected abstract boolean isContentLengthInvalid​(S start,
                                                          int maxContentLength)
                                                   throws java.lang.Exception
        Determine if the message start's content length is known, and if it greater than maxContentLength.
        Parameters:
        start - The message which may indicate the content length.
        maxContentLength - The maximum allowed content length.
        Returns:
        true if the message start's content length is known, and if it greater than maxContentLength. false otherwise.
        Throws:
        java.lang.Exception
      • newContinueResponse

        protected abstract java.lang.Object newContinueResponse​(S start,
                                                                int maxContentLength,
                                                                io.netty.channel.ChannelPipeline pipeline)
                                                         throws java.lang.Exception
        Returns the 'continue response' for the specified start message if necessary. For example, this method is useful to handle an HTTP 100-continue header.
        Returns:
        the 'continue response', or null if there's no message to send
        Throws:
        java.lang.Exception
      • ignoreContentAfterContinueResponse

        protected abstract boolean ignoreContentAfterContinueResponse​(java.lang.Object msg)
                                                               throws java.lang.Exception
        Determine if all objects for the current request/response should be ignored or not. Messages will stop being ignored the next time isContentMessage(Object) returns true.
        Parameters:
        msg - The return value from newContinueResponse(Object, int, ChannelPipeline).
        Returns:
        true if all objects for the current request/response should be ignored or not. false otherwise.
        Throws:
        java.lang.Exception
      • beginAggregation

        protected abstract O beginAggregation​(S start,
                                              io.netty.buffer.ByteBuf content)
                                       throws java.lang.Exception
        Creates a new aggregated message from the specified start message and the specified content. If the start message implements ByteBufHolder, its content is appended to the specified content. This aggregator will continue to append the received content to the specified content.
        Throws:
        java.lang.Exception
      • aggregate

        protected void aggregate​(O aggregated,
                                 C content)
                          throws java.lang.Exception
        Transfers the information provided by the specified content message to the specified aggregated message. Note that the content of the specified content message has been appended to the content of the specified aggregated message already, so that you don't need to. Use this method to transfer the additional information that the content message provides to aggregated.
        Throws:
        java.lang.Exception
      • finishAggregation

        protected void finishAggregation​(O aggregated)
                                  throws java.lang.Exception
        Invoked when the specified aggregated message is about to be passed to the next handler in the pipeline.
        Throws:
        java.lang.Exception
      • handleOversizedMessage

        protected void handleOversizedMessage​(io.netty.channel.ChannelHandlerContext ctx,
                                              S oversized)
                                       throws java.lang.Exception
        Invoked when an incoming request exceeds the maximum content length. The default behvaior is to trigger an exceptionCaught() event with a TooLongFrameException.
        Parameters:
        ctx - the ChannelHandlerContext
        oversized - the accumulated message up to this point, whose type is S or O
        Throws:
        java.lang.Exception
      • channelReadComplete

        public void channelReadComplete​(io.netty.channel.ChannelHandlerContext ctx)
                                 throws java.lang.Exception
        Specified by:
        channelReadComplete in interface io.netty.channel.ChannelInboundHandler
        Overrides:
        channelReadComplete in class io.netty.channel.ChannelInboundHandlerAdapter
        Throws:
        java.lang.Exception
      • channelInactive

        public void channelInactive​(io.netty.channel.ChannelHandlerContext ctx)
                             throws java.lang.Exception
        Specified by:
        channelInactive in interface io.netty.channel.ChannelInboundHandler
        Overrides:
        channelInactive in class io.netty.channel.ChannelInboundHandlerAdapter
        Throws:
        java.lang.Exception
      • handlerAdded

        public void handlerAdded​(io.netty.channel.ChannelHandlerContext ctx)
                          throws java.lang.Exception
        Specified by:
        handlerAdded in interface io.netty.channel.ChannelHandler
        Overrides:
        handlerAdded in class io.netty.channel.ChannelHandlerAdapter
        Throws:
        java.lang.Exception
      • 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