Class NettyHttpServerContext
- All Implemented Interfaces:
HttpContext
- Since:
- 4.0
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) boolean(package private) booleanprivate final NettyHttpChannel(package private) booleanprivate final Collection<HttpCookie>private final io.netty5.handler.codec.http.HttpRequestprivate final NettyHttpServerRequest(package private) final NettyHttpServerResponseprivate HttpHandlerprivate final io.netty5.channel.Channelprivate final NettyHttpServerprivate Stringprivate NettyWebSocketServerChannel -
Constructor Summary
ConstructorsConstructorDescriptionNettyHttpServerContext(@NonNull NettyHttpServer nettyHttpServer, @NonNull NettyHttpChannel channel, @NonNull URI uri, @NonNull Map<String, String> pathParameters, io.netty5.handler.codec.http.HttpRequest httpRequest) Constructs a new netty http server context instance. -
Method Summary
Modifier and TypeMethodDescriptionaddCookie(@NonNull HttpCookie httpCookie) Adds the given cookie to the response, removing the current one if there is already a cookie with the same name.addInvocationHint(@NonNull String key, @NonNull Object value) Adds an invocation hint for the current handler call chain.<T> @NonNull HttpContextaddInvocationHints(@NonNull String key, @NonNull Collection<T> value) Adds an invocation hint for the current handler call chain.booleanSets the current handler as the last handler, cancelling the call of all handlers which are still waiting to be called down the line.cancelNext(boolean cancelNext) Sets whether the current handler should be the last handler in the listener call chain, cancelling the call of all handlers which are still waiting to be called down the line.channel()Get the channel to which the request came which is currently processed.Removes all cookies previously set in the response.booleanSets that the connection to the client should be terminated after the last handler in the chain.closeAfter(boolean value) Sets whether the connection to client should be closed after the last handler in the chain.Get the http component which received the request wrapped by this context.Gets a cookie by its name from the current request.cookies()Get all cookies which were set in the current request wrapped in this context.cookies(@NonNull Collection<HttpCookie> cookies) Sets the given cookies in the response, removing all previously set cookies.booleanGet if a cookie with the given name was sent by the client sending the request to the server.Get all invocation hints which are registered for the given key.Get the path of the handler, supplied while registering, which currently handling the request.voidpathPrefix(@NonNull String pathPrefix) Sets the current path prefix of the handler being processed.peekLast()Peeks (gets, but does not remove) the last handler in the chain.voidpushChain(@NonNull HttpHandler lastHandler) Sets the last handler which was processed in the processing chain, for later access from the next handler in the chain.removeCookie(@NonNull String name) Removes the given cookie from the response, if set.request()Get the current request which is handled in this context.response()Get the current response which will be sent to the client.@NonNull eu.cloudnetservice.common.concurrent.Task<WebSocketChannel>upgrade()Upgrades this context to a websocket connection.Get the web socket channel to which this request was upgraded.
-
Field Details
-
httpServerResponse
-
invocationHints
-
nettyChannel
private final io.netty5.channel.Channel nettyChannel -
httpRequest
private final io.netty5.handler.codec.http.HttpRequest httpRequest -
channel
-
nettyHttpServer
-
httpServerRequest
-
cookies
-
closeAfter
volatile boolean closeAfter -
cancelNext
volatile boolean cancelNext -
cancelSendResponse
volatile boolean cancelSendResponse -
pathPrefix
-
lastHandler
-
webSocketServerChannel
-
-
Constructor Details
-
NettyHttpServerContext
public NettyHttpServerContext(@NonNull @NonNull NettyHttpServer nettyHttpServer, @NonNull @NonNull NettyHttpChannel channel, @NonNull @NonNull URI uri, @NonNull @NonNull Map<String, String> pathParameters, @NonNull io.netty5.handler.codec.http.HttpRequest httpRequest) Constructs a new netty http server context instance.- Parameters:
nettyHttpServer- the http server which received the request handled by this context.channel- the channel to which the request was sent.uri- the uri of the request.pathParameters- the path parameters pre-parsed, by default an empty map.httpRequest- the http request which was received originally.- Throws:
NullPointerException- if one of the constructor parameters is null.
-
-
Method Details
-
upgrade
Upgrades this context to a websocket connection. In normal cases, as CloudNet currently only supports Http 1.X the client sends an upgrade header in its request to signal that an upgrade to a websocket channel is requested. See Mozilla - Upgrade docs for more information on how to use the header and the handshaking between the client and server works.This method blocks the current thread until the handshake processes completed. Either successfully or with a failure of any kind.
If the upgrade to a websocket connection is not possible, the method automatically answers with a reason text to the client why the upgrade failed. Either because the socket version is not supported, or an exception occurred. Supported web socket versions are:
- Wire protocol version 13 (RFC-6455/draft version 17 of the hybi specification)
If the upgrade fails this method returns a task which is completed exceptionally and sends a response to the client indicating the reason for the failure. After that, the connection is still able to send out http requests but will no longer accept / handle these.
- Specified by:
upgradein interfaceHttpContext- Returns:
- a task completed with the upgraded web socket channel or an exception if the upgrade was not possible.
-
webSocketChanel
Get the web socket channel to which this request was upgraded. If the request was not upgraded, or the upgrade was not possible this method returns null.- Specified by:
webSocketChanelin interfaceHttpContext- Returns:
- the upgraded web socket channel, or null if the connection upgrade was not done or failed.
-
channel
Get the channel to which the request came which is currently processed.- Specified by:
channelin interfaceHttpContext- Returns:
- the channel to which the request came.
-
request
Get the current request which is handled in this context.- Specified by:
requestin interfaceHttpContext- Returns:
- the current request.
-
response
Get the current response which will be sent to the client.- Specified by:
responsein interfaceHttpContext- Returns:
- the current response.
-
cancelNext
public boolean cancelNext()Sets the current handler as the last handler, cancelling the call of all handlers which are still waiting to be called down the line. This is useful when a handler should set the final result of the request, not allowing any handler to overwrite it.- Specified by:
cancelNextin interfaceHttpContext- Returns:
- true if the cancel state was updated, false otherwise.
-
cancelNext
Sets whether the current handler should be the last handler in the listener call chain, cancelling the call of all handlers which are still waiting to be called down the line. This is useful when a handler should set the final result of the request, not allowing any handler to overwrite it.- Specified by:
cancelNextin interfaceHttpContext- Parameters:
cancelNext- if the next handlers in the chain should be skipped.- Returns:
- the same instance of the context as used to call the method, for chaining.
-
peekLast
Peeks (gets, but does not remove) the last handler in the chain.- Specified by:
peekLastin interfaceHttpContext- Returns:
- the last handler in the chain, null if there are no handlers.
-
component
Get the http component which received the request wrapped by this context.- Specified by:
componentin interfaceHttpContext- Returns:
- the http component which received the request.
-
closeAfter
Sets whether the connection to client should be closed after the last handler in the chain. This defaults to true. If set to false, the connection will not be closed and theconnectionheader will automatically be set tokeep-alive. Mdn docs are providing a more in-depth walk through which other components may be set by a developer to customize the keep alive header.- Specified by:
closeAfterin interfaceHttpContext- Parameters:
value- true if the connection should get closed after the last handler, false to keep it open.- Returns:
- the same instance of the context as used to call the method, for chaining.
-
closeAfter
public boolean closeAfter()Sets that the connection to the client should be terminated after the last handler in the chain.- Specified by:
closeAfterin interfaceHttpContext- Returns:
- true if the closing state was updated, false otherwise.
-
cookie
Gets a cookie by its name from the current request. Cookies are decoded and encoded in a relaxed (lax) format, this means that duplicate cookies are allowed. If there are multiple cookies with the same name, the first one is returned by this method. If no cookie with the given name is present, this method returns null.- Specified by:
cookiein interfaceHttpContext- Parameters:
name- the name of the cookie to get.- Returns:
- the first cookie with the given name sent by the client.
-
cookies
Get all cookies which were set in the current request wrapped in this context. Cookies are decoded and encoded in a relaxed (lax) format, this means that duplicate cookies are allowed.- Specified by:
cookiesin interfaceHttpContext- Returns:
- all cookies set by in the wrapped request.
-
hasCookie
Get if a cookie with the given name was sent by the client sending the request to the server. Cookies are decoded and encoded in a relaxed (lax) format, this means that duplicate cookies are allowed. If there are multiple cookies with the same name set in the request this method returns true anyway.- Specified by:
hasCookiein interfaceHttpContext- Parameters:
name- the name of the cookie to check for.- Returns:
- true if a cookie with the given name is present, false otherwise.
-
cookies
Sets the given cookies in the response, removing all previously set cookies.- Specified by:
cookiesin interfaceHttpContext- Parameters:
cookies- the new cookies of the response.- Returns:
- the same instance of the context as used to call the method, for chaining.
-
addCookie
Adds the given cookie to the response, removing the current one if there is already a cookie with the same name.- Specified by:
addCookiein interfaceHttpContext- Parameters:
httpCookie- the cookie to add.- Returns:
- the same instance of the context as used to call the method, for chaining.
-
removeCookie
Removes the given cookie from the response, if set.- Specified by:
removeCookiein interfaceHttpContext- Parameters:
name- the name of the cookie to remove.- Returns:
- the same instance of the context as used to call the method, for chaining.
-
clearCookies
Removes all cookies previously set in the response.- Specified by:
clearCookiesin interfaceHttpContext- Returns:
- the same instance of the context as used to call the method, for chaining.
-
pathPrefix
Get the path of the handler, supplied while registering, which currently handling the request.- Specified by:
pathPrefixin interfaceHttpContext- Returns:
- the path of the handler handling the request.
-
invocationHints
Get all invocation hints which are registered for the given key. Invocation hints are only valid for the current handler call chain, and will be reset after a handler was called. Hints can for example get registered in a http context preprocessor.This method never returns null. If no hints are registered for the given key then an empty collection is returned.
- Specified by:
invocationHintsin interfaceHttpContext- Parameters:
key- the key of the hints to retrieve.- Returns:
- the registered hints for the given key, or an empty collection if no hints are registered.
-
addInvocationHint
@NonNull public @NonNull HttpContext addInvocationHint(@NonNull @NonNull String key, @NonNull @NonNull Object value) Adds an invocation hint for the current handler call chain. Invocation hints are only valid for the current handler call chain, and will be reset after a handler was called. Hints can for example get registered in a http context preprocessor.One key can be mapped to multiple hints.
- Specified by:
addInvocationHintin interfaceHttpContext- Parameters:
key- the key of the hint to register.value- the hint.- Returns:
- the same instance as used to call the method, for chaining.
-
addInvocationHints
@NonNull public <T> @NonNull HttpContext addInvocationHints(@NonNull @NonNull String key, @NonNull @NonNull Collection<T> value) Adds an invocation hint for the current handler call chain. Invocation hints are only valid for the current handler call chain, and will be reset after a handler was called. Hints can for example get registered in a http context preprocessor.One key can be mapped to multiple hints.
- Specified by:
addInvocationHintsin interfaceHttpContext- Parameters:
key- the key of the hint to register.value- the hints to add.- Returns:
- the same instance as used to call the method, for chaining.
-
pathPrefix
Sets the current path prefix of the handler being processed.- Parameters:
pathPrefix- the path prefix of the current handler.- Throws:
NullPointerException- if the given path prefix is null.
-
pushChain
Sets the last handler which was processed in the processing chain, for later access from the next handler in the chain. UsepeekLast()to get the previous handler in the chain.- Parameters:
lastHandler- the last processed handler in the chain.- Throws:
NullPointerException- if the last handler is null.
-