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 of ByteBufHolder
)O
- the type of the aggregated message (must be a subtype of S
and ByteBufHolder
)public abstract class MessageAggregator<I,S,C extends ByteBufHolder,O extends ByteBufHolder> extends MessageToMessageDecoder<I>
ChannelHandler
that aggregates a series of message objects into a single aggregated message.
'A series of messages' is composed of the following:
isLastContentMessage(ByteBufHolder)
return true
for, the aggregator will finish the aggregation and produce the aggregated message and expect
another start message.
ChannelHandler.Sharable, ChannelHandler.Skip
Modifier | Constructor and Description |
---|---|
protected |
MessageAggregator(int maxContentLength)
Creates a new instance.
|
protected |
MessageAggregator(int maxContentLength,
Class<? extends I> inboundMessageType) |
Modifier and Type | Method and Description |
---|---|
boolean |
acceptInboundMessage(Object msg)
Returns
true if the given message should be handled. |
protected void |
aggregate(O aggregated,
C content)
Transfers the information provided by the specified content message to the specified aggregated message.
|
protected abstract O |
beginAggregation(S start,
ByteBuf content)
Creates a new aggregated message from the specified start message and the specified content.
|
void |
channelInactive(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.fireChannelInactive() to forward
to the next ChannelHandler in the ChannelPipeline . |
protected abstract long |
contentLength(S start)
Retrieves the length of the whole content from the specified start message.
|
protected 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 specified
aggregated message is about to be passed to the next handler in the pipeline. |
protected void |
handleOversizedMessage(ChannelHandlerContext ctx,
S oversized)
Invoked when an incoming request exceeds the maximum content length.
|
void |
handlerAdded(ChannelHandlerContext ctx)
Do nothing by default, sub-classes may override this method.
|
void |
handlerRemoved(ChannelHandlerContext ctx)
Do nothing by default, sub-classes may override this method.
|
protected abstract boolean |
hasContentLength(S start)
Returns
true if and only if the specified start message already contains the information about the
length of the whole content. |
protected abstract boolean |
isAggregated(I msg)
Returns
true if and only if the specified message is already aggregated. |
protected abstract boolean |
isContentMessage(I msg)
Returns
true if and only if the specified message is a content message. |
boolean |
isHandlingOversizedMessage() |
protected abstract boolean |
isLastContentMessage(C msg)
Returns
true if and only if the specified message is the last content message. |
protected abstract boolean |
isStartMessage(I msg)
Returns
true if and only if the specified message is a start message. |
int |
maxContentLength()
Returns the maximum allowed length of the aggregated message.
|
int |
maxCumulationBufferComponents()
Returns the maximum number of components in the cumulation buffer.
|
protected abstract Object |
newContinueResponse(S start)
Returns the 'continue response' for the specified start message if necessary.
|
void |
setMaxCumulationBufferComponents(int maxCumulationBufferComponents)
Sets the maximum number of components in the cumulation buffer.
|
channelRead
bind, channelActive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, close, connect, deregister, disconnect, exceptionCaught, flush, isSharable, read, userEventTriggered, write
protected MessageAggregator(int maxContentLength)
maxContentLength
- the maximum length of the aggregated content.
If the length of the aggregated content exceeds this value,
handleOversizedMessage(ChannelHandlerContext, Object)
will be called.public boolean acceptInboundMessage(Object msg) throws Exception
MessageToMessageDecoder
true
if the given message should be handled. If false
it will be passed to the next
ChannelHandler
in the ChannelPipeline
.acceptInboundMessage
in class MessageToMessageDecoder<I>
Exception
protected abstract boolean isStartMessage(I msg) throws Exception
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;
Exception
protected abstract boolean isContentMessage(I msg) throws Exception
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;
Exception
protected abstract boolean isLastContentMessage(C msg) throws Exception
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();
Exception
protected abstract boolean isAggregated(I msg) throws Exception
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.Exception
public final int maxContentLength()
public final int maxCumulationBufferComponents()
public final void setMaxCumulationBufferComponents(int maxCumulationBufferComponents)
2
.public final boolean isHandlingOversizedMessage()
protected final ChannelHandlerContext ctx()
protected void decode(ChannelHandlerContext ctx, I msg, List<Object> out) throws Exception
MessageToMessageDecoder
decode
in class MessageToMessageDecoder<I>
ctx
- the ChannelHandlerContext
which this MessageToMessageDecoder
belongs tomsg
- the message to decode to an other oneout
- the List
to which decoded messages should be addedException
- is thrown if an error accourprotected abstract boolean hasContentLength(S start) throws Exception
true
if and only if the specified start message already contains the information about the
length of the whole content.Exception
protected abstract long contentLength(S start) throws Exception
hasContentLength(Object)
returned true
.Exception
protected abstract Object newContinueResponse(S start) throws Exception
null
if there's no message to sendException
protected abstract O beginAggregation(S start, ByteBuf content) throws Exception
ByteBufHolder
, its content is appended to the specified content
.
This aggregator will continue to append the received content to the specified content
.Exception
protected void aggregate(O aggregated, C content) throws Exception
aggregated
.Exception
protected void finishAggregation(O aggregated) throws Exception
aggregated
message is about to be passed to the next handler in the pipeline.Exception
protected void handleOversizedMessage(ChannelHandlerContext ctx, S oversized) throws Exception
exceptionCaught()
event with a TooLongFrameException
.ctx
- the ChannelHandlerContext
oversized
- the accumulated message up to this point, whose type is S
or O
Exception
public void channelInactive(ChannelHandlerContext ctx) throws Exception
ChannelHandlerAdapter
ChannelHandlerContext.fireChannelInactive()
to forward
to the next ChannelHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelInactive
in interface ChannelHandler
channelInactive
in class ChannelHandlerAdapter
Exception
public void handlerAdded(ChannelHandlerContext ctx) throws Exception
ChannelHandlerAdapter
handlerAdded
in interface ChannelHandler
handlerAdded
in class ChannelHandlerAdapter
Exception
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception
ChannelHandlerAdapter
handlerRemoved
in interface ChannelHandler
handlerRemoved
in class ChannelHandlerAdapter
Exception
Copyright © 2008–2015 The Netty Project. All rights reserved.