Class NettyHttpServerResponse
java.lang.Object
eu.cloudnetservice.driver.network.netty.http.NettyHttpMessage
eu.cloudnetservice.driver.network.netty.http.NettyHttpServerResponse
- All Implemented Interfaces:
HttpMessage<HttpResponse>,HttpResponse
The default netty based implementation of a http response.
- Since:
- 4.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final NettyHttpServerContext(package private) final io.netty5.handler.codec.http.FullHttpResponseprivate InputStream -
Constructor Summary
ConstructorsConstructorDescriptionNettyHttpServerResponse(@NonNull NettyHttpServerContext context, @NonNull io.netty5.handler.codec.http.HttpRequest httpRequest) Constructs a new netty http response 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(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.removeHeader(@NonNull String name) Removes a header by the given name from this message.status()The status code of the http response.status(@NonNull HttpResponseCode code) Sets the status of this response.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
-
httpResponse
final io.netty5.handler.codec.http.FullHttpResponse httpResponse -
context
-
responseInputStream
-
-
Constructor Details
-
NettyHttpServerResponse
public NettyHttpServerResponse(@NonNull @NonNull NettyHttpServerContext context, @NonNull @NonNull io.netty5.handler.codec.http.HttpRequest httpRequest) Constructs a new netty http response instance.- Parameters:
context- the context in which the request (and this response to the request) is handled.httpRequest- the original unwrapped request sent to the server.- Throws:
NullPointerException- if either the context or request is null.
-
-
Method Details
-
status
The status code of the http response. The response status code set here should, but must not be a valid one. See the mdn documentation about status codes for more information.- Specified by:
statusin interfaceHttpResponse- Returns:
- the http response code of this response.
-
status
Sets the status of this response.- Specified by:
statusin interfaceHttpResponse- Parameters:
code- the new response status.- Returns:
- the same instance as used to call the method, for chaining.
-
context
Get the context this message is processed in.- Specified by:
contextin interfaceHttpMessage<HttpResponse>- Returns:
- the associated context of this message.
-
header
Get a header value by the given name.- Specified by:
headerin interfaceHttpMessage<HttpResponse>- 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<HttpResponse>- 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<HttpResponse>- 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 HttpResponse header(@NonNull @NonNull String name, @NonNull @NonNull String value) Sets the given header, replacing the current one if already set.- Specified by:
headerin interfaceHttpMessage<HttpResponse>- 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<HttpResponse>- 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<HttpResponse>- 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<HttpResponse>- 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<HttpResponse>- 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<HttpResponse>- Returns:
- the version of this http message.
-
version
Sets the version of this http message.- Specified by:
versionin interfaceHttpMessage<HttpResponse>- 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<HttpResponse>- 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<HttpResponse>- 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<HttpResponse>- 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<HttpResponse>- 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<HttpResponse>- 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<HttpResponse>- 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<HttpResponse>- Returns:
- if a body is present.
-