public class HttpObjectAggregator extends MessageAggregator<HttpObject,HttpMessage,HttpContent,FullHttpMessage>
ChannelHandler
that aggregates an HttpMessage
and its following HttpContent
s into a single FullHttpRequest
or FullHttpResponse
(depending on if it used to handle requests or responses)
with no following HttpContent
s. 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", 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 |
ChannelHandler.Sharable
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(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(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 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, setMaxCumulationBufferComponents
channelRead
channelActive, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
ensureNotSharable, isSharable
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 Exception
MessageAggregator
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;
isStartMessage
in class MessageAggregator<HttpObject,HttpMessage,HttpContent,FullHttpMessage>
Exception
protected boolean isContentMessage(HttpObject msg) throws Exception
MessageAggregator
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;
isContentMessage
in class MessageAggregator<HttpObject,HttpMessage,HttpContent,FullHttpMessage>
Exception
protected boolean isLastContentMessage(HttpContent msg) throws Exception
MessageAggregator
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();
isLastContentMessage
in class MessageAggregator<HttpObject,HttpMessage,HttpContent,FullHttpMessage>
Exception
protected boolean isAggregated(HttpObject msg) throws Exception
MessageAggregator
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.isAggregated
in class MessageAggregator<HttpObject,HttpMessage,HttpContent,FullHttpMessage>
Exception
protected boolean isContentLengthInvalid(HttpMessage start, int maxContentLength)
MessageAggregator
start
'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 Object newContinueResponse(HttpMessage start, int maxContentLength, ChannelPipeline pipeline)
MessageAggregator
newContinueResponse
in class MessageAggregator<HttpObject,HttpMessage,HttpContent,FullHttpMessage>
null
if there's no message to sendprotected boolean closeAfterContinueResponse(Object msg)
MessageAggregator
MessageAggregator.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(Object msg)
MessageAggregator
MessageAggregator.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 Exception
MessageAggregator
ByteBufHolder
, 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>
Exception
protected void aggregate(FullHttpMessage aggregated, HttpContent content) throws Exception
MessageAggregator
aggregated
.aggregate
in class MessageAggregator<HttpObject,HttpMessage,HttpContent,FullHttpMessage>
Exception
protected void finishAggregation(FullHttpMessage aggregated) throws Exception
MessageAggregator
aggregated
message is about to be passed to the next handler in the pipeline.finishAggregation
in class MessageAggregator<HttpObject,HttpMessage,HttpContent,FullHttpMessage>
Exception
protected void handleOversizedMessage(ChannelHandlerContext ctx, HttpMessage oversized) throws Exception
MessageAggregator
exceptionCaught()
event with a TooLongFrameException
.handleOversizedMessage
in class MessageAggregator<HttpObject,HttpMessage,HttpContent,FullHttpMessage>
ctx
- the ChannelHandlerContext
oversized
- the accumulated message up to this point, whose type is S
or O
Exception
Copyright © 2008–2024 The Netty Project. All rights reserved.