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 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

      boolean headerAsBoolean(@NonNull @NonNull String name)
      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

      @NonNull T removeHeader(@NonNull @NonNull String name)
      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

      @NonNull T clearHeaders()
      Removes all headers from this message.
      Returns:
      the same message as used to call the method, for chaining.
    • hasHeader

      boolean hasHeader(@NonNull @NonNull String name)
      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, prefer bodyStream() instead.
      Returns:
      the body content of this http message.
    • bodyAsString

      @NonNull @NonNull String 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

      @NonNull T body(byte[] byteArray)
      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 using body(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

      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.