Class RedirectView

java.lang.Object
org.apache.shiro.web.util.RedirectView

public class RedirectView extends Object
View that redirects to an absolute, context relative, or current request relative URL, exposing all model attributes as HTTP query parameters.

A URL for this view is supposed to be a HTTP redirect URL, i.e. suitable for HttpServletResponse's sendRedirect method, which is what actually does the redirect if the HTTP 1.0 flag is on, or via sending back an HTTP 303 code - if the HTTP 1.0 compatibility flag is off.

Note that while the default value for the "contextRelative" flag is off, you will probably want to almost always set it to true. With the flag off, URLs starting with "/" are considered relative to the web server root, while with the flag on, they are considered relative to the web application root. Since most web apps will never know or care what their context path actually is, they are much better off setting this flag to true, and submitting paths which are to be considered relative to the web application root.

Note that in a Servlet 2.2 environment, i.e. a servlet container which is only compliant to the limits of this spec, this class will probably fail when feeding in URLs which are not fully absolute, or relative to the current request (no leading "/"), as these are the only two types of URL that sendRedirect supports in a Servlet 2.2 environment.

This class was borrowed from a nearly identical version found in the Spring Framework, with minor modifications to avoid a dependency on Spring itself for a very small amount of code - we couldn't have done it better, and don't want to repeat all of their great effort ;). The original author names and copyright (Apache 2.0) has been left in place. A special thanks to Rod Johnson, Juergen Hoeller, and Colin Sampaleanu for making this available.

Since:
0.2
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The default encoding scheme: UTF-8
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor for use as a bean.
    Create a new RedirectView with the given URL.
    RedirectView(String url, boolean contextRelative)
    Create a new RedirectView with the given URL.
    RedirectView(String url, boolean contextRelative, boolean http10Compatible)
    Create a new RedirectView with the given URL.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    appendQueryProperties(StringBuilder targetUrl, Map model, String encodingScheme)
    Append query properties to the redirect URL.
     
    protected Map
    Determine name-value pairs for query strings, which will be stringified, URL-encoded and formatted by appendQueryProperties.
    final void
    renderMergedOutputModel(Map model, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
    Convert model to request parameters and redirect to the given URL.
    protected void
    sendRedirect(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, String targetUrl, boolean http10Compatible)
    Send a redirect back to the HTTP client
    void
    setContextRelative(boolean contextRelative)
    Set whether to interpret a given URL that starts with a slash ("/") as relative to the current ServletContext, i.e. as relative to the web application root.
    void
    setEncodingScheme(String encodingScheme)
    Set the encoding scheme for this view.
    void
    setHttp10Compatible(boolean http10Compatible)
    Set whether to stay compatible with HTTP 1.0 clients.
    void
     
    protected String
    urlEncode(String input, String encodingScheme)
    URL-encode the given input String with the given encoding scheme, using URLEncoder.encode(input, enc).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • RedirectView

      public RedirectView()
      Constructor for use as a bean.
    • RedirectView

      public RedirectView(String url)
      Create a new RedirectView with the given URL.

      The given URL will be considered as relative to the web server, not as relative to the current ServletContext.

      Parameters:
      url - the URL to redirect to
      See Also:
    • RedirectView

      public RedirectView(String url, boolean contextRelative)
      Create a new RedirectView with the given URL.
      Parameters:
      url - the URL to redirect to
      contextRelative - whether to interpret the given URL as relative to the current ServletContext
    • RedirectView

      public RedirectView(String url, boolean contextRelative, boolean http10Compatible)
      Create a new RedirectView with the given URL.
      Parameters:
      url - the URL to redirect to
      contextRelative - whether to interpret the given URL as relative to the current ServletContext
      http10Compatible - whether to stay compatible with HTTP 1.0 clients
  • Method Details

    • getUrl

      public String getUrl()
    • setUrl

      public void setUrl(String url)
    • setContextRelative

      public void setContextRelative(boolean contextRelative)
      Set whether to interpret a given URL that starts with a slash ("/") as relative to the current ServletContext, i.e. as relative to the web application root.

      Default is "false": A URL that starts with a slash will be interpreted as absolute, i.e. taken as-is. If true, the context path will be prepended to the URL in such a case.

      Parameters:
      contextRelative - whether to interpret a given URL that starts with a slash ("/") as relative to the current ServletContext, i.e. as relative to the web application root.
      See Also:
      • HttpServletRequest.getContextPath()
    • setHttp10Compatible

      public void setHttp10Compatible(boolean http10Compatible)
      Set whether to stay compatible with HTTP 1.0 clients.

      In the default implementation, this will enforce HTTP status code 302 in any case, i.e. delegate to HttpServletResponse.sendRedirect. Turning this off will send HTTP status code 303, which is the correct code for HTTP 1.1 clients, but not understood by HTTP 1.0 clients.

      Many HTTP 1.1 clients treat 302 just like 303, not making any difference. However, some clients depend on 303 when redirecting after a POST request; turn this flag off in such a scenario.

      Parameters:
      http10Compatible - whether to stay compatible with HTTP 1.0 clients.
      See Also:
      • HttpServletResponse.sendRedirect(java.lang.String)
    • setEncodingScheme

      public void setEncodingScheme(String encodingScheme)
      Set the encoding scheme for this view. Default is UTF-8.
      Parameters:
      encodingScheme - the encoding scheme for this view. Default is UTF-8.
    • renderMergedOutputModel

      public final void renderMergedOutputModel(Map model, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws IOException
      Convert model to request parameters and redirect to the given URL.
      Parameters:
      model - the model to convert
      request - the incoming HttpServletRequest
      response - the outgoing HttpServletResponse
      Throws:
      IOException - if there is a problem issuing the redirect
      See Also:
    • appendQueryProperties

      protected void appendQueryProperties(StringBuilder targetUrl, Map model, String encodingScheme) throws UnsupportedEncodingException
      Append query properties to the redirect URL. Stringifies, URL-encodes and formats model attributes as query properties.
      Parameters:
      targetUrl - the StringBuffer to append the properties to
      model - Map that contains model attributes
      encodingScheme - the encoding scheme to use
      Throws:
      UnsupportedEncodingException - if string encoding failed
      See Also:
    • urlEncode

      protected String urlEncode(String input, String encodingScheme) throws UnsupportedEncodingException
      URL-encode the given input String with the given encoding scheme, using URLEncoder.encode(input, enc).
      Parameters:
      input - the unencoded input String
      encodingScheme - the encoding scheme
      Returns:
      the encoded output String
      Throws:
      UnsupportedEncodingException - if thrown by the JDK URLEncoder
      See Also:
    • queryProperties

      protected Map queryProperties(Map model)
      Determine name-value pairs for query strings, which will be stringified, URL-encoded and formatted by appendQueryProperties.

      This implementation returns all model elements as-is.

      Parameters:
      model - the model elements for which to determine name-value pairs.
      Returns:
      the name-value pairs for query strings.
      See Also:
    • sendRedirect

      protected void sendRedirect(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, String targetUrl, boolean http10Compatible) throws IOException
      Send a redirect back to the HTTP client
      Parameters:
      request - current HTTP request (allows for reacting to request method)
      response - current HTTP response (for sending response headers)
      targetUrl - the name URL to redirect to
      http10Compatible - whether to stay compatible with HTTP 1.0 clients
      Throws:
      IOException - if thrown by response methods