Options
All
  • Public
  • Public/Protected
  • All
Menu

Class 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 {@link Handler#handle} 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.

If you use the internal error handler

Hierarchy

  • RoutingContext

Index

Methods

acceptableLanguages

  • Returns the languages for the current request. The languages are determined from the Accept-Language 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 LanguageHeader

addBodyEndHandler

  • addBodyEndHandler(handler: ((res: void) => void) | Handler<void>): number
  • Provides a handler that will be called after the last part of the body is written to the wire. The handler is called asynchronously of when the response has been received by the client. This provides a hook allowing you to do more operations once the request has been sent over the wire. Do not use this for resource cleanup as this handler might never get called (e.g. if the connection is reset).

    Parameters

    • handler: ((res: void) => void) | Handler<void>

    Returns number

addCookie

  • Parameters

    • cookie: Cookie

    Returns RoutingContext

addEndHandler

  • addEndHandler(handler: ((res: AsyncResult<void>) => void) | Handler<AsyncResult<void>>): number
  • Add an end handler for the request/response context. This will be called when the response is disposed or an exception has been encountered to allow consistent cleanup. The handler is called asynchronously of when the response has been received by the client.

    Parameters

    • handler: ((res: AsyncResult<void>) => void) | Handler<AsyncResult<void>>

    Returns number

addHeadersEndHandler

  • addHeadersEndHandler(handler: ((res: void) => void) | Handler<void>): number
  • 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.

    Parameters

    • handler: ((res: void) => void) | Handler<void>

    Returns number

attachment

  • Set Content-Disposition get to "attachment" with optional filename mime type.

    Parameters

    • filename: string

    Returns RoutingContext

body

  • Returns RequestBody

clearUser

  • clearUser(): void
  • 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.

    Returns void

cookieCount

  • cookieCount(): number
  • Returns number

cookieMap

  • cookieMap(): {}
  • Returns {}

    • [key: string]: Cookie

currentRoute

  • currentRoute(): Route | null
  • Returns Route | null

data

  • data(): {}
  • Returns {}

    • [key: string]: any

end

  • end(chunk: string): PromiseLike<void>
  • end(chunk: string, handler: ((res: AsyncResult<void>) => void) | Handler<AsyncResult<void>>): RoutingContext
  • end(buffer: Buffer): PromiseLike<void>
  • end(buffer: Buffer, handler: ((res: AsyncResult<void>) => void) | Handler<AsyncResult<void>>): RoutingContext
  • end(): PromiseLike<void>
  • end(handler: ((res: AsyncResult<void>) => void) | Handler<AsyncResult<void>>): RoutingContext
  • See {@link RoutingContext#end}

    Parameters

    • chunk: string

    Returns PromiseLike<void>

  • See {@link RoutingContext#end}

    Parameters

    • chunk: string
    • handler: ((res: AsyncResult<void>) => void) | Handler<AsyncResult<void>>

    Returns RoutingContext

  • See {@link RoutingContext#end}

    Parameters

    • buffer: Buffer

    Returns PromiseLike<void>

  • See {@link RoutingContext#end}

    Parameters

    • buffer: Buffer
    • handler: ((res: AsyncResult<void>) => void) | Handler<AsyncResult<void>>

    Returns RoutingContext

  • See {@link RoutingContext#end}

    Returns PromiseLike<void>

  • See {@link RoutingContext#end}

    Parameters

    • handler: ((res: AsyncResult<void>) => void) | Handler<AsyncResult<void>>

    Returns RoutingContext

etag

  • Set the ETag of a response. This will normalize the quotes if necessary.

    etag('md5hashsum'); etag('"md5hashsum"'); ('W/"123456789"');

    Parameters

    • etag: string

    Returns RoutingContext

fail

  • fail(statusCode: number): void
  • fail(throwable: Throwable | Error): void
  • fail(statusCode: number, throwable: Throwable | Error): void
  • 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 It will trigger the error handler matching the status code. You can define such error handler with {@link Router#errorHandler}. If no error handler is not defined, It will send a default failure response with provided status code.

    Parameters

    • statusCode: number

    Returns void

  • Fail the context with the specified throwable and 500 status code.

    This will cause the router to route the context to any matching failure handlers for the request. If no failure handlers match It will trigger the error handler matching the status code. You can define such error handler with {@link Router#errorHandler}. If no error handler is not defined, It will send a default failure response with 500 status code.

    Parameters

    • throwable: Throwable | Error

    Returns void

  • Fail the context with the specified throwable and the specified the status code.

    This will cause the router to route the context to any matching failure handlers for the request. If no failure handlers match It will trigger the error handler matching the status code. You can define such error handler with {@link Router#errorHandler}. If no error handler is not defined, It will send a default failure response with provided status code.

    Parameters

    • statusCode: number
    • throwable: Throwable | Error

    Returns void

failed

  • failed(): boolean
  • Returns boolean

failure

  • failure(): Throwable | null
  • If the context is being routed to failure handlers after a failure has been triggered by calling {@link 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 Throwable | null

fileUploads

  • Returns FileUpload

get

  • get<T>(key: string): T | null
  • get<T>(key: string, defaultValue: T): T
  • Get some data from the context. The data is available in any handlers that receive the context.

    Type parameters

    • T

    Parameters

    • key: string

    Returns T | null

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

    Type parameters

    • T

    Parameters

    • key: string
    • defaultValue: T

    Returns T

getAcceptableContentType

  • getAcceptableContentType(): string | null
  • 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 string | null

getBody

  • getBody(): Buffer | null
  • Returns Buffer | null

getBodyAsJson

  • getBodyAsJson(maxAllowedLength: number): {} | null
  • getBodyAsJson(): {} | null
  • Parameters

    • maxAllowedLength: number

    Returns {} | null

  • Returns {} | null

getBodyAsJsonArray

  • getBodyAsJsonArray(maxAllowedLength: number): any[] | null
  • getBodyAsJsonArray(): any[] | null
  • Parameters

    • maxAllowedLength: number

    Returns any[] | null

  • Returns any[] | null

getBodyAsString

  • getBodyAsString(): string | null
  • getBodyAsString(encoding: string): string | null
  • Returns string | null

  • Parameters

    • encoding: string

    Returns string | null

getCookie

  • getCookie(name: string): Cookie | null
  • Parameters

    • name: string

    Returns Cookie | null

is

  • is(type: string): boolean
  • Check if the incoming request contains the "Content-Type" get field, and it contains the give mime type. If there is no request body, false is returned. If there is no content type, false is returned. Otherwise, it returns true if the type that matches.

    Examples:

    // With Content-Type: text/html; charset=utf-8 is("html"); // => true is("text/html"); // => true

    // When Content-Type is application/json is("application/json"); // => true is("html"); // => false

    Parameters

    • type: string

    Returns boolean

isFresh

  • isFresh(): boolean
  • Check if the request is fresh, aka Last-Modified and/or the ETag still match.

    Returns boolean

isSessionAccessed

  • isSessionAccessed(): boolean
  • Whether the {@link RoutingContext#session} has been already called or not. This is usually used by the SessionHandler.

    Returns boolean

json

  • json(json: any): PromiseLike<void>
  • json(json: any, handler: ((res: AsyncResult<void>) => void) | Handler<AsyncResult<void>>): RoutingContext
  • See {@link RoutingContext#json}.

    Parameters

    • json: any

    Returns PromiseLike<void>

  • See {@link RoutingContext#json}.

    Parameters

    • json: any
    • handler: ((res: AsyncResult<void>) => void) | Handler<AsyncResult<void>>

    Returns RoutingContext

lastModified

  • Set the Last-Modified date using a String.

    Parameters

    • instant: string

    Returns RoutingContext

  • Set the Last-Modified date using a Instant.

    Parameters

    • instant: Date

    Returns RoutingContext

mountPoint

  • mountPoint(): string | null
  • Returns string | null

next

  • next(): void
  • 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.

    Returns void

normalisedPath

  • normalisedPath(): string
  • Use {@link RoutingContext#normalizedPath} instead

    Returns string

normalizedPath

  • normalizedPath(): string
  • Return the normalized path for the request.

    The normalized 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 normalized 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 normalized path as opposed to if accessing server resources requested by a client.

    Returns string

parsedHeaders

  • The headers:

    1. Accept
    2. Accept-Charset
    3. Accept-Encoding
    4. Accept-Language
    5. Content-Type
    Parsed into ParsedHeaderValue

    Returns ParsedHeaderValues

pathParam

  • pathParam(name: string): string | null
  • Gets the value of a single path parameter

    Parameters

    • name: string

    Returns string | null

pathParams

  • pathParams(): {}
  • Returns a map of named parameters as defined in path declaration with their actual values

    Returns {}

    • [key: string]: string

preferredLanguage

  • Helper to return the user preferred language. It is the same action as returning the first element of the acceptable languages.

    Returns LanguageHeader

put

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

    Parameters

    • key: string
    • obj: any

    Returns RoutingContext

queryParam

  • queryParam(name: string): string
  • Gets the value of a single query parameter. For more info {@link RoutingContext#queryParams}

    Parameters

    • name: string

    Returns string

queryParams

  • queryParams(): MultiMap
  • queryParams(encoding: any): MultiMap
  • Returns a map of all query parameters inside the query string
    The query parameters are lazily decoded: the decoding happens on the first time this method is called. If the query string is invalid it fails the context

    Returns MultiMap

  • Always decode the current query string with the given encoding. The decode result is never cached. Callers to this method are expected to cache the result if needed. Usually users should use {@link RoutingContext#queryParams}.

    This method is only useful when the requests without content type (GET requests as an example) expect that query params are in the ASCII format ISO-5559-1.

    Parameters

    • encoding: any

    Returns MultiMap

redirect

  • redirect(url: string): PromiseLike<void>
  • redirect(url: string, handler: ((res: AsyncResult<void>) => void) | Handler<AsyncResult<void>>): RoutingContext
  • See {@link RoutingContext#redirect}.

    Parameters

    • url: string

    Returns PromiseLike<void>

  • See {@link RoutingContext#redirect}.

    Parameters

    • url: string
    • handler: ((res: AsyncResult<void>) => void) | Handler<AsyncResult<void>>

    Returns RoutingContext

remove

  • remove<T>(key: string): T | null
  • Remove some data from the context. The data is available in any handlers that receive the context.

    Type parameters

    • T

    Parameters

    • key: string

    Returns T | null

removeBodyEndHandler

  • removeBodyEndHandler(handlerID: number): boolean
  • Remove a body end handler

    Parameters

    • handlerID: number

    Returns boolean

removeCookie

  • removeCookie(name: string): Cookie | null
  • removeCookie(name: string, invalidate: boolean): Cookie | null
  • Parameters

    • name: string

    Returns Cookie | null

  • Parameters

    • name: string
    • invalidate: boolean

    Returns Cookie | null

removeEndHandler

  • removeEndHandler(handlerID: number): boolean
  • Remove an end handler

    Parameters

    • handlerID: number

    Returns boolean

removeHeadersEndHandler

  • removeHeadersEndHandler(handlerID: number): boolean
  • Remove a headers end handler

    Parameters

    • handlerID: number

    Returns boolean

request

  • request(): HttpServerRequest
  • Returns HttpServerRequest

reroute

  • reroute(path: string): void
  • reroute(method: HttpMethod, path: string): void
  • 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. Query params will also be allowed and available.

    Parameters

    • path: string

    Returns void

  • Restarts the current router with a new method and path. All path parameters are then parsed and available on the params list. Query params will also be allowed and available.

    Parameters

    • method: HttpMethod
    • path: string

    Returns void

response

  • response(): HttpServerResponse
  • Returns HttpServerResponse

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 Session | null

setAcceptableContentType

  • setAcceptableContentType(contentType: string | null | undefined): void
  • Set the acceptable content type. Used by

    Parameters

    • contentType: string | null | undefined

    Returns void

setBody

  • setBody(body: Buffer): void
  • Parameters

    • body: Buffer

    Returns void

setSession

  • setSession(session: Session): void
  • Parameters

    Returns void

setUser

  • setUser(user: User): void
  • Set the user. Usually used by auth handlers to inject a User. You will not normally call this method.

    Parameters

    • user: User

    Returns void

statusCode

  • statusCode(): number
  • If the context is being routed to failure handlers after a failure has been triggered by calling {@link 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 number

user

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

    Returns User | null

vertx

  • vertx(): Vertx
  • Returns Vertx

Generated using TypeDoc