Response.Formatter, Response.Forwarding
Constructor and Description |
---|
ResponseImpl(javax.servlet.http.HttpServletResponse response,
com.google.inject.Injector injector,
Route route,
Map<String,Object> locals,
BodyConverterSelector selector,
MediaTypeProvider typeProvider,
Charset charset,
Optional<String> referer) |
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.
|
Response |
cookie(Cookie cookie)
Adds the specified cookie to the response.
|
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".
|
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 . |
void |
send(Body body)
Responsible of writing the given body into the HTTP response.
|
Optional<Status> |
status() |
Response |
status(Status status)
Set the HTTP response status.
|
String |
toString() |
Optional<MediaType> |
type() |
Response |
type(MediaType type)
Set the response media type and set the
Content-Type header. |
List<MediaType> |
viewableTypes() |
public ResponseImpl(javax.servlet.http.HttpServletResponse response, com.google.inject.Injector injector, Route route, Map<String,Object> locals, BodyConverterSelector selector, MediaTypeProvider typeProvider, Charset charset, Optional<String> referer)
public void download(String filename, Reader reader) throws Exception
Response
Content-Disposition
"filename=" parameter (i.e. the one that will
appear in the browser dialog) is set to path by default. However, you may provide an override
filename.public void download(String filename, InputStream stream) throws Exception
Response
Content-Disposition
"filename=" parameter (i.e. the one that will
appear in the browser dialog) is set to path by default. However, you may provide an override
filename.public Response cookie(Cookie cookie)
Response
public Response clearCookie(String name)
Response
clearCookie
in interface Response
name
- Cookie's name.public Mutant header(String name)
Response
public Response header(String name, byte value)
Response
public Response header(String name, char value)
Response
public Response header(String name, double value)
Response
public Response header(String name, float value)
Response
public Response header(String name, int value)
Response
public Response header(String name, long value)
Response
public Response header(String name, Date value)
Response
public Response header(String name, short value)
Response
public Response header(String name, CharSequence value)
Response
public boolean committed()
Response
public Charset charset()
Response
application.charset
.public Response charset(Charset charset)
Response
Charset
to use and set the Content-Type
header with the current
charset.public Response length(int length)
Response
Content-Length
header.public Map<String,Object> locals()
Response
public <T> T local(String name)
Response
public Response local(String name, Object value)
Response
public void send(Body body) throws Exception
Response
Body.Formatter
that
best matches the Accept
header will be selected for writing the response.public Response.Formatter format()
Response
** / *
.
get("/jsonOrHtml", (req, rsp) -> rsp.format() .when("text/html", () -> Viewable.of("view", model)) .when("application/json", () -> model) .when("*", () -> Status.NOT_ACCEPTABLE) .send() );
public void redirect(Status status, String location) throws Exception
Response
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");
public Response status(Status status)
Response
public Response type(MediaType type)
Response
Content-Type
header.Copyright © 2014. All rights reserved.