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
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 ChannelInboundHandler in the ChannelPipeline . |
void |
channelReadComplete(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.fireChannelReadComplete() to forward
to the next ChannelInboundHandler in the ChannelPipeline . |
protected abstract boolean |
closeAfterContinueResponse(Object msg)
Determine if the channel should be closed after the result of
newContinueResponse(Object, int, ChannelPipeline) is written. |
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 |
ignoreContentAfterContinueResponse(Object msg)
Determine if all objects for the current request/response should be ignored or not.
|
protected abstract boolean |
isAggregated(I msg)
Returns
true if and only if the specified message is already aggregated. |
protected abstract boolean |
isContentLengthInvalid(S start,
int maxContentLength)
Determine if the message
start 's content length is known, and if it greater than
maxContentLength . |
protected abstract boolean |
isContentMessage(I msg)
Returns
true if and only if the specified message is a content message. |
boolean |
isHandlingOversizedMessage()
Deprecated.
This method will be removed in future releases.
|
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 in bytes.
|
int |
maxCumulationBufferComponents()
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.
|
void |
setMaxCumulationBufferComponents(int maxCumulationBufferComponents)
Sets the maximum number of components in the cumulation buffer.
|
channelRead
channelActive, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
ensureNotSharable, isSharable
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
ChannelInboundHandler
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
.@Deprecated 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 occursprotected abstract boolean isContentLengthInvalid(S start, int maxContentLength) throws Exception
start
's content length is known, and if it greater than
maxContentLength
.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.Exception
protected abstract Object newContinueResponse(S start, int maxContentLength, ChannelPipeline pipeline) throws Exception
null
if there's no message to sendException
protected abstract boolean closeAfterContinueResponse(Object msg) throws Exception
newContinueResponse(Object, int, ChannelPipeline)
is written.msg
- The return value from newContinueResponse(Object, int, ChannelPipeline)
.true
if the channel should be closed after the result of
newContinueResponse(Object, int, ChannelPipeline)
is written. false
otherwise.Exception
protected abstract boolean ignoreContentAfterContinueResponse(Object msg) throws Exception
isContentMessage(Object)
returns true
.msg
- The return value from newContinueResponse(Object, int, ChannelPipeline)
.true
if all objects for the current request/response should be ignored or not.
false
otherwise.Exception
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 channelReadComplete(ChannelHandlerContext ctx) throws Exception
ChannelInboundHandlerAdapter
ChannelHandlerContext.fireChannelReadComplete()
to forward
to the next ChannelInboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelReadComplete
in interface ChannelInboundHandler
channelReadComplete
in class ChannelInboundHandlerAdapter
Exception
public void channelInactive(ChannelHandlerContext ctx) throws Exception
ChannelInboundHandlerAdapter
ChannelHandlerContext.fireChannelInactive()
to forward
to the next ChannelInboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelInactive
in interface ChannelInboundHandler
channelInactive
in class ChannelInboundHandlerAdapter
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–2017 The Netty Project. All rights reserved.