public interface RoutingContext
A new instance is created for each HTTP request that is received in the
Router.accept(HttpServerRequest)
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.
Modifier and Type | Method and Description |
---|---|
default List<LanguageHeader> |
acceptableLanguages()
Returns the languages for the current request.
|
List<Locale> |
acceptableLocales()
Deprecated.
|
int |
addBodyEndHandler(io.vertx.core.Handler<Void> handler)
Provides a handler that will be called after the last part of the body is written to the wire.
|
RoutingContext |
addCookie(Cookie cookie)
Add a cookie.
|
int |
addHeadersEndHandler(io.vertx.core.Handler<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() |
Set<Cookie> |
cookies() |
Route |
currentRoute() |
Map<String,Object> |
data() |
void |
fail(int statusCode)
Fail the context with the specified status code.
|
void |
fail(Throwable throwable)
Fail the context with the specified throwable.
|
boolean |
failed() |
Throwable |
failure()
If the context is being routed to failure handlers after a failure has been triggered by calling
fail(Throwable) then this will return that throwable. |
Set<FileUpload> |
fileUploads() |
<T> T |
get(String key)
Get some data from the context.
|
String |
getAcceptableContentType()
If the route specifies produces matches, e.g.
|
io.vertx.core.buffer.Buffer |
getBody() |
io.vertx.core.json.JsonObject |
getBodyAsJson() |
io.vertx.core.json.JsonArray |
getBodyAsJsonArray() |
String |
getBodyAsString() |
String |
getBodyAsString(String encoding)
Get the entire HTTP request body as a string, assuming the specified encoding.
|
Cookie |
getCookie(String name)
Get the cookie with the specified name.
|
String |
mountPoint() |
void |
next()
Tell the router to route this context to the next matching route (if any).
|
String |
normalisedPath()
Return the normalised path for the request.
|
ParsedHeaderValues |
parsedHeaders()
The headers:
Accept
Accept-Charset
Accept-Encoding
Accept-Language
Content-Type
Parsed into
ParsedHeaderValue |
String |
pathParam(String name)
Gets the value of a single path parameter
|
Map<String,String> |
pathParams()
Returns a map of named parameters as defined in path declaration with their actual values
|
default LanguageHeader |
preferredLanguage()
Helper to return the user preferred language.
|
default Locale |
preferredLocale()
Deprecated.
Use
preferredLanguage() instead |
RoutingContext |
put(String key,
Object obj)
Put some arbitrary data in the context.
|
List<String> |
queryParam(String query)
Gets the value of a single query parameter
|
io.vertx.core.MultiMap |
queryParams()
Returns a map of all query parameters inside the query string
|
<T> T |
remove(String key)
Remove some data from the context.
|
boolean |
removeBodyEndHandler(int handlerID)
Remove a body end handler
|
Cookie |
removeCookie(String name)
Expire a cookie, notifying a User Agent to remove it from its cookie jar.
|
boolean |
removeHeadersEndHandler(int handlerID)
Remove a headers end handler
|
io.vertx.core.http.HttpServerRequest |
request() |
void |
reroute(io.vertx.core.http.HttpMethod method,
String path)
Restarts the current router with a new method and path.
|
default void |
reroute(String path)
Restarts the current router with a new path and reusing the original method.
|
io.vertx.core.http.HttpServerResponse |
response() |
Session |
session()
Get the session.
|
void |
setAcceptableContentType(String contentType)
Set the acceptable content type.
|
void |
setBody(io.vertx.core.buffer.Buffer body)
Set the body.
|
void |
setSession(Session session)
Set the session.
|
void |
setUser(io.vertx.ext.auth.User user)
Set the user.
|
int |
statusCode()
If the context is being routed to failure handlers after a failure has been triggered by calling
fail(int) then this will return that status code. |
io.vertx.ext.auth.User |
user()
Get the authenticated user (if any).
|
io.vertx.core.Vertx |
vertx() |
io.vertx.core.http.HttpServerRequest request()
io.vertx.core.http.HttpServerResponse response()
void next()
If next is not called for a handler then the handler should make sure it ends the response or no response will be sent.
void fail(int statusCode)
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.
statusCode
- the HTTP status codevoid fail(Throwable 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.
throwable
- a throwable representing the failureRoutingContext put(String key, Object obj)
key
- the key for the dataobj
- the data<T> T get(String key)
T
- the type of the datakey
- the key for the dataClassCastException
- if the data is not of the expected type<T> T remove(String key)
T
- the type of the datakey
- the key for the dataClassCastException
- if the data is not of the expected typeio.vertx.core.Vertx vertx()
Router
for this contextString mountPoint()
Route currentRoute()
String normalisedPath()
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 HttpServerRequest.path()
if accessing server resources requested by a client.
Cookie getCookie(String name)
CookieHandler
for this to work.name
- the cookie nameRoutingContext addCookie(Cookie cookie)
CookieHandler
for this to work.cookie
- the cookieCookie removeCookie(String name)
CookieHandler
for this to work.name
- the name of the cookieint cookieCount()
CookieHandler
for this to work.Set<Cookie> cookies()
CookieHandler
for this to be populated.String getBodyAsString()
BodyHandler
for this to be populated.String getBodyAsString(String encoding)
BodyHandler
for this to be populated.encoding
- the encoding, e.g. "UTF-16"io.vertx.core.json.JsonObject getBodyAsJson()
JsonObject
. The context must have first been routed to a
BodyHandler
for this to be populated.io.vertx.core.json.JsonArray getBodyAsJsonArray()
JsonArray
. The context must have first been routed to a
BodyHandler
for this to be populated.io.vertx.core.buffer.Buffer getBody()
Buffer
. The context must have first been routed to a
BodyHandler
for this to be populated.Set<FileUpload> fileUploads()
BodyHandler
for this to work.Session session()
SessionHandler
for this to be populated.
Sessions live for a browser session, and are maintained by session cookies.io.vertx.ext.auth.User user()
Throwable failure()
fail(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.int statusCode()
fail(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.String getAcceptableContentType()
ParsedHeaderValues parsedHeaders()
ParsedHeaderValue
int addHeadersEndHandler(io.vertx.core.Handler<Void> handler)
handler
- the handlerboolean removeHeadersEndHandler(int handlerID)
handlerID
- the id as returned from addHeadersEndHandler(Handler)
.int addBodyEndHandler(io.vertx.core.Handler<Void> handler)
handler
- the handlerboolean removeBodyEndHandler(int handlerID)
handlerID
- the id as returned from addBodyEndHandler(Handler)
.boolean failed()
void setBody(io.vertx.core.buffer.Buffer body)
BodyHandler
. You will not normally call this method.body
- the bodyvoid setSession(Session session)
SessionHandler
. You will not normally call this method.session
- the sessionvoid setUser(io.vertx.ext.auth.User user)
user
- the uservoid clearUser()
void setAcceptableContentType(String contentType)
contentType
- the content typedefault void reroute(String path)
path
- the new http path.void reroute(io.vertx.core.http.HttpMethod method, String path)
method
- the new http requestpath
- the new http path.@Deprecated List<Locale> acceptableLocales()
acceptableLanguages()
or parsedHeaders()
.ParsedHeaderValues.acceptLanguage()
default List<LanguageHeader> acceptableLanguages()
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.@Deprecated default Locale preferredLocale()
preferredLanguage()
insteaddefault LanguageHeader preferredLanguage()
Map<String,String> pathParams()
String pathParam(String name)
name
- the name of parameter as defined in path declarationio.vertx.core.MultiMap queryParams()
Copyright © 2018 Eclipse. All rights reserved.