Package: io.vertx.groovy.ext.web

## [Groovy] Class RoutingContext

- io.vertx.groovy.ext.web.RoutingContext

- Represents the context for the handling of a request in Vert.x-Web.

  A new instance is created for each HTTP request that is received in the Router.accept of the router.

  The same instance is passed to any matching request or failure handlers during the routing of the request or failure.

  The context provides access to the and and allows you to maintain arbitrary data that lives for the lifetime of the context. Contexts are discarded once they have been routed to the handler for the request.

  The context also provides access to the Session, cookies and body for the request, given the correct handlers in the application.

- - ### Constructor Summary

    Constructors
    Constructor and description
    `RoutingContext (java.lang.Object delegate)`

  - ### Methods Summary

    Methods
    Type | Name and description
    --- | ---
    `java.util.List<Locale>` | `acceptableLocales()` Returns the locales for the current request.
    `int` | `addBodyEndHandler(io.vertx.core.Handler<java.lang.Void> handler)` Add a handler that will be called just before the response body has been completely written.
    `RoutingContext` | `addCookie(Cookie cookie)` Add a cookie.
    `int` | `addHeadersEndHandler(io.vertx.core.Handler<java.lang.Void> handler)` Add a handler that will be called just before headers are written to the response.
    `void` | `clearUser()` Clear the current user object in the context.
    `int` | `cookieCount()` Returns: the number of cookies.
    `java.util.Set<Cookie>` | `cookies()` Returns: a set of all the cookies.
    `Route` | `currentRoute()` Returns: the current route this context is being routed through.
    `void` | `fail(int statusCode)` Fail the context with the specified status code.
    `void` | `fail(java.lang.Throwable throwable)` Fail the context with the specified throwable.
    `boolean` | `failed()` Returns: true if the context is being routed to failure handlers.
    `java.lang.Throwable` | `failure()` If the context is being routed to failure handlers after a failure has been triggered by calling RoutingContext.fail then this will return that throwable.
    `java.util.Set<FileUpload>` | `fileUploads()` Returns: a set of fileuploads (if any) for the request.
    `T` | `get(java.lang.String key)` Get some data from the context.
    `java.lang.String` | `getAcceptableContentType()` If the route specifies produces matches, e.g. produces `text/html` and `text/plain`, and the `accept` header matches one or more of these then this returns the most acceptable match.
    `Buffer` | `getBody()` Returns: Get the entire HTTP request body as a .
    `java.util.Map<java.lang.String, java.lang.Object>` | `getBodyAsJson()` Returns: Get the entire HTTP request body as a .
    `java.util.List<java.lang.Object>` | `getBodyAsJsonArray()` Returns: Get the entire HTTP request body as a .
    `java.lang.String` | `getBodyAsString()` Returns: the entire HTTP request body as a string, assuming UTF-8 encoding.
    `java.lang.String` | `getBodyAsString(java.lang.String encoding)` Get the entire HTTP request body as a string, assuming the specified encoding.
    `Cookie` | `getCookie(java.lang.String name)` Get the cookie with the specified name.
    `java.lang.Object` | `getDelegate()`
    `java.lang.String` | `mountPoint()` Returns: the mount point for this router.
    `void` | `next()` Tell the router to route this context to the next matching route (if any).
    `java.lang.String` | `normalisedPath()` Return the normalised path for the request.
    `java.lang.String` | `pathParam(java.lang.String name)` Gets the value of a single path parameter
    `java.util.Map<java.lang.String, java.lang.String>` | `pathParams()` Returns a map of named parameters as defined in path declaration with their actual values
    `Locale` | `preferredLocale()` Helper to return the user preferred locale.
    `RoutingContext` | `put(java.lang.String key, java.lang.Object obj)` Put some arbitrary data in the context.
    `boolean` | `removeBodyEndHandler(int handlerID)` Remove a body end handler
    `Cookie` | `removeCookie(java.lang.String name)` Remove a cookie.
    `boolean` | `removeHeadersEndHandler(int handlerID)` Remove a headers end handler
    `HttpServerRequest` | `request()` @return Returns: the HTTP request object
    `void` | `reroute(java.lang.String path)` Restarts the current router with a new path and reusing the original method.
    `void` | `reroute(io.vertx.core.http.HttpMethod method, java.lang.String path)` Restarts the current router with a new method and path.
    `HttpServerResponse` | `response()` @return Returns: the HTTP response object
    `Session` | `session()` Get the session.
    `void` | `setAcceptableContentType(java.lang.String contentType)` Set the acceptable content type.
    `void` | `setBody(Buffer body)` Set the body.
    `void` | `setSession(Session session)` Set the session.
    `void` | `setUser(User user)` Set the user.
    `int` | `statusCode()` If the context is being routed to failure handlers after a failure has been triggered by calling RoutingContext.fail then this will return that status code.
    `User` | `user()` Get the authenticated user (if any).
    `Vertx` | `vertx()` @return Returns: the Vert.x instance associated to the initiating Router for this context

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class java.lang.Object` | `java.lang.Object#wait(long, int), java.lang.Object#wait(long), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll()`

