Module io.netty5.codec.http
Package io.netty5.handler.codec.http
Class HttpObjectAggregator<C extends HttpContent<C>>
- java.lang.Object
-
- io.netty5.channel.ChannelHandlerAdapter
-
- io.netty5.handler.codec.MessageToMessageDecoder<I>
-
- io.netty5.handler.codec.MessageAggregator<HttpObject,HttpMessage,HttpContent<C>,FullHttpMessage<?>>
-
- io.netty5.handler.codec.http.HttpObjectAggregator<C>
-
- All Implemented Interfaces:
ChannelHandler
- Direct Known Subclasses:
HttpClientUpgradeHandler
,HttpServerUpgradeHandler
public class HttpObjectAggregator<C extends HttpContent<C>> extends MessageAggregator<HttpObject,HttpMessage,HttpContent<C>,FullHttpMessage<?>>
AChannelHandler
that aggregates anHttpMessage
and its followingHttpContent
s into a singleFullHttpRequest
orFullHttpResponse
(depending on if it used to handle requests or responses) with no followingHttpContent
s. It is useful when you don't want to take care of HTTP messages whose transfer encoding is 'chunked'. Insert this handler afterHttpResponseDecoder
in theChannelPipeline
if being used to handle responses, or afterHttpRequestDecoder
andHttpResponseEncoder
in theChannelPipeline
if being used to handle requests.ChannelPipeline
p = ...; ... p.addLast("decoder", newHttpRequestDecoder
()); p.addLast("encoder", newHttpResponseEncoder
()); p.addLast("aggregator", newHttpObjectAggregator
(1048576)); ... p.addLast("handler", new HttpRequestHandler());For convenience, consider putting a
Be aware thatHttpServerCodec
before theHttpObjectAggregator
as it functions as both aHttpRequestDecoder
and aHttpResponseEncoder
.HttpObjectAggregator
may end up sending aHttpResponse
: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
-
-
Constructor Summary
Constructors Constructor Description HttpObjectAggregator(int maxContentLength)
Creates a new instance.HttpObjectAggregator(int maxContentLength, boolean closeOnExpectationFailed)
Creates a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
aggregate(BufferAllocator allocator, FullHttpMessage<?> aggregated, HttpContent<C> content)
Aggregated the passedcontent
in the passedaggregate
.protected FullHttpMessage<?>
beginAggregation(BufferAllocator allocator, HttpMessage start)
Creates a new aggregated message from the specified start message.void
channelExceptionCaught(ChannelHandlerContext ctx, Throwable cause)
Gets called if aThrowable
was thrown when handling inbound events.protected boolean
closeAfterContinueResponse(Object msg)
Determine if the channel should be closed after the result ofMessageAggregator.newContinueResponse(Object, int, ChannelPipeline)
is written.protected void
finishAggregation(BufferAllocator allocator, FullHttpMessage<?> aggregated)
Invoked when the specifiedaggregated
message is about to be passed to the next handler in the pipeline.protected void
handleOversizedMessage(ChannelHandlerContext ctx, Object oversized)
Invoked when an incoming request exceeds the maximum content length.protected boolean
ignoreContentAfterContinueResponse(Object msg)
Determine if all objects for the current request/response should be ignored or not.protected boolean
isAggregated(Object msg)
Returnstrue
if and only if the specified message is already aggregated.protected boolean
isContentLengthInvalid(HttpMessage start, int maxContentLength)
Determine if the messagestart
's content length is known, and if it greater thanmaxContentLength
.protected boolean
isLastContentMessage(HttpContent<C> msg)
Returnstrue
if and only if the specified message is the last content message.protected int
lengthForAggregation(FullHttpMessage<?> msg)
Returns the length in bytes of the passed message.protected int
lengthForContent(HttpContent<C> msg)
Returns the length in bytes of the passed message.protected Object
newContinueResponse(HttpMessage start, int maxContentLength, ChannelPipeline pipeline)
Returns the 'continue response' for the specified start message if necessary.protected HttpContent<C>
tryContentMessage(Object msg)
protected HttpMessage
tryStartMessage(Object msg)
-
Methods inherited from class io.netty5.handler.codec.MessageAggregator
acceptInboundMessage, channelInactive, channelReadComplete, ctx, decode, handlerAdded, handlerRemoved, maxContentLength
-
Methods inherited from class io.netty5.handler.codec.MessageToMessageDecoder
channelRead, decodeAndClose
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty5.channel.ChannelHandler
bind, channelActive, channelInboundEvent, channelRegistered, channelShutdown, channelUnregistered, channelWritabilityChanged, close, connect, deregister, disconnect, flush, isSharable, pendingOutboundBytes, read, register, sendOutboundEvent, shutdown, write
-
-
-
-
Constructor Detail
-
HttpObjectAggregator
public HttpObjectAggregator(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, Object)
will be called.
-
HttpObjectAggregator
public HttpObjectAggregator(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, Object)
will be called.closeOnExpectationFailed
- If a 100-continue response is detected but the content length is too large thentrue
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
-
tryStartMessage
protected HttpMessage tryStartMessage(Object msg)
Description copied from class:MessageAggregator
- Specified by:
tryStartMessage
in classMessageAggregator<HttpObject,HttpMessage,HttpContent<C extends HttpContent<C>>,FullHttpMessage<?>>
-
tryContentMessage
protected HttpContent<C> tryContentMessage(Object msg)
Description copied from class:MessageAggregator
- Specified by:
tryContentMessage
in classMessageAggregator<HttpObject,HttpMessage,HttpContent<C extends HttpContent<C>>,FullHttpMessage<?>>
-
isAggregated
protected boolean isAggregated(Object msg) throws Exception
Description copied from class:MessageAggregator
Returnstrue
if and only if the specified message is already aggregated. If this method returnstrue
, this handler will simply forward the message to the next handler as-is.- Specified by:
isAggregated
in classMessageAggregator<HttpObject,HttpMessage,HttpContent<C extends HttpContent<C>>,FullHttpMessage<?>>
- Throws:
Exception
-
lengthForContent
protected int lengthForContent(HttpContent<C> msg)
Description copied from class:MessageAggregator
Returns the length in bytes of the passed message.- Specified by:
lengthForContent
in classMessageAggregator<HttpObject,HttpMessage,HttpContent<C extends HttpContent<C>>,FullHttpMessage<?>>
- Parameters:
msg
- to calculate length.- Returns:
- Length in bytes of the passed message.
-
lengthForAggregation
protected int lengthForAggregation(FullHttpMessage<?> msg)
Description copied from class:MessageAggregator
Returns the length in bytes of the passed message.- Specified by:
lengthForAggregation
in classMessageAggregator<HttpObject,HttpMessage,HttpContent<C extends HttpContent<C>>,FullHttpMessage<?>>
- Parameters:
msg
- to calculate length.- Returns:
- Length in bytes of the passed message.
-
isLastContentMessage
protected boolean isLastContentMessage(HttpContent<C> msg) throws Exception
Description copied from class:MessageAggregator
Returnstrue
if and only if the specified message is the last content message. Typically, this method is implemented as a singlereturn
statement withinstanceof
:return msg instanceof MyLastContentMessage;
or withinstanceof
and boolean field check:return msg instanceof MyContentMessage && msg.isLastFragment();
- Specified by:
isLastContentMessage
in classMessageAggregator<HttpObject,HttpMessage,HttpContent<C extends HttpContent<C>>,FullHttpMessage<?>>
- Throws:
Exception
-
isContentLengthInvalid
protected boolean isContentLengthInvalid(HttpMessage start, int maxContentLength)
Description copied from class:MessageAggregator
Determine if the messagestart
's content length is known, and if it greater thanmaxContentLength
.- Specified by:
isContentLengthInvalid
in classMessageAggregator<HttpObject,HttpMessage,HttpContent<C extends HttpContent<C>>,FullHttpMessage<?>>
- Parameters:
start
- The message which may indicate the content length.maxContentLength
- The maximum allowed content length.- Returns:
true
if the messagestart
's content length is known, and if it greater thanmaxContentLength
.false
otherwise.
-
newContinueResponse
protected Object newContinueResponse(HttpMessage start, int maxContentLength, ChannelPipeline pipeline)
Description copied from class:MessageAggregator
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 classMessageAggregator<HttpObject,HttpMessage,HttpContent<C extends HttpContent<C>>,FullHttpMessage<?>>
- Returns:
- the 'continue response', or
null
if there's no message to send
-
closeAfterContinueResponse
protected boolean closeAfterContinueResponse(Object msg)
Description copied from class:MessageAggregator
Determine if the channel should be closed after the result ofMessageAggregator.newContinueResponse(Object, int, ChannelPipeline)
is written.- Specified by:
closeAfterContinueResponse
in classMessageAggregator<HttpObject,HttpMessage,HttpContent<C extends HttpContent<C>>,FullHttpMessage<?>>
- Parameters:
msg
- The return value fromMessageAggregator.newContinueResponse(Object, int, ChannelPipeline)
.- Returns:
true
if the channel should be closed after the result ofMessageAggregator.newContinueResponse(Object, int, ChannelPipeline)
is written.false
otherwise.
-
ignoreContentAfterContinueResponse
protected boolean ignoreContentAfterContinueResponse(Object msg)
Description copied from class:MessageAggregator
Determine if all objects for the current request/response should be ignored or not. Messages will stop being ignored the next timeMessageAggregator.tryContentMessage(Object)
returns anon null
value.- Specified by:
ignoreContentAfterContinueResponse
in classMessageAggregator<HttpObject,HttpMessage,HttpContent<C extends HttpContent<C>>,FullHttpMessage<?>>
- Parameters:
msg
- The return value fromMessageAggregator.newContinueResponse(Object, int, ChannelPipeline)
.- Returns:
true
if all objects for the current request/response should be ignored or not.false
otherwise.
-
beginAggregation
protected FullHttpMessage<?> beginAggregation(BufferAllocator allocator, HttpMessage start) throws Exception
Description copied from class:MessageAggregator
Creates a new aggregated message from the specified start message.- Specified by:
beginAggregation
in classMessageAggregator<HttpObject,HttpMessage,HttpContent<C extends HttpContent<C>>,FullHttpMessage<?>>
- Throws:
Exception
-
aggregate
protected void aggregate(BufferAllocator allocator, FullHttpMessage<?> aggregated, HttpContent<C> content) throws Exception
Description copied from class:MessageAggregator
Aggregated the passedcontent
in the passedaggregate
.- Specified by:
aggregate
in classMessageAggregator<HttpObject,HttpMessage,HttpContent<C extends HttpContent<C>>,FullHttpMessage<?>>
- Throws:
Exception
-
finishAggregation
protected void finishAggregation(BufferAllocator allocator, FullHttpMessage<?> aggregated) throws Exception
Description copied from class:MessageAggregator
Invoked when the specifiedaggregated
message is about to be passed to the next handler in the pipeline.- Overrides:
finishAggregation
in classMessageAggregator<HttpObject,HttpMessage,HttpContent<C extends HttpContent<C>>,FullHttpMessage<?>>
- Throws:
Exception
-
handleOversizedMessage
protected void handleOversizedMessage(ChannelHandlerContext ctx, Object oversized) throws Exception
Description copied from class:MessageAggregator
Invoked when an incoming request exceeds the maximum content length. The default behavior is to trigger anexceptionCaught()
event with aTooLongFrameException
.- Overrides:
handleOversizedMessage
in classMessageAggregator<HttpObject,HttpMessage,HttpContent<C extends HttpContent<C>>,FullHttpMessage<?>>
- Parameters:
ctx
- theChannelHandlerContext
oversized
- the accumulated message up to this point.- Throws:
Exception
-
channelExceptionCaught
public void channelExceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
Description copied from interface:ChannelHandler
Gets called if aThrowable
was thrown when handling inbound events.- Throws:
Exception
-
-