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

final class NettyHttpServerRequest extends NettyHttpMessage implements HttpRequest
The default netty based implementation of a http request.
Since:
4.0
  • Field Details

    • context

      private final NettyHttpServerContext context
    • uri

      private final URI uri
    • httpRequest

      private final io.netty5.handler.codec.http.HttpRequest httpRequest
    • pathParameters

      private final Map<String,String> pathParameters
    • queryParameters

      private final Map<String,List<String>> 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

      @NonNull public @NonNull Map<String,String> 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:
      pathParameters in interface HttpRequest
      Returns:
      all parsed path parameters for the current request.
    • path

      @NonNull public @NonNull String path()
      Get the full, originally requested path.
      Specified by:
      path in interface HttpRequest
      Returns:
      the full, originally requested path.
    • uri

      @NonNull public @NonNull String uri()
      Get the full requested uri.
      Specified by:
      uri in interface HttpRequest
      Returns:
      the full requested uri.
    • method

      @NonNull public @NonNull String 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:
      method in interface HttpRequest
      Returns:
      the request method of the request.
    • queryParameters

      @NonNull public @NonNull Map<String,List<String>> 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:
      queryParameters in interface HttpRequest
      Returns:
      the query parameters supplied in the request uri.
    • context

      @NonNull public @NonNull HttpContext context()
      Get the context this message is processed in.
      Specified by:
      context in interface HttpMessage<HttpRequest>
      Returns:
      the associated context of this message.
    • header

      Get a header value by the given name.
      Specified by:
      header in interface HttpMessage<HttpRequest>
      Parameters:
      name - the name of the header to get.
      Returns:
      the header value or null if the header does not exist.
    • headerAsInt

      @Nullable public @Nullable Integer headerAsInt(@NonNull @NonNull String name)
      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:
      headerAsInt in interface HttpMessage<HttpRequest>
      Parameters:
      name - the name of the header to get.
      Returns:
      the header value converted to an int or null.
    • headerAsBoolean

      public 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.
      Specified by:
      headerAsBoolean in interface HttpMessage<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

      Sets the given header, replacing the current one if already set.
      Specified by:
      header in interface HttpMessage<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

      @NonNull public @NonNull HttpRequest removeHeader(@NonNull @NonNull String name)
      Removes a header by the given name from this message.
      Specified by:
      removeHeader in interface HttpMessage<HttpRequest>
      Parameters:
      name - the name of the header to remove.
      Returns:
      the same message as used to call the method, for chaining.
    • clearHeaders

      @NonNull public @NonNull HttpRequest clearHeaders()
      Removes all headers from this message.
      Specified by:
      clearHeaders in interface HttpMessage<HttpRequest>
      Returns:
      the same message as used to call the method, for chaining.
    • hasHeader

      public boolean hasHeader(@NonNull @NonNull String name)
      Checks if this message has a header with the given name set.
      Specified by:
      hasHeader in interface HttpMessage<HttpRequest>
      Parameters:
      name - the name of the header to check.
      Returns:
      true if the header is present, false otherwise.
    • headers

      @NonNull public @NonNull Map<String,String> 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:
      headers in interface HttpMessage<HttpRequest>
      Returns:
      all headers of the message collected to a map, duplication free.
    • version

      @NonNull public @NonNull HttpVersion version()
      Get the http version of this http message. CloudNet currently only supports Http 1 and Http 1.1.
      Specified by:
      version in interface HttpMessage<HttpRequest>
      Returns:
      the version of this http message.
    • version

      Sets the version of this http message.
      Specified by:
      version in interface HttpMessage<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, prefer HttpMessage.bodyStream() instead.
      Specified by:
      body in interface HttpMessage<HttpRequest>
      Returns:
      the body content of this http message.
    • bodyAsString

      @NonNull public @NonNull String bodyAsString()
      Converts the body of the http message into an utf-8 encoded string.
      Specified by:
      bodyAsString in interface HttpMessage<HttpRequest>
      Returns:
      the body of the message converted to a string.
    • body

      @NonNull public @NonNull HttpRequest 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 HttpMessage.body(InputStream) instead.
      Specified by:
      body in interface HttpMessage<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:
      body in interface HttpMessage<HttpRequest>
      Parameters:
      text - the body as a string.
      Returns:
      the same message as used to call the method, for chaining.
    • bodyStream

      @Nullable public @Nullable 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.
      Specified by:
      bodyStream in interface HttpMessage<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:
      body in interface HttpMessage<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:
      hasBody in interface HttpMessage<HttpRequest>
      Returns:
      if a body is present.