- java.lang.Object
-
- io.netty5.channel.ChannelHandlerAdapter
-
- io.netty5.handler.codec.MessageToMessageCodec<HttpRequest,HttpObject>
-
- io.netty5.handler.codec.http.HttpContentEncoder
-
- All Implemented Interfaces:
ChannelHandler
- Direct Known Subclasses:
HttpContentCompressor
public abstract class HttpContentEncoder extends MessageToMessageCodec<HttpRequest,HttpObject>
Encodes the content of the outboundHttpResponse
andHttpContent
. The original content is replaced with the new content encoded by theEmbeddedChannel
, which is created bybeginEncode(HttpResponse, String)
. Once encoding is finished, the value of the 'Content-Encoding' header is set to the target content encoding, as returned bybeginEncode(HttpResponse, String)
. Also, the 'Content-Length' header is updated to the length of the encoded content. If there is no supported or allowed encoding in the correspondingHttpRequest
's"Accept-Encoding"
header,beginEncode(HttpResponse, String)
should returnnull
so that no encoding occurs (i.e. pass-through).Please note that this is an abstract class. You have to extend this class and implement
beginEncode(HttpResponse, String)
properly to make this class functional. For example, refer to the source code ofHttpContentCompressor
.This handler must be placed after
HttpObjectEncoder
in the pipeline so that this handler can intercept HTTP responses beforeHttpObjectEncoder
converts them intoBuffer
s.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
HttpContentEncoder.Result
-
Constructor Summary
Constructors Constructor Description HttpContentEncoder()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description boolean
acceptOutboundMessage(Object msg)
Returnstrue
if and only if the specified message can be encoded by this codec.protected abstract HttpContentEncoder.Result
beginEncode(HttpResponse httpResponse, String acceptEncoding)
Prepare to encode the HTTP message content.void
channelInactive(ChannelHandlerContext ctx)
TheChannel
of theChannelHandlerContext
was registered is now inactive and reached its end of lifetime.protected void
decode(ChannelHandlerContext ctx, HttpRequest msg)
protected void
decodeAndClose(ChannelHandlerContext ctx, HttpRequest msg)
protected void
encodeAndClose(ChannelHandlerContext ctx, HttpObject msg, List<Object> out)
void
handlerRemoved(ChannelHandlerContext ctx)
Gets called after theChannelHandler
was removed from the actual context and it doesn't handle events anymore.-
Methods inherited from class io.netty5.handler.codec.MessageToMessageCodec
acceptInboundMessage, channelRead, encode, write
-
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, channelReadComplete, channelRegistered, channelShutdown, channelUnregistered, channelWritabilityChanged, close, connect, deregister, disconnect, flush, handlerAdded, isSharable, pendingOutboundBytes, read, register, sendOutboundEvent, shutdown
-
-
-
-
Method Detail
-
acceptOutboundMessage
public boolean acceptOutboundMessage(Object msg) throws Exception
Description copied from class:MessageToMessageCodec
Returnstrue
if and only if the specified message can be encoded by this codec.- Overrides:
acceptOutboundMessage
in classMessageToMessageCodec<HttpRequest,HttpObject>
- Parameters:
msg
- the message- Throws:
Exception
-
decode
protected void decode(ChannelHandlerContext ctx, HttpRequest msg) throws Exception
- Overrides:
decode
in classMessageToMessageCodec<HttpRequest,HttpObject>
- Throws:
Exception
- See Also:
MessageToMessageDecoder.decode(ChannelHandlerContext, Object)
-
decodeAndClose
protected void decodeAndClose(ChannelHandlerContext ctx, HttpRequest msg) throws Exception
- Overrides:
decodeAndClose
in classMessageToMessageCodec<HttpRequest,HttpObject>
- Throws:
Exception
- See Also:
MessageToMessageDecoder.decodeAndClose(ChannelHandlerContext, Object)
-
encodeAndClose
protected void encodeAndClose(ChannelHandlerContext ctx, HttpObject msg, List<Object> out) throws Exception
- Overrides:
encodeAndClose
in classMessageToMessageCodec<HttpRequest,HttpObject>
- Throws:
Exception
- See Also:
MessageToMessageEncoder.encodeAndClose(ChannelHandlerContext, Object, List)
-
beginEncode
protected abstract HttpContentEncoder.Result beginEncode(HttpResponse httpResponse, String acceptEncoding) throws Exception
Prepare to encode the HTTP message content.- Parameters:
httpResponse
- the http responseacceptEncoding
- the value of the"Accept-Encoding"
header- Returns:
- the result of preparation, which is composed of the determined
target content encoding and a new
EmbeddedChannel
that encodes the content into the target content encoding.null
ifacceptEncoding
is unsupported or rejected and thus the content should be handled as-is (i.e. no encoding). - 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
-
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
-
-