public class HttpObjectAggregator extends MessageAggregator<HttpObject,HttpMessage,HttpContent,FullHttpMessage>
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 |
| Modifier and Type | Class and Description |
|---|---|
private static class |
HttpObjectAggregator.AggregatedFullHttpMessage |
private static class |
HttpObjectAggregator.AggregatedFullHttpRequest |
private static class |
HttpObjectAggregator.AggregatedFullHttpResponse |
ChannelHandler.Sharable| Modifier and Type | Field and Description |
|---|---|
private boolean |
closeOnExpectationFailed |
private static FullHttpResponse |
CONTINUE |
private static FullHttpResponse |
EXPECTATION_FAILED |
private static InternalLogger |
logger |
private static FullHttpResponse |
TOO_LARGE |
private static FullHttpResponse |
TOO_LARGE_CLOSE |
| Constructor and Description |
|---|
HttpObjectAggregator(int maxContentLength)
Creates a new instance.
|
HttpObjectAggregator(int maxContentLength,
boolean closeOnExpectationFailed)
Creates a new instance.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
aggregate(FullHttpMessage aggregated,
HttpContent content)
Transfers the information provided by the specified content message to the specified aggregated message.
|
protected FullHttpMessage |
beginAggregation(HttpMessage start,
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
MessageAggregator.newContinueResponse(Object, int, ChannelPipeline) is written. |
protected void |
finishAggregation(FullHttpMessage aggregated)
Invoked when the specified
aggregated message is about to be passed to the next handler in the pipeline. |
protected void |
handleOversizedMessage(ChannelHandlerContext ctx,
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(HttpObject msg)
Returns
true if and only if the specified message is already aggregated. |
protected boolean |
isContentLengthInvalid(HttpMessage start,
int maxContentLength)
Determine if the message
start's content length is known, and if it greater than
maxContentLength. |
protected boolean |
isContentMessage(HttpObject msg)
Returns
true if and only if the specified message is a content message. |
protected boolean |
isLastContentMessage(HttpContent msg)
Returns
true if and only if the specified message is the last content message. |
protected boolean |
isStartMessage(HttpObject msg)
Returns
true if and only if the specified message is a start message. |
protected java.lang.Object |
newContinueResponse(HttpMessage start,
int maxContentLength,
ChannelPipeline pipeline)
Returns the 'continue response' for the specified start message if necessary.
|
acceptInboundMessage, channelInactive, channelReadComplete, ctx, decode, handlerAdded, handlerRemoved, isHandlingOversizedMessage, maxContentLength, maxCumulationBufferComponents, setMaxCumulationBufferComponentschannelReadchannelActive, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggeredensureNotSharable, isSharableprivate static final InternalLogger logger
private static final FullHttpResponse CONTINUE
private static final FullHttpResponse EXPECTATION_FAILED
private static final FullHttpResponse TOO_LARGE_CLOSE
private static final FullHttpResponse TOO_LARGE
private final boolean closeOnExpectationFailed
public HttpObjectAggregator(int maxContentLength)
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.public HttpObjectAggregator(int maxContentLength,
boolean closeOnExpectationFailed)
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.protected boolean isStartMessage(HttpObject msg) throws java.lang.Exception
MessageAggregatortrue 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;
isStartMessage in class MessageAggregator<HttpObject,HttpMessage,HttpContent,FullHttpMessage>java.lang.Exceptionprotected boolean isContentMessage(HttpObject msg) throws java.lang.Exception
MessageAggregatortrue 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;
isContentMessage in class MessageAggregator<HttpObject,HttpMessage,HttpContent,FullHttpMessage>java.lang.Exceptionprotected boolean isLastContentMessage(HttpContent msg) throws java.lang.Exception
MessageAggregatortrue 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();
isLastContentMessage in class MessageAggregator<HttpObject,HttpMessage,HttpContent,FullHttpMessage>java.lang.Exceptionprotected boolean isAggregated(HttpObject msg) throws java.lang.Exception
MessageAggregatortrue 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.isAggregated in class MessageAggregator<HttpObject,HttpMessage,HttpContent,FullHttpMessage>java.lang.Exceptionprotected boolean isContentLengthInvalid(HttpMessage start, int maxContentLength)
MessageAggregatorstart's content length is known, and if it greater than
maxContentLength.isContentLengthInvalid in class MessageAggregator<HttpObject,HttpMessage,HttpContent,FullHttpMessage>start - The message which may indicate the content length.maxContentLength - The maximum allowed content length.true if the message start's content length is known, and if it greater than
maxContentLength. false otherwise.protected java.lang.Object newContinueResponse(HttpMessage start, int maxContentLength, ChannelPipeline pipeline)
MessageAggregatornewContinueResponse in class MessageAggregator<HttpObject,HttpMessage,HttpContent,FullHttpMessage>null if there's no message to sendprotected boolean closeAfterContinueResponse(java.lang.Object msg)
MessageAggregatorMessageAggregator.newContinueResponse(Object, int, ChannelPipeline) is written.closeAfterContinueResponse in class MessageAggregator<HttpObject,HttpMessage,HttpContent,FullHttpMessage>msg - The return value from MessageAggregator.newContinueResponse(Object, int, ChannelPipeline).true if the channel should be closed after the result of
MessageAggregator.newContinueResponse(Object, int, ChannelPipeline) is written. false otherwise.protected boolean ignoreContentAfterContinueResponse(java.lang.Object msg)
MessageAggregatorMessageAggregator.isContentMessage(Object) returns true.ignoreContentAfterContinueResponse in class MessageAggregator<HttpObject,HttpMessage,HttpContent,FullHttpMessage>msg - The return value from MessageAggregator.newContinueResponse(Object, int, ChannelPipeline).true if all objects for the current request/response should be ignored or not.
false otherwise.protected FullHttpMessage beginAggregation(HttpMessage start, ByteBuf content) throws java.lang.Exception
MessageAggregatorByteBufHolder, its content is appended to the specified content.
This aggregator will continue to append the received content to the specified content.beginAggregation in class MessageAggregator<HttpObject,HttpMessage,HttpContent,FullHttpMessage>java.lang.Exceptionprotected void aggregate(FullHttpMessage aggregated, HttpContent content) throws java.lang.Exception
MessageAggregatoraggregated.aggregate in class MessageAggregator<HttpObject,HttpMessage,HttpContent,FullHttpMessage>java.lang.Exceptionprotected void finishAggregation(FullHttpMessage aggregated) throws java.lang.Exception
MessageAggregatoraggregated message is about to be passed to the next handler in the pipeline.finishAggregation in class MessageAggregator<HttpObject,HttpMessage,HttpContent,FullHttpMessage>java.lang.Exceptionprotected void handleOversizedMessage(ChannelHandlerContext ctx, HttpMessage oversized) throws java.lang.Exception
MessageAggregatorexceptionCaught() event with a TooLongFrameException.handleOversizedMessage in class MessageAggregator<HttpObject,HttpMessage,HttpContent,FullHttpMessage>ctx - the ChannelHandlerContextoversized - the accumulated message up to this point, whose type is S or Ojava.lang.Exception