Class EspressoHttpObjectAggregator

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

    public class EspressoHttpObjectAggregator
    extends EspressoMessageAggregator<io.netty.handler.codec.http.HttpObject,​io.netty.handler.codec.http.HttpMessage,​io.netty.handler.codec.http.HttpContent,​io.netty.handler.codec.http.FullHttpMessage>
    Forked from Netty 4.1.42.Final. Main change is to extend EspressoMessageAggregator to avoid creating TypeParameterMatcher. A ChannelHandler that aggregates an HttpMessage and its following HttpContents into a single FullHttpRequest or FullHttpResponse (depending on if it used to handle requests or responses) with no following HttpContents. It is useful when you don't want to take care of HTTP messages whose transfer encoding is 'chunked'. Insert this handler after HttpResponseDecoder in the ChannelPipeline if being used to handle responses, or after HttpRequestDecoder and HttpResponseEncoder in the ChannelPipeline if being used to handle requests.
      ChannelPipeline p = ...;
      ...
      p.addLast("decoder", new HttpRequestDecoder());
      p.addLast("encoder", new HttpResponseEncoder());
      p.addLast("aggregator", new HttpObjectAggregator(1048576));
      ...
      p.addLast("handler", new HttpRequestHandler());
      

    For convenience, consider putting a HttpServerCodec before the HttpObjectAggregator as it functions as both a HttpRequestDecoder and a HttpResponseEncoder.

    Be aware that HttpObjectAggregator may end up sending a HttpResponse:
    Response Status Condition When Sent
    100 Continue A '100-continue' expectation is received and the 'content-length' doesn't exceed maxContentLength
    417 Expectation Failed A '100-continue' expectation is received and the 'content-length' exceeds maxContentLength
    413 Request Entity Too Large Either the 'content-length' or the bytes received so far exceed maxContentLength
    See Also:
    FullHttpRequest, FullHttpResponse, HttpResponseDecoder, HttpServerCodec
    • Nested Class Summary

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

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

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void aggregate​(io.netty.handler.codec.http.FullHttpMessage aggregated, io.netty.handler.codec.http.HttpContent content)
      Transfers the information provided by the specified content message to the specified aggregated message.
      protected io.netty.handler.codec.http.FullHttpMessage beginAggregation​(io.netty.handler.codec.http.HttpMessage start, io.netty.buffer.ByteBuf content)
      Creates a new aggregated message from the specified start message and the specified content.
      protected boolean closeAfterContinueResponse​(java.lang.Object msg)
      Determine if the channel should be closed after the result of EspressoMessageAggregator.newContinueResponse(Object, int, ChannelPipeline) is written.
      protected void finishAggregation​(io.netty.handler.codec.http.FullHttpMessage 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, io.netty.handler.codec.http.HttpMessage oversized)
      Invoked when an incoming request exceeds the maximum content length.
      protected boolean ignoreContentAfterContinueResponse​(java.lang.Object msg)
      Determine if all objects for the current request/response should be ignored or not.
      protected boolean isAggregated​(io.netty.handler.codec.http.HttpObject msg)
      Returns true if and only if the specified message is already aggregated.
      protected boolean isContentLengthInvalid​(io.netty.handler.codec.http.HttpMessage start, int maxContentLength)
      Determine if the message start's content length is known, and if it greater than maxContentLength.
      protected boolean isContentMessage​(io.netty.handler.codec.http.HttpObject msg)
      Returns true if and only if the specified message is a content message.
      protected boolean isLastContentMessage​(io.netty.handler.codec.http.HttpContent msg)
      Returns true if and only if the specified message is the last content message.
      protected boolean isStartMessage​(io.netty.handler.codec.http.HttpObject msg)
      Returns true if and only if the specified message is a start message.
      protected java.lang.Object newContinueResponse​(io.netty.handler.codec.http.HttpMessage start, int maxContentLength, io.netty.channel.ChannelPipeline pipeline)
      Returns the 'continue response' for the specified start message if necessary.
      • 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

      • EspressoHttpObjectAggregator

        public EspressoHttpObjectAggregator​(int maxContentLength)
        Creates a new instance.
        Parameters:
        maxContentLength - the maximum length of the aggregated content in bytes. If the length of the aggregated content exceeds this value, handleOversizedMessage(ChannelHandlerContext, HttpMessage) will be called.
      • EspressoHttpObjectAggregator

        public EspressoHttpObjectAggregator​(int maxContentLength,
                                            boolean closeOnExpectationFailed)
        Creates a new instance.
        Parameters:
        maxContentLength - the maximum length of the aggregated content in bytes. If the length of the aggregated content exceeds this value, handleOversizedMessage(ChannelHandlerContext, HttpMessage) will be called.
        closeOnExpectationFailed - If a 100-continue response is detected but the content length is too large then true means close the connection. otherwise the connection will remain open and data will be consumed and discarded until the next request is received.
    • Method Detail

      • isStartMessage

        protected boolean isStartMessage​(io.netty.handler.codec.http.HttpObject msg)
                                  throws java.lang.Exception
        Description copied from class: EspressoMessageAggregator
        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;
         
        Specified by:
        isStartMessage in class EspressoMessageAggregator<io.netty.handler.codec.http.HttpObject,​io.netty.handler.codec.http.HttpMessage,​io.netty.handler.codec.http.HttpContent,​io.netty.handler.codec.http.FullHttpMessage>
        Throws:
        java.lang.Exception
      • isContentMessage

        protected boolean isContentMessage​(io.netty.handler.codec.http.HttpObject msg)
                                    throws java.lang.Exception
        Description copied from class: EspressoMessageAggregator
        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;
         
        Specified by:
        isContentMessage in class EspressoMessageAggregator<io.netty.handler.codec.http.HttpObject,​io.netty.handler.codec.http.HttpMessage,​io.netty.handler.codec.http.HttpContent,​io.netty.handler.codec.http.FullHttpMessage>
        Throws:
        java.lang.Exception
      • isLastContentMessage

        protected boolean isLastContentMessage​(io.netty.handler.codec.http.HttpContent msg)
                                        throws java.lang.Exception
        Description copied from class: EspressoMessageAggregator
        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();
         
        Specified by:
        isLastContentMessage in class EspressoMessageAggregator<io.netty.handler.codec.http.HttpObject,​io.netty.handler.codec.http.HttpMessage,​io.netty.handler.codec.http.HttpContent,​io.netty.handler.codec.http.FullHttpMessage>
        Throws:
        java.lang.Exception
      • isAggregated

        protected boolean isAggregated​(io.netty.handler.codec.http.HttpObject msg)
                                throws java.lang.Exception
        Description copied from class: EspressoMessageAggregator
        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.
        Specified by:
        isAggregated in class EspressoMessageAggregator<io.netty.handler.codec.http.HttpObject,​io.netty.handler.codec.http.HttpMessage,​io.netty.handler.codec.http.HttpContent,​io.netty.handler.codec.http.FullHttpMessage>
        Throws:
        java.lang.Exception
      • isContentLengthInvalid

        protected boolean isContentLengthInvalid​(io.netty.handler.codec.http.HttpMessage start,
                                                 int maxContentLength)
        Description copied from class: EspressoMessageAggregator
        Determine if the message start's content length is known, and if it greater than maxContentLength.
        Specified by:
        isContentLengthInvalid in class EspressoMessageAggregator<io.netty.handler.codec.http.HttpObject,​io.netty.handler.codec.http.HttpMessage,​io.netty.handler.codec.http.HttpContent,​io.netty.handler.codec.http.FullHttpMessage>
        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.
      • newContinueResponse

        protected java.lang.Object newContinueResponse​(io.netty.handler.codec.http.HttpMessage start,
                                                       int maxContentLength,
                                                       io.netty.channel.ChannelPipeline pipeline)
        Description copied from class: EspressoMessageAggregator
        Returns the 'continue response' for the specified start message if necessary. For example, this method is useful to handle an HTTP 100-continue header.
        Specified by:
        newContinueResponse in class EspressoMessageAggregator<io.netty.handler.codec.http.HttpObject,​io.netty.handler.codec.http.HttpMessage,​io.netty.handler.codec.http.HttpContent,​io.netty.handler.codec.http.FullHttpMessage>
        Returns:
        the 'continue response', or null if there's no message to send
      • beginAggregation

        protected io.netty.handler.codec.http.FullHttpMessage beginAggregation​(io.netty.handler.codec.http.HttpMessage start,
                                                                               io.netty.buffer.ByteBuf content)
                                                                        throws java.lang.Exception
        Description copied from class: EspressoMessageAggregator
        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.
        Specified by:
        beginAggregation in class EspressoMessageAggregator<io.netty.handler.codec.http.HttpObject,​io.netty.handler.codec.http.HttpMessage,​io.netty.handler.codec.http.HttpContent,​io.netty.handler.codec.http.FullHttpMessage>
        Throws:
        java.lang.Exception
      • aggregate

        protected void aggregate​(io.netty.handler.codec.http.FullHttpMessage aggregated,
                                 io.netty.handler.codec.http.HttpContent content)
                          throws java.lang.Exception
        Description copied from class: EspressoMessageAggregator
        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.
        Overrides:
        aggregate in class EspressoMessageAggregator<io.netty.handler.codec.http.HttpObject,​io.netty.handler.codec.http.HttpMessage,​io.netty.handler.codec.http.HttpContent,​io.netty.handler.codec.http.FullHttpMessage>
        Throws:
        java.lang.Exception
      • finishAggregation

        protected void finishAggregation​(io.netty.handler.codec.http.FullHttpMessage aggregated)
                                  throws java.lang.Exception
        Description copied from class: EspressoMessageAggregator
        Invoked when the specified aggregated message is about to be passed to the next handler in the pipeline.
        Overrides:
        finishAggregation in class EspressoMessageAggregator<io.netty.handler.codec.http.HttpObject,​io.netty.handler.codec.http.HttpMessage,​io.netty.handler.codec.http.HttpContent,​io.netty.handler.codec.http.FullHttpMessage>
        Throws:
        java.lang.Exception
      • handleOversizedMessage

        protected void handleOversizedMessage​(io.netty.channel.ChannelHandlerContext ctx,
                                              io.netty.handler.codec.http.HttpMessage oversized)
                                       throws java.lang.Exception
        Description copied from class: EspressoMessageAggregator
        Invoked when an incoming request exceeds the maximum content length. The default behvaior is to trigger an exceptionCaught() event with a TooLongFrameException.
        Overrides:
        handleOversizedMessage in class EspressoMessageAggregator<io.netty.handler.codec.http.HttpObject,​io.netty.handler.codec.http.HttpMessage,​io.netty.handler.codec.http.HttpContent,​io.netty.handler.codec.http.FullHttpMessage>
        Parameters:
        ctx - the ChannelHandlerContext
        oversized - the accumulated message up to this point, whose type is S or O
        Throws:
        java.lang.Exception