SPR-8612 Allow explicit status code in HTTP 1.0 compatibility mode
This commit is contained in:
parent
936c83b030
commit
224bce1b64
|
|
@ -49,9 +49,9 @@ import org.springframework.web.util.WebUtils;
|
|||
* <p>View that redirects to an absolute, context relative, or current request
|
||||
* relative URL. The URL may be a URI template in which case the URI template
|
||||
* variables will be replaced with values available in the model. By default
|
||||
* all primitive model attributes (or collections thereof), not used as URI
|
||||
* template variables, are exposed as HTTP query parameters, but this
|
||||
* behavior can be changed by overriding the
|
||||
* all primitive model attributes (or collections thereof) are exposed as HTTP
|
||||
* query parameters (assuming they've not been used as URI template variables),
|
||||
* but this behavior can be changed by overriding the
|
||||
* {@link #isEligibleProperty(String, Object)} method.
|
||||
*
|
||||
* <p>A URL for this view is supposed to be a HTTP redirect URL, i.e.
|
||||
|
|
@ -264,11 +264,14 @@ public class RedirectView extends AbstractUrlBasedView {
|
|||
|
||||
FlashMap flashMap = RequestContextUtils.getFlashMap(request);
|
||||
if (!CollectionUtils.isEmpty(flashMap)) {
|
||||
flashMap.setExpectedUrl(request, targetUrl.toString()).setExpectedRequestParams(model);
|
||||
flashMap.setExpectedUrlPath(request, targetUrl.toString());
|
||||
}
|
||||
|
||||
|
||||
if (this.exposeModelAttributes) {
|
||||
appendQueryProperties(targetUrl, model, enc);
|
||||
if (!CollectionUtils.isEmpty(flashMap)) {
|
||||
flashMap.setExpectedRequestParams(model);
|
||||
}
|
||||
}
|
||||
|
||||
return targetUrl.toString();
|
||||
|
|
@ -468,14 +471,22 @@ public class RedirectView extends AbstractUrlBasedView {
|
|||
HttpServletRequest request, HttpServletResponse response, String targetUrl, boolean http10Compatible)
|
||||
throws IOException {
|
||||
|
||||
String encodedRedirectURL = response.encodeRedirectURL(targetUrl);
|
||||
|
||||
if (http10Compatible) {
|
||||
// Always send status code 302.
|
||||
response.sendRedirect(response.encodeRedirectURL(targetUrl));
|
||||
if (this.statusCode != null) {
|
||||
response.setStatus(this.statusCode.value());
|
||||
response.setHeader("Location", encodedRedirectURL);
|
||||
}
|
||||
else {
|
||||
// Send status code 302 by default.
|
||||
response.sendRedirect(encodedRedirectURL);
|
||||
}
|
||||
}
|
||||
else {
|
||||
HttpStatus statusCode = getHttp11StatusCode(request, response, targetUrl);
|
||||
response.setStatus(statusCode.value());
|
||||
response.setHeader("Location", response.encodeRedirectURL(targetUrl));
|
||||
response.setHeader("Location", encodedRedirectURL);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -69,20 +69,32 @@ public class RedirectViewTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void explicitStatusCode() throws Exception {
|
||||
public void explicitStatusCodeHttp11() throws Exception {
|
||||
RedirectView rv = new RedirectView();
|
||||
rv.setUrl("http://url.somewhere.com");
|
||||
rv.setHttp10Compatible(false);
|
||||
rv.setStatusCode(HttpStatus.CREATED);
|
||||
rv.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
rv.render(new HashMap<String, Object>(), request, response);
|
||||
assertEquals(201, response.getStatus());
|
||||
assertEquals(301, response.getStatus());
|
||||
assertEquals("http://url.somewhere.com", response.getHeader("Location"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void attributeStatusCode() throws Exception {
|
||||
public void explicitStatusCodeHttp10() throws Exception {
|
||||
RedirectView rv = new RedirectView();
|
||||
rv.setUrl("http://url.somewhere.com");
|
||||
rv.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
rv.render(new HashMap<String, Object>(), request, response);
|
||||
assertEquals(301, response.getStatus());
|
||||
assertEquals("http://url.somewhere.com", response.getHeader("Location"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void attributeStatusCodeHttp11() throws Exception {
|
||||
RedirectView rv = new RedirectView();
|
||||
rv.setUrl("http://url.somewhere.com");
|
||||
rv.setHttp10Compatible(false);
|
||||
|
|
|
|||
Loading…
Reference in New Issue