Package io.netty.handler.codec.http
Class HttpContentDecoder
- java.lang.Object
-
- io.netty.channel.ChannelHandlerAdapter
-
- io.netty.channel.ChannelInboundHandlerAdapter
-
- io.netty.handler.codec.MessageToMessageDecoder<HttpObject>
-
- io.netty.handler.codec.http.HttpContentDecoder
-
- All Implemented Interfaces:
ChannelHandler
,ChannelInboundHandler
- Direct Known Subclasses:
HttpContentDecompressor
public abstract class HttpContentDecoder extends MessageToMessageDecoder<HttpObject>
Decodes the content of the receivedHttpRequest
andHttpContent
. The original content is replaced with the new content decoded by theEmbeddedChannel
, which is created bynewContentDecoder(String)
. Once decoding 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 decoded content. If the content encoding of the original is not supported by the decoder,newContentDecoder(String)
should returnnull
so that no decoding occurs (i.e. pass-through).Please note that this is an abstract class. You have to extend this class and implement
newContentDecoder(String)
properly to make this class functional. For example, refer to the source code ofHttpContentDecompressor
.This handler must be placed after
HttpObjectDecoder
in the pipeline so that this handler can intercept HTTP requests afterHttpObjectDecoder
convertsByteBuf
s into HTTP requests.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
continueResponse
protected ChannelHandlerContext
ctx
private EmbeddedChannel
decoder
(package private) static java.lang.String
IDENTITY
private boolean
needRead
-
Constructor Summary
Constructors Constructor Description HttpContentDecoder()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
channelInactive(ChannelHandlerContext ctx)
CallsChannelHandlerContext.fireChannelInactive()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.void
channelReadComplete(ChannelHandlerContext ctx)
CallsChannelHandlerContext.fireChannelReadComplete()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.private void
cleanup()
private void
cleanupSafely(ChannelHandlerContext ctx)
private void
decode(ByteBuf in, java.util.List<java.lang.Object> out)
protected void
decode(ChannelHandlerContext ctx, HttpObject msg, java.util.List<java.lang.Object> out)
Decode from one message to an other.private void
decodeContent(HttpContent c, java.util.List<java.lang.Object> out)
private void
fetchDecoderOutput(java.util.List<java.lang.Object> out)
private void
finishDecode(java.util.List<java.lang.Object> out)
protected java.lang.String
getTargetContentEncoding(java.lang.String contentEncoding)
Returns the expected content encoding of the decoded content.void
handlerAdded(ChannelHandlerContext ctx)
Do nothing by default, sub-classes may override this method.void
handlerRemoved(ChannelHandlerContext ctx)
Do nothing by default, sub-classes may override this method.protected abstract EmbeddedChannel
newContentDecoder(java.lang.String contentEncoding)
Returns a newEmbeddedChannel
that decodes the HTTP message content encoded in the specified contentEncoding.-
Methods inherited from class io.netty.handler.codec.MessageToMessageDecoder
acceptInboundMessage, channelRead
-
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, isSharable
-
-
-
-
Field Detail
-
IDENTITY
static final java.lang.String IDENTITY
-
ctx
protected ChannelHandlerContext ctx
-
decoder
private EmbeddedChannel decoder
-
continueResponse
private boolean continueResponse
-
needRead
private boolean needRead
-
-
Method Detail
-
decode
protected void decode(ChannelHandlerContext ctx, HttpObject msg, java.util.List<java.lang.Object> out) throws java.lang.Exception
Description copied from class:MessageToMessageDecoder
Decode from one message to an other. This method will be called for each written message that can be handled by this decoder.- Specified by:
decode
in classMessageToMessageDecoder<HttpObject>
- Parameters:
ctx
- theChannelHandlerContext
which thisMessageToMessageDecoder
belongs tomsg
- the message to decode to an other oneout
- theList
to which decoded messages should be added- Throws:
java.lang.Exception
- is thrown if an error occurs
-
decodeContent
private void decodeContent(HttpContent c, java.util.List<java.lang.Object> out)
-
channelReadComplete
public void channelReadComplete(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelReadComplete()
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelReadComplete
in interfaceChannelInboundHandler
- Overrides:
channelReadComplete
in classChannelInboundHandlerAdapter
- Throws:
java.lang.Exception
-
newContentDecoder
protected abstract EmbeddedChannel newContentDecoder(java.lang.String contentEncoding) throws java.lang.Exception
Returns a newEmbeddedChannel
that decodes the HTTP message content encoded in the specified contentEncoding.- Parameters:
contentEncoding
- the value of the"Content-Encoding"
header- Returns:
- a new
EmbeddedChannel
if the specified encoding is supported.null
otherwise (alternatively, you can throw an exception to block unknown encoding). - Throws:
java.lang.Exception
-
getTargetContentEncoding
protected java.lang.String getTargetContentEncoding(java.lang.String contentEncoding) throws java.lang.Exception
Returns the expected content encoding of the decoded content. This getMethod returns"identity"
by default, which is the case for most decoders.- Parameters:
contentEncoding
- the value of the"Content-Encoding"
header- Returns:
- the expected content encoding of the new content
- 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
-
handlerAdded
public void handlerAdded(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelHandlerAdapter
Do nothing by default, sub-classes may override this method.- Specified by:
handlerAdded
in interfaceChannelHandler
- Overrides:
handlerAdded
in classChannelHandlerAdapter
- Throws:
java.lang.Exception
-
cleanup
private void cleanup()
-
cleanupSafely
private void cleanupSafely(ChannelHandlerContext ctx)
-
decode
private void decode(ByteBuf in, java.util.List<java.lang.Object> out)
-
finishDecode
private void finishDecode(java.util.List<java.lang.Object> out)
-
fetchDecoderOutput
private void fetchDecoderOutput(java.util.List<java.lang.Object> out)
-
-