Class HttpContentEncoder
- java.lang.Object
-
- org.jboss.netty.channel.SimpleChannelHandler
-
- org.jboss.netty.handler.codec.http.HttpContentEncoder
-
- All Implemented Interfaces:
ChannelDownstreamHandler
,ChannelHandler
,ChannelUpstreamHandler
,LifeCycleAwareChannelHandler
- Direct Known Subclasses:
HttpContentCompressor
public abstract class HttpContentEncoder extends SimpleChannelHandler implements LifeCycleAwareChannelHandler
Encodes the content of the outboundHttpResponse
andHttpChunk
. The original content is replaced with the new content encoded by theEncoderEmbedder
, which is created bynewContentEncoder(HttpMessage, String)
. Once encoding is finished, the value of the 'Content-Encoding' header is set to the target content encoding, as returned bygetTargetContentEncoding(String)
. Also, the 'Content-Length' header is updated to the length of the encoded content. If there is no supported encoding in the correspondingHttpRequest
's"Accept-Encoding"
header,newContentEncoder(HttpMessage, 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
newContentEncoder(HttpMessage, String)
andgetTargetContentEncoding(String)
properly to make this class functional. For example, refer to the source code ofHttpContentCompressor
.This handler must be placed after
HttpMessageEncoder
in the pipeline so that this handler can intercept HTTP responses beforeHttpMessageEncoder
converts them intoChannelBuffer
s.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.jboss.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Queue<java.lang.String>
acceptEncodingQueue
private EncoderEmbedder<ChannelBuffer>
encoder
private boolean
offerred
-
Constructor Summary
Constructors Modifier Constructor Description protected
HttpContentEncoder()
Creates a new instance.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method 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 aChannel
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 newEncoderEmbedder
that encodes the HTTP message content.void
writeRequested(ChannelHandlerContext ctx, MessageEvent e)
Invoked whenChannel.write(Object)
is called.-
Methods inherited from class org.jboss.netty.channel.SimpleChannelHandler
bindRequested, channelBound, channelConnected, channelDisconnected, channelInterestChanged, channelOpen, channelUnbound, childChannelClosed, childChannelOpen, closeRequested, connectRequested, disconnectRequested, exceptionCaught, handleDownstream, handleUpstream, setInterestOpsRequested, unbindRequested, writeComplete
-
-
-
-
Field Detail
-
acceptEncodingQueue
private final java.util.Queue<java.lang.String> acceptEncodingQueue
-
encoder
private volatile EncoderEmbedder<ChannelBuffer> encoder
-
offerred
private volatile boolean offerred
-
-
Method Detail
-
messageReceived
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws java.lang.Exception
Description copied from class:SimpleChannelHandler
Invoked when a message object (e.g:ChannelBuffer
) was received from a remote peer.- Overrides:
messageReceived
in classSimpleChannelHandler
- Throws:
java.lang.Exception
-
writeRequested
public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws java.lang.Exception
Description copied from class:SimpleChannelHandler
Invoked whenChannel.write(Object)
is called.- Overrides:
writeRequested
in classSimpleChannelHandler
- Throws:
java.lang.Exception
-
channelClosed
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
Description copied from class:SimpleChannelHandler
Invoked when aChannel
was closed and all its related resources were released.- Overrides:
channelClosed
in classSimpleChannelHandler
- Throws:
java.lang.Exception
-
newContentEncoder
protected abstract EncoderEmbedder<ChannelBuffer> newContentEncoder(HttpMessage msg, java.lang.String acceptEncoding) throws java.lang.Exception
Returns a newEncoderEmbedder
that encodes the HTTP message content.- Parameters:
acceptEncoding
- the value of the"Accept-Encoding"
header- Returns:
- a new
EncoderEmbedder
if there is a supported encoding inacceptEncoding
.null
otherwise. - Throws:
java.lang.Exception
-
getTargetContentEncoding
protected abstract java.lang.String getTargetContentEncoding(java.lang.String acceptEncoding) throws java.lang.Exception
Returns the expected content encoding of the encoded content.- Parameters:
acceptEncoding
- the value of the"Accept-Encoding"
header- Returns:
- the expected content encoding of the new content
- Throws:
java.lang.Exception
-
encode
private ChannelBuffer encode(ChannelBuffer buf)
-
finishEncode
private ChannelBuffer finishEncode()
-
beforeAdd
public void beforeAdd(ChannelHandlerContext ctx) throws java.lang.Exception
- Specified by:
beforeAdd
in interfaceLifeCycleAwareChannelHandler
- Throws:
java.lang.Exception
-
afterAdd
public void afterAdd(ChannelHandlerContext ctx) throws java.lang.Exception
- Specified by:
afterAdd
in interfaceLifeCycleAwareChannelHandler
- Throws:
java.lang.Exception
-
beforeRemove
public void beforeRemove(ChannelHandlerContext ctx) throws java.lang.Exception
- Specified by:
beforeRemove
in interfaceLifeCycleAwareChannelHandler
- Throws:
java.lang.Exception
-
afterRemove
public void afterRemove(ChannelHandlerContext ctx) throws java.lang.Exception
- Specified by:
afterRemove
in interfaceLifeCycleAwareChannelHandler
- Throws:
java.lang.Exception
-
-