Class MessageAggregator<I,​S,​C extends AutoCloseable,​A extends AutoCloseable>

  • 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
    A - the type of the aggregated message
    All Implemented Interfaces:
    ChannelHandler
    Direct Known Subclasses:
    HttpObjectAggregator, WebSocketFrameAggregator

    public abstract class MessageAggregator<I,​S,​C extends AutoCloseable,​A extends AutoCloseable>
    extends MessageToMessageDecoder<I>
    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(AutoCloseable) return true for, the aggregator will finish the aggregation and produce the aggregated message and expect another start message.

    • Constructor Detail

      • MessageAggregator

        protected MessageAggregator​(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.
      • MessageAggregator

        protected MessageAggregator​(int maxContentLength,
                                    Class<? extends I> inboundMessageType)
    • Method Detail

      • tryStartMessage

        protected abstract S tryStartMessage​(Object msg)
        If the passed msg is a start message then cast and return the same, else return null.
      • tryContentMessage

        protected abstract C tryContentMessage​(Object msg)
        If the passed msg is a content message then cast and return the same, else return null.
      • isLastContentMessage

        protected abstract boolean isLastContentMessage​(C msg)
                                                 throws 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:
        Exception
      • isAggregated

        protected abstract boolean isAggregated​(Object msg)
                                         throws 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:
        Exception
      • lengthForContent

        protected abstract int lengthForContent​(C msg)
        Returns the length in bytes of the passed message.
        Parameters:
        msg - to calculate length.
        Returns:
        Length in bytes of the passed message.
      • lengthForAggregation

        protected abstract int lengthForAggregation​(A msg)
        Returns the length in bytes of the passed message.
        Parameters:
        msg - to calculate length.
        Returns:
        Length in bytes of the passed message.
      • maxContentLength

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

        protected abstract boolean isContentLengthInvalid​(S start,
                                                          int maxContentLength)
                                                   throws 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:
        Exception
      • newContinueResponse

        protected abstract Object newContinueResponse​(S start,
                                                      int maxContentLength,
                                                      ChannelPipeline pipeline)
                                               throws 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:
        Exception
      • ignoreContentAfterContinueResponse

        protected abstract boolean ignoreContentAfterContinueResponse​(Object msg)
                                                               throws Exception
        Determine if all objects for the current request/response should be ignored or not. Messages will stop being ignored the next time tryContentMessage(Object) returns a non null value.
        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:
        Exception
      • beginAggregation

        protected abstract A beginAggregation​(BufferAllocator allocator,
                                              S start)
                                       throws Exception
        Creates a new aggregated message from the specified start message.
        Throws:
        Exception
      • aggregate

        protected abstract void aggregate​(BufferAllocator allocator,
                                          A aggregated,
                                          C content)
                                   throws Exception
        Aggregated the passed content in the passed aggregate.
        Throws:
        Exception
      • finishAggregation

        protected void finishAggregation​(BufferAllocator allocator,
                                         A aggregated)
                                  throws Exception
        Invoked when the specified aggregated message is about to be passed to the next handler in the pipeline.
        Throws:
        Exception