public abstract class HttpContentEncoder extends SimpleChannelHandler implements LifeCycleAwareChannelHandler
HttpResponse and HttpChunk.
The original content is replaced with the new content encoded by the
EncoderEmbedder, which is created by newContentEncoder(HttpMessage, String).
Once encoding is finished, the value of the 'Content-Encoding' header
is set to the target content encoding, as returned by getTargetContentEncoding(String).
Also, the 'Content-Length' header is updated to the length of the
encoded content. If there is no supported encoding in the
corresponding HttpRequest's "Accept-Encoding" header,
newContentEncoder(HttpMessage, 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 newContentEncoder(HttpMessage, String) and getTargetContentEncoding(String)
properly to make this class functional. For example, refer to the source
code of HttpContentCompressor.
This handler must be placed after HttpMessageEncoder in the pipeline
so that this handler can intercept HTTP responses before HttpMessageEncoder
converts them into ChannelBuffers.
ChannelHandler.Sharable| Modifier and Type | Field and Description |
|---|---|
private java.util.Queue<java.lang.String> |
acceptEncodingQueue |
private EncoderEmbedder<ChannelBuffer> |
encoder |
private boolean |
offerred |
| Modifier | Constructor and Description |
|---|---|
protected |
HttpContentEncoder()
Creates a new instance.
|
| Modifier and Type | Method and Description |
|---|---|
void |
afterAdd(ChannelHandlerContext ctx) |
void |
afterRemove(ChannelHandlerContext ctx) |
void |
beforeAdd(ChannelHandlerContext ctx) |
void |
beforeRemove(ChannelHandlerContext ctx) |
void |
channelClosed(ChannelHandlerContext ctx,
ChannelStateEvent e)
Invoked when a
Channel was closed and all its related resources
were released. |
private ChannelBuffer |
encode(ChannelBuffer buf) |
private ChannelBuffer |
finishEncode() |
protected abstract java.lang.String |
getTargetContentEncoding(java.lang.String acceptEncoding)
Returns the expected content encoding of the encoded content.
|
void |
messageReceived(ChannelHandlerContext ctx,
MessageEvent e)
Invoked when a message object (e.g:
ChannelBuffer) was received
from a remote peer. |
protected abstract EncoderEmbedder<ChannelBuffer> |
newContentEncoder(HttpMessage msg,
java.lang.String acceptEncoding)
Returns a new
EncoderEmbedder that encodes the HTTP message
content. |
void |
writeRequested(ChannelHandlerContext ctx,
MessageEvent e)
Invoked when
Channel.write(Object) is called. |
bindRequested, channelBound, channelConnected, channelDisconnected, channelInterestChanged, channelOpen, channelUnbound, childChannelClosed, childChannelOpen, closeRequested, connectRequested, disconnectRequested, exceptionCaught, handleDownstream, handleUpstream, setInterestOpsRequested, unbindRequested, writeCompleteprivate final java.util.Queue<java.lang.String> acceptEncodingQueue
private volatile EncoderEmbedder<ChannelBuffer> encoder
private volatile boolean offerred
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws java.lang.Exception
SimpleChannelHandlerChannelBuffer) was received
from a remote peer.messageReceived in class SimpleChannelHandlerjava.lang.Exceptionpublic void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws java.lang.Exception
SimpleChannelHandlerChannel.write(Object) is called.writeRequested in class SimpleChannelHandlerjava.lang.Exceptionpublic void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
SimpleChannelHandlerChannel was closed and all its related resources
were released.channelClosed in class SimpleChannelHandlerjava.lang.Exceptionprotected abstract EncoderEmbedder<ChannelBuffer> newContentEncoder(HttpMessage msg, java.lang.String acceptEncoding) throws java.lang.Exception
EncoderEmbedder that encodes the HTTP message
content.acceptEncoding - the value of the "Accept-Encoding" headerEncoderEmbedder if there is a supported encoding
in acceptEncoding. null otherwise.java.lang.Exceptionprotected abstract java.lang.String getTargetContentEncoding(java.lang.String acceptEncoding)
throws java.lang.Exception
acceptEncoding - the value of the "Accept-Encoding" headerjava.lang.Exceptionprivate ChannelBuffer encode(ChannelBuffer buf)
private ChannelBuffer finishEncode()
public void beforeAdd(ChannelHandlerContext ctx) throws java.lang.Exception
beforeAdd in interface LifeCycleAwareChannelHandlerjava.lang.Exceptionpublic void afterAdd(ChannelHandlerContext ctx) throws java.lang.Exception
afterAdd in interface LifeCycleAwareChannelHandlerjava.lang.Exceptionpublic void beforeRemove(ChannelHandlerContext ctx) throws java.lang.Exception
beforeRemove in interface LifeCycleAwareChannelHandlerjava.lang.Exceptionpublic void afterRemove(ChannelHandlerContext ctx) throws java.lang.Exception
afterRemove in interface LifeCycleAwareChannelHandlerjava.lang.Exception