public interface Response
Modifier and Type | Interface and Description |
---|---|
static interface |
Response.Formatter
Handle content negotiation.
|
static class |
Response.Forwarding
A forwarding response.
|
Modifier and Type | Method and Description |
---|---|
Charset |
charset()
If charset is not set this method returns charset defined in the request body.
|
Response |
charset(Charset charset)
Set the
Charset to use and set the Content-Type header with the current
charset. |
Response |
clearCookie(String name)
Discard a cookie from response.
|
boolean |
committed()
Returns a boolean indicating if the response has been committed.
|
default Response |
cookie(Cookie.Definition cookie)
Adds the specified cookie to the response.
|
Response |
cookie(Cookie cookie)
Adds the specified cookie to the response.
|
default Response |
cookie(String name,
String value)
Adds the specified cookie to the response.
|
default void |
download(File file)
Transfer the file at path as an "attachment".
|
default void |
download(String location)
Transfer the file at path as an "attachment".
|
default void |
download(String filename,
File file)
Transfer the file at path as an "attachment".
|
void |
download(String filename,
InputStream stream)
Transfer the file at path as an "attachment".
|
void |
download(String filename,
Reader reader)
Transfer the file at path as an "attachment".
|
default void |
download(String filename,
String location)
Transfer the file at path as an "attachment".
|
Response.Formatter |
format()
Performs content-negotiation on the Accept HTTP header on the request object.
|
Mutant |
header(String name)
Get a header with the given name.
|
Response |
header(String name,
byte value)
Sets a response header with the given name and value.
|
Response |
header(String name,
char value)
Sets a response header with the given name and value.
|
Response |
header(String name,
CharSequence value)
Sets a response header with the given name and value.
|
Response |
header(String name,
Date value)
Sets a response header with the given name and value.
|
Response |
header(String name,
double value)
Sets a response header with the given name and value.
|
Response |
header(String name,
float value)
Sets a response header with the given name and value.
|
Response |
header(String name,
int value)
Sets a response header with the given name and value.
|
Response |
header(String name,
long value)
Sets a response header with the given name and value.
|
Response |
header(String name,
short value)
Sets a response header with the given name and value.
|
Response |
length(int length)
Set the length of the response and set the
Content-Length header. |
<T> T |
local(String name)
Get a local variable by it's name.
|
Response |
local(String name,
Object value)
Put a local using a var's name.
|
Map<String,Object> |
locals()
Response local variables are scoped to the request, and therefore only available to the view(s)
rendered during that request / response cycle.
|
void |
redirect(Status status,
String location)
Redirect to the given url with status code defaulting to
Status.FOUND . |
default void |
redirect(String location)
Redirect to the given url with status code defaulting to
Status.FOUND . |
void |
send(Body body)
Responsible of writing the given body into the HTTP response.
|
default void |
send(Object body)
Responsible of writing the given body into the HTTP response.
|
Optional<Status> |
status() |
default Response |
status(int status)
Set the HTTP response status.
|
Response |
status(Status status)
Set the HTTP response status.
|
Optional<MediaType> |
type() |
Response |
type(MediaType type)
Set the response media type and set the
Content-Type header. |
default Response |
type(String type)
Set the response media type and set the
Content-Type header. |
void download(@Nonnull String filename, @Nonnull InputStream stream) throws Exception
Content-Disposition
"filename=" parameter (i.e. the one that will
appear in the browser dialog) is set to filename.filename
- A file name to use.stream
- A stream to attach.Exception
- If something goes wrong.void download(@Nonnull String filename, @Nonnull Reader reader) throws Exception
Content-Disposition
"filename=" parameter (i.e. the one that will
appear in the browser dialog) is set to filename.filename
- A file name to use.reader
- A reader to attach.Exception
- If something goes wrong.default void download(@Nonnull String location) throws Exception
Content-Disposition
"filename=" parameter (i.e. the one that will
appear in the browser dialog) is set to filename by default.location
- Classpath location of the file.Exception
- If something goes wrong.default void download(String filename, @Nonnull String location) throws Exception
Content-Disposition
"filename=" parameter (i.e. the one that will
appear in the browser dialog) is set to filename by default.filename
- A file name to use.location
- classpath location of the file.Exception
- If something goes wrong.default void download(@Nonnull File file) throws Exception
Content-Disposition
"filename=" parameter (i.e. the one that will
appear in the browser dialog) is set to filename by default.file
- A file to use.Exception
- If something goes wrong.default void download(String filename, @Nonnull File file) throws Exception
Content-Disposition
"filename=" parameter (i.e. the one that will
appear in the browser dialog) is set to filename.filename
- A file name to use.file
- A file to use.Exception
- If something goes wrong.default Response cookie(@Nonnull String name, @Nonnull String value)
name
- A cookie's name.value
- A cookie's value.default Response cookie(@Nonnull Cookie.Definition cookie)
cookie
- A cookie definition.Response cookie(@Nonnull Cookie cookie)
cookie
- A cookie.Response clearCookie(@Nonnull String name)
name
- Cookie's name.@Nonnull Mutant header(@Nonnull String name)
name
- A name.Response header(@Nonnull String name, char value)
name
- Header's name.value
- Header's value.Response header(@Nonnull String name, byte value)
name
- Header's name.value
- Header's value.Response header(@Nonnull String name, short value)
name
- Header's name.value
- Header's value.Response header(@Nonnull String name, int value)
name
- Header's name.value
- Header's value.Response header(@Nonnull String name, long value)
name
- Header's name.value
- Header's value.Response header(@Nonnull String name, float value)
name
- Header's name.value
- Header's value.Response header(@Nonnull String name, double value)
name
- Header's name.value
- Header's value.Response header(@Nonnull String name, CharSequence value)
name
- Header's name.value
- Header's value.Response header(@Nonnull String name, Date value)
name
- Header's name.value
- Header's value.@Nonnull Charset charset()
application.charset
.@Nonnull Response charset(@Nonnull Charset charset)
Charset
to use and set the Content-Type
header with the current
charset.charset
- A charset.@Nonnull Response length(int length)
Content-Length
header.length
- Length of response.@Nonnull Response type(@Nonnull MediaType type)
Content-Type
header.type
- A media type.@Nonnull default Response type(@Nonnull String type)
Content-Type
header.type
- A media type.default void send(@Nonnull Object body) throws Exception
Body.Formatter
that
best matches the Accept
header will be selected for writing the response.body
- The HTTP body.Exception
- If the response write fails.void send(@Nonnull Body body) throws Exception
Body.Formatter
that
best matches the Accept
header will be selected for writing the response.body
- A HTTP body.Exception
- If the response write fails.@Nonnull Response.Formatter format()
** / *
.
get("/jsonOrHtml", (req, rsp) -> rsp.format() .when("text/html", () -> Viewable.of("view", model)) .when("application/json", () -> model) .when("*", () -> Status.NOT_ACCEPTABLE) .send() );
default void redirect(@Nonnull String location) throws Exception
Status.FOUND
.
rsp.redirect("/foo/bar"); rsp.redirect("http://example.com"); rsp.redirect("http://example.com"); rsp.redirect("../login");Redirects can be a fully qualified URI for redirecting to a different site:
rsp.redirect("http://google.com");Redirects can be relative to the root of the host name. For example, if you were on
http://example.com/admin/post/new
, the following redirect to /admin would
land you at http://example.com/admin
:
rsp.redirect("/admin");Redirects can be relative to the current URL. A redirection of post/new, from
http://example.com/blog/admin/
(notice the trailing slash), would give you
http://example.com/blog/admin/post/new.
rsp.redirect("post/new");Redirecting to post/new from
http://example.com/blog/admin
(no trailing slash),
will take you to http://example.com/blog/post/new
.
If you found the above behavior confusing, think of path segments as directories (have trailing slashes) and files, it will start to make sense.
Pathname relative redirects are also possible. If you were onhttp://example.com/admin/post/new
, the following redirect would land you at
http//example.com/admin
:
rsp.redirect("..");A back redirection will redirect the request back to the
Referer
, defaulting to
/
when missing.
rsp.redirect("back");
location
- Either a relative or absolute location.Exception
- If redirection fails.void redirect(@Nonnull Status status, @Nonnull String location) throws Exception
Status.FOUND
.
rsp.redirect("/foo/bar"); rsp.redirect("http://example.com"); rsp.redirect("http://example.com"); rsp.redirect("../login");Redirects can be a fully qualified URI for redirecting to a different site:
rsp.redirect("http://google.com");Redirects can be relative to the root of the host name. For example, if you were on
http://example.com/admin/post/new
, the following redirect to /admin would
land you at http://example.com/admin
:
rsp.redirect("/admin");Redirects can be relative to the current URL. A redirection of post/new, from
http://example.com/blog/admin/
(notice the trailing slash), would give you
http://example.com/blog/admin/post/new.
rsp.redirect("post/new");Redirecting to post/new from
http://example.com/blog/admin
(no trailing slash),
will take you to http://example.com/blog/post/new
.
If you found the above behavior confusing, think of path segments as directories (have trailing slashes) and files, it will start to make sense.
Pathname relative redirects are also possible. If you were onhttp://example.com/admin/post/new
, the following redirect would land you at
http//example.com/admin
:
rsp.redirect("..");A back redirection will redirect the request back to the
Referer
, defaulting to
/
when missing.
rsp.redirect("back");
status
- A redirect status.location
- Either a relative or absolute location.Exception
- If redirection fails.@Nonnull Optional<Status> status()
@Nonnull Response status(@Nonnull Status status)
status
- A HTTP status.@Nonnull default Response status(int status)
status
- A HTTP status.boolean committed()
@Nonnull Map<String,Object> locals()
@Nonnull <T> T local(@Nonnull String name)
T
- A local type.name
- A var's nameCopyright © 2014. All rights reserved.