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 abstractChannelHandlerthat 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)returntruefor, the aggregator will finish the aggregation and produce the aggregated message and expect another start message.
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedMessageAggregator(int maxContentLength)Creates a new instance.protectedMessageAggregator(int maxContentLength, Class<? extends I> inboundMessageType)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description booleanacceptInboundMessage(Object msg)Returnstrueif the given message should be handled.protected abstract voidaggregate(BufferAllocator allocator, A aggregated, C content)Aggregated the passedcontentin the passedaggregate.protected abstract AbeginAggregation(BufferAllocator allocator, S start)Creates a new aggregated message from the specified start message.voidchannelInactive(ChannelHandlerContext ctx)TheChannelof theChannelHandlerContextwas registered is now inactive and reached its end of lifetime.voidchannelReadComplete(ChannelHandlerContext ctx)Invoked when the last message read by the current read operation has been consumed byChannelHandler.channelRead(ChannelHandlerContext, Object).protected abstract booleancloseAfterContinueResponse(Object msg)Determine if the channel should be closed after the result ofnewContinueResponse(Object, int, ChannelPipeline)is written.protected ChannelHandlerContextctx()protected voiddecode(ChannelHandlerContext ctx, I msg)Decode from one message to another.protected voidfinishAggregation(BufferAllocator allocator, A aggregated)Invoked when the specifiedaggregatedmessage is about to be passed to the next handler in the pipeline.protected voidhandleOversizedMessage(ChannelHandlerContext ctx, Object oversized)Invoked when an incoming request exceeds the maximum content length.voidhandlerAdded(ChannelHandlerContext ctx)Gets called after theChannelHandlerwas added to the actual context and it's ready to handle events.voidhandlerRemoved(ChannelHandlerContext ctx)Gets called after theChannelHandlerwas removed from the actual context and it doesn't handle events anymore.protected abstract booleanignoreContentAfterContinueResponse(Object msg)Determine if all objects for the current request/response should be ignored or not.protected abstract booleanisAggregated(Object msg)Returnstrueif and only if the specified message is already aggregated.protected abstract booleanisContentLengthInvalid(S start, int maxContentLength)Determine if the messagestart's content length is known, and if it greater thanmaxContentLength.protected abstract booleanisLastContentMessage(C msg)Returnstrueif and only if the specified message is the last content message.protected abstract intlengthForAggregation(A msg)Returns the length in bytes of the passed message.protected abstract intlengthForContent(C msg)Returns the length in bytes of the passed message.intmaxContentLength()Returns the maximum allowed length of the aggregated message in bytes.protected abstract ObjectnewContinueResponse(S start, int maxContentLength, ChannelPipeline pipeline)Returns the 'continue response' for the specified start message if necessary.protected abstract CtryContentMessage(Object msg)protected abstract StryStartMessage(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:MessageToMessageDecoderReturnstrueif the given message should be handled. Iffalseit will be passed to the nextChannelHandlerin theChannelPipeline.- Overrides:
acceptInboundMessagein classMessageToMessageDecoder<I>- Throws:
Exception
-
isLastContentMessage
protected abstract boolean isLastContentMessage(C msg) throws Exception
Returnstrueif and only if the specified message is the last content message. Typically, this method is implemented as a singlereturnstatement withinstanceof:return msg instanceof MyLastContentMessage;
or withinstanceofand boolean field check:return msg instanceof MyContentMessage && msg.isLastFragment();
- Throws:
Exception
-
isAggregated
protected abstract boolean isAggregated(Object msg) throws Exception
Returnstrueif 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:MessageToMessageDecoderDecode 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:
decodein classMessageToMessageDecoder<I>- Parameters:
ctx- theChannelHandlerContextwhich thisMessageToMessageDecoderbelongs 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:
trueif the messagestart's content length is known, and if it greater thanmaxContentLength.falseotherwise.- 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
nullif 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:
trueif the channel should be closed after the result ofnewContinueResponse(Object, int, ChannelPipeline)is written.falseotherwise.- 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 nullvalue.- Parameters:
msg- The return value fromnewContinueResponse(Object, int, ChannelPipeline).- Returns:
trueif all objects for the current request/response should be ignored or not.falseotherwise.- 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 passedcontentin the passedaggregate.- Throws:
Exception
-
finishAggregation
protected void finishAggregation(BufferAllocator allocator, A aggregated) throws Exception
Invoked when the specifiedaggregatedmessage 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- theChannelHandlerContextoversized- the accumulated message up to this point.- Throws:
Exception
-
channelReadComplete
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandlerInvoked when the last message read by the current read operation has been consumed byChannelHandler.channelRead(ChannelHandlerContext, Object). IfChannelOption.AUTO_READis off, no further attempt to read an inbound data from the currentChannelwill be made untilChannelHandlerContext.read()is called.- Throws:
Exception
-
channelInactive
public void channelInactive(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandlerTheChannelof theChannelHandlerContextwas registered is now inactive and reached its end of lifetime.- Throws:
Exception
-
handlerAdded
public void handlerAdded(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandlerGets called after theChannelHandlerwas 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:ChannelHandlerGets called after theChannelHandlerwas removed from the actual context and it doesn't handle events anymore.- Throws:
Exception
-
-