Rapidoid - Extremely Fast, Simple and Powerful Java Web Framework!

API Documentation

The HTTP Request and Response API (in interface Req):

HTTP REQUEST DATA
String verb()
Gets the verb of the HTTP request.
String uri()
Gets the uri of the HTTP request.
String path()
Gets the path of the HTTP request.
String query()
Gets the query of the HTTP request.
byte[] body()
Gets the raw body data of the HTTP request.
String host()
Gets the value of the Host header of the HTTP request.
String zone()
Gets the name of the application zone handling the request.
The default zone name is main for the On API, and admin for the Admin API.
String contextPath()
Gets the context path of the application zone handling the request.
The default context path is / for the On API, and /_ for the Admin API.
String clientIpAddress()
Gets the IP address of the HTTP client directly sending the request.
This can be the address of a real user, or a HTTP proxy (if the user uses such), or a reverse proxy (if the application/server uses such).
String realIpAddress()
A best-effort attempt to infer the real IP address of the end user/client/client proxy.
If a reverse proxy is detected with high confidence (or configured), its headers will be used to get the real IP address of the user.
Otherwise, the value of Req#clientIpAddress() is returned.
long connectionId()
Gets the HTTP connection ID, which is unique per HTTP server instance.
long requestId()
Gets the HTTP request ID, which is unique per HTTP server instance.
URL PARAMETERS:
Map<String, String> params()
Gets the URL parameters of the HTTP request.
String param(String name)
Returns the value of the specified mandatory URL parameter from the HTTP request, or throws a runtime exception if it is not found.
String param(String name, String defaultValue)
Returns the value of the specified optional URL parameter from the HTTP request, or the specified default value, if not found.
T param(Class<T> beanType)
Returns a new instance of the specified bean type, with properties initialized from the URL parameters of the HTTP request.
POSTED PARAMETERS IN THE REQUEST BODY:
Map<String, Object> posted()
Gets the posted parameters of the HTTP request body.
T posted(String name)
Returns the value of the specified posted parameter from the HTTP request body, or throws a runtime exception if it is not found.
T posted(String name, T defaultValue)
Returns the value of the specified posted parameter from the HTTP request body, or the specified default value, if it is not found.
T posted(Class<T> beanType)
Returns a new instance of the specified bean type, with properties initialized from the posted parameters of the HTTP request.
UPLOADED FILES IN THE REQUEST BODY:
Map<String, List<Upload>> files()
Gets the uploaded files from the HTTP request body.
List<Upload> files(String name)
Returns the uploaded files with the specified form parameter name (not filename) from the HTTP request body, or throws a runtime exception if not found.
Upload file(String name)
Returns exactly one posted file with the specified form parameter name (not filename) from the HTTP request body, or throws a runtime exception if not found.
REQUEST DATA PARAMETERS (URL PARAMETERS + POSTED PARAMETERS + UPLOADED FILES):
Map<String, Object> data()
Gets the data parameters (URL parameters + posted parameters + uploaded files) of the HTTP request.
T data(String name)
Returns the value of the specified data parameter from the HTTP request, or throws a runtime exception if it is not found.
T data(String name, T defaultValue)
Returns the value of the specified data parameter from the HTTP request, or the specified default value, if it is not found.
T data(Class<T> beanType)
Returns a new instance of the specified bean type, with properties initialized from the data parameters of the HTTP request.
EXTRA ATTRIBUTES ATTACHED TO THE REQUEST:
Map<String, Object> attrs()
Gets the extra attributes of the HTTP request.
T attr(String name)
Returns the value of an extra attribute from the HTTP request, or throws a runtime exception if it is not found.
T attr(String name, T defaultValue)
Returns the value of the specified extra attribute from the HTTP request, or the specified default value, if it is not found.
SERVER-SIDE SESSION:
String sessionId()
Returns the ID of the session (the value of the "JSESSIONID" cookie). If a session doesn't exist, a new session is created.
boolean hasSession()
Does the HTTP request have a server-side session attached?
Map<String, Serializable> session()
Provides read/write access to the server-side session attributes of the HTTP request/response.
T session(String name)
Returns the value of the specified server-side session attribute from the HTTP request/response, or throws a runtime exception if it is not found.
T session(String name, T defaultValue)
Returns the value of the specified server-side session attribute from the HTTP request/response, or the specified default value, if it is not found.
TOKEN DATA:
boolean hasToken()
Does the HTTP request have a token attached?
Map<String, Serializable> token()
Provides read/write access to the token attributes of the HTTP request/response.
T token(String name)
Returns the value of the specified token attribute from the HTTP request/response, or throws a runtime exception if it is not found.
T token(String name, T defaultValue)
Returns the value of the specified token attribute from the HTTP request/response, or the specified default value, if it is not found.
REQUEST HEADERS:
Map<String, String> headers()
Gets the headers of the HTTP request.
String header(String name)
Returns the value of the specified header from the HTTP request, or throws a runtime exception if it is not found.
String header(String name, String defaultValue)
Returns the value of the specified header from the HTTP request, or the specified default value, if it is not found.
REQUEST COOKIES:
Map<String, String> cookies()
Gets the cookies of the HTTP request.
String cookie(String name)
Returns the value of the specified cookie from the HTTP request, or throws a runtime exception if it is not found.
String cookie(String name, String defaultValue)
Returns the value of the specified cookie from the HTTP request, or the specified default value, if it is not found.
RESPONSE:
Resp response()
Gets the reference to the response object.
ASYNCHRONOUS REQUEST HANDLING:
Req async()
Informs the HTTP server that the request will be handled asynchronously (typically on another thread). When the response is complete, the Req#done() or Resp#done() method must be called, to inform the server.
boolean isAsync()
Is/was the request being handled in asynchronous mode?
Req done()
Informs the HTTP server that the asynchronous handling has finished and the response is complete.
boolean isDone()
Has the request handling and response construction finished?
WEB APPLICATION SETUP:
HttpRoutes routes()
Provides access to the HTTP routes of the web application setup.
Route route()
Provides access to the matching HTTP route (if any) of the web application setup.
In case a generic handler handles the request, or no matching route was found, null is returned.
Customization custom()
Provides access to the customization of the web application setup.
void revert()
Reverts the previous processing of the request, usually with intention to process the same request again.
OutputStream out()
First renders the response headers, then returns an OutputStream representing the response body. The response body will be constructed by writing to the OutputStream.
MediaType contentType()
Gets the Content-Type header of the HTTP response if it has been assigned, or the default value as configured in the HTTP route.
long handle()
Returns the request handle, which is used when resuming the request handling in asynchronous way.
See Resp#resume.

