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
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 intoByteBuf
s.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
HttpContentEncoder.Result
private static class
HttpContentEncoder.State
-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Queue<java.lang.CharSequence>
acceptEncodingQueue
private static int
CONTINUE_CODE
private EmbeddedChannel
encoder
private HttpContentEncoder.State
state
private static java.lang.CharSequence
ZERO_LENGTH_CONNECT
private static java.lang.CharSequence
ZERO_LENGTH_HEAD
-
Constructor Summary
Constructors Constructor Description HttpContentEncoder()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description boolean
acceptOutboundMessage(java.lang.Object msg)
Returnstrue
if and only if the specified message can be encoded by this codec.protected abstract HttpContentEncoder.Result
beginEncode(HttpResponse httpResponse, java.lang.String acceptEncoding)
Prepare to encode the HTTP message content.void
channelInactive(ChannelHandlerContext ctx)
CallsChannelHandlerContext.fireChannelInactive()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.private void
cleanup()
private void
cleanupSafely(ChannelHandlerContext ctx)
protected void
decode(ChannelHandlerContext ctx, HttpRequest msg, java.util.List<java.lang.Object> out)
private void
encode(ByteBuf in, java.util.List<java.lang.Object> out)
protected void
encode(ChannelHandlerContext ctx, HttpObject msg, java.util.List<java.lang.Object> out)
private boolean
encodeContent(HttpContent c, java.util.List<java.lang.Object> out)
private void
encodeFullResponse(HttpResponse newRes, HttpContent content, java.util.List<java.lang.Object> out)
private static void
ensureContent(HttpObject msg)
private static void
ensureHeaders(HttpObject msg)
private void
fetchEncoderOutput(java.util.List<java.lang.Object> out)
private void
finishEncode(java.util.List<java.lang.Object> out)
void
handlerRemoved(ChannelHandlerContext ctx)
Do nothing by default, sub-classes may override this method.private static boolean
isPassthru(HttpVersion version, int code, java.lang.CharSequence httpMethod)
-
Methods inherited from class io.netty.handler.codec.MessageToMessageCodec
acceptInboundMessage, channelRead, 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, channelReadComplete, 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 Detail
-
ZERO_LENGTH_HEAD
private static final java.lang.CharSequence ZERO_LENGTH_HEAD
-
ZERO_LENGTH_CONNECT
private static final java.lang.CharSequence ZERO_LENGTH_CONNECT
-
CONTINUE_CODE
private static final int CONTINUE_CODE
-
acceptEncodingQueue
private final java.util.Queue<java.lang.CharSequence> acceptEncodingQueue
-
encoder
private EmbeddedChannel encoder
-
state
private HttpContentEncoder.State state
-
-
Method Detail
-
acceptOutboundMessage
public boolean acceptOutboundMessage(java.lang.Object msg) throws java.lang.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:
java.lang.Exception
-
decode
protected void decode(ChannelHandlerContext ctx, HttpRequest msg, java.util.List<java.lang.Object> out) throws java.lang.Exception
- Specified by:
decode
in classMessageToMessageCodec<HttpRequest,HttpObject>
- Throws:
java.lang.Exception
- See Also:
MessageToMessageDecoder.decode(ChannelHandlerContext, Object, List)
-
encode
protected void encode(ChannelHandlerContext ctx, HttpObject msg, java.util.List<java.lang.Object> out) throws java.lang.Exception
- Specified by:
encode
in classMessageToMessageCodec<HttpRequest,HttpObject>
- Throws:
java.lang.Exception
- See Also:
MessageToMessageEncoder.encode(ChannelHandlerContext, Object, List)
-
encodeFullResponse
private void encodeFullResponse(HttpResponse newRes, HttpContent content, java.util.List<java.lang.Object> out)
-
isPassthru
private static boolean isPassthru(HttpVersion version, int code, java.lang.CharSequence httpMethod)
-
ensureHeaders
private static void ensureHeaders(HttpObject msg)
-
ensureContent
private static void ensureContent(HttpObject msg)
-
encodeContent
private boolean encodeContent(HttpContent c, java.util.List<java.lang.Object> out)
-
beginEncode
protected abstract HttpContentEncoder.Result beginEncode(HttpResponse httpResponse, java.lang.String acceptEncoding) throws java.lang.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:
java.lang.Exception
-
handlerRemoved
public void handlerRemoved(ChannelHandlerContext ctx) throws java.lang.Exception
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:
java.lang.Exception
-
channelInactive
public void channelInactive(ChannelHandlerContext ctx) throws java.lang.Exception
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:
java.lang.Exception
-
cleanup
private void cleanup()
-
cleanupSafely
private void cleanupSafely(ChannelHandlerContext ctx)
-
encode
private void encode(ByteBuf in, java.util.List<java.lang.Object> out)
-
finishEncode
private void finishEncode(java.util.List<java.lang.Object> out)
-
fetchEncoderOutput
private void fetchEncoderOutput(java.util.List<java.lang.Object> out)
-
-