- - ### Constructor Detail

    - #### RoutingContext(java.lang.Object delegate)

  - ### Method Detail

    - #### java.util.List<Locale> acceptableLocales()

      Returns the locales for the current request. The locales are determined from the `accept-languages` header and sorted on quality. When 2 or more entries have the same quality then the order used to return the best match is based on the lowest index on the original list. For example if a user has en-US and en-GB with same quality and this order the best match will be en-US because it was declared as first entry by the client.

      Returns:
        the best matched locale for the request

    - #### int addBodyEndHandler(io.vertx.core.Handler<java.lang.Void> handler)

      Add a handler that will be called just before the response body has been completely written. This gives you a hook where you can write any extra data to the response before it has ended when it will be too late.

      Returns:
        the id of the handler. This can be used if you later want to remove the handler.
      Parameters:
        `handler` - the handler

    - #### RoutingContext addCookie(Cookie cookie)

      Add a cookie. This will be sent back to the client in the response. The context must have first been routed to a CookieHandler for this to work.

      Returns:
        a reference to this, so the API can be used fluently
      Parameters:
        `cookie` - the cookie

    - #### int addHeadersEndHandler(io.vertx.core.Handler<java.lang.Void> handler)

      Add a handler that will be called just before headers are written to the response. This gives you a hook where you can write any extra headers before the response has been written when it will be too late.

      Returns:
        the id of the handler. This can be used if you later want to remove the handler.
      Parameters:
        `handler` - the handler

    - #### void clearUser()

      Clear the current user object in the context. This usually is used for implementing a log out feature, since the current user is unbounded from the routing context.

    - #### int cookieCount()

      Returns:
        the number of cookies. The context must have first been routed to a CookieHandler for this to work.

    - #### java.util.Set<Cookie> cookies()

      Returns:
        a set of all the cookies. The context must have first been routed to a CookieHandler for this to be populated.

    - #### Route currentRoute()

      Returns:
        the current route this context is being routed through.

    - #### void fail(int statusCode)

      Fail the context with the specified status code.

      This will cause the router to route the context to any matching failure handlers for the request. If no failure handlers match a default failure response will be sent.

      Parameters:
        `statusCode` - the HTTP status code

    - #### void fail(java.lang.Throwable throwable)

      Fail the context with the specified throwable.

      This will cause the router to route the context to any matching failure handlers for the request. If no failure handlers match a default failure response with status code 500 will be sent.

      Parameters:
        `throwable` - a throwable representing the failure

    - #### boolean failed()

      Returns:
        true if the context is being routed to failure handlers.

    - #### java.lang.Throwable failure()

      If the context is being routed to failure handlers after a failure has been triggered by calling RoutingContext.fail then this will return that throwable. It can be used by failure handlers to render a response, e.g. create a failure response page.

      Returns:
        the throwable used when signalling failure

    - #### java.util.Set<FileUpload> fileUploads()

      Returns:
        a set of fileuploads (if any) for the request. The context must have first been routed to a BodyHandler for this to work.

    - #### T get(java.lang.String key)

      Get some data from the context. The data is available in any handlers that receive the context.

      Returns:
        the data
      Parameters:
        `key` - the key for the data

    - #### java.lang.String getAcceptableContentType()

      If the route specifies produces matches, e.g. produces `text/html` and `text/plain`, and the `accept` header matches one or more of these then this returns the most acceptable match.

      Returns:
        the most acceptable content type.

    - #### Buffer getBody()

      Returns:
        Get the entire HTTP request body as a . The context must have first been routed to a BodyHandler for this to be populated.

    - #### java.util.Map<java.lang.String, java.lang.Object> getBodyAsJson()

      Returns:
        Get the entire HTTP request body as a . The context must have first been routed to a BodyHandler for this to be populated.

    - #### java.util.List<java.lang.Object> getBodyAsJsonArray()

      Returns:
        Get the entire HTTP request body as a . The context must have first been routed to a BodyHandler for this to be populated.

    - #### java.lang.String getBodyAsString()

      Returns:
        the entire HTTP request body as a string, assuming UTF-8 encoding. The context must have first been routed to a BodyHandler for this to be populated.

    - #### java.lang.String getBodyAsString(java.lang.String encoding)

      Get the entire HTTP request body as a string, assuming the specified encoding. The context must have first been routed to a BodyHandler for this to be populated.

      Returns:
        the body
      Parameters:
        `encoding` - the encoding, e.g. "UTF-16"

    - #### Cookie getCookie(java.lang.String name)

      Get the cookie with the specified name. The context must have first been routed to a CookieHandler for this to work.

      Returns:
        the cookie
      Parameters:
        `name` - the cookie name

    - #### java.lang.Object getDelegate()

    - #### java.lang.String mountPoint()

      Returns:
        the mount point for this router. It will be null for a top level router. For a sub-router it will be the path at which the subrouter was mounted.

    - #### void next()

      Tell the router to route this context to the next matching route (if any). This method, if called, does not need to be called during the execution of the handler, it can be called some arbitrary time later, if required.

      If next is not called for a handler then the handler should make sure it ends the response or no response will be sent.

    - #### java.lang.String normalisedPath()

      Return the normalised path for the request.

      The normalised path is where the URI path has been decoded, i.e. any unicode or other illegal URL characters that were encoded in the original URL with `%` will be returned to their original form. E.g. `%20` will revert to a space. Also `+` reverts to a space in a query.

      The normalised path will also not contain any `..` character sequences to prevent resources being accessed outside of the permitted area.

      It's recommended to always use the normalised path as opposed to if accessing server resources requested by a client.

      Returns:
        the normalised path

    - #### java.lang.String pathParam(java.lang.String name)

      Gets the value of a single path parameter

      Returns:
        the actual value of the parameter or null if it doesn't exist
      Parameters:
        `name` - the name of parameter as defined in path declaration

    - #### java.util.Map<java.lang.String, java.lang.String> pathParams()

      Returns a map of named parameters as defined in path declaration with their actual values

      Returns:
        the map of named parameters

    - #### Locale preferredLocale()

      Helper to return the user preferred locale. It is the same action as returning the first element of the acceptable locales.

      Returns:
        the users preferred locale.

    - #### RoutingContext put(java.lang.String key, java.lang.Object obj)

      Put some arbitrary data in the context. This will be available in any handlers that receive the context.

      Returns:
        a reference to this, so the API can be used fluently
      Parameters:
        `key` - the key for the data
        `obj` - the data

    - #### boolean removeBodyEndHandler(int handlerID)

      Remove a body end handler

      Returns:
        true if the handler existed and was removed, false otherwise
      Parameters:
        `handlerID` - the id as returned from RoutingContext.addBodyEndHandler.

    - #### Cookie removeCookie(java.lang.String name)

      Remove a cookie. The context must have first been routed to a CookieHandler for this to work.

      Returns:
        the cookie, if it existed, or null
      Parameters:
        `name` - the name of the cookie

    - #### boolean removeHeadersEndHandler(int handlerID)

      Remove a headers end handler

      Returns:
        true if the handler existed and was removed, false otherwise
      Parameters:
        `handlerID` - the id as returned from RoutingContext.addHeadersEndHandler.

    - #### HttpServerRequest request()

      Returns:
        the HTTP request object

    - #### void reroute(java.lang.String path)

      Restarts the current router with a new path and reusing the original method. All path parameters are then parsed and available on the params list.

      Parameters:
        `path` - the new http path.

    - #### void reroute(io.vertx.core.http.HttpMethod method, java.lang.String path)

      Restarts the current router with a new method and path. All path parameters are then parsed and available on the params list.

      Parameters:
        `method` - the new http request
        `path` - the new http path.

    - #### HttpServerResponse response()

      Returns:
        the HTTP response object

    - #### Session session()

      Get the session. The context must have first been routed to a SessionHandler for this to be populated. Sessions live for a browser session, and are maintained by session cookies.

      Returns:
        the session.

    - #### void setAcceptableContentType(java.lang.String contentType)

      Set the acceptable content type. Used by

      Parameters:
        `contentType` - the content type

    - #### void setBody(Buffer body)

      Set the body. Used by the BodyHandler. You will not normally call this method.

      Parameters:
        `body` - the body

    - #### void setSession(Session session)

      Set the session. Used by the SessionHandler. You will not normally call this method.

      Parameters:
        `session` - the session

    - #### void setUser(User user)

      Set the user. Usually used by auth handlers to inject a User. You will not normally call this method.

      Parameters:
        `user` - the user

    - #### int statusCode()

      If the context is being routed to failure handlers after a failure has been triggered by calling RoutingContext.fail then this will return that status code. It can be used by failure handlers to render a response, e.g. create a failure response page. When the status code has not been set yet (it is undefined) its value will be -1.

      Returns:
        the status code used when signalling failure

    - #### User user()

      Get the authenticated user (if any). This will usually be injected by an auth handler if authentication if successful.

      Returns:
        the user, or null if the current user is not authenticated.

    - #### Vertx vertx()

      Returns:
        the Vert.x instance associated to the initiating Router for this context