The HTTP Request and Response API (in interface Resp):

Resp result(Object content)
Sets the content to be serialized into a body when the HTTP response is rendered.
Object result()
Gets the content to be serialized into a body when the HTTP response is rendered.
Resp body(byte[] body)
Sets the HTTP response body from a byte[] data that is written as a HTTP response body when rendered.
Resp body(ByteBuffer body)
Sets the HTTP response body from a ByteBuffer data that is written as a HTTP response body when rendered.
Object body()
Gets the HTTP response body data (of type byte[] or ByteBuffer) that is written as a HTTP response body when rendered.
Resp raw(byte[] raw)
Sets the raw HTTP response (headers and body) from a byte[] data that is written as a HTTP response when rendered.
Resp raw(ByteBuffer raw)
Sets the raw HTTP response (headers and body) from a ByteBuffer data that is written as a HTTP response when rendered.
Object raw()
Gets the raw HTTP response (headers and body) data (of type byte[] or ByteBuffer) that is written as a HTTP response when rendered.
Resp code(int code)
Sets the status code (e.g. 200, 404, 500) of the HTTP response.
int code()
Gets the status code (e.g. 200, 404, 500) of the HTTP response.
Resp contentType(MediaType contentType)
Sets the Content-Type header to be rendered in the HTTP response.
MediaType contentType()
Gets the Content-Type header to be rendered in the HTTP response.
Resp redirect(String redirectURI)
Sets the redirect URI of the HTTP response.
Setting this will cause a HTTP 30x redirect response.
String redirect()
Gets the redirect URI of the HTTP response.
Resp filename(String filename)
Sets the filename when serving a file in the HTTP response.
String filename()
Gets the filename when serving a file in the HTTP response.
Resp view(String viewName)
Sets a custom name of the view (V from MVC) of the HTTP response.
This also sets mvc to true.
The default view name equals the request path without the "/" prefix, except for the "/" path, where the view name is "index".
E.g. "/abc" -> "abc", "/" -> "index", "/my/books" -> "my/books".
String view()
Gets the (default or customized) name of the view (V from MVC) of the HTTP response.
The default view name equals the request path without the "/" prefix, except for the "/" path, where the view name is "index".
E.g. "/abc" -> "abc", "/" -> "index", "/my/books" -> "my/books".
Resp noView()
Disables the view rendering for the target MVC route. The page decorator remains enabled.
Resp file(File file)
Sets the file to be served when the HTTP response is rendered.
File file()
Gets the file to be served when the HTTP response is rendered.
Map<String, String> headers()
Provides read/write access to the headers of the HTTP response.
Resp header(String name, String value)
Sets a header of the HTTP response.
Map<String, String> cookies()
Provides read/write access to the cookies of the HTTP response.
Resp cookie(String name, String value, String... extras)
Sets a cookie of the HTTP response.
Map<String, Serializable> session()
Provides read/write access to the server-side session attributes of the HTTP request/response.
Resp session(String name, Serializable value)
Sets a session attribute of the HTTP response.
Map<String, Serializable> token()
Provides read/write access to the token attributes of the HTTP request/response.
Resp token(String name, Serializable value)
Sets a token attribute of the HTTP response.
Map<String, Object> model()
Provides read/write access to the model (M from MVC) that will be rendered by the view renderer.
Resp model(String name, Object value)
Sets an attribute of the model (M from MVC) that will be rendered by the view renderer.
Resp done()
Informs the HTTP server that the asynchronous handling has finished and the response is complete.
Alias to request().done().
Resp plain(Object content)
Sets the Content-Type: text/plain; charset=utf-8 header and the content of the HTTP response.
Alias to contentType(MediaType.PLAIN_TEXT_UTF_8).body(content).
Resp html(Object content)
Sets the Content-Type: text/html; charset=utf-8 header and the content of the HTTP response.
Alias to contentType(MediaType.HTML_UTF_8).body(content).
Resp json(Object content)
Sets the Content-Type: application/json header and the content of the HTTP response.
Alias to contentType(MediaType.JSON).body(content).
Resp binary(Object content)
Sets the Content-Type: application/octet-stream header and the content of the HTTP response.
Alias to contentType(MediaType.BINARY).body(content).
boolean mvc()
Checks whether the response model and view will be rendered in a MVC fashion.
A typical renderer would use Resp#view to get the view name, and Resp#model to get the model. A custom view renderer can be configured/implemented via the On.custom().viewResolver(...) method.
Resp mvc(boolean mvc)
Sets whether the response model and view will be rendered in a MVC fashion.
A typical renderer would use Resp#view to get the view name, and Resp#model to get the model. A custom view renderer can be configured/implemented via the On.custom().viewResolver(...) method.
OutputStream out()
First renders the response headers, then returns an OutputStream representing the response body. The response body will be constructed by writing to the OutputStream.
Req request()
Gets the reference to the request object.
boolean login(String username, String password)
Initiates a user login process with the specified username and password.
After a successful login, the username will be persisted in the token.
Returns information whether the login was successful
void logout()
Initiates a user logout process, clearing the login information (username) from the token.
Screen screen()
Provides access to the screen model for custom (MVC) page rendering.
void resume(AsyncLogic asyncLogic)
Resumes the asynchronous request handling.

Contents: