Class HttpContentEncoder

java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.netty.channel.ChannelDuplexHandler
io.netty.handler.codec.MessageToMessageCodec<HttpRequest,HttpObject>
io.netty.handler.codec.http.HttpContentEncoder
All Implemented Interfaces:
io.netty.channel.ChannelHandler, io.netty.channel.ChannelInboundHandler, io.netty.channel.ChannelOutboundHandler
Direct Known Subclasses:
HttpContentCompressor

public abstract class HttpContentEncoder extends io.netty.handler.codec.MessageToMessageCodec<HttpRequest,HttpObject>
Encodes the content of the outbound 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 ByteBufs.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
     

    Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler

    io.netty.channel.ChannelHandler.Sharable
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
     
    protected abstract HttpContentEncoder.Result
    beginEncode(HttpResponse httpResponse, String acceptEncoding)
    Prepare to encode the HTTP message content.
    void
    channelInactive(io.netty.channel.ChannelHandlerContext ctx)
     
    protected void
    decode(io.netty.channel.ChannelHandlerContext ctx, HttpRequest msg, List<Object> out)
     
    protected void
    encode(io.netty.channel.ChannelHandlerContext ctx, HttpObject msg, List<Object> out)
     
    void
    handlerRemoved(io.netty.channel.ChannelHandlerContext ctx)
     

    Methods inherited from class io.netty.handler.codec.MessageToMessageCodec

    acceptInboundMessage, channelRead, write

    Methods inherited from class io.netty.channel.ChannelDuplexHandler

    bind, close, connect, deregister, disconnect, flush, read

    Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter

    channelActive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered

    Methods inherited from class io.netty.channel.ChannelHandlerAdapter

    ensureNotSharable, handlerAdded, isSharable

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface io.netty.channel.ChannelHandler

    handlerAdded
  • Constructor Details

    • HttpContentEncoder

      public HttpContentEncoder()
  • Method Details

    • acceptOutboundMessage

      public boolean acceptOutboundMessage(Object msg) throws Exception
      Overrides:
      acceptOutboundMessage in class io.netty.handler.codec.MessageToMessageCodec<HttpRequest,HttpObject>
      Throws:
      Exception
    • decode

      protected void decode(io.netty.channel.ChannelHandlerContext ctx, HttpRequest msg, List<Object> out) throws Exception
      Specified by:
      decode in class io.netty.handler.codec.MessageToMessageCodec<HttpRequest,HttpObject>
      Throws:
      Exception
    • encode

      protected void encode(io.netty.channel.ChannelHandlerContext ctx, HttpObject msg, List<Object> out) throws Exception
      Specified by:
      encode in class io.netty.handler.codec.MessageToMessageCodec<HttpRequest,HttpObject>
      Throws:
      Exception
    • beginEncode

      protected abstract HttpContentEncoder.Result beginEncode(HttpResponse httpResponse, String acceptEncoding) throws Exception
      Prepare to encode the HTTP message content.
      Parameters:
      httpResponse - the http response
      acceptEncoding - the value of the "Accept-Encoding" header
      Returns:
      the result of preparation, which is composed of the determined target content encoding and a new EmbeddedChannel 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).
      Throws:
      Exception
    • handlerRemoved

      public void handlerRemoved(io.netty.channel.ChannelHandlerContext ctx) throws Exception
      Specified by:
      handlerRemoved in interface io.netty.channel.ChannelHandler
      Overrides:
      handlerRemoved in class io.netty.channel.ChannelHandlerAdapter
      Throws:
      Exception
    • channelInactive

      public void channelInactive(io.netty.channel.ChannelHandlerContext ctx) throws Exception
      Specified by:
      channelInactive in interface io.netty.channel.ChannelInboundHandler
      Overrides:
      channelInactive in class io.netty.channel.ChannelInboundHandlerAdapter
      Throws:
      Exception