public class RoutingContext extends Object
A new instance is created for each HTTP request that is received in the
Router.accept(io.vertx.rxjava.core.http.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.
original
non RX-ified interface using Vert.x codegen.Constructor and Description |
---|
RoutingContext(RoutingContext delegate) |
Modifier and Type | Method and Description |
---|---|
int |
addBodyEndHandler(Handler<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(Handler<Future> 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() |
void |
fail(int statusCode)
Fail the context with the specified status code.
|
boolean |
failed() |
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() |
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.
|
Object |
getDelegate() |
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.
|
RoutingContext |
put(String key,
Object obj)
Put some arbitrary data in the context.
|
boolean |
removeBodyEndHandler(int handlerID)
Remove a body end handler
|
Cookie |
removeCookie(String name)
Remove a cookie.
|
boolean |
removeHeadersEndHandler(int handlerID)
Remove a headers end handler
|
HttpServerRequest |
request() |
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. |
User |
user()
Get the authenticated user (if any).
|
Vertx |
vertx() |
public RoutingContext(RoutingContext delegate)
public Object 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 a default failure response will be sent.
statusCode
- the HTTP status codepublic 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 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 HttpServerRequest.path()
if accessing server resources requested by a client.
public Cookie getCookie(String name)
CookieHandler
for this to work.name
- the cookie namepublic RoutingContext addCookie(Cookie cookie)
CookieHandler
for this to work.cookie
- the cookiepublic Cookie removeCookie(String name)
CookieHandler
for this to work.name
- the name of the cookiepublic int cookieCount()
CookieHandler
for this to work.public Set<Cookie> cookies()
CookieHandler
for this to be populated.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()
JsonObject
. The context must have first been routed to a
BodyHandler
for this to be populated.public Buffer getBody()
Buffer
. The context must have first been routed to a
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 User user()
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.public String getAcceptableContentType()
public int addHeadersEndHandler(Handler<Future> handler)
handler
- the handlerpublic boolean removeHeadersEndHandler(int handlerID)
handlerID
- the id as returned from RoutingContext
.public int addBodyEndHandler(Handler<Void> handler)
handler
- the handlerpublic boolean removeBodyEndHandler(int handlerID)
handlerID
- the id as returned from RoutingContext
.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 static RoutingContext newInstance(RoutingContext arg)
Copyright © 2015. All Rights Reserved.