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 io.netty.buffer.ByteBufHolder,O extends io.netty.buffer.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.
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,
io.netty.buffer.ByteBuf content)
Creates a new aggregated message from the specified start message and the specified content.
|
void |
channelInactive(io.netty.channel.ChannelHandlerContext ctx) |
void |
channelReadComplete(io.netty.channel.ChannelHandlerContext ctx) |
protected abstract boolean |
closeAfterContinueResponse(Object msg)
Determine if the channel should be closed after the result of
newContinueResponse(Object, int, ChannelPipeline) is written. |
protected io.netty.channel.ChannelHandlerContext |
ctx() |
protected void |
decode(io.netty.channel.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(io.netty.channel.ChannelHandlerContext ctx,
S oversized)
Invoked when an incoming request exceeds the maximum content length.
|
void |
handlerAdded(io.netty.channel.ChannelHandlerContext ctx) |
void |
handlerRemoved(io.netty.channel.ChannelHandlerContext ctx) |
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,
io.netty.channel.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
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 io.netty.channel.ChannelHandlerContext ctx()
protected void decode(io.netty.channel.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, io.netty.channel.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, io.netty.buffer.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(io.netty.channel.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(io.netty.channel.ChannelHandlerContext ctx) throws Exception
channelReadComplete
in interface io.netty.channel.ChannelInboundHandler
channelReadComplete
in class io.netty.channel.ChannelInboundHandlerAdapter
Exception
public void channelInactive(io.netty.channel.ChannelHandlerContext ctx) throws Exception
channelInactive
in interface io.netty.channel.ChannelInboundHandler
channelInactive
in class io.netty.channel.ChannelInboundHandlerAdapter
Exception
public void handlerAdded(io.netty.channel.ChannelHandlerContext ctx) throws Exception
handlerAdded
in interface io.netty.channel.ChannelHandler
handlerAdded
in class io.netty.channel.ChannelHandlerAdapter
Exception
Copyright © 2008–2019 The Netty Project. All rights reserved.