public abstract class HttpContentEncoder extends MessageToMessageCodec<HttpRequest,HttpObject>
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.
Modifier and Type | Class and Description |
---|---|
static class |
HttpContentEncoder.Result |
private static class |
HttpContentEncoder.State |
ChannelHandler.Sharable
Modifier and Type | Field and Description |
---|---|
private java.lang.CharSequence |
acceptEncoding |
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 and Description |
---|
HttpContentEncoder() |
Modifier and Type | Method and Description |
---|---|
boolean |
acceptOutboundMessage(java.lang.Object msg)
Returns
true if and only if the specified message can be encoded by this codec. |
protected abstract HttpContentEncoder.Result |
beginEncode(HttpResponse headers,
java.lang.String acceptEncoding)
Prepare to encode the HTTP message content.
|
void |
channelInactive(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.fireChannelInactive() to forward
to the next ChannelInboundHandler in the ChannelPipeline . |
private void |
cleanup() |
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 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) |
acceptInboundMessage, channelRead, write
bind, close, connect, deregister, disconnect, flush, read
channelActive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
handlerAdded, isSharable
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
exceptionCaught, handlerAdded
private static final java.lang.CharSequence ZERO_LENGTH_HEAD
private static final java.lang.CharSequence ZERO_LENGTH_CONNECT
private static final int CONTINUE_CODE
private final java.util.Queue<java.lang.CharSequence> acceptEncodingQueue
private java.lang.CharSequence acceptEncoding
private EmbeddedChannel encoder
private HttpContentEncoder.State state
public boolean acceptOutboundMessage(java.lang.Object msg) throws java.lang.Exception
MessageToMessageCodec
true
if and only if the specified message can be encoded by this codec.acceptOutboundMessage
in class MessageToMessageCodec<HttpRequest,HttpObject>
msg
- the messagejava.lang.Exception
protected void decode(ChannelHandlerContext ctx, HttpRequest msg, java.util.List<java.lang.Object> out) throws java.lang.Exception
decode
in class MessageToMessageCodec<HttpRequest,HttpObject>
java.lang.Exception
MessageToMessageDecoder.decode(ChannelHandlerContext, Object, List)
protected void encode(ChannelHandlerContext ctx, HttpObject msg, java.util.List<java.lang.Object> out) throws java.lang.Exception
encode
in class MessageToMessageCodec<HttpRequest,HttpObject>
java.lang.Exception
MessageToMessageEncoder.encode(ChannelHandlerContext, Object, List)
private static boolean isPassthru(HttpVersion version, int code, java.lang.CharSequence httpMethod)
private static void ensureHeaders(HttpObject msg)
private static void ensureContent(HttpObject msg)
private boolean encodeContent(HttpContent c, java.util.List<java.lang.Object> out)
protected abstract HttpContentEncoder.Result beginEncode(HttpResponse headers, java.lang.String acceptEncoding) throws java.lang.Exception
headers
- the headersacceptEncoding
- the value of the "Accept-Encoding"
headerEmbeddedChannel
that
encodes the content into the target content encoding.
null
if acceptEncoding
is unsupported or rejected
and thus the content should be handled as-is (i.e. no encoding).java.lang.Exception
public void handlerRemoved(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelHandlerAdapter
handlerRemoved
in interface ChannelHandler
handlerRemoved
in class ChannelHandlerAdapter
java.lang.Exception
public void channelInactive(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelInboundHandlerAdapter
ChannelHandlerContext.fireChannelInactive()
to forward
to the next ChannelInboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelInactive
in interface ChannelInboundHandler
channelInactive
in class ChannelInboundHandlerAdapter
java.lang.Exception
private void cleanup()
private void encode(ByteBuf in, java.util.List<java.lang.Object> out)
private void finishEncode(java.util.List<java.lang.Object> out)
private void fetchEncoderOutput(java.util.List<java.lang.Object> out)