public class RoutingContext extends Object
A new instance is created for each HTTP request that is received in the
Handler
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
NOTE: This class has been automatically generated from theoriginal
non RX-ified interface using Vert.x codegen.Modifier and Type | Field and Description |
---|---|
static io.vertx.lang.rx.TypeArg<RoutingContext> |
__TYPE_ARG |
Constructor and Description |
---|
RoutingContext(Object delegate) |
RoutingContext(RoutingContext delegate) |
Modifier and Type | Method and Description |
---|---|
List<LanguageHeader> |
acceptableLanguages()
Returns the languages for the current request.
|
List<Locale> |
acceptableLocales()
Deprecated.
|
int |
addBodyEndHandler(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 |
addEndHandler(Handler<AsyncResult<Void>> handler)
Add an end handler for the request/response context.
|
int |
addHeadersEndHandler(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()
Deprecated.
|
Route |
currentRoute() |
Map<String,Object> |
data() |
boolean |
equals(Object o) |
void |
fail(int statusCode)
Fail the context with the specified status code.
|
void |
fail(int statusCode,
Throwable throwable)
Fail the context with the specified throwable and the specified the status code.
|
void |
fail(Throwable throwable)
Fail the context with the specified throwable and 500 status code.
|
boolean |
failed() |
Throwable |
failure()
If the context is being routed to failure handlers after a failure has been triggered by calling
fail(int) 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.
|
Buffer |
getBody() |
JsonObject |
getBodyAsJson() |
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.
|
RoutingContext |
getDelegate() |
int |
hashCode() |
boolean |
isSessionAccessed()
Whether the
session() has been already called or not. |
String |
mountPoint() |
static RoutingContext |
newInstance(RoutingContext arg) |
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
|
LanguageHeader |
preferredLanguage()
Helper to return the user preferred language.
|
Locale |
preferredLocale()
Deprecated.
|
RoutingContext |
put(String key,
Object obj)
Put some arbitrary data in the context.
|
List<String> |
queryParam(String name)
Gets the value of a single query parameter.
|
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. |
<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.
|
Cookie |
removeCookie(String name,
boolean invalidate)
Remove a cookie from the cookie set.
|
boolean |
removeEndHandler(int handlerID)
Remove an end handler
|
boolean |
removeHeadersEndHandler(int handlerID)
Remove a headers end handler
|
HttpServerRequest |
request() |
void |
reroute(HttpMethod method,
String path)
Restarts the current router with a new method and path.
|
void |
reroute(String path)
Restarts the current router with a new path and reusing the original method.
|
HttpServerResponse |
response() |
Session |
session()
Get the session.
|
void |
setAcceptableContentType(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
fail(int) then this will return that status code. |
String |
toString() |
User |
user()
Get the authenticated user (if any).
|
Vertx |
vertx() |
public static final io.vertx.lang.rx.TypeArg<RoutingContext> __TYPE_ARG
public RoutingContext(RoutingContext delegate)
public RoutingContext(Object delegate)
public RoutingContext getDelegate()
public HttpServerRequest request()
public HttpServerResponse response()
public 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.
public 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 It will trigger the error handler matching the status code. You can define such error handler with
Router.errorHandler(int, io.vertx.core.Handler<io.vertx.reactivex.ext.web.RoutingContext>)
. If no error handler is not defined, It will send a default failure response with provided status code.
statusCode
- the HTTP status codepublic void 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 It will trigger the error handler matching the status code. You can define such error handler with
Router.errorHandler(int, io.vertx.core.Handler<io.vertx.reactivex.ext.web.RoutingContext>)
. If no error handler is not defined, It will send a default failure response with 500 status code.
throwable
- a throwable representing the failurepublic void fail(int statusCode, Throwable throwable)
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, io.vertx.core.Handler<io.vertx.reactivex.ext.web.RoutingContext>)
. If no error handler is not defined, It will send a default failure response with provided status code.
statusCode
- the HTTP status codethrowable
- a throwable representing the failurepublic RoutingContext put(String key, Object obj)
key
- the key for the dataobj
- the datapublic <T> T get(String key)
key
- the key for the datapublic <T> T remove(String key)
key
- the key for the datapublic Vertx vertx()
Router
for this contextpublic String mountPoint()
public Route currentRoute()
public 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 if accessing server resources requested by a client.
public Cookie getCookie(String name)
name
- the cookie namepublic RoutingContext addCookie(Cookie cookie)
cookie
- the cookiepublic Cookie removeCookie(String name)
name
- the name of the cookiepublic Cookie removeCookie(String name, boolean invalidate)
name
- the name of the cookieinvalidate
- public int cookieCount()
@Deprecated public Set<Cookie> cookies()
public String getBodyAsString()
BodyHandler
for this to be populated.public String getBodyAsString(String encoding)
BodyHandler
for this to be populated.encoding
- the encoding, e.g. "UTF-16"public JsonObject getBodyAsJson()
BodyHandler
for this to be populated. null
or the "null"
JSON literal then null
is returned.public JsonArray getBodyAsJsonArray()
BodyHandler
for this to be populated. null
or the "null"
JSON literal then null
is returned.public Buffer getBody()
BodyHandler
for this to be populated.public Set<FileUpload> fileUploads()
BodyHandler
for this to work.public Session session()
SessionHandler
for this to be populated.
Sessions live for a browser session, and are maintained by session cookies.public boolean isSessionAccessed()
session()
has been already called or not. This is usually used by the
SessionHandler
.public User user()
public Throwable failure()
fail(int)
then this will return that throwable. It can be used by failure handlers to render a response,
e.g. create a failure response page.public 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.public String getAcceptableContentType()
public ParsedHeaderValues parsedHeaders()
ParsedHeaderValue
public int addHeadersEndHandler(Handler<Void> handler)
handler
- the handlerpublic boolean removeHeadersEndHandler(int handlerID)
handlerID
- the id as returned from addHeadersEndHandler(io.vertx.core.Handler<java.lang.Void>)
.public int addBodyEndHandler(Handler<Void> handler)
handler
- the handlerpublic boolean removeBodyEndHandler(int handlerID)
handlerID
- the id as returned from addBodyEndHandler(io.vertx.core.Handler<java.lang.Void>)
.public int addEndHandler(Handler<AsyncResult<Void>> handler)
handler
- the handler that will be called with either a success or failure result.public boolean removeEndHandler(int handlerID)
handlerID
- the id as returned from addEndHandler(io.vertx.core.Handler<io.vertx.core.AsyncResult<java.lang.Void>>)
.public boolean failed()
public void setBody(Buffer body)
BodyHandler
. You will not normally call this method.body
- the bodypublic void setSession(Session session)
SessionHandler
. You will not normally call this method.session
- the sessionpublic void setUser(User user)
user
- the userpublic void clearUser()
public void setAcceptableContentType(String contentType)
contentType
- the content typepublic void reroute(String path)
path
- the new http path.public void reroute(HttpMethod method, String path)
method
- the new http requestpath
- the new http path.@Deprecated public List<Locale> acceptableLocales()
public 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 public Locale preferredLocale()
public LanguageHeader preferredLanguage()
public Map<String,String> pathParams()
public String pathParam(String name)
name
- the name of parameter as defined in path declarationpublic MultiMap queryParams()
public List<String> queryParam(String name)
queryParams()
name
- The name of query parametername
was foundpublic static RoutingContext newInstance(RoutingContext arg)
Copyright © 2023 Eclipse. All rights reserved.