public interface Request extends Registry
Modifier and Type | Interface and Description |
---|---|
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.
|
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. |
default List<Upload> |
files(String name)
Get a list of file
Upload with the given name. |
default Map<String,String> |
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 |
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.
|
Map<String,Mutant> |
headers() |
String |
hostname()
The fully qualified name of the resource being requested, as obtained from the Host HTTP
header.
|
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 |
params()
Get all the available parameters.
|
default String |
path()
Given:
|
int |
port() |
String |
protocol() |
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. |
MediaType |
type() |
<T> Optional<T> |
unset(String name)
Remove a request local attribute.
|
default boolean |
xhr() |
default String path()
http://domain.com/some/path.html -> /some/path.html http://domain.com/a.html -> /a.html
String contextPath()
application.path
. Default is: /
default String method()
MediaType type()
Content-Type
header. Default is: /.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 testdefault boolean is(MediaType... types)
types
- Types to testdefault boolean is(List<MediaType> types)
types
- Types to testdefault 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.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.Mutant params()
/path/:name
or /path/{name}
?name=jooby
Content-Type
is
application/x-www-form-urlencoded
or multipart/form-data
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.default Upload file(String name)
Upload
with the given name. The request must be a POST with
multipart/form-data
content-type.name
- File's name.Upload
.default List<Upload> files(String name)
Upload
with the given name. The request must be a POST with
multipart/form-data
content-type.name
- File's name.Upload
.Mutant header(String name)
name
- A header's name.Mutant cookie(String name)
name
- Cookie's name.Mutant body() throws Exception
Exception
- If body can't be converted or there is no HTTP body.Charset charset()
application.charset
.default List<Locale> locales()
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)
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.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.default Locale locale()
application.lang
.long length()
-1
if the length is not known.String ip()
int port()
host
header or the server port where the client
connection was accepted on.String hostname()
Session session()
Optional<Session> ifSession()
default boolean xhr()
X-Requested-With
header is set to XMLHttpRequest
.String protocol()
boolean secure()
Request set(String name, Object value)
name
- Attribute's name.value
- Attribute's local. NOT null.default Map<String,String> 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.default Request flash(String name, 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.default Optional<String> ifFlash(String name)
name
- Attribute's name.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.<T> Optional<T> ifGet(String name)
T
- Target type.name
- Attribute's name.default <T> T get(String name, T def)
T
- Target type.name
- Attribute's name.def
- A default value.default <T> T get(String name)
T
- Target type.name
- Attribute's name.Err
- with Status.BAD_REQUEST
.<T> Optional<T> unset(String name)
T
- Target type.name
- Attribute's name.Map<String,Object> attributes()
default Request set(Class<?> type, Object value)
RequestScoped
object.type
- Object type.value
- Actual object to bind.default Request set(com.google.inject.TypeLiteral<?> type, Object value)
RequestScoped
object.type
- Seed type.value
- Actual object to bind.Request set(com.google.inject.Key<?> key, Object value)
RequestScoped
object.key
- Seed key.value
- Actual object to bind.Copyright © 2016. All rights reserved.