Interface HttpMessage<T extends HttpMessage<T>>
- Type Parameters:
T- the generic type of the class implementing this interface.
- All Known Subinterfaces:
HttpRequest,HttpResponse
- All Known Implementing Classes:
NettyHttpServerRequest,NettyHttpServerResponse
public interface HttpMessage<T extends HttpMessage<T>>
Represents a http message sent from or to the server. The common use case is a http request and response.
- Since:
- 4.0
- See Also:
-
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.removeHeader(@NonNull String name) Removes a header by the given name from this message.version()Get the http version of this http message.version(@NonNull HttpVersion version) Sets the version of this http message.
-
Method Details
-
context
Get the context this message is processed in.- Returns:
- the associated context of this message.
-
header
Get a header value by the given name.- Parameters:
name- the name of the header to get.- Returns:
- the header value or null if the header does not exist.
- Throws:
NullPointerException- if the given name is null.
-
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.- Parameters:
name- the name of the header to get.- Returns:
- the header value converted to an int or null.
- Throws:
NullPointerException- if the given name is 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.- Parameters:
name- the name of the header to get.- Returns:
- the value converted to a bool or false if the header does not exist.
- Throws:
NullPointerException- if the given name is null.
-
header
Sets the given header, replacing the current one if already set.- 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.
- Throws:
NullPointerException- if either the given name or value is null.
-
removeHeader
Removes a header by the given name from this message.- Parameters:
name- the name of the header to remove.- Returns:
- the same message as used to call the method, for chaining.
- Throws:
NullPointerException- if the given name is null.
-
clearHeaders
Removes all headers from this message.- 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.- Parameters:
name- the name of the header to check.- Returns:
- true if the header is present, false otherwise.
- Throws:
NullPointerException- if the given name is null.
-
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.- 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.- Returns:
- the version of this http message.
-
version
Sets the version of this http message.- Parameters:
version- the version to use.- Returns:
- the same message as used to call the method, for chaining.
- Throws:
NullPointerException- if the given http version is null.
-
body
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, preferbodyStream()instead.- Returns:
- the body content of this http message.
-
bodyAsString
Converts the body of the http message into an utf-8 encoded string.- 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 usingbody(InputStream)instead.- Parameters:
byteArray- the body as a byte array.- Returns:
- the same message as used to call the method, for chaining.
- Throws:
UnsupportedOperationException- if setting the body is not supported for the http message.
-
body
Sets the body to the given string. This might be useful to e.g. return a json response.- Parameters:
text- the body as a string.- Returns:
- the same message as used to call the method, for chaining.
- Throws:
UnsupportedOperationException- if setting the body is not supported for the http message.
-
bodyStream
@UnknownNullability InputStream 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.- 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.- Parameters:
body- the new body stream to use.- Returns:
- the same message as used to call the method, for chaining.
- Throws:
UnsupportedOperationException- if setting the body is not supported for the http message.
-
hasBody
boolean hasBody()Get if the current http message has a body present.- Returns:
- if a body is present.
-