public interface Redirector
Location
header value, and issues the redirect response.
The redirector is not typically called directly.
Instead, handlers use the redirect(Context, int, Object)
method (or similar) that obtains a redirector from the context registry.
Ratpack provides a default implementation
that is used unless a custom implementation is available.
It is rarely necessary to implement a custom redirector.
One reason to do so though is to use domain objects to represent redirect destinations instead of strings.
For example, a custom redirector may know how to transform a Person
object within your application to a relevant URL.
It may be more convenient to implement such a mapping between domain objects and URLs within a custom redirector.
Such custom redirectors typically wrap the standard()
implementation.
Context.redirect(int, Object)
Modifier and Type | Field and Description |
---|---|
static TypeToken<Redirector> |
TYPE
A type token for this type.
|
Modifier and Type | Method and Description |
---|---|
void |
redirect(Context context,
int code,
java.lang.Object to)
Issues a HTTP redirect response, transforming the given
to value into a value for the Location header. |
default void |
redirect(Context context,
java.lang.String to,
int code)
Deprecated.
|
static Redirector |
standard()
The default redirect issuing strategy.
|
static final TypeToken<Redirector> TYPE
static Redirector standard()
Ratpack makes this redirector available via the base server registry, making it the default.
This redirector always issues absolute URLs, interpreted from the toString()
value of the given to
value.
One exception to this rule is the case of URI
, where the URI.toASCIIString()
is used.
The string value is transformed into an absolute URL in the following ways:
http://
or https://
(literal URL) - value is used without modification//
(protocol relative) - current public request protocol is prepended (based on PublicAddress
)
/
(absolute path) - protocol, host and port are prepended (based on PublicAddress
)
PublicAddress
and Request.getPath()
)
@Deprecated default void redirect(Context context, java.lang.String to, int code)
redirect(Context, int, Object)
redirect(Context, int, Object)
.
This interface provides a default implementation that simply forwards to redirect(Context, int, Object)
.
context
- the request processing context when the redirect was requestedto
- the location to redirect tocode
- the status code to issue with the redirectredirect(Context, int, Object)
void redirect(Context context, int code, java.lang.Object to)
to
value into a value for the Location
header.
Implementations may interpret the to
value in any manner.
See standard()
for details on the default strategy.
context
- the request processing context when the redirect was requestedcode
- the status code to issue with the redirectto
- the location to redirect to