Class RoutingContext
- All Implemented Interfaces:
MutinyDelegate
A new instance is created for each HTTP request that is received in the
Handler.handle(Object) 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 HttpServerRequest and HttpServerResponse
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
NOTE: This class has been automatically generated from the original non Mutiny-ified interface.
- Author:
- Tim Fox
- See Also:
-
RoutingContext
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionRoutingContext(io.vertx.ext.web.RoutingContext delegate) Create a new instance ofRoutingContextdelegating to the given (non-null) instance ofRoutingContext.RoutingContext(Object delegate) -
Method Summary
Modifier and TypeMethodDescriptionReturns the languages for the current request.intaddBodyEndHandler(Runnable handler) Provides a handler that will be called after the last part of the body is written to the wire.intaddEndHandler(Consumer<io.vertx.core.AsyncResult<Void>> handler) Add an end handler for the request/response context.intaddHeadersEndHandler(Runnable handler) Add a handler that will be called just before headers are written to the response.attachment(String filename) Set Content-Disposition get to "attachment" with optionalfilenamemime type.body()voidCancel all unfinished file upload in progress and delete all uploaded files.data()io.smallrye.mutiny.Uni<Void>end()Shortcut to the response end.io.smallrye.mutiny.Uni<Void>end(io.vertx.core.buffer.Buffer buffer) Shortcut to the response end.io.smallrye.mutiny.Uni<Void>Shortcut to the response end.voidShortcut to the response end.voidendAndAwait(io.vertx.core.buffer.Buffer buffer) Shortcut to the response end.voidendAndAwait(String chunk) Shortcut to the response end.Shortcut to the response end.endAndForget(io.vertx.core.buffer.Buffer buffer) Shortcut to the response end.endAndForget(String chunk) Shortcut to the response end.booleanSet the ETag of a response.voidfail(int statusCode) Fail the context with the specified status code.voidFail the context with the specified throwable and the specified the status code.voidFail the context with the specified throwable and 500 status code.booleanfailed()failure()If the context is being routed to failure handlers after a failure has been triggered by callingfail(Throwable)then this will return that throwable.<T> TGet some data from the context.<T> TGet some data from the context.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.io.vertx.ext.web.RoutingContextGet the delegate instance.inthashCode()booleanCheck if the incoming request contains the "Content-Type" get field, and it contains the give mime `type`.booleanisFresh()Check if the request is fresh, aka Last-Modified and/or the ETag still match.booleanWhether thesession()has been already called or not.io.smallrye.mutiny.Uni<Void>Encode an Object to JSON and end the request.voidjsonAndAwait(Object json) Encode an Object to JSON and end the request.jsonAndForget(Object json) Encode an Object to JSON and end the request.lastModified(String instant) Set the Last-Modified date using a String.lastModified(Instant instant) Set the Last-Modified date using a Instant.static RoutingContextnewInstance(io.vertx.ext.web.RoutingContext delegate) Creates a new instance of theRoutingContext.voidnext()Tell the router to route this context to the next matching route (if any).Returns the normalized path of the current request.The headers: Accept Accept-Charset Accept-Encoding Accept-Language Content-Type Parsed intoParsedHeaderValueGets the value of a single path parameterReturns a map of named parameters as defined in path declaration with their actual valuesHelper to return the user preferred language.Put some arbitrary data in the context.queryParam(String name) Gets the value of a single query parameter.io.vertx.core.MultiMapReturns 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.io.vertx.core.MultiMapqueryParams(Charset encoding) Always decode the current query string with the givenencoding.io.smallrye.mutiny.Uni<Void>Perform a 302 redirect tourl.voidredirectAndAwait(String url) Perform a 302 redirect tourl.redirectAndForget(String url) Perform a 302 redirect tourl.<T> TRemove some data from the context.booleanremoveBodyEndHandler(int handlerID) Remove a body end handlerbooleanremoveEndHandler(int handlerID) Remove an end handlerbooleanremoveHeadersEndHandler(int handlerID) Remove a headers end handlerrequest()voidRestarts the current router with a new method and path.voidRestarts the current router with a new path and reusing the original method.response()session()Get the session.voidsetAcceptableContentType(String contentType) Set the acceptable content type.intIf the context is being routed to failure handlers after a failure has been triggered by callingfail(int)then this will return that status code.toString()user()Get the authenticated user (if any).Control the user associated with this request.vertx()
-
Field Details
-
__TYPE_ARG
-
-
Constructor Details
-
RoutingContext
public RoutingContext(io.vertx.ext.web.RoutingContext delegate) Create a new instance ofRoutingContextdelegating to the given (non-null) instance ofRoutingContext. -
RoutingContext
-
-
Method Details
-
getDelegate
public io.vertx.ext.web.RoutingContext getDelegate()Get the delegate instance.This method returns the instance on which this shim is delegating the calls. And so, give you access to the bare API.
- Specified by:
getDelegatein interfaceMutinyDelegate- Returns:
- the delegate instance
-
redirect
Perform a 302 redirect tourl. If a custom 3xx code is already defined, then that one will be preferred. The string "back" is special-cased to provide Referrer support, when Referrer is not present "/" is used. Examples: redirect('back'); redirect('/login'); redirect('http://google.com');Unlike the bare Vert.x variant, this method returns a
Uni. The uni emits the result of the operation as item. If the operation fails, the uni emits the failure.Don't forget to subscribe on it to trigger the operation.
- Parameters:
url- the target url- Returns:
- A
Unirepresenting the asynchronous result of this operation. - See Also:
-
RoutingContext.redirect(String)
-
redirectAndAwait
Perform a 302 redirect tourl. If a custom 3xx code is already defined, then that one will be preferred. The string "back" is special-cased to provide Referrer support, when Referrer is not present "/" is used. Examples: redirect('back'); redirect('/login'); redirect('http://google.com');Unlike the bare Vert.x variant, this method returns a
Void. This method awaits indefinitely for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in aRuntimeException).- Parameters:
url- the target url- See Also:
-
RoutingContext.redirect(String)
-
redirectAndForget
Perform a 302 redirect tourl. If a custom 3xx code is already defined, then that one will be preferred. The string "back" is special-cased to provide Referrer support, when Referrer is not present "/" is used. Examples: redirect('back'); redirect('/login'); redirect('http://google.com');Unlike the bare Vert.x variant, this method ignores the
Voidresult or any failure.- Parameters:
url- the target url- Returns:
- The current instance to chain operations if needed.
- See Also:
-
RoutingContext.redirect(String)
-
json
Encode an Object to JSON and end the request. WhenContent-Typeis not set then correctContent-Typewill be applied to the responseUnlike the bare Vert.x variant, this method returns a
Uni. The uni emits the result of the operation as item. If the operation fails, the uni emits the failure.Don't forget to subscribe on it to trigger the operation.
- Parameters:
json- the json- Returns:
- A
Unirepresenting the asynchronous result of this operation. - See Also:
-
RoutingContext.json(Object)
-
jsonAndAwait
Encode an Object to JSON and end the request. WhenContent-Typeis not set then correctContent-Typewill be applied to the responseUnlike the bare Vert.x variant, this method returns a
Void. This method awaits indefinitely for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in aRuntimeException).- Parameters:
json- the json- See Also:
-
RoutingContext.json(Object)
-
jsonAndForget
Encode an Object to JSON and end the request. WhenContent-Typeis not set then correctContent-Typewill be applied to the responseUnlike the bare Vert.x variant, this method ignores the
Voidresult or any failure.- Parameters:
json- the json- Returns:
- The current instance to chain operations if needed.
- See Also:
-
RoutingContext.json(Object)
-
end
Shortcut to the response end.Unlike the bare Vert.x variant, this method returns a
Uni. The uni emits the result of the operation as item. If the operation fails, the uni emits the failure.Don't forget to subscribe on it to trigger the operation.
- Parameters:
chunk- a chunk- Returns:
- A
Unirepresenting the asynchronous result of this operation. - See Also:
-
RoutingContext.end(String)
-
endAndAwait
Shortcut to the response end.Unlike the bare Vert.x variant, this method returns a
Void. This method awaits indefinitely for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in aRuntimeException).- Parameters:
chunk- a chunk- See Also:
-
RoutingContext.end(String)
-
endAndForget
Shortcut to the response end.Unlike the bare Vert.x variant, this method ignores the
Voidresult or any failure.- Parameters:
chunk- a chunk- Returns:
- The current instance to chain operations if needed.
- See Also:
-
RoutingContext.end(String)
-
end
Shortcut to the response end.Unlike the bare Vert.x variant, this method returns a
Uni. The uni emits the result of the operation as item. If the operation fails, the uni emits the failure.Don't forget to subscribe on it to trigger the operation.
- Parameters:
buffer- a chunk- Returns:
- A
Unirepresenting the asynchronous result of this operation. - See Also:
-
RoutingContext.end(Buffer)
-
endAndAwait
public void endAndAwait(io.vertx.core.buffer.Buffer buffer) Shortcut to the response end.Unlike the bare Vert.x variant, this method returns a
Void. This method awaits indefinitely for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in aRuntimeException).- Parameters:
buffer- a chunk- See Also:
-
RoutingContext.end(Buffer)
-
endAndForget
Shortcut to the response end.Unlike the bare Vert.x variant, this method ignores the
Voidresult or any failure.- Parameters:
buffer- a chunk- Returns:
- The current instance to chain operations if needed.
- See Also:
-
RoutingContext.end(Buffer)
-
end
Shortcut to the response end.Unlike the bare Vert.x variant, this method returns a
Uni. The uni emits the result of the operation as item. If the operation fails, the uni emits the failure.Don't forget to subscribe on it to trigger the operation.
- Returns:
- A
Unirepresenting the asynchronous result of this operation. - See Also:
-
RoutingContext.end()
-
endAndAwait
public void endAndAwait()Shortcut to the response end.Unlike the bare Vert.x variant, this method returns a
Void. This method awaits indefinitely for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in aRuntimeException).- See Also:
-
RoutingContext.end()
-
endAndForget
Shortcut to the response end.Unlike the bare Vert.x variant, this method ignores the
Voidresult or any failure.- Returns:
- The current instance to chain operations if needed.
- See Also:
-
RoutingContext.end()
-
request
- Returns:
- the HTTP request object
-
response
- Returns:
- the HTTP response object
-
next
public 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.
-
fail
public 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 It will trigger the error handler matching the status code. You can define such error handler with
Router#errorHandler(int, Handler). If no error handler is not defined, It will send a default failure response with provided status code.- Parameters:
statusCode- the HTTP status code
-
fail
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
Router#errorHandler(int, Handler). If no error handler is not defined, It will send a default failure response with 500 status code.- Parameters:
throwable- a throwable representing the failure
-
fail
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
Router#errorHandler(int, Handler). If no error handler is not defined, It will send a default failure response with provided status code.- Parameters:
statusCode- the HTTP status codethrowable- a throwable representing the failure
-
put
Put some arbitrary data in the context. This will be available in any handlers that receive the context.- Parameters:
key- the key for the dataobj- the data- Returns:
- a reference to this, so the API can be used fluently
-
get
Get some data from the context. The data is available in any handlers that receive the context.- Type Parameters:
T- the type of the data- Parameters:
key- the key for the data- Returns:
- the data. Can be
null.
-
get
Get some data from the context. The data is available in any handlers that receive the context.- Type Parameters:
T- the type of the data- Parameters:
key- the key for the datadefaultValue- when the underlying data doesn't contain the key this will be the return value.- Returns:
- the data
-
remove
Remove some data from the context. The data is available in any handlers that receive the context.- Type Parameters:
T- the type of the data- Parameters:
key- the key for the data- Returns:
- the previous data associated with the key. Can be
null.
-
data
- Returns:
- all the context data as a map
-
vertx
- Returns:
- the Vert.x instance associated to the initiating
Routerfor this context
-
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.
-
currentRoute
- Returns:
- the current route this context is being routed through. Can be
null.
-
normalizedPath
Returns the normalized path of the current request.Unlike
HttpRequestHead.path(), which returns the raw path as sent by the client, this value is normalized for routing purposes. The normalization process:- resolves dot-segments such as
".."and".", - collapses multiple
'/'characters into a single slash, - ensures the path always starts with
'/', - normalizes
nullpaths to"/"
This is the value used internally by Vert.x Web when matching routes. For any security-sensitive checks or custom authorization logic, prefer using this normalized path instead of
request().path(), as the raw path may still contain traversal-like sequences (e.g."/a/../b") that resolve to the same route.- Returns:
- the normalized path
- resolves dot-segments such as
-
body
-
fileUploads
- Returns:
- a list of
FileUpload(if any) for the request. The context must have first been routed to aBodyHandlerfor this to work.
-
cancelAndCleanupFileUploads
public void cancelAndCleanupFileUploads()Cancel all unfinished file upload in progress and delete all uploaded files. -
session
Get the session. The context must have first been routed to aSessionHandlerfor this to be populated. Sessions live for a browser session, and are maintained by session cookies.- Returns:
- the session. Can be
null.
-
isSessionAccessed
public boolean isSessionAccessed()Whether thesession()has been already called or not. This is usually used by theSessionHandler.- Returns:
- true if the session has been accessed.
-
userContext
Control the user associated with this request. The user context allows accessing the security user object as well as perform authentication refreshes, logout and other operations.- Returns:
- the user context
-
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.
-
failure
If the context is being routed to failure handlers after a failure has been triggered by callingfail(Throwable)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. Can be
null.
-
statusCode
public int statusCode()If the context is being routed to failure handlers after a failure has been triggered by callingfail(int)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
-
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. Can be
null.
-
parsedHeaders
The headers:- Accept
- Accept-Charset
- Accept-Encoding
- Accept-Language
- Content-Type
ParsedHeaderValue- Returns:
- A container with the parsed headers.
-
addHeadersEndHandler
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- the handler- Returns:
- the id of the handler. This can be used if you later want to remove the handler.
-
removeHeadersEndHandler
public boolean removeHeadersEndHandler(int handlerID) Remove a headers end handler- Parameters:
handlerID- the id as returned fromio.vertx.mutiny.ext.web.RoutingContext#addHeadersEndHandler(Handler).- Returns:
- true if the handler existed and was removed, false otherwise
-
addBodyEndHandler
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- the handler- Returns:
- the id of the handler. This can be used if you later want to remove the handler.
-
removeBodyEndHandler
public boolean removeBodyEndHandler(int handlerID) Remove a body end handler- Parameters:
handlerID- the id as returned fromio.vertx.mutiny.ext.web.RoutingContext#addBodyEndHandler(Handler).- Returns:
- true if the handler existed and was removed, false otherwise
-
addEndHandler
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- the handler that will be called with either a success or failure result.- Returns:
- the id of the handler. This can be used if you later want to remove the handler.
-
removeEndHandler
public boolean removeEndHandler(int handlerID) Remove an end handler- Parameters:
handlerID- the id as returned fromio.vertx.mutiny.ext.web.RoutingContext#addEndHandler(Handler).- Returns:
- true if the handler existed and was removed, false otherwise
-
failed
public boolean failed()- Returns:
- true if the context is being routed to failure handlers.
-
setAcceptableContentType
Set the acceptable content type. Used by- Parameters:
contentType- the content type. Can benull.
-
reroute
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- the new http path.
-
reroute
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- the new http requestpath- the new http path.
-
acceptableLanguages
Returns the languages for the current request. The languages are determined from theAccept-Languageheader 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 language for the request
-
preferredLanguage
Helper to return the user preferred language. It is the same action as returning the first element of the acceptable languages.- Returns:
- the users preferred locale.
-
pathParams
Returns a map of named parameters as defined in path declaration with their actual values- Returns:
- the map of named parameters
-
pathParam
Gets the value of a single path parameter- Parameters:
name- the name of parameter as defined in path declaration- Returns:
- the actual value of the parameter or null if it doesn't exist
-
queryParams
public io.vertx.core.MultiMap queryParams()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:
- the multimap of query parameters
-
queryParams
Always decode the current query string with the givenencoding. The decode result is never cached. Callers to this method are expected to cache the result if needed. Usually users should usequeryParams(). This method is only useful when the requests without content type (GETrequests as an example) expect that query params are in the ASCII formatISO-5559-1.- Parameters:
encoding- a non null character set.- Returns:
- the multimap of query parameters
-
queryParam
Gets the value of a single query parameter. For more infoqueryParams()- Parameters:
name- The name of query parameter- Returns:
- The list of all parameters matching the parameter name. It returns an empty list if no query parameter with
namewas found
-
attachment
Set Content-Disposition get to "attachment" with optionalfilenamemime type.- Parameters:
filename- the filename for the attachment
-
is
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- content type- Returns:
- The most close value
-
isFresh
public boolean isFresh()Check if the request is fresh, aka Last-Modified and/or the ETag still match.- Returns:
- true if content is fresh according to the cache.
-
etag
Set the ETag of a response. This will normalize the quotes if necessary. etag('md5hashsum'); etag('"md5hashsum"'); ('W/"123456789"');- Parameters:
etag- the etag value
-
lastModified
Set the Last-Modified date using a Instant.- Parameters:
instant- the last modified instant
-
lastModified
Set the Last-Modified date using a String.- Parameters:
instant- the last modified instant
-
newInstance
Creates a new instance of theRoutingContext. -
hashCode
public int hashCode() -
equals
-
toString
-