Module io.avaje.jex

Interface Context


public interface Context
Provides access to functions for handling the request and response.
  • Method Details

    • attribute

      <T> T attribute(String key)
      Gets the attribute with the specified key from the request.
      Type Parameters:
      T - The type of the attribute.
      Parameters:
      key - The attribute key.
      Returns:
      The attribute value, or null if not found.
    • attribute

      Context attribute(String key, Object value)
      Sets an attribute on the request, accessible to other handlers in the request lifecycle.
      Parameters:
      key - The attribute key.
      value - The attribute value.
    • basicAuthCredentials

      BasicAuthCredentials basicAuthCredentials()
      Gets basic-auth credentials from the request.
      Returns:
      The Base64 decoded username and password from the Authorization header, or null if no header is sent
      Throws:
      IllegalStateException - if the Authorization header is malformed
    • body

      String body()
      Return the request body as String.
    • bodyAsBytes

      byte[] bodyAsBytes()
      Returns the request body as a byte array.
      Returns:
      The request body as a byte array.
    • bodyAsClass

      default <T> T bodyAsClass(Class<T> beanType)
      Return the request body as bean.
      Parameters:
      beanType - The bean type
    • bodyAsInputStream

      InputStream bodyAsInputStream()
      Returns the request body as an input stream.
      Returns:
      The request body as an input stream.
    • bodyAsType

      <T> T bodyAsType(Type beanType)
      Return the request body as bean.
      Parameters:
      beanType - The bean type
    • bodyStreamAsClass

      default <T> T bodyStreamAsClass(Class<T> beanType)
      Return the request body as bean using bodyAsInputStream().
      Parameters:
      beanType - The bean type
    • bodyStreamAsType

      <T> T bodyStreamAsType(Type beanType)
      Return the request body as bean of the given type using bodyAsInputStream().
      Parameters:
      beanType - The bean type
    • contentLength

      long contentLength()
      Return the request content length.
    • contentLength

      default Context contentLength(long length)
      Manually set the response content length.
    • contentType

      String contentType()
      Return the request content type.
    • contentType

      Context contentType(String contentType)
      Set the response content type.
    • contentType

      default Context contentType(ContentType contentType)
      Set the response content type.
    • contextPath

      String contextPath()
      Return the request context path.
    • cookie

      Context cookie(Context.Cookie cookie)
      Sets a cookie using the provided Context.Cookie object.
      Parameters:
      cookie - The cookie object to set.
    • cookie

      String cookie(String name)
      Returns the value of a cookie with the specified name from the request.
      Parameters:
      name - The name of the cookie.
      Returns:
      The value of the cookie, or null if the cookie is not found.
    • cookie

      Context cookie(String name, String value)
      Sets a cookie with the specified name and value, with no expiration date.
      Parameters:
      name - The name of the cookie.
      value - The value of the cookie.
    • cookie

      Context cookie(String name, String value, int maxAge)
      Sets a cookie with the specified name, value, and maximum age in seconds.
      Parameters:
      name - The name of the cookie.
      value - The value of the cookie.
      maxAge - The maximum age of the cookie in seconds.
    • cookieMap

      Map<String,String> cookieMap()
      Returns a map containing all the cookie names and their corresponding values from the request.
      Returns:
      A map of cookie names to their values.
    • exchange

      HttpExchange exchange()
      Return the underlying JDK HttpExchange object backing the context
    • formParam

      default String formParam(String key)
      Return the first form param value for the specified key or null.
    • formParam

      default String formParam(String key, String defaultValue)
      Return the first form param value for the specified key or the default value.
    • formParamMap

      Map<String,List<String>> formParamMap()
      Returns a map with all the form param keys and values.
    • formParams

      default List<String> formParams(String key)
      Return the form params for the specified key, or empty list.
    • fullUrl

      default String fullUrl()
      Return the full request url, including query string (if present)
    • rangedWrite

      void rangedWrite(InputStream inputStream, long totalBytes)
      Reads HTTP Range headers and determines which part of the provided InputStream to write back.
      Parameters:
      inputStream - data to write
      totalBytes - total size of the data
    • rangedWrite

      default void rangedWrite(InputStream inputStream)
      Writes input stream to rangedWrite(InputStream, long) with currently available data via InputStream.available()
    • header

      String header(String key)
      Return the request header value by name.
      Parameters:
      key - The first value of the header
    • headerValues

      List<String> headerValues(String key)
      Return the request headers.
      Parameters:
      key - all values of the header key
    • header

      Context header(String key, List<String> value)
      Set the response header.
      Parameters:
      key - The header key
      value - The header value
    • header

      Context header(String key, String value)
      Set the response header.
      Parameters:
      key - The header key
      value - The header value
    • headerMap

      Map<String,String> headerMap()
      Return all the request headers as a map.
      Returns:
      all the headers as a single value Map
    • headerMap

      Context headerMap(Map<String,List<String>> headers)
      Sets the response headers using the provided map.
      Parameters:
      headers - A map containing the header names as keys and their corresponding values as lists.
      Returns:
      The updated context object.
    • requestHeaders

      Headers requestHeaders()
      Return underlying request headers.
      Returns:
      the request headers
    • responseHeaders

      Headers responseHeaders()
      Return underlying response headers.
      Returns:
      the response headers
    • headers

      default Context headers(Map<String,String> headers)
      Add the response headers using the provided map.
    • host

      String host()
      Returns the host name of the request.
      Returns:
      The host name of the request, or null if not available.
    • html

      void html(String content)
      Write html content to the response.
    • ip

      String ip()
      Returns the IP address of the client making the request.
      Returns:
      The IP address of the client.
    • json

      void json(Object bean)
      Set the content type as application/json and write the response.
      Parameters:
      bean - the object to serialize and write
    • jsonb

      default <T> void jsonb(io.avaje.jsonb.JsonType<T> jsonType, T value)
      Optimized json write using avaje jsonb
      Parameters:
      jsonType - the serializer for the value.
      value - the pojo to serialize
    • jsonStream

      <E> void jsonStream(Iterator<E> iterator)
      Write the stream as a JSON stream with new line delimiters application/x-json-stream.
      Parameters:
      iterator - The iterator of beans to write as json
    • jsonStream

      <E> void jsonStream(Stream<E> stream)
      Write the stream as a JSON stream with new line delimiters application/x-json-stream.
      Parameters:
      stream - The stream of beans to write as json
    • jsonService

      JsonService jsonService()
      Returns the configured JsonService instance.}
      Returns:
      The json service if configured. null otherwise.
    • matchedPath

      String matchedPath()
      Returns the matched path as a raw expression, without any parameter substitution.
      Returns:
      The matched path as a raw string.
    • method

      String method()
      Returns the HTTP method used in the request (e.g., GET, POST, PUT, DELETE).
      Returns:
      The HTTP method of the request.
    • outputStream

      OutputStream outputStream()
      Return the outputStream to write content. It is expected that the contentType(String) has been set prior to obtaining and writing to the outputStream.
      Returns:
      The outputStream to write content to.
    • path

      String path()
      Returns the path part of the request URI.
      Returns:
      The path part of the request URI.
    • pathParam

      String pathParam(String name)
      Return the path parameter.
      Parameters:
      name - The path parameter name.
    • pathParamMap

      Map<String,String> pathParamMap()
      Return all the path parameters as a map.
    • port

      int port()
      Returns the port number used in the request.
      Returns:
      The port number of the request.
    • protocol

      String protocol()
      Returns the protocol used in the request (e.g., HTTP/1.1).
      Returns:
      The protocol of the request.
    • queryParam

      String queryParam(String name)
      Return the first query parameter value.
      Parameters:
      name - The query parameter name
    • queryParam

      default String queryParam(String name, String defaultValue)
      Return the first query parameter value or the default value if it does not exist.
      Parameters:
      name - The query parameter name
    • queryParamMap

      Map<String,String> queryParamMap()
      Return all the query parameters as a map.

      Note this returns the first value for any given key if that key has multiple values.

    • queryParams

      List<String> queryParams(String name)
      Return all the query parameters for the given parameter name.
    • queryString

      String queryString()
      Return the request query string, or null.
    • redirect

      void redirect(String location)
      Redirects the client to the specified location using a 302 (Found) status code.
      Parameters:
      location - The URL to redirect to.
    • redirect

      void redirect(String location, int httpStatusCode)
      Redirects the client to the specified location using the given HTTP status code.
      Parameters:
      location - The URL to redirect to.
      httpStatusCode - The HTTP status code to use for the redirect.
    • removeCookie

      Context removeCookie(String name)
      Removes a cookie with the specified name.
      Parameters:
      name - The name of the cookie to remove.
    • removeCookie

      Context removeCookie(String name, String path)
      Removes a cookie with the specified name and path.
      Parameters:
      name - The name of the cookie to remove.
      path - The path of the cookie to remove.
    • render

      default Context render(String name)
      Render a template typically as html.
      Parameters:
      name - The template name
    • render

      Context render(String name, Map<String,Object> model)
      Render a template typically as html with the given model.
      Parameters:
      name - The template name
      model - The model used with the template
    • responseHeader

      String responseHeader(String key)
      Returns the value of the specified response header.
      Parameters:
      key - The name of the header.
      Returns:
      The value of the header, or null if not found.
    • responseHeaderValues

      List<String> responseHeaderValues(String key)
      Returns the value of the specified response header.
      Parameters:
      key - The name of the header.
      Returns:
      The value of the header, or null if not found.
    • responseSent

      boolean responseSent()
      Return true if the response has been sent.
    • routeRoles

      Set<Role> routeRoles()
      Returns a set of roles associated with the current route.
      Returns:
      A set of roles.
    • scheme

      String scheme()
      Return the request scheme.
    • sslSession

      SSLSession sslSession()
      Get the SSLSession for this exchange.
      Returns:
      the SSLSession
    • status

      int status()
      Return the current response status.
    • status

      Context status(int statusCode)
      Set the status code on the response.
    • status

      default Context status(HttpStatus statusCode)
      Set the status code on the response.
    • text

      void text(String content)
      Write plain text content to the response.
    • uri

      URI uri()
      Return the request uri.
    • userAgent

      default String userAgent()
      Return the request user agent, or null.
    • write

      default void write(byte[] bytes)
      Writes the given bytes directly to the response.
      Parameters:
      bytes - The byte array to write.
    • write

      void write(byte[] bufferBytes, int length)
      Writes the given length of bytes from this buffer directly to the response.

      The bytes written will be from position 0 to length.

      Parameters:
      bufferBytes - The byte array to write.
      length - The number of bytes to write from the buffer.
    • write

      void write(InputStream is)
      Writes the content from the given InputStream directly to the response body.
      Parameters:
      is - The input stream containing the content to write.
    • write

      void write(String content)
      Writes the given string content directly to the response.
      Parameters:
      content - The string content to write.