Package io.netty.handler.codec.http
Class HttpObjectEncoder<H extends HttpMessage>
- java.lang.Object
-
- io.netty.channel.ChannelHandlerAdapter
-
- io.netty.channel.ChannelOutboundHandlerAdapter
-
- io.netty.handler.codec.MessageToMessageEncoder<java.lang.Object>
-
- io.netty.handler.codec.http.HttpObjectEncoder<H>
-
- All Implemented Interfaces:
ChannelHandler
,ChannelOutboundHandler
- Direct Known Subclasses:
HttpRequestEncoder
,HttpResponseEncoder
,RtspEncoder
,RtspObjectEncoder
public abstract class HttpObjectEncoder<H extends HttpMessage> extends MessageToMessageEncoder<java.lang.Object>
Encodes anHttpMessage
or anHttpContent
into aByteBuf
.Extensibility
Please note that this encoder is designed to be extended to implement a protocol derived from HTTP, such as RTSP and ICAP. To implement the encoder of such a derived protocol, extend this class and implement all abstract methods properly.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description private static ByteBuf
CRLF_BUF
(package private) static int
CRLF_SHORT
private static float
HEADERS_WEIGHT_HISTORICAL
private static float
HEADERS_WEIGHT_NEW
private float
headersEncodedSizeAccumulator
Used to calculate an exponential moving average of the encoded size of the initial line and the headers for a guess for future buffer allocations.private static int
ST_CONTENT_ALWAYS_EMPTY
private static int
ST_CONTENT_CHUNK
private static int
ST_CONTENT_NON_CHUNK
private static int
ST_INIT
private int
state
private static float
TRAILERS_WEIGHT_HISTORICAL
private static float
TRAILERS_WEIGHT_NEW
private float
trailersEncodedSizeAccumulator
Used to calculate an exponential moving average of the encoded size of the trailers for a guess for future buffer allocations.private static byte[]
ZERO_CRLF_CRLF
private static ByteBuf
ZERO_CRLF_CRLF_BUF
private static int
ZERO_CRLF_MEDIUM
-
Constructor Summary
Constructors Constructor Description HttpObjectEncoder()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description boolean
acceptOutboundMessage(java.lang.Object msg)
Returnstrue
if the given message should be handled.private static long
contentLength(java.lang.Object msg)
protected void
encode(ChannelHandlerContext ctx, java.lang.Object msg, java.util.List<java.lang.Object> out)
Encode from one message to an other.private static java.lang.Object
encodeAndRetain(java.lang.Object msg)
protected static void
encodeAscii(java.lang.String s, ByteBuf buf)
Deprecated.private void
encodeChunkedContent(ChannelHandlerContext ctx, java.lang.Object msg, long contentLength, java.util.List<java.lang.Object> out)
protected void
encodeHeaders(HttpHeaders headers, ByteBuf buf)
Encode theHttpHeaders
into aByteBuf
.protected abstract void
encodeInitialLine(ByteBuf buf, H message)
protected boolean
isContentAlwaysEmpty(H msg)
Determine whether a message has a content or not.private static int
padSizeForAccumulation(int readableBytes)
Add some additional overhead to the buffer.protected void
sanitizeHeadersBeforeEncode(H msg, boolean isAlwaysEmpty)
Allows to sanitize headers of the message before encoding these.-
Methods inherited from class io.netty.handler.codec.MessageToMessageEncoder
write
-
Methods inherited from class io.netty.channel.ChannelOutboundHandlerAdapter
bind, close, connect, deregister, disconnect, flush, read
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, exceptionCaught, handlerAdded, handlerRemoved, 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
exceptionCaught, handlerAdded, handlerRemoved
-
-
-
-
Field Detail
-
CRLF_SHORT
static final int CRLF_SHORT
- See Also:
- Constant Field Values
-
ZERO_CRLF_MEDIUM
private static final int ZERO_CRLF_MEDIUM
- See Also:
- Constant Field Values
-
ZERO_CRLF_CRLF
private static final byte[] ZERO_CRLF_CRLF
-
CRLF_BUF
private static final ByteBuf CRLF_BUF
-
ZERO_CRLF_CRLF_BUF
private static final ByteBuf ZERO_CRLF_CRLF_BUF
-
HEADERS_WEIGHT_NEW
private static final float HEADERS_WEIGHT_NEW
- See Also:
- Constant Field Values
-
HEADERS_WEIGHT_HISTORICAL
private static final float HEADERS_WEIGHT_HISTORICAL
- See Also:
- Constant Field Values
-
TRAILERS_WEIGHT_NEW
private static final float TRAILERS_WEIGHT_NEW
- See Also:
- Constant Field Values
-
TRAILERS_WEIGHT_HISTORICAL
private static final float TRAILERS_WEIGHT_HISTORICAL
- See Also:
- Constant Field Values
-
ST_INIT
private static final int ST_INIT
- See Also:
- Constant Field Values
-
ST_CONTENT_NON_CHUNK
private static final int ST_CONTENT_NON_CHUNK
- See Also:
- Constant Field Values
-
ST_CONTENT_CHUNK
private static final int ST_CONTENT_CHUNK
- See Also:
- Constant Field Values
-
ST_CONTENT_ALWAYS_EMPTY
private static final int ST_CONTENT_ALWAYS_EMPTY
- See Also:
- Constant Field Values
-
state
private int state
-
headersEncodedSizeAccumulator
private float headersEncodedSizeAccumulator
Used to calculate an exponential moving average of the encoded size of the initial line and the headers for a guess for future buffer allocations.
-
trailersEncodedSizeAccumulator
private float trailersEncodedSizeAccumulator
Used to calculate an exponential moving average of the encoded size of the trailers for a guess for future buffer allocations.
-
-
Method Detail
-
encode
protected void encode(ChannelHandlerContext ctx, java.lang.Object msg, java.util.List<java.lang.Object> out) throws java.lang.Exception
Description copied from class:MessageToMessageEncoder
Encode from one message to an other. This method will be called for each written message that can be handled by this encoder.- Specified by:
encode
in classMessageToMessageEncoder<java.lang.Object>
- Parameters:
ctx
- theChannelHandlerContext
which thisMessageToMessageEncoder
belongs tomsg
- the message to encode to an other oneout
- theList
into which the encoded msg should be added needs to do some kind of aggregation- Throws:
java.lang.Exception
- is thrown if an error occurs
-
encodeHeaders
protected void encodeHeaders(HttpHeaders headers, ByteBuf buf)
Encode theHttpHeaders
into aByteBuf
.
-
encodeChunkedContent
private void encodeChunkedContent(ChannelHandlerContext ctx, java.lang.Object msg, long contentLength, java.util.List<java.lang.Object> out)
-
sanitizeHeadersBeforeEncode
protected void sanitizeHeadersBeforeEncode(H msg, boolean isAlwaysEmpty)
Allows to sanitize headers of the message before encoding these.
-
isContentAlwaysEmpty
protected boolean isContentAlwaysEmpty(H msg)
Determine whether a message has a content or not. Some message may have headers indicating a content without having an actual content, e.g the response to an HEAD or CONNECT request.- Parameters:
msg
- the message to test- Returns:
true
to signal the message has no content
-
acceptOutboundMessage
public boolean acceptOutboundMessage(java.lang.Object msg) throws java.lang.Exception
Description copied from class:MessageToMessageEncoder
Returnstrue
if the given message should be handled. Iffalse
it will be passed to the nextChannelOutboundHandler
in theChannelPipeline
.- Overrides:
acceptOutboundMessage
in classMessageToMessageEncoder<java.lang.Object>
- Throws:
java.lang.Exception
-
encodeAndRetain
private static java.lang.Object encodeAndRetain(java.lang.Object msg)
-
contentLength
private static long contentLength(java.lang.Object msg)
-
padSizeForAccumulation
private static int padSizeForAccumulation(int readableBytes)
Add some additional overhead to the buffer. The rational is that it is better to slightly over allocate and waste some memory, rather than under allocate and require a resize/copy.- Parameters:
readableBytes
- The readable bytes in the buffer.- Returns:
- The
readableBytes
with some additional padding.
-
encodeAscii
@Deprecated protected static void encodeAscii(java.lang.String s, ByteBuf buf)
Deprecated.
-
-