Interface Redirector


  • public interface Redirector
    Interprets objects as a 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.

    See Also:
    Context.redirect(int, Object)
    • Field Detail

      • TYPE

        static final com.google.common.reflect.TypeToken<Redirector> TYPE
        A type token for this type.
        Since:
        1.1
    • Method Detail

      • standard

        static Redirector standard()
        The default redirect issuing strategy.

        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:

        1. starts with http:// or https:// (literal URL) - value is used without modification
        2. starts with // (protocol relative) - current public request protocol is prepended (based on PublicAddress)
        3. starts with / (absolute path) - protocol, host and port are prepended (based on PublicAddress)
        4. else (relative path) - protocol, host, port and current request path are prepended (based on PublicAddress and Request.getPath())
        Returns:
        the standard redirector
        Since:
        1.3
      • redirect

        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.

        Implementations may interpret the to value in any manner. See standard() for details on the default strategy.

        Parameters:
        context - the request processing context when the redirect was requested
        code - the status code to issue with the redirect
        to - the location to redirect to
        Since:
        1.3