Module io.jooby
Package io.jooby

Interface DefaultContext

All Superinterfaces:
Context, Registry

public interface DefaultContext extends Context
Like Context but with couple of default methods.
Since:
2.0.2
Author:
edgar
  • Method Details

    • require

      @NonNull default <T> T require(@NonNull Class<T> type, @NonNull String name) throws RegistryException
      Description copied from interface: Registry
      Provides an instance of the given type where name matches it.
      Specified by:
      require in interface Registry
      Type Parameters:
      T - Object type.
      Parameters:
      type - Object type.
      name - Object name.
      Returns:
      Instance of this type.
      Throws:
      RegistryException - If there was a runtime failure while providing an instance.
    • require

      @NonNull default <T> T require(@NonNull Class<T> type) throws RegistryException
      Description copied from interface: Registry
      Provides an instance of the given type.
      Specified by:
      require in interface Registry
      Type Parameters:
      T - Object type.
      Parameters:
      type - Object type.
      Returns:
      Instance of this type.
      Throws:
      RegistryException - If there was a runtime failure while providing an instance.
    • require

      @NonNull default <T> T require(@NonNull ServiceKey<T> key) throws RegistryException
      Description copied from interface: Registry
      Provides an instance of the given type.
      Specified by:
      require in interface Registry
      Type Parameters:
      T - Object type.
      Parameters:
      key - Object key.
      Returns:
      Instance of this type.
      Throws:
      RegistryException - If there was a runtime failure while providing an instance.
    • getUser

      @Nullable default <T> T getUser()
      Description copied from interface: Context
      Current user or null if none was set.
      Specified by:
      getUser in interface Context
      Type Parameters:
      T - User type.
      Returns:
      Current user or null if none was set.
    • setUser

      @NonNull default Context setUser(@Nullable Object user)
      Description copied from interface: Context
      Set current user.
      Specified by:
      setUser in interface Context
      Parameters:
      user - Current user.
      Returns:
      This context.
    • matches

      default boolean matches(String pattern)
      Description copied from interface: Context
      Check if the request path matches the given pattern.
      Specified by:
      matches in interface Context
      Parameters:
      pattern - Pattern to use.
      Returns:
      True if request path matches the pattern.
    • getAttribute

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

      @NonNull default Context setAttribute(@NonNull String key, Object value)
      Description copied from interface: Context
      Set an application attribute.
      Specified by:
      setAttribute in interface Context
      Parameters:
      key - Attribute key.
      value - Attribute value.
      Returns:
      This router.
    • flash

      @NonNull default FlashMap flash()
      Description copied from interface: Context
      Flash map.
      Specified by:
      flash in interface Context
      Returns:
      Flash map.
    • flash

      @NonNull default Value flash(@NonNull String name)
      Get a flash attribute.
      Specified by:
      flash in interface Context
      Parameters:
      name - Attribute's name.
      Returns:
      Flash attribute.
    • session

      @NonNull default Value session(@NonNull String name)
      Description copied from interface: Context
      Find a session attribute using the given name. If there is no session or attribute under that name a missing value is returned.
      Specified by:
      session in interface Context
      Parameters:
      name - Attribute's name.
      Returns:
      Session's attribute or missing.
    • session

      @NonNull default Session session()
      Description copied from interface: Context
      Find a session or creates a new session.
      Specified by:
      session in interface Context
      Returns:
      Session.
    • sessionOrNull

      @Nullable default Session sessionOrNull()
      Description copied from interface: Context
      Find an existing session.
      Specified by:
      sessionOrNull in interface Context
      Returns:
      Existing session or null.
    • forward

      @NonNull default Object forward(@NonNull String path)
      Description copied from interface: Context
      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.).

      Specified by:
      forward in interface Context
      Parameters:
      path - Path to forward the request.
      Returns:
      Forward result.
    • cookie

      @NonNull default Value cookie(@NonNull String name)
      Description copied from interface: Context
      Get a cookie matching the given name.
      Specified by:
      cookie in interface Context
      Parameters:
      name - Cookie's name.
      Returns:
      Cookie value.
    • path

      @NonNull default Value path(@NonNull String name)
      Description copied from interface: Context
      Path variable. Value is decoded.
      Specified by:
      path in interface Context
      Parameters:
      name - Path key.
      Returns:
      Associated value or a missing value, but never a null reference.
    • path

      @NonNull default <T> T path(@NonNull Class<T> type)
      Description copied from interface: Context
      Convert the Context.pathMap() to the given type.
      Specified by:
      path in interface Context
      Type Parameters:
      T - Target type.
      Parameters:
      type - Target type.
      Returns:
      Instance of target type.
    • path

      @NonNull default ValueNode path()
      Description copied from interface: Context
      Convert Context.pathMap() to a ValueNode object.
      Specified by:
      path in interface Context
      Returns:
      A value object.
    • query

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

      @NonNull default String queryString()
      Description copied from interface: Context
      Query string with the leading ? or empty string. This is the raw query string, without decoding it.
      Specified by:
      queryString in interface Context
      Returns:
      Query string with the leading ? or empty string. This is the raw query string, without decoding it.
    • query

      @NonNull default <T> T query(@NonNull Class<T> type)
      Description copied from interface: Context
      Convert the queryString to the given type.
      Specified by:
      query in interface Context
      Type Parameters:
      T - Target type.
      Parameters:
      type - Target type.
      Returns:
      Query string converted to target type.
    • queryMap

      @NonNull default Map<String,String> queryMap()
      Description copied from interface: Context
      Query string as simple map.
      /search?q=jooby&sort=name
      Produces
      {q: jooby, sort: name}
      Specified by:
      queryMap in interface Context
      Returns:
      Query string as map.
    • header

      @NonNull default Value header(@NonNull String name)
      Description copied from interface: Context
      Get a header that matches the given name.
      Specified by:
      header in interface Context
      Parameters:
      name - Header name. Case insensitive.
      Returns:
      A header value or missing value, never a null reference.
    • headerMap

      @NonNull default Map<String,String> headerMap()
      Description copied from interface: Context
      Header as single-value map.
      Specified by:
      headerMap in interface Context
      Returns:
      Header as single-value map.
    • accept

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

      default MediaType accept(@NonNull List<MediaType> produceTypes)
      Description copied from interface: Context
      Check if the accept type list matches the given produces list and return the most specific media type from produces list.
      Specified by:
      accept in interface Context
      Parameters:
      produceTypes - Produced types.
      Returns:
      The most specific produces type.
    • getRequestURL

      @NonNull default String getRequestURL()
      Description copied from interface: Context
      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.

      Specified by:
      getRequestURL in interface Context
      Returns:
      Full/entire request url using the Host header.
    • getRequestURL

      @NonNull default String getRequestURL(@NonNull String path)
      Description copied from interface: Context
      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.

      Specified by:
      getRequestURL in interface Context
      Parameters:
      path - Path or suffix to use, can also include query string parameters.
      Returns:
      Full/entire request url using the Host header.
    • getRequestType

      @Nullable default MediaType getRequestType()
      Description copied from interface: Context
      Request Content-Type header or null when missing.
      Specified by:
      getRequestType in interface Context
      Returns:
      Request Content-Type header or null when missing.
    • getRequestType

      @NonNull default MediaType getRequestType(MediaType defaults)
      Description copied from interface: Context
      Request Content-Type header or null when missing.
      Specified by:
      getRequestType in interface Context
      Parameters:
      defaults - Default content type to use when the header is missing.
      Returns:
      Request Content-Type header or null when missing.
    • getRequestLength

      default long getRequestLength()
      Description copied from interface: Context
      Request Content-Length header or -1 when missing.
      Specified by:
      getRequestLength in interface Context
      Returns:
      Request Content-Length header or -1 when missing.
    • getHostAndPort

      @Nullable default String getHostAndPort()
      Description copied from interface: Context
      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.

      Specified by:
      getHostAndPort in interface Context
      Returns:
      Return the host that this request was sent to, in general this will be the value of the Host header.
    • getServerHost

      @NonNull default String getServerHost()
      Description copied from interface: Context
      Server host.
      Specified by:
      getServerHost in interface Context
      Returns:
      Server host.
    • getServerPort

      default int getServerPort()
      Description copied from interface: Context
      Server port for current request.
      Specified by:
      getServerPort in interface Context
      Returns:
      Server port for current request.
    • getPort

      default int getPort()
      Description copied from interface: Context
      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 Context.getServerPort().

      Specified by:
      getPort in interface Context
      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.
    • getHost

      @NonNull default String getHost()
      Description copied from interface: Context
      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 Context.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.

      Specified by:
      getHost in interface Context
      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.
    • isSecure

      default boolean isSecure()
      Description copied from interface: Context
      Returns a boolean indicating whether this request was made using a secure channel, such as HTTPS.
      Specified by:
      isSecure in interface Context
      Returns:
      a boolean indicating if the request was made using a secure channel
    • form

      @NonNull default ValueNode form(@NonNull String name)
      Description copied from interface: Context
      Get a form field that matches the given name.

      File upload retrieval is available using Context.file(String).

      Only for multipart/form-data request.

      Specified by:
      form in interface Context
      Parameters:
      name - Field name.
      Returns:
      Multipart value.
    • form

      @NonNull default <T> T form(@NonNull Class<T> type)
      Description copied from interface: Context
      Convert form data to the given type.

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

      Specified by:
      form in interface Context
      Type Parameters:
      T - Target type.
      Parameters:
      type - Target type.
      Returns:
      Target value.
    • formMap

      @NonNull default Map<String,String> formMap()
      Description copied from interface: Context
      Form data as single-value map.

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

      Specified by:
      formMap in interface Context
      Returns:
      Single-value map.
    • files

      @NonNull default List<FileUpload> files()
      Description copied from interface: Context
      All file uploads. Only for multipart/form-data request.
      Specified by:
      files in interface Context
      Returns:
      All file uploads.
    • files

      @NonNull default List<FileUpload> files(@NonNull String name)
      Description copied from interface: Context
      All file uploads that matches the given field name.

      Only for multipart/form-data request.

      Specified by:
      files in interface Context
      Parameters:
      name - Field name. Please note this is the form field name, not the actual file name.
      Returns:
      All file uploads.
    • file

      @NonNull default FileUpload file(@NonNull String name)
      Description copied from interface: Context
      A file upload that matches the given field name.

      Only for multipart/form-data request.

      Specified by:
      file in interface Context
      Parameters:
      name - Field name. Please note this is the form field name, not the actual file name.
      Returns:
      A file upload.
    • body

      @NonNull default <T> T body(@NonNull Class<T> type)
      Description copied from interface: Context
      Convert the HTTP body to the given type.
      Specified by:
      body in interface Context
      Type Parameters:
      T - Conversion type.
      Parameters:
      type - Reified type.
      Returns:
      Instance of conversion type.
    • body

      @NonNull default <T> T body(@NonNull Type type)
      Description copied from interface: Context
      Convert the HTTP body to the given type.
      Specified by:
      body in interface Context
      Type Parameters:
      T - Conversion type.
      Parameters:
      type - Reified type.
      Returns:
      Instance of conversion type.
    • convertOrNull

      @NonNull default <T> T convertOrNull(@NonNull ValueNode value, @NonNull Class<T> type)
      Description copied from interface: Context
      Converts a value (single or hash) into the given type.
      Specified by:
      convertOrNull in interface Context
      Type Parameters:
      T - Generic type.
      Parameters:
      value - Value to convert.
      type - Expected type.
      Returns:
      Converted value or null.
    • decode

      @NonNull default <T> T decode(@NonNull Type type, @NonNull MediaType contentType)
      Description copied from interface: Context
      Convert the HTTP body to the given type.
      Specified by:
      decode in interface Context
      Type Parameters:
      T - Conversion type.
      Parameters:
      type - Generic type.
      contentType - Content type to use.
      Returns:
      Instance of conversion type.
    • decoder

      @NonNull default MessageDecoder decoder(@NonNull MediaType contentType)
      Description copied from interface: Context
      Get a decoder for the given content type or get an StatusCode.UNSUPPORTED_MEDIA_TYPE.
      Specified by:
      decoder in interface Context
      Parameters:
      contentType - Content type.
      Returns:
      MessageDecoder.
    • setResponseHeader

      @NonNull default Context setResponseHeader(@NonNull String name, @NonNull Date value)
      Description copied from interface: Context
      Set response header.
      Specified by:
      setResponseHeader in interface Context
      Parameters:
      name - Header name.
      value - Header value.
      Returns:
      This context.
    • setResponseHeader

      @NonNull default Context setResponseHeader(@NonNull String name, @NonNull Instant value)
      Description copied from interface: Context
      Set response header.
      Specified by:
      setResponseHeader in interface Context
      Parameters:
      name - Header name.
      value - Header value.
      Returns:
      This context.
    • setResponseHeader

      @NonNull default Context setResponseHeader(@NonNull String name, @NonNull Object value)
      Description copied from interface: Context
      Set response header.
      Specified by:
      setResponseHeader in interface Context
      Parameters:
      name - Header name.
      value - Header value.
      Returns:
      This context.
    • setResponseType

      @NonNull default Context setResponseType(@NonNull MediaType contentType)
      Description copied from interface: Context
      Set response content type header.
      Specified by:
      setResponseType in interface Context
      Parameters:
      contentType - Content type.
      Returns:
      This context.
    • setResponseCode

      @NonNull default Context setResponseCode(@NonNull StatusCode statusCode)
      Description copied from interface: Context
      Set response status code.
      Specified by:
      setResponseCode in interface Context
      Parameters:
      statusCode - Status code.
      Returns:
      This context.
    • render

      @NonNull default Context render(@NonNull Object value)
      Description copied from interface: Context
      Render a value and send the response to client.
      Specified by:
      render in interface Context
      Parameters:
      value - Object value.
      Returns:
      This context.
    • responseStream

      @NonNull default OutputStream responseStream(@NonNull MediaType contentType)
      Description copied from interface: Context
      HTTP response channel as output stream. Usually for chunked responses.
      Specified by:
      responseStream in interface Context
      Parameters:
      contentType - Media type.
      Returns:
      HTTP channel as output stream. Usually for chunked responses.
    • responseStream

      @NonNull default Context responseStream(@NonNull MediaType contentType, @NonNull SneakyThrows.Consumer<OutputStream> consumer) throws Exception
      Description copied from interface: Context
      HTTP response channel as output stream. Usually for chunked responses.
      Specified by:
      responseStream in interface Context
      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 default Context responseStream(@NonNull SneakyThrows.Consumer<OutputStream> consumer) throws Exception
      Description copied from interface: Context
      HTTP response channel as output stream. Usually for chunked responses.
      Specified by:
      responseStream in interface Context
      Parameters:
      consumer - Output stream consumer.
      Returns:
      HTTP channel as output stream. Usually for chunked responses.
      Throws:
      Exception - Is something goes wrong.
    • responseWriter

      @NonNull default PrintWriter responseWriter()
      Description copied from interface: Context
      HTTP response channel as response writer.
      Specified by:
      responseWriter in interface Context
      Returns:
      HTTP channel as response writer. Usually for chunked response.
    • responseWriter

      @NonNull default PrintWriter responseWriter(@NonNull MediaType contentType)
      Description copied from interface: Context
      HTTP response channel as response writer.
      Specified by:
      responseWriter in interface Context
      Parameters:
      contentType - Content type.
      Returns:
      HTTP channel as response writer. Usually for chunked response.
    • responseWriter

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

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

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

      @NonNull default Context sendRedirect(@NonNull String location)
      Description copied from interface: Context
      Send a 302 response.
      Specified by:
      sendRedirect in interface Context
      Parameters:
      location - Location.
      Returns:
      This context.
    • sendRedirect

      @NonNull default Context sendRedirect(@NonNull StatusCode redirect, @NonNull String location)
      Description copied from interface: Context
      Send a redirect response.
      Specified by:
      sendRedirect in interface Context
      Parameters:
      redirect - Redirect status code.
      location - Location.
      Returns:
      This context.
    • send

      @NonNull default Context send(@NonNull byte[]... data)
      Description copied from interface: Context
      Send response data.
      Specified by:
      send in interface Context
      Parameters:
      data - Response.
      Returns:
      This context.
    • send

      @NonNull default Context send(@NonNull String data)
      Description copied from interface: Context
      Send response data.
      Specified by:
      send in interface Context
      Parameters:
      data - Response. Use UTF-8 charset.
      Returns:
      This context.
    • send

      @NonNull default Context send(@NonNull FileDownload file)
      Description copied from interface: Context
      Send a file download response.
      Specified by:
      send in interface Context
      Parameters:
      file - File download.
      Returns:
      This context.
    • send

      @NonNull default Context send(@NonNull Path file)
      Description copied from interface: Context
      Send a file response.
      Specified by:
      send in interface Context
      Parameters:
      file - File response.
      Returns:
      This context.
    • sendError

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

      @NonNull default Context sendError(@NonNull Throwable cause, @NonNull StatusCode code)
      Send an error response. This method set the error code.
      Specified by:
      sendError in interface Context
      Parameters:
      cause - Error. If this is a fatal error it is going to be rethrow it.
      code - Default error code.
      Returns:
      This context.