Package io.netty.handler.codec
Class MessageAggregator<I,S,C extends ByteBufHolder,O extends ByteBufHolder>
java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.netty.handler.codec.MessageToMessageDecoder<I>
io.netty.handler.codec.MessageAggregator<I,S,C,O>
- Type Parameters:
I
- the type that covers both start message and content messageS
- the type of the start messageC
- the type of the content message (must be a subtype ofByteBufHolder
)O
- the type of the aggregated message (must be a subtype ofS
andByteBufHolder
)
- All Implemented Interfaces:
ChannelHandler
,ChannelInboundHandler
- Direct Known Subclasses:
AbstractMemcacheObjectAggregator
,HttpObjectAggregator
,RedisBulkStringAggregator
,StompSubframeAggregator
,WebSocketFrameAggregator
public abstract class MessageAggregator<I,S,C extends ByteBufHolder,O extends ByteBufHolder>
extends MessageToMessageDecoder<I>
An abstract
ChannelHandler
that aggregates a series of message objects into a single aggregated message.
'A series of messages' is composed of the following:
- a single start message which optionally contains the first part of the content, and
- 1 or more content messages.
isLastContentMessage(ByteBufHolder)
return true
for, the aggregator will finish the aggregation and produce the aggregated message and expect
another start message.
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
private ChannelFutureListener
private ChannelHandlerContext
private O
private static final int
private boolean
private boolean
private final int
private int
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
MessageAggregator
(int maxContentLength) Creates a new instance.protected
MessageAggregator
(int maxContentLength, Class<? extends I> inboundMessageType) -
Method Summary
Modifier and TypeMethodDescriptionboolean
Returnstrue
if the given message should be handled.protected void
Transfers the information provided by the specified content message to the specified aggregated message.private static void
appendPartialContent
(CompositeByteBuf content, ByteBuf partialContent) protected abstract O
beginAggregation
(S start, ByteBuf content) Creates a new aggregated message from the specified start message and the specified content.void
CallsChannelHandlerContext.fireChannelInactive()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.void
CallsChannelHandlerContext.fireChannelReadComplete()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.protected abstract boolean
Determine if the channel should be closed after the result ofnewContinueResponse(Object, int, ChannelPipeline)
is written.protected final ChannelHandlerContext
ctx()
protected void
decode
(ChannelHandlerContext ctx, I msg, List<Object> out) Decode from one message to an other.protected void
finishAggregation
(O aggregated) Invoked when the specifiedaggregated
message is about to be passed to the next handler in the pipeline.private void
finishAggregation0
(O aggregated) protected void
handleOversizedMessage
(ChannelHandlerContext ctx, S oversized) Invoked when an incoming request exceeds the maximum content length.void
Do nothing by default, sub-classes may override this method.void
Do nothing by default, sub-classes may override this method.protected abstract boolean
Determine if all objects for the current request/response should be ignored or not.private void
invokeHandleOversizedMessage
(ChannelHandlerContext ctx, S oversized) protected abstract boolean
isAggregated
(I msg) Returnstrue
if and only if the specified message is already aggregated.protected abstract boolean
isContentLengthInvalid
(S start, int maxContentLength) Determine if the messagestart
's content length is known, and if it greater thanmaxContentLength
.protected abstract boolean
isContentMessage
(I msg) Returnstrue
if and only if the specified message is a content message.final boolean
Deprecated.This method will be removed in future releases.protected abstract boolean
isLastContentMessage
(C msg) Returnstrue
if and only if the specified message is the last content message.protected abstract boolean
isStartMessage
(I msg) Returnstrue
if and only if the specified message is a start message.final int
Returns the maximum allowed length of the aggregated message in bytes.final int
Returns the maximum number of components in the cumulation buffer.protected abstract Object
newContinueResponse
(S start, int maxContentLength, ChannelPipeline pipeline) Returns the 'continue response' for the specified start message if necessary.protected final void
final void
setMaxCumulationBufferComponents
(int maxCumulationBufferComponents) Sets the maximum number of components in the cumulation buffer.private static void
validateMaxContentLength
(int maxContentLength) Methods inherited from class io.netty.handler.codec.MessageToMessageDecoder
channelRead
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, isSharable
-
Field Details
-
DEFAULT_MAX_COMPOSITEBUFFER_COMPONENTS
private static final int DEFAULT_MAX_COMPOSITEBUFFER_COMPONENTS- See Also:
-
maxContentLength
private final int maxContentLength -
currentMessage
-
handlingOversizedMessage
private boolean handlingOversizedMessage -
maxCumulationBufferComponents
private int maxCumulationBufferComponents -
ctx
-
continueResponseWriteListener
-
aggregating
private boolean aggregating -
handleIncompleteAggregateDuringClose
private boolean handleIncompleteAggregateDuringClose
-
-
Constructor Details
-
MessageAggregator
protected MessageAggregator(int maxContentLength) Creates a new instance.- Parameters:
maxContentLength
- the maximum length of the aggregated content. If the length of the aggregated content exceeds this value,handleOversizedMessage(ChannelHandlerContext, Object)
will be called.
-
MessageAggregator
-
-
Method Details
-
validateMaxContentLength
private static void validateMaxContentLength(int maxContentLength) -
acceptInboundMessage
Description copied from class:MessageToMessageDecoder
Returnstrue
if the given message should be handled. Iffalse
it will be passed to the nextChannelInboundHandler
in theChannelPipeline
.- Overrides:
acceptInboundMessage
in classMessageToMessageDecoder<I>
- Throws:
Exception
-
isStartMessage
Returnstrue
if and only if the specified message is a start message. Typically, this method is implemented as a singlereturn
statement withinstanceof
:return msg instanceof MyStartMessage;
- Throws:
Exception
-
isContentMessage
Returnstrue
if and only if the specified message is a content message. Typically, this method is implemented as a singlereturn
statement withinstanceof
:return msg instanceof MyContentMessage;
- Throws:
Exception
-
isLastContentMessage
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 invalid input: '&'invalid input: '&' msg.isLastFragment();
- Throws:
Exception
-
isAggregated
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.- Throws:
Exception
-
maxContentLength
public final int maxContentLength()Returns the maximum allowed length of the aggregated message in bytes. -
maxCumulationBufferComponents
public final int maxCumulationBufferComponents()Returns the maximum number of components in the cumulation buffer. If the number of the components in the cumulation buffer exceeds this value, the components of the cumulation buffer are consolidated into a single component, involving memory copies. The default value of this property is 1024<I,S, C extends ByteBufHolder, O extends ByteBufHolder>. -
setMaxCumulationBufferComponents
public final void setMaxCumulationBufferComponents(int maxCumulationBufferComponents) Sets the maximum number of components in the cumulation buffer. If the number of the components in the cumulation buffer exceeds this value, the components of the cumulation buffer are consolidated into a single component, involving memory copies. The default value of this property is 1024<I,S, C extends ByteBufHolder, O extends ByteBufHolder> and its minimum allowed value is 2
. -
isHandlingOversizedMessage
Deprecated.This method will be removed in future releases. -
ctx
-
decode
Description copied from class:MessageToMessageDecoder
Decode from one message to an other. This method will be called for each written message that can be handled by this decoder.- Specified by:
decode
in classMessageToMessageDecoder<I>
- Parameters:
ctx
- theChannelHandlerContext
which thisMessageToMessageDecoder
belongs tomsg
- the message to decode to an other oneout
- theList
to which decoded messages should be added- Throws:
Exception
- is thrown if an error occurs
-
appendPartialContent
-
isContentLengthInvalid
Determine if the messagestart
's content length is known, and if it greater thanmaxContentLength
.- 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.- Throws:
Exception
-
newContinueResponse
protected abstract Object newContinueResponse(S start, int maxContentLength, ChannelPipeline pipeline) throws Exception Returns the 'continue response' for the specified start message if necessary. For example, this method is useful to handle an HTTP 100-continue header.- Returns:
- the 'continue response', or
null
if there's no message to send - Throws:
Exception
-
closeAfterContinueResponse
Determine if the channel should be closed after the result ofnewContinueResponse(Object, int, ChannelPipeline)
is written.- Parameters:
msg
- The return value fromnewContinueResponse(Object, int, ChannelPipeline)
.- Returns:
true
if the channel should be closed after the result ofnewContinueResponse(Object, int, ChannelPipeline)
is written.false
otherwise.- Throws:
Exception
-
ignoreContentAfterContinueResponse
Determine if all objects for the current request/response should be ignored or not. Messages will stop being ignored the next timeisContentMessage(Object)
returnstrue
.- Parameters:
msg
- The return value fromnewContinueResponse(Object, int, ChannelPipeline)
.- Returns:
true
if all objects for the current request/response should be ignored or not.false
otherwise.- Throws:
Exception
-
beginAggregation
Creates 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
.- Throws:
Exception
-
aggregate
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 toaggregated
.- Throws:
Exception
-
finishAggregation0
- Throws:
Exception
-
finishAggregation
Invoked when the specifiedaggregated
message is about to be passed to the next handler in the pipeline.- Throws:
Exception
-
invokeHandleOversizedMessage
- Throws:
Exception
-
handleOversizedMessage
Invoked when an incoming request exceeds the maximum content length. The default behvaior is to trigger anexceptionCaught()
event with aTooLongFrameException
.- Parameters:
ctx
- theChannelHandlerContext
oversized
- the accumulated message up to this point, whose type isS
orO
- Throws:
Exception
-
channelReadComplete
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelReadComplete()
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelReadComplete
in interfaceChannelInboundHandler
- Overrides:
channelReadComplete
in classMessageToMessageDecoder<I>
- Throws:
Exception
-
channelInactive
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelInactive()
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelInactive
in interfaceChannelInboundHandler
- Overrides:
channelInactive
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
handlerAdded
Description copied from class:ChannelHandlerAdapter
Do nothing by default, sub-classes may override this method.- Specified by:
handlerAdded
in interfaceChannelHandler
- Overrides:
handlerAdded
in classChannelHandlerAdapter
- Throws:
Exception
-
handlerRemoved
Description copied from class:ChannelHandlerAdapter
Do nothing by default, sub-classes may override this method.- Specified by:
handlerRemoved
in interfaceChannelHandler
- Overrides:
handlerRemoved
in classChannelHandlerAdapter
- Throws:
Exception
-
releaseCurrentMessage
protected final void releaseCurrentMessage()
-