Module io.jooby
Package io.jooby

Interface Context

All Superinterfaces:
Registry
All Known Subinterfaces:
DefaultContext
All Known Implementing Classes:
ForwardingContext

public interface Context extends Registry
HTTP context allows you to interact with the HTTP Request and manipulate the HTTP Response.
Since:
2.0.0
Author:
edgar
  • Field Details

  • Method Details

    • getAttributes

      @NonNull Map<String,Object> getAttributes()
      Context attributes (a.k.a request attributes).
      Returns:
      Mutable Context attributes.
    • getAttribute

      @Nullable <T> T getAttribute(@NonNull String key)
      Get an attribute by his key. This is just an utility method around getAttributes(). This method look first in current context and fallback to application attributes.
      Type Parameters:
      T - Attribute type.
      Parameters:
      key - Attribute key.
      Returns:
      Attribute value or null.
    • setAttribute

      @NonNull Context setAttribute(@NonNull String key, Object value)
      Set an application attribute.
      Parameters:
      key - Attribute key.
      value - Attribute value.
      Returns:
      This router.
    • getRouter

      @NonNull Router getRouter()
      Get the HTTP router (usually this represents an instance of Jooby.
      Returns:
      HTTP router (usually this represents an instance of Jooby.
    • forward

      @NonNull Object forward(@NonNull String path)
      Forward executing to another route. We use the given path to find a matching route.

      NOTE: the entire handler pipeline is executed (filter, decorator, etc.).

      Parameters:
      path - Path to forward the request.
      Returns:
      Forward result.
    • convert

      @NonNull default <T> T convert(@NonNull ValueNode value, @NonNull Class<T> type)
      Converts a value (single or hash) into the given type.
      Type Parameters:
      T - Generic type.
      Parameters:
      value - Value to convert.
      type - Expected type.
      Returns:
      Converted value.
    • convertOrNull

      @Nullable <T> T convertOrNull(@NonNull ValueNode value, @NonNull Class<T> type)
      Converts a value (single or hash) into the given type.
      Type Parameters:
      T - Generic type.
      Parameters:
      value - Value to convert.
      type - Expected type.
      Returns:
      Converted value or null.
    • flash

      @NonNull FlashMap flash()
      Flash map.
      Returns:
      Flash map.
    • flash

      @NonNull Value flash(@NonNull String name)
      Get a flash attribute.
      Parameters:
      name - Attribute's name.
      Returns:
      Flash attribute.
    • session

      @NonNull Session session()
      Find a session or creates a new session.
      Returns:
      Session.
    • session

      @NonNull Value session(@NonNull String name)
      Find a session attribute using the given name. If there is no session or attribute under that name a missing value is returned.
      Parameters:
      name - Attribute's name.
      Returns:
      Session's attribute or missing.
    • sessionOrNull

      @Nullable Session sessionOrNull()
      Find an existing session.
      Returns:
      Existing session or null.
    • cookie

      @NonNull Value cookie(@NonNull String name)
      Get a cookie matching the given name.
      Parameters:
      name - Cookie's name.
      Returns:
      Cookie value.
    • cookieMap

      @NonNull Map<String,String> cookieMap()
      Request cookies.
      Returns:
      Request cookies.
    • getMethod

      @NonNull String getMethod()
      HTTP method in upper-case form.
      Returns:
      HTTP method in upper-case form.
    • setMethod

      @NonNull Context setMethod(@NonNull String method)
      Set HTTP method in upper-case form.
      Parameters:
      method - HTTP method in upper-case form.
      Returns:
      This context.
    • getRoute

      @NonNull Route getRoute()
      Matching route.
      Returns:
      Matching route.
    • matches

      boolean matches(@NonNull String pattern)
      Check if the request path matches the given pattern.
      Parameters:
      pattern - Pattern to use.
      Returns:
      True if request path matches the pattern.
    • setRoute

      @NonNull Context setRoute(@NonNull Route route)
      Set matching route. This is part of public API, but shouldn't be use by application code.
      Parameters:
      route - Matching route.
      Returns:
      This context.
    • getContextPath

      @NonNull default String getContextPath()
      Get application context path (a.k.a as base path).
      Returns:
      Application context path (a.k.a as base path).
    • getRequestPath

      @NonNull String getRequestPath()
      Request path without decoding (a.k.a raw Path) without query string.
      Returns:
      Request path without decoding (a.k.a raw Path) without query string.
    • setRequestPath

      @NonNull Context setRequestPath(@NonNull String path)
      Set request path. This is usually done by Web Server or framework, but by user.
      Parameters:
      path - Request path.
      Returns:
      This context.
    • path

      @NonNull Value path(@NonNull String name)
      Path variable. Value is decoded.
      Parameters:
      name - Path key.
      Returns:
      Associated value or a missing value, but never a null reference.
    • path

      @NonNull <T> T path(@NonNull Class<T> type)
      Convert the pathMap() to the given type.
      Type Parameters:
      T - Target type.
      Parameters:
      type - Target type.
      Returns:
      Instance of target type.
    • path

      @NonNull ValueNode path()
      Convert pathMap() to a ValueNode object.
      Returns:
      A value object.
    • pathMap

      @NonNull Map<String,String> pathMap()
      Path map represent all the path keys with their values.
      
       {
         get("/:id", ctx -> ctx.pathMap());
       }
       
      A call to:
      GET /678
      Produces a path map like: id: 678
      Returns:
      Path map from path pattern.
    • setPathMap

      @NonNull Context setPathMap(@NonNull Map<String,String> pathMap)
      Set path map. This method is part of public API but shouldn't be use it by application code.
      Parameters:
      pathMap - Path map.
      Returns:
      This context.
    • query

      @NonNull QueryString query()
      Query string as ValueNode object.
      Returns:
      Query string as ValueNode object.
    • query

      @NonNull ValueNode query(@NonNull String name)
      Get a query parameter that matches the given name.
      
       {
         get("/search", ctx -> {
           String q = ctx.query("q").value();
           ...
         });
      
       }
       
      Parameters:
      name - Parameter name.
      Returns:
      A query value.
    • queryString

      @NonNull String queryString()
      Query string with the leading ? or empty string. This is the raw query string, without decoding it.
      Returns:
      Query string with the leading ? or empty string. This is the raw query string, without decoding it.
    • query

      @NonNull <T> T query(@NonNull Class<T> type)
      Convert the queryString to the given type.
      Type Parameters:
      T - Target type.
      Parameters:
      type - Target type.
      Returns:
      Query string converted to target type.
    • queryMap

      @NonNull Map<String,String> queryMap()
      Query string as simple map.
      /search?q=jooby&sort=name
      Produces
      {q: jooby, sort: name}
      Returns:
      Query string as map.
    • header

      @NonNull ValueNode header()
      Request headers as ValueNode.
      Returns:
      Request headers as ValueNode.
    • header

      @NonNull Value header(@NonNull String name)
      Get a header that matches the given name.
      Parameters:
      name - Header name. Case insensitive.
      Returns:
      A header value or missing value, never a null reference.
    • headerMap

      @NonNull Map<String,String> headerMap()
      Header as single-value map.
      Returns:
      Header as single-value map.
    • accept

      boolean accept(@NonNull MediaType contentType)
      True if the given type matches the `Accept` header. This method returns true if there is no accept header.
      Parameters:
      contentType - Content type to match.
      Returns:
      True for matching type or if content header is absent.
    • accept

      @Nullable MediaType accept(@NonNull List<MediaType> produceTypes)
      Check if the accept type list matches the given produces list and return the most specific media type from produces list.
      Parameters:
      produceTypes - Produced types.
      Returns:
      The most specific produces type.
    • getRequestType

      @Nullable MediaType getRequestType()
      Request Content-Type header or null when missing.
      Returns:
      Request Content-Type header or null when missing.
    • isPreflight

      default boolean isPreflight()
      Test whenever this is a CORS preflight request.
      Returns:
      Test whenever this is a CORS preflight request.
    • getRequestType

      @NonNull MediaType getRequestType(MediaType defaults)
      Request Content-Type header or null when missing.
      Parameters:
      defaults - Default content type to use when the header is missing.
      Returns:
      Request Content-Type header or null when missing.
    • getRequestLength

      long getRequestLength()
      Request Content-Length header or -1 when missing.
      Returns:
      Request Content-Length header or -1 when missing.
    • locales

      @NonNull default List<Locale> locales(BiFunction<List<Locale.LanguageRange>,List<Locale>,List<Locale>> filter)
      Returns a list of locales that best matches the current request.

      The first filter argument is the value of Accept-Language as a list of Locale.LanguageRange instances while the second argument is a list of supported locales specified by Jooby.setLocales(List) or by the application.lang configuration property.

      The next example returns a list of matching Locale instances using the filtering mechanism defined in RFC 4647:

      
       ctx.locales(Locale::filter)
       
      Parameters:
      filter - A locale filter.
      Returns:
      A list of matching locales.
    • locales

      @NonNull default List<Locale> locales()
      Returns:
      A list of matching locales or empty list.
      See Also:
    • locale

      @NonNull default Locale locale(BiFunction<List<Locale.LanguageRange>,List<Locale>,Locale> filter)
      Returns a locale that best matches the current request.

      The first filter argument is the value of Accept-Language as a list of Locale.LanguageRange instances while the second argument is a list of supported locales specified by Jooby.setLocales(List) or by the application.lang configuration property.

      The next example returns a Locale instance for the best-matching language tag using the lookup mechanism defined in RFC 4647.

      
       ctx.locale(Locale::lookup)
       
      Parameters:
      filter - A locale filter.
      Returns:
      A matching locale.
    • locale

      @NonNull default Locale locale()
      Returns a locale that best matches the current request or the default locale specified by Jooby.setLocales(List) or by the application.lang configuration property.
      Returns:
      A matching locale.
    • getUser

      @Nullable <T> T getUser()
      Current user or null if none was set.
      Type Parameters:
      T - User type.
      Returns:
      Current user or null if none was set.
    • setUser

      @NonNull Context setUser(@Nullable Object user)
      Set current user.
      Parameters:
      user - Current user.
      Returns:
      This context.
    • getRequestURL

      @NonNull String getRequestURL()
      Recreates full/entire url of the current request using the Host header.

      If you run behind a reverse proxy that has been configured to send the X-Forwarded-* header, please consider to set Router.setTrustProxy(boolean) option.

      Returns:
      Full/entire request url using the Host header.
    • getRequestURL

      @NonNull String getRequestURL(@NonNull String path)
      Recreates full/entire request url using the Host header with a custom path/suffix.

      If you run behind a reverse proxy that has been configured to send the X-Forwarded-* header, please consider to set Router.setTrustProxy(boolean) option.

      Parameters:
      path - Path or suffix to use, can also include query string parameters.
      Returns:
      Full/entire request url using the Host header.
    • getRemoteAddress

      @NonNull String getRemoteAddress()
      The IP address of the client or last proxy that sent the request.

      If you run behind a reverse proxy that has been configured to send the X-Forwarded-* header, please consider to set Router.setTrustProxy(boolean) option.

      Returns:
      The IP address of the client or last proxy that sent the request or empty string for interrupted requests.
    • setRemoteAddress

      @NonNull Context setRemoteAddress(@NonNull String remoteAddress)
      Set IP address of client or last proxy that sent the request.
      Parameters:
      remoteAddress - Remote Address.
      Returns:
      This context.
    • getHost

      @NonNull String getHost()
      Return the host that this request was sent to, in general this will be the value of the Host header, minus the port specifier. Unless, it is set manually using the setHost(String) method.

      If you run behind a reverse proxy that has been configured to send the X-Forwarded-* header, please consider to set Router.setTrustProxy(boolean) option.

      Returns:
      Return the host that this request was sent to, in general this will be the value of the Host header, minus the port specifier.
    • setHost

      @NonNull Context setHost(@NonNull String host)
      Set the host (without the port value).

      Please keep in mind this method doesn't alter/modify the host header.

      Parameters:
      host - Host value.
      Returns:
      This context.
    • getHostAndPort

      @NonNull String getHostAndPort()
      Return the host and port that this request was sent to, in general this will be the value of the Host.

      If you run behind a reverse proxy that has been configured to send the X-Forwarded-* header, please consider to set Router.setTrustProxy(boolean) option.

      Returns:
      Return the host that this request was sent to, in general this will be the value of the Host header.
    • getPort

      int getPort()
      Return the port that this request was sent to. In general this will be the value of the Host header, minus the host name.

      If no host header is present, this method returns the value of getServerPort().

      Returns:
      Return the port that this request was sent to. In general this will be the value of the Host header, minus the host name.
    • setPort

      @NonNull Context setPort(int port)
      Set port this request was sent to.
      Parameters:
      port - Port.
      Returns:
      This context.
    • getProtocol

      @NonNull String getProtocol()
      The name of the protocol the request. Always in lower-case.
      Returns:
      The name of the protocol the request. Always in lower-case.
    • getClientCertificates

      @NonNull List<Certificate> getClientCertificates()
      The certificates presented by the client for mutual TLS. Empty if ssl is not enabled, or client authentication is not required.
      Returns:
      The certificates presented by the client for mutual TLS. Empty if ssl is not enabled, or client authentication is not required.
    • getServerPort

      int getServerPort()
      Server port for current request.
      Returns:
      Server port for current request.
    • getServerHost

      @NonNull String getServerHost()
      Server host.
      Returns:
      Server host.
    • isSecure

      boolean isSecure()
      Returns a boolean indicating whether this request was made using a secure channel, such as HTTPS.
      Returns:
      a boolean indicating if the request was made using a secure channel
    • getScheme

      @NonNull String getScheme()
      HTTP scheme in lower case.
      Returns:
      HTTP scheme in lower case.
    • setScheme

      @NonNull Context setScheme(@NonNull String scheme)
      Set HTTP scheme in lower case.
      Parameters:
      scheme - HTTP scheme in lower case.
      Returns:
      This context.
    • form

      @NonNull Formdata form()
      Get form data.

      Only for application/x-www-form-urlencoded or multipart/form-data request.

      Returns:
      Multipart value.
    • form

      @NonNull ValueNode form(@NonNull String name)
      Get a form field that matches the given name.

      File upload retrieval is available using file(String).

      Only for multipart/form-data request.

      Parameters:
      name - Field name.
      Returns:
      Multipart value.
    • form

      @NonNull <T> T form(@NonNull Class<T> type)
      Convert form data to the given type.

      Only for application/x-www-form-urlencoded or multipart/form-data request.

      Type Parameters:
      T - Target type.
      Parameters:
      type - Target type.
      Returns:
      Target value.
    • formMap

      @NonNull Map<String,String> formMap()
      Form data as single-value map.

      Only for application/x-www-form-urlencoded or multipart/form-data request.

      Returns:
      Single-value map.
    • files

      @NonNull List<FileUpload> files()
      All file uploads. Only for multipart/form-data request.
      Returns:
      All file uploads.
    • files

      @NonNull List<FileUpload> files(@NonNull String name)
      All file uploads that matches the given field name.

      Only for multipart/form-data request.

      Parameters:
      name - Field name. Please note this is the form field name, not the actual file name.
      Returns:
      All file uploads.
    • file

      @NonNull FileUpload file(@NonNull String name)
      A file upload that matches the given field name.

      Only for multipart/form-data request.

      Parameters:
      name - Field name. Please note this is the form field name, not the actual file name.
      Returns:
      A file upload.
    • lookup

      default Value lookup(String name)
      Searches for a parameter in the following order: ParamSource.PATH, ParamSource.QUERY, ParamSource.FORM returning the first non-missing Value, or a 'missing' Value if none found.
      Parameters:
      name - The name of the parameter.
      Returns:
      The first non-missing Value or a Value representing a missing value if none found.
    • lookup

      default Value lookup(@NonNull String name, ParamSource... sources)
      Searches for a parameter in the specified sources, in the specified order, returning the first non-missing Value, or a 'missing' Value if none found.

      At least one ParamSource must be specified.

      Parameters:
      name - The name of the parameter.
      sources - Sources to search in.
      Returns:
      The first non-missing Value or a Value representing a missing value if none found.
      Throws:
      IllegalArgumentException - If no ParamSources are specified.
    • lookup

      default ParamLookup lookup()
      Returns a ParamLookup instance which is a fluent interface covering the functionality of the lookup(String, ParamSource...) method.
      
       Value foo = ctx.lookup()
         .inQuery()
         .inPath()
         .get("foo");
       
      Returns:
      A ParamLookup instance.
      See Also:
    • body

      @NonNull Body body()
      HTTP body which provides access to body content.
      Returns:
      HTTP body which provides access to body content.
    • body

      @NonNull <T> T body(@NonNull Class<T> type)
      Convert the HTTP body to the given type.
      Type Parameters:
      T - Conversion type.
      Parameters:
      type - Reified type.
      Returns:
      Instance of conversion type.
    • body

      @NonNull <T> T body(@NonNull Type type)
      Convert the HTTP body to the given type.
      Type Parameters:
      T - Conversion type.
      Parameters:
      type - Reified type.
      Returns:
      Instance of conversion type.
    • decode

      @NonNull <T> T decode(@NonNull Type type, @NonNull MediaType contentType)
      Convert the HTTP body to the given type.
      Type Parameters:
      T - Conversion type.
      Parameters:
      type - Generic type.
      contentType - Content type to use.
      Returns:
      Instance of conversion type.
    • decoder

      @NonNull MessageDecoder decoder(@NonNull MediaType contentType)
      Get a decoder for the given content type or get an StatusCode.UNSUPPORTED_MEDIA_TYPE.
      Parameters:
      contentType - Content type.
      Returns:
      MessageDecoder.
    • isInIoThread

      boolean isInIoThread()
      True when request runs in IO threads.
      Returns:
      True when request runs in IO threads.
    • dispatch

      @NonNull Context dispatch(@NonNull Runnable action)
      Dispatch context to a worker threads. Worker threads allow to execute blocking code. The default worker thread pool is provided by web server or by application code using the Jooby.setWorker(Executor).

      Example:

      
       get("/", ctx -> {
         return ctx.dispatch(() -> {
      
           // run blocking code
      
         }):
       });
      
       
      Parameters:
      action - Application code.
      Returns:
      This context.
    • dispatch

      @NonNull Context dispatch(@NonNull Executor executor, @NonNull Runnable action)
      Dispatch context to the given executor.

      Example:

      
       Executor executor = ...;
       get("/", ctx -> {
         return ctx.dispatch(executor, () -> {
      
           // run blocking code
      
         }):
       });
      
       
      Parameters:
      executor - Executor to use.
      action - Application code.
      Returns:
      This context.
    • detach

      @NonNull Context detach(@NonNull Route.Handler next) throws Exception
      Tells context that response will be generated form a different thread. This operation is similar to dispatch(Runnable) except there is no thread dispatching here.

      This operation integrates easily with third-party libraries like rxJava or others.

      Parameters:
      next - Application code.
      Returns:
      This context.
      Throws:
      Exception - When detach operation fails.
    • upgrade

      @NonNull Context upgrade(@NonNull WebSocket.Initializer handler)
      Perform a websocket handsahke and upgrade a HTTP GET into a websocket protocol.

      NOTE: This method is part of Public API, but shouldn't be used by client code.

      Parameters:
      handler - Web socket initializer.
      Returns:
      This context.
    • upgrade

      @NonNull Context upgrade(@NonNull ServerSentEmitter.Handler handler)
      Perform a server-sent event handshake and upgrade HTTP GET into a Server-Sent protocol.

      NOTE: This method is part of Public API, but shouldn't be used by client code.

      Parameters:
      handler - Server-Sent event handler.
      Returns:
      This context.
    • setResponseHeader

      @NonNull Context setResponseHeader(@NonNull String name, @NonNull Date value)
      Set response header.
      Parameters:
      name - Header name.
      value - Header value.
      Returns:
      This context.
    • setResponseHeader

      @NonNull Context setResponseHeader(@NonNull String name, @NonNull Instant value)
      Set response header.
      Parameters:
      name - Header name.
      value - Header value.
      Returns:
      This context.
    • setResponseHeader

      @NonNull Context setResponseHeader(@NonNull String name, @NonNull Object value)
      Set response header.
      Parameters:
      name - Header name.
      value - Header value.
      Returns:
      This context.
    • setResponseHeader

      @NonNull Context setResponseHeader(@NonNull String name, @NonNull String value)
      Set response header.
      Parameters:
      name - Header name.
      value - Header value.
      Returns:
      This context.
    • removeResponseHeader

      @NonNull Context removeResponseHeader(@NonNull String name)
      Remove a response header.
      Parameters:
      name - Header's name.
      Returns:
      This context.
    • removeResponseHeaders

      @NonNull Context removeResponseHeaders()
      Clear/reset all the headers, including cookies.
      Returns:
      This context.
    • setResponseLength

      @NonNull Context setResponseLength(long length)
      Set response content length header.
      Parameters:
      length - Response length.
      Returns:
      This context.
    • getResponseHeader

      @Nullable String getResponseHeader(@NonNull String name)
      Get response header.
      Parameters:
      name - Header's name.
      Returns:
      Header's value (if set previously) or null.
    • getResponseLength

      long getResponseLength()
      Get response content length or -1 when none was set.
      Returns:
      Response content length or -1 when none was set.
    • setResponseCookie

      @NonNull Context setResponseCookie(@NonNull Cookie cookie)
      Set/add a cookie to response.
      Parameters:
      cookie - Cookie to add.
      Returns:
      This context.
    • setResponseType

      @NonNull Context setResponseType(@NonNull String contentType)
      Set response content type header.
      Parameters:
      contentType - Content type.
      Returns:
      This context.
    • setResponseType

      @NonNull Context setResponseType(@NonNull MediaType contentType)
      Set response content type header.
      Parameters:
      contentType - Content type.
      Returns:
      This context.
    • setResponseType

      @NonNull Context setResponseType(@NonNull MediaType contentType, @Nullable Charset charset)
      Set response content type header.
      Parameters:
      contentType - Content type.
      charset - Charset.
      Returns:
      This context.
    • setDefaultResponseType

      @NonNull Context setDefaultResponseType(@NonNull MediaType contentType)
      Set the default response content type header. It is used if the response content type header was not set yet.
      Parameters:
      contentType - Content type.
      Returns:
      This context.
    • getResponseType

      @NonNull MediaType getResponseType()
      Get response content type.
      Returns:
      Response content type.
    • setResponseCode

      @NonNull Context setResponseCode(@NonNull StatusCode statusCode)
      Set response status code.
      Parameters:
      statusCode - Status code.
      Returns:
      This context.
    • setResponseCode

      @NonNull Context setResponseCode(int statusCode)
      Set response status code.
      Parameters:
      statusCode - Status code.
      Returns:
      This context.
    • getResponseCode

      @NonNull StatusCode getResponseCode()
      Get response status code.
      Returns:
      Response status code.
    • render

      @NonNull Context render(@NonNull Object value)
      Render a value and send the response to client.
      Parameters:
      value - Object value.
      Returns:
      This context.
    • responseStream

      @NonNull OutputStream responseStream()
      HTTP response channel as output stream. Usually for chunked responses.
      Returns:
      HTTP channel as output stream. Usually for chunked responses.
    • responseStream

      @NonNull OutputStream responseStream(@NonNull MediaType contentType)
      HTTP response channel as output stream. Usually for chunked responses.
      Parameters:
      contentType - Media type.
      Returns:
      HTTP channel as output stream. Usually for chunked responses.
    • responseStream

      @NonNull Context responseStream(@NonNull MediaType contentType, @NonNull SneakyThrows.Consumer<OutputStream> consumer) throws Exception
      HTTP response channel as output stream. Usually for chunked responses.
      Parameters:
      contentType - Content type.
      consumer - Output stream consumer.
      Returns:
      HTTP channel as output stream. Usually for chunked responses.
      Throws:
      Exception - Is something goes wrong.
    • responseStream

      @NonNull Context responseStream(@NonNull SneakyThrows.Consumer<OutputStream> consumer) throws Exception
      HTTP response channel as output stream. Usually for chunked responses.
      Parameters:
      consumer - Output stream consumer.
      Returns:
      HTTP channel as output stream. Usually for chunked responses.
      Throws:
      Exception - Is something goes wrong.
    • responseSender

      @NonNull Sender responseSender()
      HTTP response channel as chunker.
      Returns:
      HTTP channel as chunker. Usually for chunked response.
    • responseWriter

      @NonNull PrintWriter responseWriter()
      HTTP response channel as response writer.
      Returns:
      HTTP channel as response writer. Usually for chunked response.
    • responseWriter

      @NonNull PrintWriter responseWriter(@NonNull MediaType contentType)
      HTTP response channel as response writer.
      Parameters:
      contentType - Content type.
      Returns:
      HTTP channel as response writer. Usually for chunked response.
    • responseWriter

      @NonNull PrintWriter responseWriter(@NonNull MediaType contentType, @Nullable Charset charset)
      HTTP response channel as response writer.
      Parameters:
      contentType - Content type.
      charset - Charset.
      Returns:
      HTTP channel as response writer. Usually for chunked response.
    • responseWriter

      @NonNull Context responseWriter(@NonNull SneakyThrows.Consumer<PrintWriter> consumer) throws Exception
      HTTP response channel as response writer.
      Parameters:
      consumer - Writer consumer.
      Returns:
      This context.
      Throws:
      Exception - Is something goes wrong.
    • responseWriter

      @NonNull Context responseWriter(@NonNull MediaType contentType, @NonNull SneakyThrows.Consumer<PrintWriter> consumer) throws Exception
      HTTP response channel as response writer.
      Parameters:
      contentType - Content type.
      consumer - Writer consumer.
      Returns:
      This context.
      Throws:
      Exception - Is something goes wrong.
    • responseWriter

      @NonNull Context responseWriter(@NonNull MediaType contentType, @Nullable Charset charset, @NonNull SneakyThrows.Consumer<PrintWriter> consumer) throws Exception
      HTTP response channel as response writer.
      Parameters:
      contentType - Content type.
      charset - Charset.
      consumer - Writer consumer.
      Returns:
      This context.
      Throws:
      Exception - Is something goes wrong.
    • sendRedirect

      @NonNull Context sendRedirect(@NonNull String location)
      Send a 302 response.
      Parameters:
      location - Location.
      Returns:
      This context.
    • sendRedirect

      @NonNull Context sendRedirect(@NonNull StatusCode redirect, @NonNull String location)
      Send a redirect response.
      Parameters:
      redirect - Redirect status code.
      location - Location.
      Returns:
      This context.
    • send

      @NonNull Context send(@NonNull String data)
      Send response data.
      Parameters:
      data - Response. Use UTF-8 charset.
      Returns:
      This context.
    • send

      @NonNull Context send(@NonNull String data, @NonNull Charset charset)
      Send response data.
      Parameters:
      data - Response.
      charset - Charset.
      Returns:
      This context.
    • send

      @NonNull Context send(@NonNull byte[] data)
      Send response data.
      Parameters:
      data - Response.
      Returns:
      This context.
    • send

      @NonNull Context send(@NonNull ByteBuffer data)
      Send response data.
      Parameters:
      data - Response.
      Returns:
      This context.
    • send

      @NonNull Context send(@NonNull byte[]... data)
      Send response data.
      Parameters:
      data - Response.
      Returns:
      This context.
    • send

      @NonNull Context send(@NonNull ByteBuffer[] data)
      Send response data.
      Parameters:
      data - Response.
      Returns:
      This context.
    • send

      @NonNull Context send(@NonNull ReadableByteChannel channel)
      Send response data.
      Parameters:
      channel - Response input.
      Returns:
      This context.
    • send

      @NonNull Context send(@NonNull InputStream input)
      Send response data.
      Parameters:
      input - Response.
      Returns:
      This context.
    • send

      @NonNull Context send(@NonNull FileDownload file)
      Send a file download response.
      Parameters:
      file - File download.
      Returns:
      This context.
    • send

      @NonNull Context send(@NonNull Path file)
      Send a file response.
      Parameters:
      file - File response.
      Returns:
      This context.
    • send

      @NonNull Context send(@NonNull FileChannel file)
      Send a file response.
      Parameters:
      file - File response.
      Returns:
      This context.
    • send

      @NonNull Context send(@NonNull StatusCode statusCode)
      Send an empty response with the given status code.
      Parameters:
      statusCode - Status code.
      Returns:
      This context.
    • sendError

      @NonNull Context sendError(@NonNull Throwable cause)
      Send an error response. Status code is computed via Router.errorCode(Throwable).
      Parameters:
      cause - Error. If this is a fatal error it is going to be rethrow it.
      Returns:
      This context.
    • sendError

      @NonNull Context sendError(@NonNull Throwable cause, @NonNull StatusCode statusCode)
      Send an error response.
      Parameters:
      cause - Error. If this is a fatal error it is going to be rethrow it.
      statusCode - Status code.
      Returns:
      This context.
    • isResponseStarted

      boolean isResponseStarted()
      True if response already started.
      Returns:
      True if response already started.
    • getResetHeadersOnError

      boolean getResetHeadersOnError()
      True if response headers are cleared on application error. If none set it uses the default/global value specified by RouterOption.RESET_HEADERS_ON_ERROR.
      Returns:
      True if response headers are cleared on application error. If none set it uses the default/global value specified by RouterOption.RESET_HEADERS_ON_ERROR.
    • setResetHeadersOnError

      @NonNull Context setResetHeadersOnError(boolean value)
      Set whenever reset/clear headers on application error.
      Parameters:
      value - True for reset/clear headers.
      Returns:
      This context.
    • onComplete

      @NonNull Context onComplete(@NonNull Route.Complete task)
      Add a complete listener.
      Parameters:
      task - Task to execute.
      Returns:
      This context.
    • readOnly

      @NonNull static Context readOnly(@NonNull Context ctx)
      Wrap a HTTP context and make read only. Attempt to modify the HTTP response resulted in exception.
      Parameters:
      ctx - Originating context.
      Returns:
      Read only context.
    • websocket

      @NonNull static Context websocket(@NonNull Context ctx, @NonNull WebSocket ws, boolean binary, WebSocket.WriteCallback callback)
      Wrap a HTTP context and make it WebSocket friendly. Attempt to modify the HTTP response is completely ignored, except for send(byte[]) and send(String) which are delegated to the given web socket.

      This context is necessary for creating a bridge between MessageEncoder and WebSocket.

      This method is part of Public API, but direct usage is discouraged.

      Parameters:
      ctx - Originating context.
      ws - WebSocket.
      binary - True for sending binary message.
      Returns:
      Read only context.