Module io.netty5.codec
Package io.netty5.handler.codec
Class MessageAggregator<I,S,C extends AutoCloseable,A extends AutoCloseable>
- java.lang.Object
-
- io.netty5.channel.ChannelHandlerAdapter
-
- io.netty5.handler.codec.MessageToMessageDecoder<I>
-
- io.netty5.handler.codec.MessageAggregator<I,S,C,A>
-
- Type Parameters:
I
- the type that covers both start message and content messageS
- the type of the start messageC
- the type of the content messageA
- the type of the aggregated message
- All Implemented Interfaces:
ChannelHandler
- Direct Known Subclasses:
HttpObjectAggregator
,WebSocketFrameAggregator
public abstract class MessageAggregator<I,S,C extends AutoCloseable,A extends AutoCloseable> extends MessageToMessageDecoder<I>
An abstractChannelHandler
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(AutoCloseable)
returntrue
for, the aggregator will finish the aggregation and produce the aggregated message and expect another start message.
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
MessageAggregator(int maxContentLength)
Creates a new instance.protected
MessageAggregator(int maxContentLength, Class<? extends I> inboundMessageType)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description boolean
acceptInboundMessage(Object msg)
Returnstrue
if the given message should be handled.protected abstract void
aggregate(BufferAllocator allocator, A aggregated, C content)
Aggregated the passedcontent
in the passedaggregate
.protected abstract A
beginAggregation(BufferAllocator allocator, S start)
Creates a new aggregated message from the specified start message.void
channelInactive(ChannelHandlerContext ctx)
TheChannel
of theChannelHandlerContext
was registered is now inactive and reached its end of lifetime.void
channelReadComplete(ChannelHandlerContext ctx)
Invoked when the last message read by the current read operation has been consumed byChannelHandler.channelRead(ChannelHandlerContext, Object)
.protected abstract boolean
closeAfterContinueResponse(Object msg)
Determine if the channel should be closed after the result ofnewContinueResponse(Object, int, ChannelPipeline)
is written.protected ChannelHandlerContext
ctx()
protected void
decode(ChannelHandlerContext ctx, I msg)
Decode from one message to another.protected void
finishAggregation(BufferAllocator allocator, A aggregated)
Invoked when the specifiedaggregated
message is about to be passed to the next handler in the pipeline.protected void
handleOversizedMessage(ChannelHandlerContext ctx, Object oversized)
Invoked when an incoming request exceeds the maximum content length.void
handlerAdded(ChannelHandlerContext ctx)
Gets called after theChannelHandler
was added to the actual context and it's ready to handle events.void
handlerRemoved(ChannelHandlerContext ctx)
Gets called after theChannelHandler
was removed from the actual context and it doesn't handle events anymore.protected abstract boolean
ignoreContentAfterContinueResponse(Object msg)
Determine if all objects for the current request/response should be ignored or not.protected abstract boolean
isAggregated(Object 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
isLastContentMessage(C msg)
Returnstrue
if and only if the specified message is the last content message.protected abstract int
lengthForAggregation(A msg)
Returns the length in bytes of the passed message.protected abstract int
lengthForContent(C msg)
Returns the length in bytes of the passed message.int
maxContentLength()
Returns the maximum allowed length of the aggregated message in bytes.protected abstract Object
newContinueResponse(S start, int maxContentLength, ChannelPipeline pipeline)
Returns the 'continue response' for the specified start message if necessary.protected abstract C
tryContentMessage(Object msg)
protected abstract S
tryStartMessage(Object msg)
-
Methods inherited from class io.netty5.handler.codec.MessageToMessageDecoder
channelRead, decodeAndClose
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty5.channel.ChannelHandler
bind, channelActive, channelExceptionCaught, channelInboundEvent, channelRegistered, channelShutdown, channelUnregistered, channelWritabilityChanged, close, connect, deregister, disconnect, flush, isSharable, pendingOutboundBytes, read, register, sendOutboundEvent, shutdown, write
-
-
-
-
Constructor Detail
-
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.
-
-
Method Detail
-
acceptInboundMessage
public boolean acceptInboundMessage(Object msg) throws Exception
Description copied from class:MessageToMessageDecoder
Returnstrue
if the given message should be handled. Iffalse
it will be passed to the nextChannelHandler
in theChannelPipeline
.- Overrides:
acceptInboundMessage
in classMessageToMessageDecoder<I>
- Throws:
Exception
-
isLastContentMessage
protected abstract boolean isLastContentMessage(C msg) throws Exception
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 && msg.isLastFragment();
- Throws:
Exception
-
isAggregated
protected abstract boolean isAggregated(Object msg) throws Exception
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
-
lengthForContent
protected abstract int lengthForContent(C msg)
Returns the length in bytes of the passed message.- Parameters:
msg
- to calculate length.- Returns:
- Length in bytes of the passed message.
-
lengthForAggregation
protected abstract int lengthForAggregation(A msg)
Returns the length in bytes of the passed message.- Parameters:
msg
- to calculate length.- Returns:
- Length in bytes of the passed message.
-
maxContentLength
public final int maxContentLength()
Returns the maximum allowed length of the aggregated message in bytes.
-
ctx
protected final ChannelHandlerContext ctx()
-
decode
protected void decode(ChannelHandlerContext ctx, I msg) throws Exception
Description copied from class:MessageToMessageDecoder
Decode from one message to another. This method will be called for each written message that can be handled by this decoder.The message will be disposed of after this call.
Subclasses that wish to sometimes pass messages through, should instead override the
MessageToMessageDecoder.decodeAndClose(ChannelHandlerContext, Object)
method.- Overrides:
decode
in classMessageToMessageDecoder<I>
- Parameters:
ctx
- theChannelHandlerContext
which thisMessageToMessageDecoder
belongs tomsg
- the message to decode to another one- Throws:
Exception
- is thrown if an error occurs
-
isContentLengthInvalid
protected abstract boolean isContentLengthInvalid(S start, int maxContentLength) throws Exception
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
protected abstract boolean closeAfterContinueResponse(Object msg) throws Exception
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
protected abstract boolean ignoreContentAfterContinueResponse(Object msg) throws Exception
Determine if all objects for the current request/response should be ignored or not. Messages will stop being ignored the next timetryContentMessage(Object)
returns anon null
value.- 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
protected abstract A beginAggregation(BufferAllocator allocator, S start) throws Exception
Creates a new aggregated message from the specified start message.- Throws:
Exception
-
aggregate
protected abstract void aggregate(BufferAllocator allocator, A aggregated, C content) throws Exception
Aggregated the passedcontent
in the passedaggregate
.- Throws:
Exception
-
finishAggregation
protected void finishAggregation(BufferAllocator allocator, A aggregated) throws Exception
Invoked when the specifiedaggregated
message is about to be passed to the next handler in the pipeline.- Throws:
Exception
-
handleOversizedMessage
protected void handleOversizedMessage(ChannelHandlerContext ctx, Object oversized) throws Exception
Invoked when an incoming request exceeds the maximum content length. The default behavior is to trigger anexceptionCaught()
event with aTooLongFrameException
.- Parameters:
ctx
- theChannelHandlerContext
oversized
- the accumulated message up to this point.- Throws:
Exception
-
channelReadComplete
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
Invoked when the last message read by the current read operation has been consumed byChannelHandler.channelRead(ChannelHandlerContext, Object)
. IfChannelOption.AUTO_READ
is off, no further attempt to read an inbound data from the currentChannel
will be made untilChannelHandlerContext.read()
is called.- Throws:
Exception
-
channelInactive
public void channelInactive(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
TheChannel
of theChannelHandlerContext
was registered is now inactive and reached its end of lifetime.- Throws:
Exception
-
handlerAdded
public void handlerAdded(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
Gets called after theChannelHandler
was added to the actual context and it's ready to handle events.- Throws:
Exception
-
handlerRemoved
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
Gets called after theChannelHandler
was removed from the actual context and it doesn't handle events anymore.- Throws:
Exception
-
-