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 anHttpMessageor anHttpContentinto 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 intCOPY_CONTENT_THRESHOLDprivate static ByteBufCRLF_BUF(package private) static intCRLF_SHORTprivate static floatHEADERS_WEIGHT_HISTORICALprivate static floatHEADERS_WEIGHT_NEWprivate floatheadersEncodedSizeAccumulatorUsed 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 java.util.List<java.lang.Object>outprivate static intST_CONTENT_ALWAYS_EMPTYprivate static intST_CONTENT_CHUNKprivate static intST_CONTENT_NON_CHUNKprivate static intST_INITprivate intstateprivate static floatTRAILERS_WEIGHT_HISTORICALprivate static floatTRAILERS_WEIGHT_NEWprivate floattrailersEncodedSizeAccumulatorUsed 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_CRLFprivate static ByteBufZERO_CRLF_CRLF_BUFprivate static intZERO_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 booleanacceptOutboundMessage(java.lang.Object msg)Returnstrueif the given message should be handled.private static voidaddEncodedLengthHex(ChannelHandlerContext ctx, long contentLength, java.util.List<java.lang.Object> out)private static booleanbypassEncoderIfEmpty(ByteBuf msg, java.util.List<java.lang.Object> out)private static booleancheckContentState(int state)protected voidencode(ChannelHandlerContext ctx, java.lang.Object msg, java.util.List<java.lang.Object> out)Encode from one message to an other.protected static voidencodeAscii(java.lang.String s, ByteBuf buf)Deprecated.private voidencodeByteBufAndTrailers(int state, ChannelHandlerContext ctx, java.util.List<java.lang.Object> out, ByteBuf content, HttpHeaders trailingHeaders)private voidencodeByteBufContent(ChannelHandlerContext ctx, ByteBuf content, java.util.List<java.lang.Object> out)private voidencodeByteBufHttpContent(int state, ChannelHandlerContext ctx, ByteBuf buf, ByteBuf content, HttpHeaders trailingHeaders, java.util.List<java.lang.Object> out)private voidencodeChunkedHttpContent(ChannelHandlerContext ctx, ByteBuf content, HttpHeaders trailingHeaders, java.util.List<java.lang.Object> out)private static booleanencodeContentNonChunk(java.util.List<java.lang.Object> out, ByteBuf buf, ByteBuf content)private static voidencodedChunkedFileRegionContent(ChannelHandlerContext ctx, FileRegion msg, java.util.List<java.lang.Object> out)private static intencodeEmptyLastHttpContent(int state, java.util.List<java.lang.Object> out)private voidencodeFileRegionContent(ChannelHandlerContext ctx, FileRegion msg, java.util.List<java.lang.Object> out)private voidencodeFullHttpMessage(ChannelHandlerContext ctx, java.lang.Object o, java.util.List<java.lang.Object> out)protected voidencodeHeaders(HttpHeaders headers, ByteBuf buf)Encode theHttpHeadersinto aByteBuf.private voidencodeHttpContent(ChannelHandlerContext ctx, HttpContent msg, java.util.List<java.lang.Object> out)private voidencodeHttpMessageLastContent(ChannelHandlerContext ctx, H m, java.util.List<java.lang.Object> out)private voidencodeHttpMessageNotLastContent(ChannelHandlerContext ctx, H m, java.util.List<java.lang.Object> out)private ByteBufencodeInitHttpMessage(ChannelHandlerContext ctx, H m)protected abstract voidencodeInitialLine(ByteBuf buf, H message)private voidencodeJustHttpMessage(ChannelHandlerContext ctx, H m, java.util.List<java.lang.Object> out)private voidencodeLastHttpContent(ChannelHandlerContext ctx, LastHttpContent msg, java.util.List<java.lang.Object> out)private voidencodeNotHttpMessageContentTypes(ChannelHandlerContext ctx, java.lang.Object msg, java.util.List<java.lang.Object> out)private voidencodeTrailingHeaders(ChannelHandlerContext ctx, HttpHeaders trailingHeaders, java.util.List<java.lang.Object> out)protected booleanisContentAlwaysEmpty(H msg)Determine whether a message has a content or not.private static intpadSizeForAccumulation(int readableBytes)Add some additional overhead to the buffer.protected voidsanitizeHeadersBeforeEncode(H msg, boolean isAlwaysEmpty)Allows to sanitize headers of the message before encoding these.private static voidthrowUnexpectedMessageTypeEx(java.lang.Object msg, int state)voidwrite(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise)CallsChannelOutboundInvoker.write(Object, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.private static voidwriteOutList(ChannelHandlerContext ctx, java.util.List<java.lang.Object> out, ChannelPromise promise)private static voidwritePromiseCombiner(ChannelHandlerContext ctx, java.util.List<java.lang.Object> out, ChannelPromise promise)private static voidwriteVoidPromise(ChannelHandlerContext ctx, java.util.List<java.lang.Object> out)-
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
-
COPY_CONTENT_THRESHOLD
private static final int COPY_CONTENT_THRESHOLD
- See Also:
- Constant Field Values
-
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.
-
out
private final java.util.List<java.lang.Object> out
-
-
Method Detail
-
checkContentState
private static boolean checkContentState(int state)
-
write
public void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.Exception
Description copied from class:ChannelOutboundHandlerAdapterCallsChannelOutboundInvoker.write(Object, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
writein interfaceChannelOutboundHandler- Overrides:
writein classMessageToMessageEncoder<java.lang.Object>- Parameters:
ctx- theChannelHandlerContextfor which the write operation is mademsg- the message to writepromise- theChannelPromiseto notify once the operation completes- Throws:
java.lang.Exception- thrown if an error occurs
-
writeOutList
private static void writeOutList(ChannelHandlerContext ctx, java.util.List<java.lang.Object> out, ChannelPromise promise)
-
writeVoidPromise
private static void writeVoidPromise(ChannelHandlerContext ctx, java.util.List<java.lang.Object> out)
-
writePromiseCombiner
private static void writePromiseCombiner(ChannelHandlerContext ctx, java.util.List<java.lang.Object> out, ChannelPromise promise)
-
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:MessageToMessageEncoderEncode from one message to an other. This method will be called for each written message that can be handled by this encoder.- Specified by:
encodein classMessageToMessageEncoder<java.lang.Object>- Parameters:
ctx- theChannelHandlerContextwhich thisMessageToMessageEncoderbelongs tomsg- the message to encode to an other oneout- theListinto which the encoded msg should be added needs to do some kind of aggregation- Throws:
java.lang.Exception- is thrown if an error occurs
-
encodeJustHttpMessage
private void encodeJustHttpMessage(ChannelHandlerContext ctx, H m, java.util.List<java.lang.Object> out) throws java.lang.Exception
- Throws:
java.lang.Exception
-
encodeByteBufHttpContent
private void encodeByteBufHttpContent(int state, ChannelHandlerContext ctx, ByteBuf buf, ByteBuf content, HttpHeaders trailingHeaders, java.util.List<java.lang.Object> out)
-
encodeHttpMessageNotLastContent
private void encodeHttpMessageNotLastContent(ChannelHandlerContext ctx, H m, java.util.List<java.lang.Object> out) throws java.lang.Exception
- Throws:
java.lang.Exception
-
encodeHttpMessageLastContent
private void encodeHttpMessageLastContent(ChannelHandlerContext ctx, H m, java.util.List<java.lang.Object> out) throws java.lang.Exception
- Throws:
java.lang.Exception
-
encodeNotHttpMessageContentTypes
private void encodeNotHttpMessageContentTypes(ChannelHandlerContext ctx, java.lang.Object msg, java.util.List<java.lang.Object> out)
-
encodeFullHttpMessage
private void encodeFullHttpMessage(ChannelHandlerContext ctx, java.lang.Object o, java.util.List<java.lang.Object> out) throws java.lang.Exception
- Throws:
java.lang.Exception
-
encodeContentNonChunk
private static boolean encodeContentNonChunk(java.util.List<java.lang.Object> out, ByteBuf buf, ByteBuf content)
-
throwUnexpectedMessageTypeEx
private static void throwUnexpectedMessageTypeEx(java.lang.Object msg, int state)
-
encodeFileRegionContent
private void encodeFileRegionContent(ChannelHandlerContext ctx, FileRegion msg, java.util.List<java.lang.Object> out)
-
bypassEncoderIfEmpty
private static boolean bypassEncoderIfEmpty(ByteBuf msg, java.util.List<java.lang.Object> out)
-
encodeByteBufContent
private void encodeByteBufContent(ChannelHandlerContext ctx, ByteBuf content, java.util.List<java.lang.Object> out)
-
encodeEmptyLastHttpContent
private static int encodeEmptyLastHttpContent(int state, java.util.List<java.lang.Object> out)
-
encodeLastHttpContent
private void encodeLastHttpContent(ChannelHandlerContext ctx, LastHttpContent msg, java.util.List<java.lang.Object> out)
-
encodeHttpContent
private void encodeHttpContent(ChannelHandlerContext ctx, HttpContent msg, java.util.List<java.lang.Object> out)
-
encodeByteBufAndTrailers
private void encodeByteBufAndTrailers(int state, ChannelHandlerContext ctx, java.util.List<java.lang.Object> out, ByteBuf content, HttpHeaders trailingHeaders)
-
encodeChunkedHttpContent
private void encodeChunkedHttpContent(ChannelHandlerContext ctx, ByteBuf content, HttpHeaders trailingHeaders, java.util.List<java.lang.Object> out)
-
encodeTrailingHeaders
private void encodeTrailingHeaders(ChannelHandlerContext ctx, HttpHeaders trailingHeaders, java.util.List<java.lang.Object> out)
-
encodeInitHttpMessage
private ByteBuf encodeInitHttpMessage(ChannelHandlerContext ctx, H m) throws java.lang.Exception
- Throws:
java.lang.Exception
-
encodeHeaders
protected void encodeHeaders(HttpHeaders headers, ByteBuf buf)
Encode theHttpHeadersinto aByteBuf.
-
encodedChunkedFileRegionContent
private static void encodedChunkedFileRegionContent(ChannelHandlerContext ctx, FileRegion msg, java.util.List<java.lang.Object> out)
-
addEncodedLengthHex
private static void addEncodedLengthHex(ChannelHandlerContext ctx, 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:
trueto signal the message has no content
-
acceptOutboundMessage
public boolean acceptOutboundMessage(java.lang.Object msg) throws java.lang.ExceptionDescription copied from class:MessageToMessageEncoderReturnstrueif the given message should be handled. Iffalseit will be passed to the nextChannelOutboundHandlerin theChannelPipeline.- Overrides:
acceptOutboundMessagein classMessageToMessageEncoder<java.lang.Object>- Throws:
java.lang.Exception
-
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
readableByteswith some additional padding.
-
encodeAscii
@Deprecated protected static void encodeAscii(java.lang.String s, ByteBuf buf)Deprecated.
-
-