1 16 17 package org.springframework.web.servlet.view; 18 19 import java.io.IOException ; 20 import java.io.UnsupportedEncodingException ; 21 import java.net.URLEncoder ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 27 28 import org.springframework.core.JdkVersion; 29 30 60 public class RedirectView extends AbstractUrlBasedView { 61 62 63 public static final String DEFAULT_ENCODING_SCHEME = "UTF-8"; 64 65 66 private boolean contextRelative = false; 67 68 private boolean http10Compatible = true; 69 70 private String encodingScheme = DEFAULT_ENCODING_SCHEME; 71 72 73 76 public RedirectView() { 77 } 78 79 86 public RedirectView(String url) { 87 super(url); 88 } 89 90 96 public RedirectView(String url, boolean contextRelative) { 97 super(url); 98 this.contextRelative = contextRelative; 99 } 100 101 108 public RedirectView(String url, boolean contextRelative, boolean http10Compatible) { 109 super(url); 110 this.contextRelative = contextRelative; 111 this.http10Compatible = http10Compatible; 112 } 113 114 115 124 public void setContextRelative(boolean contextRelative) { 125 this.contextRelative = contextRelative; 126 } 127 128 139 public void setHttp10Compatible(boolean http10Compatible) { 140 this.http10Compatible = http10Compatible; 141 } 142 143 146 public void setEncodingScheme(String encodingScheme) { 147 this.encodingScheme = encodingScheme; 148 } 149 150 151 156 protected final void renderMergedOutputModel( 157 Map model, HttpServletRequest request, HttpServletResponse response) throws IOException { 158 159 StringBuffer targetUrl = new StringBuffer (); 161 if (this.contextRelative && getUrl().startsWith("/")) { 162 targetUrl.append(request.getContextPath()); 164 } 165 targetUrl.append(getUrl()); 166 appendQueryProperties(targetUrl, model, this.encodingScheme); 167 168 sendRedirect(request, response, targetUrl.toString(), this.http10Compatible); 169 } 170 171 180 protected void appendQueryProperties(StringBuffer targetUrl, Map model, String encodingScheme) 181 throws UnsupportedEncodingException { 182 183 String fragment = null; 187 int anchorIndex = targetUrl.toString().indexOf('#'); 188 if (anchorIndex > -1) { 189 fragment = targetUrl.substring(anchorIndex); 190 targetUrl.delete(anchorIndex, targetUrl.length()); 191 } 192 193 boolean first = (getUrl().indexOf('?') < 0); 195 Iterator entries = queryProperties(model).entrySet().iterator(); 196 while (entries.hasNext()) { 197 if (first) { 198 targetUrl.append('?'); 199 first = false; 200 } 201 else { 202 targetUrl.append('&'); 203 } 204 Map.Entry entry = (Map.Entry ) entries.next(); 205 String encodedKey = urlEncode(entry.getKey().toString(), encodingScheme); 206 String encodedValue = 207 (entry.getValue() != null ? urlEncode(entry.getValue().toString(), encodingScheme) : ""); 208 targetUrl.append(encodedKey).append('=').append(encodedValue); 209 } 210 211 if (fragment != null) { 213 targetUrl.append(fragment); 214 } 215 } 216 217 229 protected String urlEncode(String input, String encodingScheme) throws UnsupportedEncodingException { 230 if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) { 231 if (logger.isDebugEnabled()) { 232 logger.debug("Only JDK 1.3 URLEncoder available: using platform default encoding " + 233 "instead of the requested scheme '" + encodingScheme + "'"); 234 } 235 return URLEncoder.encode(input); 236 } 237 return URLEncoder.encode(input, encodingScheme); 238 } 239 240 246 protected Map queryProperties(Map model) { 247 return model; 248 } 249 250 258 protected void sendRedirect( 259 HttpServletRequest request, HttpServletResponse response, String targetUrl, boolean http10Compatible) 260 throws IOException { 261 262 if (http10Compatible) { 263 response.sendRedirect(response.encodeRedirectURL(targetUrl)); 265 } 266 else { 267 response.setStatus(303); 269 response.setHeader("Location", response.encodeRedirectURL(targetUrl)); 270 } 271 } 272 273 } 274 | Popular Tags |