Class HttpRequestEvent

java.lang.Object
net.risingworld.api.events.Event
net.risingworld.api.events.general.HttpRequestEvent

public final class HttpRequestEvent extends Event
Called when a HTTP request is received by the webserver, targeting a route which was registered by a plugin (see Plugin.registerWebserverHandler(String)).

This event allows the plugin to read the incoming request and provide a response (status code, headers and body). If the plugin does not set a response, the webserver will return "404 Not Found" by default.

Please note that the Webserver is only active for dedicated servers!
  • Method Details

    • getMethod

      public HttpRequestEvent.HttpMethod getMethod()
      Gets the HTTP method of this request.
      Returns:
      the HTTP method (e.g HttpRequestEvent.HttpMethod.GET).
    • getPath

      public String getPath()
      Gets the requested path (without query string), for example "/plugins/myplugin/status".
      Returns:
      the requested path.
    • getQuery

      public String getQuery()
      Gets the raw query string of this request (the part after the "?"), or an empty string if the request has no query string. Example: for the url "/plugins/myplugin/status?foo=1&bar=2" this returns "foo=1&bar=2".
      Returns:
      the raw query string (without leading "?").
    • getQueryParameters

      public Map<String,String> getQueryParameters()
      Gets the query parameters of this request as a map. This is a convenience method which parses the result of getQuery().
      Returns:
      a (possibly empty) map containing the query parameters. Never null.
    • getHeader

      public String getHeader(String name)
      Gets the value of a particular request header, or null if no such header is present. The header name is treated case-insensitively.
      Parameters:
      name - the name of the header (e.g "Content-Type").
      Returns:
      the header value, or null if no such header was found.
    • getBody

      public byte[] getBody()
      Gets the request body as a byte array. For requests without a body (e.g typical GET requests), this returns an empty array.
      Returns:
      the request body as a byte array.
    • getBodyAsString

      public String getBodyAsString()
      Gets the request body as a UTF-8 string. This is a convenience method which decodes the result of getBody() using the UTF-8 charset.
      Returns:
      the request body as a string. If there is no body, an empty string is returned.
    • getRemoteAddress

      public String getRemoteAddress()
      Gets the remote address (IP) of the client which sent this request.
      Returns:
      the remote address of the client.
    • isSecure

      public boolean isSecure()
      Gets whether or not this request was received over a secure (HTTPS) connection.
      Returns:
      true if the request was received over HTTPS, false otherwise.
    • setResponseCode

      public void setResponseCode(int code)
      Sets the HTTP status code of the response (e.g 200 for "OK", 404 for "Not Found"). If not set, the response will use the status code 200 by default (as long as any other response value was set, otherwise a 404 is returned).
      Parameters:
      code - the HTTP status code.
    • setContentType

      public void setContentType(String contentType)
      Sets the content type (i.e the "Content-Type" header) of the response, for example "application/json" or "text/html".
      Parameters:
      contentType - the content type of the response.
    • setResponseHeader

      public void setResponseHeader(String name, String value)
      Sets a custom response header.
      Parameters:
      name - the name of the header.
      value - the value of the header.
    • setResponseBody

      public void setResponseBody(String body)
      Sets the response body as a string (encoded as UTF-8). This replaces any previously set response body.
      Parameters:
      body - the response body.
    • setResponseBody

      public void setResponseBody(byte[] body)
      Sets the response body as raw binary data. This replaces any previously set response body.
      Parameters:
      body - the response body.
    • setResponseFile

      public void setResponseFile(File file)
      Sends a file as response. Unlike setResponseBody(byte[]), the file is never loaded into memory: The webserver streams it directly from disk, so this is the recommended way to serve large files (e.g. videos for a VideoAsset). Range requests (required for seeking in videos) and HEAD requests are handled automatically.

      The status code is set automatically (200, 206 for partial content, 404 if the file does not exist, 416 for an invalid range), so setResponseCode(int) has no effect. The content type is derived from the file extension unless it is set via setContentType(String). A file response takes precedence over a response body.
      Parameters:
      file - the file to send.

      Example: Serve a video file and play it on a UIVideo element
      1//OnEnable():
      2File file = new File(getPath() + "/assets/intro.mp4");
      3registerWebserverHandler("intro", event -> event.setResponseFile(file));
      4
      5//The relative route is resolved by the client against the server address
      6VideoAsset asset = VideoAsset.loadFromURL(getWebserverRoute("intro"));
      7UIVideo video = new UIVideo(asset);
    • setResponseFile

      public void setResponseFile(File file, String contentType)
      Sends a file as response, using a specific content type (see setResponseFile(File)).
      Parameters:
      file - the file to send.
      contentType - the content type of the response, e.g. "video/mp4".