SPR-8448 Remove URI template vars from the model before appending to query string in RedirectView

This commit is contained in:
Rossen Stoyanchev 2011-06-15 21:07:37 +00:00
parent f288060ad8
commit 23f31d453f
2 changed files with 43 additions and 9 deletions

View File

@ -22,6 +22,7 @@ import java.lang.reflect.Array;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@ -30,15 +31,14 @@ import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.View;
import org.springframework.web.util.UriTemplate;
import org.springframework.web.util.UriUtils;
@ -232,18 +232,23 @@ public class RedirectView extends AbstractUrlBasedView {
enc = WebUtils.DEFAULT_CHARACTER_ENCODING;
}
UriTemplate uriTemplate = createUriTemplate(targetUrl, enc);
if (uriTemplate.getVariableNames().size() > 0) {
targetUrl = new StringBuilder(uriTemplate.expand(model).toString());
model = removeKeys(model, uriTemplate.getVariableNames());
UriTemplate redirectUri = createUriTemplate(targetUrl, enc);
if (redirectUri.getVariableNames().size() > 0) {
targetUrl = new StringBuilder(redirectUri.expand(model).toString());
model = removeKeys(model, redirectUri.getVariableNames());
}
if (this.exposeModelAttributes) {
List<String> uriTemplateVarNames = getUriTemplateVarNames(request);
if (!uriTemplateVarNames.isEmpty()) {
model = removeKeys(model, uriTemplateVarNames);
}
appendQueryProperties(targetUrl, model, enc);
}
sendRedirect(request, response, targetUrl.toString(), this.http10Compatible);
}
@SuppressWarnings("serial")
private UriTemplate createUriTemplate(StringBuilder targetUrl, final String encoding) {
return new UriTemplate(targetUrl.toString()) {
@Override
@ -268,6 +273,16 @@ public class RedirectView extends AbstractUrlBasedView {
return result;
}
/**
* Returns URI template variable names for the current request; or an empty list.
*/
@SuppressWarnings("unchecked")
private List<String> getUriTemplateVarNames(HttpServletRequest request) {
String key = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
Map<String, String> map = (Map<String, String>) request.getAttribute(key);
return (map != null) ? new ArrayList<String>(map.keySet()) : Collections.<String>emptyList();
}
/**
* Append query properties to the redirect URL.
* Stringifies, URL-encodes and formats model attributes as query properties.

View File

@ -25,6 +25,7 @@ import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.servlet.HandlerMapping;
public class RedirectViewUriTemplateTests {
@ -39,7 +40,7 @@ public class RedirectViewUriTemplateTests {
}
@Test
public void pathVar() throws Exception {
public void uriTemplateVar() throws Exception {
Map<String, Object> model = new HashMap<String, Object>();
model.put("foo", "bar");
@ -51,7 +52,7 @@ public class RedirectViewUriTemplateTests {
}
@Test
public void pathVarAndArrayParam() throws Exception {
public void uriTemplateVarAndArrayParam() throws Exception {
Map<String, Object> model = new HashMap<String, Object>();
model.put("foo", "bar");
model.put("fooArr", new String[] { "baz", "bazz" });
@ -63,7 +64,7 @@ public class RedirectViewUriTemplateTests {
}
@Test
public void pathVarWithObjectConversion() throws Exception {
public void uriTemplateVarWithObjectConversion() throws Exception {
Map<String, Object> model = new HashMap<String, Object>();
model.put("foo", new Long(611));
@ -73,4 +74,22 @@ public class RedirectViewUriTemplateTests {
assertEquals("/foo/611", response.getRedirectedUrl());
}
@Test
public void doNotAppendUriTemplateVarFromCurrentRequest() throws Exception {
Map<String, Object> model = new HashMap<String, Object>();
model.put("name1", "value1");
model.put("name2", "value2");
model.put("name3", "value3");
Map<String, String> uriTemplatVars = new HashMap<String, String>();
uriTemplatVars.put("name1", "value1");
uriTemplatVars.put("name2", "value2");
request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplatVars);
String url = "http://url.somewhere.com";
RedirectView redirectView = new RedirectView(url + "/{name2}");
redirectView.renderMergedOutputModel(model, request, response);
assertEquals(url + "/value2?name3=value3", response.getRedirectedUrl());
}
}