Class HttpStreamsServerHandler
- java.lang.Object
-
- io.netty.channel.ChannelHandlerAdapter
-
- io.netty.channel.ChannelInboundHandlerAdapter
-
- io.netty.channel.ChannelDuplexHandler
-
- io.micronaut.http.netty.stream.HttpStreamsServerHandler
-
- All Implemented Interfaces:
io.netty.channel.ChannelHandler,io.netty.channel.ChannelInboundHandler,io.netty.channel.ChannelOutboundHandler
@Internal public class HttpStreamsServerHandler extends io.netty.channel.ChannelDuplexHandlerHandler that readsHttpRequestmessages followed byHttpContentmessages and producesStreamedHttpRequestmessages, and converts writtenStreamedHttpResponsemessages intoHttpResponsemessages followed byHttpContentmessages.This allows request and response bodies to be handled using reactive streams.
There are two types of messages that this handler will send down the chain,
StreamedHttpRequest, andFullHttpRequest. IfChannelOption.AUTO_READis false for the channel, then anyStreamedHttpRequestmessages must be subscribed to consume the body, otherwise it's possible that no read will be done of the messages.There are three types of messages that this handler accepts for writing,
StreamedHttpResponse,WebSocketHttpResponseandFullHttpResponse. Writing any other messages may potentially lead to HTTP message mangling.As long as messages are returned in the order that they arrive, this handler implicitly supports HTTP pipelining.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringHANDLER_BODY_PUBLISHER
-
Constructor Summary
Constructors Constructor Description HttpStreamsServerHandler()Default constructor.HttpStreamsServerHandler(java.util.List<io.netty.channel.ChannelHandler> dependentHandlers)Create a new handler that is depended on by the given handlers.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidbodyRequested(io.netty.channel.ChannelHandlerContext ctx)Invoked every time a read of the incoming body is requested by the subscriber.voidchannelRead(io.netty.channel.ChannelHandlerContext ctx, java.lang.Object msg)voidchannelReadComplete(io.netty.channel.ChannelHandlerContext ctx)voidchannelWritabilityChanged(io.netty.channel.ChannelHandlerContext ctx)protected voidconsumedInMessage(io.netty.channel.ChannelHandlerContext ctx)Invoked when an incoming message is fully consumed.protected io.netty.handler.codec.http.HttpRequestcreateEmptyMessage(io.netty.handler.codec.http.HttpRequest request)Create an empty incoming message.protected io.netty.handler.codec.http.HttpRequestcreateStreamedMessage(io.netty.handler.codec.http.HttpRequest httpRequest, org.reactivestreams.Publisher<? extends io.netty.handler.codec.http.HttpContent> stream)Create a streamed incoming message with the given stream.protected intgetStreamId(java.lang.Object msg)Gets the stream ID from the message.voidhandlerRemoved(io.netty.channel.ChannelHandlerContext ctx)protected booleanhasBody(io.netty.handler.codec.http.HttpRequest request)Whether the given incoming message has a body.protected booleanisClient()protected booleanisValidInMessage(java.lang.Object msg)protected booleanisValidOutMessage(java.lang.Object msg)protected voidreceivedInMessage(io.netty.channel.ChannelHandlerContext ctx)Invoked when an incoming message is first received.protected voidreceivedOutMessage(io.netty.channel.ChannelHandlerContext ctx)Invoked when an outgoing message is first received.protected voidsentOutMessage(io.netty.channel.ChannelHandlerContext ctx)Invoked when an outgoing message is fully sent.protected voidsubscribeSubscriberToStream(StreamedHttpMessage msg, org.reactivestreams.Subscriber<io.netty.handler.codec.http.HttpContent> subscriber)Subscribe the given subscriber to the given streamed message.protected voidunbufferedWrite(io.netty.channel.ChannelHandlerContext ctx, io.netty.handler.codec.http.HttpResponse message, io.netty.channel.ChannelPromise promise)voidwrite(io.netty.channel.ChannelHandlerContext ctx, java.lang.Object msg, io.netty.channel.ChannelPromise promise)-
Methods inherited from class io.netty.channel.ChannelDuplexHandler
bind, close, connect, deregister, disconnect, flush, read
-
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelInactive, channelRegistered, channelUnregistered, exceptionCaught, userEventTriggered
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, isSharable
-
-
-
-
Field Detail
-
HANDLER_BODY_PUBLISHER
public static final java.lang.String HANDLER_BODY_PUBLISHER
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
HttpStreamsServerHandler
public HttpStreamsServerHandler()
Default constructor.
-
HttpStreamsServerHandler
public HttpStreamsServerHandler(java.util.List<io.netty.channel.ChannelHandler> dependentHandlers)
Create a new handler that is depended on by the given handlers.The list of dependent handlers will be removed from the chain when this handler is removed from the chain, for example, when the connection is upgraded to use websockets. This is useful, for example, for removing the reactive streams publisher/subscriber from the chain in that event.
- Parameters:
dependentHandlers- The handlers that depend on this handler.
-
-
Method Detail
-
hasBody
protected boolean hasBody(io.netty.handler.codec.http.HttpRequest request)
Whether the given incoming message has a body.- Parameters:
request- The incoming message- Returns:
- Whether the incoming message has body
-
createEmptyMessage
protected io.netty.handler.codec.http.HttpRequest createEmptyMessage(io.netty.handler.codec.http.HttpRequest request)
Create an empty incoming message. This must be of type FullHttpMessage, and is invoked when we've determined that an incoming message can't have a body, so we send it on as a FullHttpMessage.- Parameters:
request- The incoming message- Returns:
- An empty incoming message
-
createStreamedMessage
protected io.netty.handler.codec.http.HttpRequest createStreamedMessage(io.netty.handler.codec.http.HttpRequest httpRequest, org.reactivestreams.Publisher<? extends io.netty.handler.codec.http.HttpContent> stream)Create a streamed incoming message with the given stream.- Parameters:
httpRequest- The incoming messagestream- The publisher for the Http Content- Returns:
- An streamed incoming message
-
channelRead
public void channelRead(io.netty.channel.ChannelHandlerContext ctx, java.lang.Object msg) throws java.lang.Exception- Specified by:
channelReadin interfaceio.netty.channel.ChannelInboundHandler- Throws:
java.lang.Exception
-
receivedInMessage
protected void receivedInMessage(io.netty.channel.ChannelHandlerContext ctx)
Invoked when an incoming message is first received.Overridden by sub classes for state tracking.
- Parameters:
ctx- The channel handler context
-
sentOutMessage
protected void sentOutMessage(io.netty.channel.ChannelHandlerContext ctx)
Invoked when an outgoing message is fully sent.Overridden by sub classes for state tracking.
- Parameters:
ctx- The channel handler context
-
unbufferedWrite
protected void unbufferedWrite(io.netty.channel.ChannelHandlerContext ctx, io.netty.handler.codec.http.HttpResponse message, io.netty.channel.ChannelPromise promise)- Parameters:
ctx- The channel handler contextmessage- The messagepromise- The promise
-
isValidOutMessage
protected boolean isValidOutMessage(java.lang.Object msg)
- Parameters:
msg- The message- Returns:
- True if the handler should write the message
-
consumedInMessage
protected void consumedInMessage(io.netty.channel.ChannelHandlerContext ctx)
Invoked when an incoming message is fully consumed.Overridden by sub classes for state tracking.
- Parameters:
ctx- The channel handler context
-
bodyRequested
protected void bodyRequested(io.netty.channel.ChannelHandlerContext ctx)
Invoked every time a read of the incoming body is requested by the subscriber.Provided so that the server subclass can intercept this to send a 100 continue response.
- Parameters:
ctx- The channel handler context
-
isClient
protected final boolean isClient()
- Returns:
- Whether this is the client stream handler.
-
handlerRemoved
public void handlerRemoved(io.netty.channel.ChannelHandlerContext ctx) throws java.lang.Exception- Specified by:
handlerRemovedin interfaceio.netty.channel.ChannelHandler- Overrides:
handlerRemovedin classio.netty.channel.ChannelHandlerAdapter- Throws:
java.lang.Exception
-
receivedOutMessage
protected void receivedOutMessage(io.netty.channel.ChannelHandlerContext ctx)
Invoked when an outgoing message is first received.Overridden by sub classes for state tracking.
- Parameters:
ctx- The channel handler context
-
subscribeSubscriberToStream
protected void subscribeSubscriberToStream(StreamedHttpMessage msg, org.reactivestreams.Subscriber<io.netty.handler.codec.http.HttpContent> subscriber)
Subscribe the given subscriber to the given streamed message.Provided so that the client subclass can intercept this to hold off sending the body of an expect 100 continue request.
- Parameters:
msg- The streamed Http messagesubscriber- The subscriber for the Http Content
-
getStreamId
protected int getStreamId(java.lang.Object msg)
Gets the stream ID from the message.- Parameters:
msg- The message- Returns:
- The stream id
-
channelReadComplete
public void channelReadComplete(io.netty.channel.ChannelHandlerContext ctx) throws java.lang.Exception- Specified by:
channelReadCompletein interfaceio.netty.channel.ChannelInboundHandler- Overrides:
channelReadCompletein classio.netty.channel.ChannelInboundHandlerAdapter- Throws:
java.lang.Exception
-
write
public void write(io.netty.channel.ChannelHandlerContext ctx, java.lang.Object msg, io.netty.channel.ChannelPromise promise) throws java.lang.Exception- Specified by:
writein interfaceio.netty.channel.ChannelOutboundHandler- Overrides:
writein classio.netty.channel.ChannelDuplexHandler- Throws:
java.lang.Exception
-
channelWritabilityChanged
public void channelWritabilityChanged(io.netty.channel.ChannelHandlerContext ctx)
- Specified by:
channelWritabilityChangedin interfaceio.netty.channel.ChannelInboundHandler- Overrides:
channelWritabilityChangedin classio.netty.channel.ChannelInboundHandlerAdapter
-
isValidInMessage
protected boolean isValidInMessage(java.lang.Object msg)
- Parameters:
msg- The message- Returns:
- True if the handler should read the message
-
-