Package io.netty.handler.codec.http
Class HttpContentEncoder
java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.netty.channel.ChannelDuplexHandler
io.netty.handler.codec.MessageToMessageCodec<HttpRequest,HttpObject>
io.netty.handler.codec.http.HttpContentEncoder
- All Implemented Interfaces:
ChannelHandler
,ChannelInboundHandler
,ChannelOutboundHandler
- Direct Known Subclasses:
HttpContentCompressor
Encodes the content of the outbound
HttpResponse
and HttpContent
.
The original content is replaced with the new content encoded by the
EmbeddedChannel
, which is created by beginEncode(HttpResponse, String)
.
Once encoding is finished, the value of the 'Content-Encoding' header
is set to the target content encoding, as returned by
beginEncode(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
corresponding HttpRequest
's "Accept-Encoding"
header,
beginEncode(HttpResponse, String)
should return null
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 of
HttpContentCompressor
.
This handler must be placed after HttpObjectEncoder
in the pipeline
so that this handler can intercept HTTP responses before HttpObjectEncoder
converts them into ByteBuf
s.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
private static enum
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Queue
<CharSequence> private EmbeddedChannel
private HttpContentEncoder.State
private static final CharSequence
private static final CharSequence
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
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
CallsChannelHandlerContext.fireChannelInactive()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.private void
cleanup()
private void
protected void
decode
(ChannelHandlerContext ctx, HttpRequest msg, List<Object> out) private void
protected void
encode
(ChannelHandlerContext ctx, HttpObject msg, List<Object> out) private boolean
encodeContent
(HttpContent c, List<Object> out) private void
encodeFullResponse
(HttpResponse newRes, HttpContent content, List<Object> out) private static void
ensureContent
(HttpObject msg) private static void
ensureHeaders
(HttpObject msg) private void
fetchEncoderOutput
(List<Object> out) private void
finishEncode
(List<Object> out) void
Do nothing by default, sub-classes may override this method.private static boolean
isPassthru
(HttpVersion version, int code, CharSequence httpMethod) Methods inherited from class io.netty.handler.codec.MessageToMessageCodec
acceptInboundMessage, channelRead, channelReadComplete, write
Methods inherited from class io.netty.channel.ChannelDuplexHandler
bind, close, connect, deregister, disconnect, flush, read
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, isSharable
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.netty.channel.ChannelHandler
handlerAdded
-
Field Details
-
ZERO_LENGTH_HEAD
-
ZERO_LENGTH_CONNECT
-
acceptEncodingQueue
-
encoder
-
state
-
-
Constructor Details
-
HttpContentEncoder
public HttpContentEncoder()
-
-
Method Details
-
acceptOutboundMessage
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, List<Object> out) throws Exception - Specified by:
decode
in classMessageToMessageCodec<HttpRequest,
HttpObject> - Throws:
Exception
- See Also:
-
encode
- Specified by:
encode
in classMessageToMessageCodec<HttpRequest,
HttpObject> - Throws:
Exception
- See Also:
-
encodeFullResponse
-
isPassthru
-
ensureHeaders
-
ensureContent
-
encodeContent
-
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
Description copied from class:ChannelHandlerAdapter
Do nothing by default, sub-classes may override this method.- Specified by:
handlerRemoved
in interfaceChannelHandler
- Overrides:
handlerRemoved
in classChannelHandlerAdapter
- Throws:
Exception
-
channelInactive
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelInactive()
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelInactive
in interfaceChannelInboundHandler
- Overrides:
channelInactive
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
cleanup
private void cleanup() -
cleanupSafely
-
encode
-
finishEncode
-
fetchEncoderOutput
-