Class NettyHttpServerRequest
java.lang.Object
eu.cloudnetservice.driver.network.netty.http.NettyHttpMessage
eu.cloudnetservice.driver.network.netty.http.NettyHttpServerRequest
- All Implemented Interfaces:
HttpMessage<HttpRequest>,HttpRequest
The default netty based implementation of a http request.
- Since:
- 4.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate byte[]private final NettyHttpServerContextprivate final io.netty5.handler.codec.http.HttpRequestprivate final URI -
Constructor Summary
ConstructorsConstructorDescriptionNettyHttpServerRequest(@NonNull NettyHttpServerContext context, io.netty5.handler.codec.http.HttpRequest httpRequest, @NonNull Map<String, String> pathParameters, @NonNull URI uri) Constructs a new netty http request instance. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]body()Get the body content of this http message converted to a byte array.body(byte[] byteArray) Sets the body of the http message.Sets the body to the given string.body(@Nullable InputStream body) Sets the body of this http message to the given input stream.Converts the body of the http message into an utf-8 encoded string.Get the body as a streamable content source to reduce the heap load when reading the body content.Removes all headers from this message.context()Get the context this message is processed in.booleanhasBody()Get if the current http message has a body present.booleanChecks if this message has a header with the given name set.Get a header value by the given name.Sets the given header, replacing the current one if already set.booleanheaderAsBoolean(@NonNull String name) Get the header value by the given name, converted to a bool.headerAsInt(@NonNull String name) Get a header value by the given name, converted to an int.headers()Get all headers of this message converted to a map.method()Get the method used for this request.path()Get the full, originally requested path.The path parameters of the request, parsed before processing the next handler in the chain.Get all query parameters mapped by the key of it to the value.removeHeader(@NonNull String name) Removes a header by the given name from this message.uri()Get the full requested uri.version()Get the http version of this http message.version(@NonNull HttpVersion version) Sets the version of this http message.Methods inherited from class eu.cloudnetservice.driver.network.netty.http.NettyHttpMessage
versionFromNetty, versionToNetty
-
Field Details
-
context
-
uri
-
httpRequest
private final io.netty5.handler.codec.http.HttpRequest httpRequest -
pathParameters
-
queryParameters
-
body
private byte[] body
-
-
Constructor Details
-
NettyHttpServerRequest
public NettyHttpServerRequest(@NonNull @NonNull NettyHttpServerContext context, @NonNull io.netty5.handler.codec.http.HttpRequest httpRequest, @NonNull @NonNull Map<String, String> pathParameters, @NonNull @NonNull URI uri) Constructs a new netty http request instance.- Parameters:
context- the context in which the request is processed.httpRequest- the original netty request which gets wrapped.pathParameters- the extracted path parameters from the uri.uri- the original uri of the request.- Throws:
NullPointerException- if one of the given properties is null.
-
-
Method Details
-
pathParameters
The path parameters of the request, parsed before processing the next handler in the chain. A handler can provide a path parameter by wrapping its name into {name} would result as a key in this map at the given path index. For example the path/docs/{topic}/index/{page}would contain the path parameters topic and page, parsed from the request uri before calling the request handler. Each handler can access the path parameters of handlers beforehand, however duplicate path parameter names will override each other.- Specified by:
pathParametersin interfaceHttpRequest- Returns:
- all parsed path parameters for the current request.
-
path
Get the full, originally requested path.- Specified by:
pathin interfaceHttpRequest- Returns:
- the full, originally requested path.
-
uri
Get the full requested uri.- Specified by:
uriin interfaceHttpRequest- Returns:
- the full requested uri.
-
method
Get the method used for this request. The method might be one of- OPTIONS
- GET
- HEAD
- POST
- PUT
- PATCH
- DELETE
- TRACE
- CONNECT
See the mdn documentation for more information about http request methods.
- Specified by:
methodin interfaceHttpRequest- Returns:
- the request method of the request.
-
queryParameters
Get all query parameters mapped by the key of it to the value. Each query parameter can have multiple values set. The maximum amount of query parameters which will get decoded for a request are 1024.- Specified by:
queryParametersin interfaceHttpRequest- Returns:
- the query parameters supplied in the request uri.
-
context
Get the context this message is processed in.- Specified by:
contextin interfaceHttpMessage<HttpRequest>- Returns:
- the associated context of this message.
-
header
Get a header value by the given name.- Specified by:
headerin interfaceHttpMessage<HttpRequest>- Parameters:
name- the name of the header to get.- Returns:
- the header value or null if the header does not exist.
-
headerAsInt
Get a header value by the given name, converted to an int. Returns null if the header cannot be converted to an int or does not exist.- Specified by:
headerAsIntin interfaceHttpMessage<HttpRequest>- Parameters:
name- the name of the header to get.- Returns:
- the header value converted to an int or null.
-
headerAsBoolean
Get the header value by the given name, converted to a bool. If the header does not exist the method defaults to false.- Specified by:
headerAsBooleanin interfaceHttpMessage<HttpRequest>- Parameters:
name- the name of the header to get.- Returns:
- the value converted to a bool or false if the header does not exist.
-
header
@NonNull public @NonNull HttpRequest header(@NonNull @NonNull String name, @NonNull @NonNull String value) Sets the given header, replacing the current one if already set.- Specified by:
headerin interfaceHttpMessage<HttpRequest>- Parameters:
name- the name of the header.value- the value of the header.- Returns:
- the same message as used to call the method, for chaining.
-
removeHeader
Removes a header by the given name from this message.- Specified by:
removeHeaderin interfaceHttpMessage<HttpRequest>- Parameters:
name- the name of the header to remove.- Returns:
- the same message as used to call the method, for chaining.
-
clearHeaders
Removes all headers from this message.- Specified by:
clearHeadersin interfaceHttpMessage<HttpRequest>- Returns:
- the same message as used to call the method, for chaining.
-
hasHeader
Checks if this message has a header with the given name set.- Specified by:
hasHeaderin interfaceHttpMessage<HttpRequest>- Parameters:
name- the name of the header to check.- Returns:
- true if the header is present, false otherwise.
-
headers
Get all headers of this message converted to a map. The key of the map represents the name of the header, the value of the map represents the value of the header. This map does not allow duplicates.- Specified by:
headersin interfaceHttpMessage<HttpRequest>- Returns:
- all headers of the message collected to a map, duplication free.
-
version
Get the http version of this http message. CloudNet currently only supports Http 1 and Http 1.1.- Specified by:
versionin interfaceHttpMessage<HttpRequest>- Returns:
- the version of this http message.
-
version
Sets the version of this http message.- Specified by:
versionin interfaceHttpMessage<HttpRequest>- Parameters:
version- the version to use.- Returns:
- the same message as used to call the method, for chaining.
-
body
public byte[] body()Get the body content of this http message converted to a byte array. If there is no need to read the body fully into the heap, preferHttpMessage.bodyStream()instead.- Specified by:
bodyin interfaceHttpMessage<HttpRequest>- Returns:
- the body content of this http message.
-
bodyAsString
Converts the body of the http message into an utf-8 encoded string.- Specified by:
bodyAsStringin interfaceHttpMessage<HttpRequest>- Returns:
- the body of the message converted to a string.
-
body
Sets the body of the http message. If there is no need to load the full body into the heap, prefer streaming the content by usingHttpMessage.body(InputStream)instead.- Specified by:
bodyin interfaceHttpMessage<HttpRequest>- Parameters:
byteArray- the body as a byte array.- Returns:
- the same message as used to call the method, for chaining.
-
body
Sets the body to the given string. This might be useful to e.g. return a json response.- Specified by:
bodyin interfaceHttpMessage<HttpRequest>- Parameters:
text- the body as a string.- Returns:
- the same message as used to call the method, for chaining.
-
bodyStream
Get the body as a streamable content source to reduce the heap load when reading the body content. This method returns null if no http body is provided in the message.- Specified by:
bodyStreamin interfaceHttpMessage<HttpRequest>- Returns:
- the body as a content stream, or null if the message has no http body.
-
body
Sets the body of this http message to the given input stream. Closing of the stream will be done automatically and should not be made after calling the method. Setting the body stream to null will remove the current body.- Specified by:
bodyin interfaceHttpMessage<HttpRequest>- Parameters:
body- the new body stream to use.- Returns:
- the same message as used to call the method, for chaining.
-
hasBody
public boolean hasBody()Get if the current http message has a body present.- Specified by:
hasBodyin interfaceHttpMessage<HttpRequest>- Returns:
- if a body is present.
-