1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.web.servlet.view;
  17. import java.io.IOException;
  18. import java.io.UnsupportedEncodingException;
  19. import java.net.URLEncoder;
  20. import java.util.Iterator;
  21. import java.util.Map;
  22. import javax.servlet.http.HttpServletRequest;
  23. import javax.servlet.http.HttpServletResponse;
  24. /**
  25. * View that redirects to an internal or external URL,
  26. * exposing all model attributes as HTTP query parameters.
  27. *
  28. * <p>A URL for this view is supposed to be a HTTP redirect URL,
  29. * i.e. suitable for HttpServletResponse's sendRedirect method.
  30. *
  31. * @author Rod Johnson
  32. * @author Juergen Hoeller
  33. * @version $Id: RedirectView.java,v 1.7 2004/03/18 02:46:11 trisberg Exp $
  34. * @see javax.servlet.http.HttpServletResponse#sendRedirect
  35. */
  36. public class RedirectView extends AbstractUrlBasedView {
  37. public static final String DEFAULT_ENCODING_SCHEME = "UTF-8";
  38. private boolean contextRelative = false;
  39. private String encodingScheme = DEFAULT_ENCODING_SCHEME;
  40. /**
  41. * Constructor for use as a bean.
  42. */
  43. public RedirectView() {
  44. }
  45. /**
  46. * Create a new RedirectView with the given URL.
  47. * @param url the URL to redirect to
  48. */
  49. public RedirectView(String url) {
  50. setUrl(url);
  51. }
  52. /**
  53. * Create a new RedirectView with the given URL.
  54. * @param url the URL to redirect to
  55. * @param contextRelative whether to interpret the given URL as
  56. * relative to the current ServletContext
  57. */
  58. public RedirectView(String url, boolean contextRelative) {
  59. setUrl(url);
  60. this.contextRelative = contextRelative;
  61. }
  62. /**
  63. * Set whether to interpret the given URL as relative to the current
  64. * ServletContext, i.e. as relative to the web application root.
  65. * <p>Default is false: The URL will be intepreted as absolute, i.e.
  66. * taken as-is. If true, the context path will be prepended to the URL.
  67. * @see javax.servlet.http.HttpServletRequest#getContextPath
  68. */
  69. public void setContextRelative(boolean contextRelative) {
  70. this.contextRelative = contextRelative;
  71. }
  72. /**
  73. * Set the encoding scheme for this view.
  74. */
  75. public void setEncodingScheme(String encodingScheme) {
  76. this.encodingScheme = encodingScheme;
  77. }
  78. /**
  79. * Convert model to request parameters and redirect to the given URL.
  80. * @see #appendQueryProperties
  81. */
  82. protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response)
  83. throws IOException {
  84. StringBuffer targetUrl = new StringBuffer();
  85. if (this.contextRelative) {
  86. targetUrl.append(request.getContextPath());
  87. }
  88. targetUrl.append(getUrl());
  89. appendQueryProperties(targetUrl, model, this.encodingScheme);
  90. response.sendRedirect(response.encodeRedirectURL(targetUrl.toString()));
  91. }
  92. /**
  93. * Append query properties to the redirect URL.
  94. * Stringifies, URL-encodes and formats model attributes as query properties.
  95. * @param targetUrl the StringBuffer to append the properties to
  96. * @param model Map that contains model attributes
  97. * @param encodingScheme the encoding scheme to use
  98. * @throws UnsupportedEncodingException if string encoding failed
  99. * @see #queryProperties
  100. */
  101. protected void appendQueryProperties(StringBuffer targetUrl, Map model, String encodingScheme)
  102. throws UnsupportedEncodingException {
  103. // if there are not already some parameters, we need a ?
  104. boolean first = (getUrl().indexOf('?') < 0);
  105. Iterator entries = queryProperties(model).entrySet().iterator();
  106. while (entries.hasNext()) {
  107. if (first) {
  108. targetUrl.append('?');
  109. first = false;
  110. }
  111. else {
  112. targetUrl.append('&');
  113. }
  114. Map.Entry entry = (Map.Entry) entries.next();
  115. String encodedKey = URLEncoder.encode(entry.getKey().toString());
  116. String encodedValue = (entry.getValue() != null ? URLEncoder.encode(entry.getValue().toString()) : "");
  117. targetUrl.append(new String(encodedKey.getBytes(encodingScheme), encodingScheme));
  118. targetUrl.append("=");
  119. targetUrl.append(new String(encodedValue.getBytes(encodingScheme), encodingScheme));
  120. }
  121. }
  122. /**
  123. * Determine name-value pairs for query strings, which will be stringified,
  124. * URL-encoded and formatted by buildRedirectUrl.
  125. * <p>This implementation returns all model elements as-is.
  126. * @see #appendQueryProperties
  127. */
  128. protected Map queryProperties(Map model) {
  129. return model;
  130. }
  131. }