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 outboundHttpResponseandHttpChunk. 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 returnnullso 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
HttpMessageEncoderin the pipeline so that this handler can intercept HTTP responses beforeHttpMessageEncoderconverts them intoChannelBuffers.
-
-
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>acceptEncodingQueueprivate EncoderEmbedder<ChannelBuffer>encoderprivate booleanofferred
-
Constructor Summary
Constructors Modifier Constructor Description protectedHttpContentEncoder()Creates a new instance.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidafterAdd(ChannelHandlerContext ctx)voidafterRemove(ChannelHandlerContext ctx)voidbeforeAdd(ChannelHandlerContext ctx)voidbeforeRemove(ChannelHandlerContext ctx)voidchannelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)Invoked when aChannelwas closed and all its related resources were released.private ChannelBufferencode(ChannelBuffer buf)private ChannelBufferfinishEncode()protected abstract java.lang.StringgetTargetContentEncoding(java.lang.String acceptEncoding)Returns the expected content encoding of the encoded content.voidmessageReceived(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 newEncoderEmbedderthat encodes the HTTP message content.voidwriteRequested(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:SimpleChannelHandlerInvoked when a message object (e.g:ChannelBuffer) was received from a remote peer.- Overrides:
messageReceivedin classSimpleChannelHandler- Throws:
java.lang.Exception
-
writeRequested
public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws java.lang.Exception
Description copied from class:SimpleChannelHandlerInvoked whenChannel.write(Object)is called.- Overrides:
writeRequestedin classSimpleChannelHandler- Throws:
java.lang.Exception
-
channelClosed
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
Description copied from class:SimpleChannelHandlerInvoked when aChannelwas closed and all its related resources were released.- Overrides:
channelClosedin classSimpleChannelHandler- Throws:
java.lang.Exception
-
newContentEncoder
protected abstract EncoderEmbedder<ChannelBuffer> newContentEncoder(HttpMessage msg, java.lang.String acceptEncoding) throws java.lang.Exception
Returns a newEncoderEmbedderthat encodes the HTTP message content.- Parameters:
acceptEncoding- the value of the"Accept-Encoding"header- Returns:
- a new
EncoderEmbedderif there is a supported encoding inacceptEncoding.nullotherwise. - Throws:
java.lang.Exception
-
getTargetContentEncoding
protected abstract java.lang.String getTargetContentEncoding(java.lang.String acceptEncoding) throws java.lang.ExceptionReturns 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:
beforeAddin interfaceLifeCycleAwareChannelHandler- Throws:
java.lang.Exception
-
afterAdd
public void afterAdd(ChannelHandlerContext ctx) throws java.lang.Exception
- Specified by:
afterAddin interfaceLifeCycleAwareChannelHandler- Throws:
java.lang.Exception
-
beforeRemove
public void beforeRemove(ChannelHandlerContext ctx) throws java.lang.Exception
- Specified by:
beforeRemovein interfaceLifeCycleAwareChannelHandler- Throws:
java.lang.Exception
-
afterRemove
public void afterRemove(ChannelHandlerContext ctx) throws java.lang.Exception
- Specified by:
afterRemovein interfaceLifeCycleAwareChannelHandler- Throws:
java.lang.Exception
-
-