public interface Request extends Registry
Access to HTTP parameter/header is available via param(String)
and header(String)
methods. See some examples:
// str param String value = request.param("str").value(); // optional str String value = request.param("str").value("defs"); // int param int value = request.param("some").intValue(); // optional int param Optional <Integer > value = request.param("some").toOptional(Integer.class); // list param List <String > values = request.param("some").toList(String.class);
Due that form post are treated as HTTP params you can collect all them into a Java Object via params(Class)
or form(Class)
methods:
{
get("/search", req -> {
Query q = req.params(Query.class);
});
post("/person", req -> {
Person person = req.form(Person.class);
});
}
Form post file upload are available via files(String)
or file(String)
methods:
{
post("/upload", req -> {
try(Upload upload = req.file("myfile")) {
File file = upload.file();
// work with file.
}
});
}
Modifier and Type | Interface and Description |
---|---|
static interface |
Request.Flash
Flash scope.
|
static class |
Request.Forwarding
Forwarding request.
|
Modifier and Type | Method and Description |
---|---|
List<MediaType> |
accept() |
Optional<MediaType> |
accepts(List<MediaType> types)
Check if the given types are acceptable, returning the best match when true, or else Optional.empty.
|
default Optional<MediaType> |
accepts(MediaType... types)
Check if the given types are acceptable, returning the best match when true, or else Optional.empty.
|
default Optional<MediaType> |
accepts(String... types)
Check if the given types are acceptable, returning the best match when true, or else Optional.empty.
|
Map<String,Object> |
attributes()
A read only version of the current locals.
|
Mutant |
body()
HTTP body.
|
default <T> T |
body(Class<T> type)
Short version of body().to(type) .
|
Charset |
charset()
The charset defined in the request body.
|
String |
contextPath()
Application path (a.k.a context path).
|
Mutant |
cookie(String name)
Get a cookie with the given name (if present).
|
List<Cookie> |
cookies() |
default Upload |
file(String name)
Get a file Upload with the given name.
|
List<Upload> |
files()
Get a list of files Upload that were uploaded in the request.
|
List<Upload> |
files(String name)
Get a list of file Upload with the given name.
|
default Request.Flash |
flash()
Give you access to flash scope.
|
default String |
flash(String name)
Get a flash attribute value or throws BAD_REQUEST error if missing.
|
default Request |
flash(String name, Object value)
Set a flash attribute.
|
default <T> T |
form(Class<T> type)
Short version of params().to(type) .
|
default <T> T |
form(Class<T> type, String... xss)
Short version of params(xss).to(type) .
|
default <T> T |
get(String name)
Get a request local attribute.
|
default <T> T |
get(String name, T def)
Get a request local attribute.
|
Mutant |
header(String name)
Get a HTTP header.
|
Mutant |
header(String name, String... xss)
Get a HTTP header and apply the XSS escapers.
|
Map<String,Mutant> |
headers() |
String |
hostname()
The fully qualified name of the resource being requested, as obtained from the Host HTTP header.
|
default Optional<Upload> |
ifFile(String name)
Get a file Upload with the given name or empty.
|
default Optional<String> |
ifFlash(String name)
Get an optional for the given flash attribute's name.
|
<T> Optional<T> |
ifGet(String name)
Get a request local attribute.
|
Optional<Session> |
ifSession() |
String |
ip() |
default boolean |
is(List<MediaType> types)
True, if request accept any of the given types.
|
default boolean |
is(MediaType... types)
True, if request accept any of the given types.
|
default boolean |
is(String... types)
True, if request accept any of the given types.
|
default boolean |
isSet(String name) |
long |
length() |
default Locale |
locale()
Get a locale that best matches the current request or the default locale as specified in application.lang .
|
Locale |
locale(BiFunction<List<Locale.LanguageRange>,List<Locale>,Locale> filter)
Get a locale that best matches the current request.
|
default List<Locale> |
locales()
Get a list of locale that best matches the current request as per Locale.filter(java.util.List<java.util.Locale.LanguageRange>, java.util.Collection<java.util.Locale>, java.util.Locale.FilteringMode) .
|
List<Locale> |
locales(BiFunction<List<Locale.LanguageRange>,List<Locale>,List<Locale>> filter)
Get a list of locale that best matches the current request.
|
boolean |
matches(String pattern)
Test if the given request path matches the pattern.
|
default String |
method() |
Mutant |
param(String name)
Get a HTTP request parameter under the given name.
|
Mutant |
param(String name, String... xss)
Get a HTTP request parameter under the given name.
|
Mutant |
params()
Get all the available parameters.
|
default <T> T |
params(Class<T> type)
Short version of params().to(type) .
|
default <T> T |
params(Class<T> type, String... xss)
Short version of params(xss).to(type) .
|
Mutant |
params(String... xss)
Get all the available parameters.
|
default String |
path()
Given:
|
default String |
path(boolean escape)
Escape the path using UrlEscapers.urlFragmentEscaper() .
|
int |
port() |
String |
protocol() |
default Request |
push(String path)
Send a push promise frame to the client and push the resource identified by the given path.
|
Request |
push(String path, Map<String,Object> headers)
Send a push promise frame to the client and push the resource identified by the given path.
|
Optional<String> |
queryString()
The query string, without the leading ? .
|
String |
rawPath()
Raw path, like path() but without decoding.
|
Route |
route() |
boolean |
secure() |
Session |
session() |
default Request |
set(Class<?> type, Object value)
Seed a RequestScoped object.
|
Request |
set(com.google.inject.Key<?> key, Object value)
Seed a RequestScoped object.
|
Request |
set(String name, Object value)
Set local attribute.
|
default Request |
set(com.google.inject.TypeLiteral<?> type, Object value)
Seed a RequestScoped object.
|
long |
timestamp()
Request timestamp.
|
MediaType |
type() |
<T> Optional<T> |
unset(String name)
Remove a request local attribute.
|
default boolean |
xhr() |
@Nonnull default String path()
http://domain.com/some/path.html -> /some/path.html http://domain.com/a.html -> /a.html
@Nonnull String rawPath()
path()
but without decoding.
path()
but without decoding.
@Nonnull Optional<String> queryString()
?
.
?
.
@Nonnull default String path(boolean escape)
UrlEscapers.urlFragmentEscaper()
. Given:
http://domain.com/404<h1>X</h1> {@literal ->} /404%3Ch1%3EX%3C/h1%3E
escape
- True if we want to escape this path.
@Nonnull String contextPath()
application.path
. Default is: /
This method returns empty string for /
. Otherwise, it is identical the value of application.path
.
@Nonnull default Optional<MediaType> accepts(String... types)
// Accept: text/html req.accepts("text/html"); // => "text/html" // Accept: text/*, application/json req.accepts("text/html"); // => "text/html" req.accepts("text/html"); // => "text/html" req.accepts("application/json" "text/plain"); // => "application/json" req.accepts("application/json"); // => "application/json" // Accept: text/*, application/json req.accepts("image/png"); // => Optional.empty // Accept: text/*;q=.5, application/json req.accepts("text/html", "application/json"); // => "application/json"
types
- Types to test.
boolean matches(String pattern)
pattern
- A pattern to test for.
default boolean is(String... types)
types
- Types to test
default boolean is(MediaType... types)
types
- Types to test
default boolean is(List<MediaType> types)
types
- Types to test
@Nonnull default Optional<MediaType> accepts(MediaType... types)
// Accept: text/html req.accepts("text/html"); // => "text/html" // Accept: text/*, application/json req.accepts("text/html"); // => "text/html" req.accepts("text/html"); // => "text/html" req.accepts("application/json" "text/plain"); // => "application/json" req.accepts("application/json"); // => "application/json" // Accept: text/*, application/json req.accepts("image/png"); // => Optional.empty // Accept: text/*;q=.5, application/json req.accepts("text/html", "application/json"); // => "application/json"
types
- Types to test.
@Nonnull Optional<MediaType> accepts(List<MediaType> types)
// Accept: text/html req.accepts("text/html"); // => "text/html" // Accept: text/*, application/json req.accepts("text/html"); // => "text/html" req.accepts("text/html"); // => "text/html" req.accepts("application/json" "text/plain"); // => "application/json" req.accepts("application/json"); // => "application/json" // Accept: text/*, application/json req.accepts("image/png"); // => Optional.empty // Accept: text/*;q=.5, application/json req.accepts("text/html", "application/json"); // => "application/json"
types
- Types to test for.
@Nonnull Mutant params()
/path/:name
or /path/{name}
?name=jooby
Content-Type
is application/x-www-form-urlencoded
or multipart/form-data
@Nonnull Mutant params(String... xss)
/path/:name
or /path/{name}
?name=jooby
Content-Type
is application/x-www-form-urlencoded
or multipart/form-data
xss
- Xss filter to apply.
@Nonnull default <T> T params(Class<T> type)
params().to(type)
.
T
- Value type.
type
- Object type.
@Nonnull default <T> T form(Class<T> type)
params().to(type)
.
T
- Value type.
type
- Object type.
@Nonnull default <T> T params(Class<T> type, String... xss)
params(xss).to(type)
.
T
- Value type.
type
- Object type.
xss
- Xss filter to apply.
@Nonnull default <T> T form(Class<T> type, String... xss)
params(xss).to(type)
.
T
- Value type.
type
- Object type.
xss
- Xss filter to apply.
@Nonnull Mutant param(String name)
/path/:name
or /path/{name}
?name=jooby
Content-Type
is application/x-www-form-urlencoded
or multipart/form-data
path
, query
and body
. For example a pattern like: GET /path/:name
for /path/jooby?name=rocks
produces:
assertEquals("jooby", req.param(name).value()); assertEquals("jooby", req.param(name).toList().get(0)); assertEquals("rocks", req.param(name).toList().get(1));Uploads can be retrieved too when
Content-Type
is multipart/form-data
see Upload
for more information.
name
- A parameter's name.
@Nonnull Mutant param(String name, String... xss)
/path/:name
or /path/{name}
?name=jooby
Content-Type
is application/x-www-form-urlencoded
or multipart/form-data
path
, query
and body
. For example a pattern like: GET /path/:name
for /path/jooby?name=rocks
produces:
assertEquals("jooby", req.param(name).value()); assertEquals("jooby", req.param(name).toList().get(0)); assertEquals("rocks", req.param(name).toList().get(1));Uploads can be retrieved too when
Content-Type
is multipart/form-data
see Upload
for more information.
name
- A parameter's name.
xss
- Xss filter to apply.
@Nonnull default Upload file(String name) throws IOException
Upload
with the given name. The request must be a POST with multipart/form-data
content-type.
name
- File's name.
Upload
.
IOException
@Nonnull default Optional<Upload> ifFile(String name) throws IOException
Upload
with the given name or empty. The request must be a POST with multipart/form-data
content-type.
name
- File's name.
Upload
.
IOException
@Nonnull List<Upload> files(String name) throws IOException
Upload
with the given name. The request must be a POST with multipart/form-data
content-type.
name
- File's name.
Upload
.
IOException
@Nonnull List<Upload> files() throws IOException
Upload
that were uploaded in the request. The request must be a POST with multipart/form-data
content-type.
Upload
.
IOException
@Nonnull Mutant header(String name)
name
- A header's name.
@Nonnull Mutant header(String name, String... xss)
name
- A header's name.
xss
- Xss escapers.
@Nonnull Mutant cookie(String name)
name
- Cookie's name.
@Nonnull Mutant body() throws Exception
raw
data or a data like json, xml, etc...
Exception
- If body can't be converted or there is no HTTP body.
@Nonnull default <T> T body(Class<T> type) throws Exception
body().to(type)
. HTTP body. Please don't use this method for form submits. This method is used for getting raw
or a parsed data like json, xml, etc...
T
- Value type.
type
- Object type.
Exception
- If body can't be converted or there is no HTTP body.
@Nonnull Charset charset()
application.charset
.
@Nonnull default List<Locale> locales()
Locale.filter(java.util.List<java.util.Locale.LanguageRange>, java.util.Collection<java.util.Locale>, java.util.Locale.FilteringMode)
.
@Nonnull List<Locale> locales(BiFunction<List<Locale.LanguageRange>,List<Locale>,List<Locale>> filter)
Accept-Language
as Locale.LanguageRange
and filter while the second argument is a list of supported locales defined by the application.lang
property. The next example returns a list of matching Locale
instances using the filtering mechanism defined in RFC 4647:
req.locales(Locale::filter)
filter
- A locale filter.
@Nonnull Locale locale(BiFunction<List<Locale.LanguageRange>,List<Locale>,Locale> filter)
Accept-Language
as Locale.LanguageRange
and filter while the second argument is a list of supported locales defined by the application.lang
property. The next example returns a Locale
instance for the best-matching language tag using the lookup mechanism defined in RFC 4647.
req.locale(Locale::lookup)
filter
- A locale filter.
@Nonnull default Locale locale()
application.lang
.
long length()
-1
if the length is not known.
int port()
host
header or the server port where the client connection was accepted on.
@Nonnull String hostname()
@Nonnull Session session()
@Nonnull Optional<Session> ifSession()
default boolean xhr()
X-Requested-With
header is set to XMLHttpRequest
.
@Nonnull String protocol()
boolean secure()
@Nonnull Request set(String name, Object value)
name
- Attribute's name.
value
- Attribute's local. NOT null.
@Nonnull default Request.Flash flash() throws Err
{
use(new FlashScope());
get("/", req -> {
Map<String, String> flash = req.flash();
return flash;
});
}
As you can see in the example above, the FlashScope
needs to be install it by calling Jooby.use(org.jooby.Jooby.Module)
otherwise a call to this method ends in BAD_REQUEST
.
FlashScope
.
Err
- Bad request error if the FlashScope
was not installed it.
@Nonnull default Request flash(String name, @Nullable Object value)
flash.
. For example a call to flash("success", "OK")
is accessible from template engines using flash.success
name
- Attribute's name.
value
- Attribute's value.
@Nonnull default Optional<String> ifFlash(String name)
name
- Attribute's name.
@Nonnull default String flash(String name) throws Err
BAD_REQUEST error
if missing.
name
- Attribute's name.
Err
- Bad request error if flash attribute is missing.
default boolean isSet(String name)
name
- Attribute's name.
@Nonnull <T> Optional<T> ifGet(String name)
T
- Target type.
name
- Attribute's name.
@Nonnull default <T> T get(String name, T def)
T
- Target type.
name
- Attribute's name.
def
- A default value.
@Nonnull default <T> T get(String name)
T
- Target type.
name
- Attribute's name.
Err
- with Status.BAD_REQUEST
.
@Nonnull <T> Optional<T> unset(String name)
T
- Target type.
name
- Attribute's name.
@Nonnull Map<String,Object> attributes()
@Nonnull default Request set(Class<?> type, Object value)
RequestScoped
object.
type
- Object type.
value
- Actual object to bind.
@Nonnull default Request set(com.google.inject.TypeLiteral<?> type, Object value)
RequestScoped
object.
type
- Seed type.
value
- Actual object to bind.
@Nonnull Request set(com.google.inject.Key<?> key, Object value)
RequestScoped
object.
key
- Seed key.
value
- Actual object to bind.
@Nonnull default Request push(String path)
path
- Path of the resource to push.
@Nonnull Request push(String path, Map<String,Object> headers)
path
- Path of the resource to push.
headers
- Headers to send.
long timestamp()
Copyright © 2019. All rights reserved.