Class HttpObjectAggregator
java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.netty.handler.codec.MessageToMessageDecoder<HttpObject>
io.netty.handler.codec.MessageAggregator<HttpObject, HttpMessage, HttpContent, FullHttpMessage>
io.netty.handler.codec.http.HttpObjectAggregator
- All Implemented Interfaces:
ChannelHandler, ChannelInboundHandler
- Direct Known Subclasses:
HttpClientUpgradeHandler, HttpServerUpgradeHandler
public class HttpObjectAggregator
extends MessageAggregator<HttpObject, HttpMessage, HttpContent, FullHttpMessage>
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.
ChannelPipelinep = ...; ... p.addLast("decoder", newHttpRequestDecoder()); p.addLast("encoder", newHttpResponseEncoder()); p.addLast("aggregator", newHttpObjectAggregator(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.
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:
-
Nested Class Summary
Nested classes/interfaces inherited from interface ChannelHandler
ChannelHandler.Sharable -
Constructor Summary
ConstructorsConstructorDescriptionHttpObjectAggregator(int maxContentLength) Creates a new instance.HttpObjectAggregator(int maxContentLength, boolean closeOnExpectationFailed) Creates a new instance. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaggregate(FullHttpMessage aggregated, HttpContent content) Transfers the information provided by the specified content message to the specified aggregated message.protected FullHttpMessagebeginAggregation(HttpMessage start, ByteBuf content) Creates a new aggregated message from the specified start message and the specified content.protected booleanDetermine if the channel should be closed after the result ofMessageAggregator.newContinueResponse(Object, int, ChannelPipeline)is written.protected voidfinishAggregation(FullHttpMessage aggregated) Invoked when the specifiedaggregatedmessage is about to be passed to the next handler in the pipeline.protected voidhandleOversizedMessage(ChannelHandlerContext ctx, HttpMessage oversized) Invoked when an incoming request exceeds the maximum content length.protected booleanDetermine if all objects for the current request/response should be ignored or not.protected booleanisAggregated(HttpObject msg) Returnstrueif and only if the specified message is already aggregated.protected booleanisContentLengthInvalid(HttpMessage start, int maxContentLength) Determine if the messagestart's content length is known, and if it greater thanmaxContentLength.protected booleanReturnstrueif and only if the specified message is a content message.protected booleanReturnstrueif and only if the specified message is the last content message.protected booleanisStartMessage(HttpObject msg) Returnstrueif and only if the specified message is a start message.protected ObjectnewContinueResponse(HttpMessage start, int maxContentLength, ChannelPipeline pipeline) Returns the 'continue response' for the specified start message if necessary.Methods inherited from class MessageAggregator
acceptInboundMessage, channelInactive, channelReadComplete, ctx, decode, handlerAdded, handlerRemoved, isHandlingOversizedMessage, maxContentLength, maxCumulationBufferComponents, releaseCurrentMessage, setMaxCumulationBufferComponentsMethods inherited from class MessageToMessageDecoder
channelReadMethods inherited from class ChannelInboundHandlerAdapter
channelActive, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggeredMethods inherited from class ChannelHandlerAdapter
ensureNotSharable, isSharable
-
Constructor Details
-
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, HttpMessage)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, HttpMessage)will be called.closeOnExpectationFailed- If a 100-continue response is detected but the content length is too large thentruemeans close the connection. otherwise the connection will remain open and data will be consumed and discarded until the next request is received.
-
-
Method Details
-
isStartMessage
Description copied from class:MessageAggregatorReturnstrueif and only if the specified message is a start message. Typically, this method is implemented as a singlereturnstatement withinstanceof:return msg instanceof MyStartMessage;
- Specified by:
isStartMessagein classMessageAggregator<HttpObject, HttpMessage, HttpContent, FullHttpMessage>- Throws:
Exception
-
isContentMessage
Description copied from class:MessageAggregatorReturnstrueif and only if the specified message is a content message. Typically, this method is implemented as a singlereturnstatement withinstanceof:return msg instanceof MyContentMessage;
- Specified by:
isContentMessagein classMessageAggregator<HttpObject, HttpMessage, HttpContent, FullHttpMessage>- Throws:
Exception
-
isLastContentMessage
Description copied from class:MessageAggregatorReturnstrueif and only if the specified message is the last content message. Typically, this method is implemented as a singlereturnstatement withinstanceof:return msg instanceof MyLastContentMessage;
or withinstanceofand boolean field check:return msg instanceof MyContentMessage invalid input: '&'invalid input: '&' msg.isLastFragment();
- Specified by:
isLastContentMessagein classMessageAggregator<HttpObject, HttpMessage, HttpContent, FullHttpMessage>- Throws:
Exception
-
isAggregated
Description copied from class:MessageAggregatorReturnstrueif 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:
isAggregatedin classMessageAggregator<HttpObject, HttpMessage, HttpContent, FullHttpMessage>- Throws:
Exception
-
isContentLengthInvalid
Description copied from class:MessageAggregatorDetermine if the messagestart's content length is known, and if it greater thanmaxContentLength.- Specified by:
isContentLengthInvalidin classMessageAggregator<HttpObject, HttpMessage, HttpContent, FullHttpMessage>- Parameters:
start- The message which may indicate the content length.maxContentLength- The maximum allowed content length.- Returns:
trueif the messagestart's content length is known, and if it greater thanmaxContentLength.falseotherwise.
-
newContinueResponse
protected Object newContinueResponse(HttpMessage start, int maxContentLength, ChannelPipeline pipeline) Description copied from class:MessageAggregatorReturns 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:
newContinueResponsein classMessageAggregator<HttpObject, HttpMessage, HttpContent, FullHttpMessage>- Returns:
- the 'continue response', or
nullif there's no message to send
-
closeAfterContinueResponse
Description copied from class:MessageAggregatorDetermine if the channel should be closed after the result ofMessageAggregator.newContinueResponse(Object, int, ChannelPipeline)is written.- Specified by:
closeAfterContinueResponsein classMessageAggregator<HttpObject, HttpMessage, HttpContent, FullHttpMessage>- Parameters:
msg- The return value fromMessageAggregator.newContinueResponse(Object, int, ChannelPipeline).- Returns:
trueif the channel should be closed after the result ofMessageAggregator.newContinueResponse(Object, int, ChannelPipeline)is written.falseotherwise.
-
ignoreContentAfterContinueResponse
Description copied from class:MessageAggregatorDetermine if all objects for the current request/response should be ignored or not. Messages will stop being ignored the next timeMessageAggregator.isContentMessage(Object)returnstrue.- Specified by:
ignoreContentAfterContinueResponsein classMessageAggregator<HttpObject, HttpMessage, HttpContent, FullHttpMessage>- Parameters:
msg- The return value fromMessageAggregator.newContinueResponse(Object, int, ChannelPipeline).- Returns:
trueif all objects for the current request/response should be ignored or not.falseotherwise.
-
beginAggregation
Description copied from class:MessageAggregatorCreates a new aggregated message from the specified start message and the specified content. If the start message implementsByteBufHolder, its content is appended to the specifiedcontent. This aggregator will continue to append the received content to the specifiedcontent.- Specified by:
beginAggregationin classMessageAggregator<HttpObject, HttpMessage, HttpContent, FullHttpMessage>- Throws:
Exception
-
aggregate
Description copied from class:MessageAggregatorTransfers 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 toaggregated.- Overrides:
aggregatein classMessageAggregator<HttpObject, HttpMessage, HttpContent, FullHttpMessage>- Throws:
Exception
-
finishAggregation
Description copied from class:MessageAggregatorInvoked when the specifiedaggregatedmessage is about to be passed to the next handler in the pipeline.- Overrides:
finishAggregationin classMessageAggregator<HttpObject, HttpMessage, HttpContent, FullHttpMessage>- Throws:
Exception
-
handleOversizedMessage
protected void handleOversizedMessage(ChannelHandlerContext ctx, HttpMessage oversized) throws Exception Description copied from class:MessageAggregatorInvoked when an incoming request exceeds the maximum content length. The default behvaior is to trigger anexceptionCaught()event with aTooLongFrameException.- Overrides:
handleOversizedMessagein classMessageAggregator<HttpObject, HttpMessage, HttpContent, FullHttpMessage>- Parameters:
ctx- theChannelHandlerContextoversized- the accumulated message up to this point, whose type isSorO- Throws:
Exception
-