Skip to content

Commit 70907fd

Browse files
committed
Only retrieve the FlashMapManager if a non-empty output FlashMap has been found
Issue: SPR-10937 (cherry picked from commit 4ac6801)
1 parent 494cc22 commit 70907fd

File tree

2 files changed

+27
-31
lines changed

2 files changed

+27
-31
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContextUtils.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.util.Locale;
2020
import java.util.Map;
21-
2221
import javax.servlet.ServletContext;
2322
import javax.servlet.ServletRequest;
2423
import javax.servlet.http.HttpServletRequest;
@@ -41,6 +40,7 @@
4140
* Locale, ThemeResolver, Theme, and MultipartResolver.
4241
*
4342
* @author Juergen Hoeller
43+
* @author Rossen Stoyanchev
4444
* @since 03.03.2003
4545
* @see RequestContext
4646
* @see org.springframework.web.servlet.DispatcherServlet
@@ -160,7 +160,7 @@ public static Theme getTheme(HttpServletRequest request) {
160160
* Return a read-only {@link Map} with "input" flash attributes saved on a
161161
* previous request.
162162
* @param request the current request
163-
* @return a read-only Map, or {@code null}
163+
* @return a read-only Map, or {@code null} if not found
164164
* @see FlashMap
165165
*/
166166
@SuppressWarnings("unchecked")
@@ -170,8 +170,8 @@ public static Theme getTheme(HttpServletRequest request) {
170170

171171
/**
172172
* Return the "output" FlashMap with attributes to save for a subsequent request.
173-
* @param request current request
174-
* @return a {@link FlashMap} instance, never {@code null}
173+
* @param request the current request
174+
* @return a {@link FlashMap} instance (never {@code null} within a DispatcherServlet request)
175175
* @see FlashMap
176176
*/
177177
public static FlashMap getOutputFlashMap(HttpServletRequest request) {
@@ -182,6 +182,7 @@ public static FlashMap getOutputFlashMap(HttpServletRequest request) {
182182
* Return the FlashMapManager instance to save flash attributes with
183183
* before a redirect.
184184
* @param request the current request
185+
* @return a {@link FlashMapManager} instance (never {@code null} within a DispatcherServlet request)
185186
*/
186187
public static FlashMapManager getFlashMapManager(HttpServletRequest request) {
187188
return (FlashMapManager) request.getAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE);

spring-webmvc/src/main/java/org/springframework/web/servlet/view/RedirectView.java

+22-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,7 +33,6 @@
3333

3434
import org.springframework.beans.BeanUtils;
3535
import org.springframework.http.HttpStatus;
36-
import org.springframework.util.Assert;
3736
import org.springframework.util.CollectionUtils;
3837
import org.springframework.util.ObjectUtils;
3938
import org.springframework.util.StringUtils;
@@ -250,15 +249,15 @@ protected boolean isContextRequired() {
250249
return false;
251250
}
252251

252+
253253
/**
254254
* Convert model to request parameters and redirect to the given URL.
255255
* @see #appendQueryProperties
256256
* @see #sendRedirect
257257
*/
258258
@Override
259-
protected void renderMergedOutputModel(
260-
Map<String, Object> model, HttpServletRequest request, HttpServletResponse response)
261-
throws IOException {
259+
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
260+
HttpServletResponse response) throws IOException {
262261

263262
String targetUrl = createTargetUrl(model, request);
264263
targetUrl = updateTargetUrl(targetUrl, model, request, response);
@@ -268,11 +267,13 @@ protected void renderMergedOutputModel(
268267
UriComponents uriComponents = UriComponentsBuilder.fromUriString(targetUrl).build();
269268
flashMap.setTargetRequestPath(uriComponents.getPath());
270269
flashMap.addTargetRequestParams(uriComponents.getQueryParams());
270+
FlashMapManager flashMapManager = RequestContextUtils.getFlashMapManager(request);
271+
if (flashMapManager == null) {
272+
throw new IllegalStateException("FlashMapManager not found despite output FlashMap having been set");
273+
}
274+
flashMapManager.saveOutputFlashMap(flashMap, request, response);
271275
}
272276

273-
FlashMapManager flashMapManager = RequestContextUtils.getFlashMapManager(request);
274-
flashMapManager.saveOutputFlashMap(flashMap, request, response);
275-
276277
sendRedirect(request, response, targetUrl, this.http10Compatible);
277278
}
278279

@@ -304,7 +305,6 @@ protected final String createTargetUrl(Map<String, Object> model, HttpServletReq
304305
Map<String, String> variables = getCurrentRequestUriVariables(request);
305306
targetUrl = replaceUriTemplateVariables(targetUrl.toString(), model, variables, enc);
306307
}
307-
308308
if (this.exposeModelAttributes) {
309309
appendQueryProperties(targetUrl, model, enc);
310310
}
@@ -327,15 +327,17 @@ protected StringBuilder replaceUriTemplateVariables(
327327
throws UnsupportedEncodingException {
328328

329329
StringBuilder result = new StringBuilder();
330-
Matcher m = URI_TEMPLATE_VARIABLE_PATTERN.matcher(targetUrl);
330+
Matcher matcher = URI_TEMPLATE_VARIABLE_PATTERN.matcher(targetUrl);
331331
int endLastMatch = 0;
332-
while (m.find()) {
333-
String name = m.group(1);
334-
Object value = model.containsKey(name) ? model.remove(name) : currentUriVariables.get(name);
335-
Assert.notNull(value, "Model has no value for '" + name + "'");
336-
result.append(targetUrl.substring(endLastMatch, m.start()));
332+
while (matcher.find()) {
333+
String name = matcher.group(1);
334+
Object value = (model.containsKey(name) ? model.remove(name) : currentUriVariables.get(name));
335+
if (value == null) {
336+
throw new IllegalArgumentException("Model has no value for key '" + name + "'");
337+
}
338+
result.append(targetUrl.substring(endLastMatch, matcher.start()));
337339
result.append(UriUtils.encodePathSegment(value.toString(), encodingScheme));
338-
endLastMatch = m.end();
340+
endLastMatch = matcher.end();
339341
}
340342
result.append(targetUrl.substring(endLastMatch, targetUrl.length()));
341343
return result;
@@ -344,7 +346,7 @@ protected StringBuilder replaceUriTemplateVariables(
344346
@SuppressWarnings("unchecked")
345347
private Map<String, String> getCurrentRequestUriVariables(HttpServletRequest request) {
346348
Map<String, String> uriVars =
347-
(Map<String, String>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
349+
(Map<String, String>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
348350
return (uriVars != null) ? uriVars : Collections.<String, String> emptyMap();
349351
}
350352

@@ -441,7 +443,6 @@ protected boolean isEligibleProperty(String key, Object value) {
441443
if (isEligibleValue(value)) {
442444
return true;
443445
}
444-
445446
if (value.getClass().isArray()) {
446447
int length = Array.getLength(value);
447448
if (length == 0) {
@@ -455,7 +456,6 @@ protected boolean isEligibleProperty(String key, Object value) {
455456
}
456457
return true;
457458
}
458-
459459
if (value instanceof Collection) {
460460
Collection coll = (Collection) value;
461461
if (coll.isEmpty()) {
@@ -468,7 +468,6 @@ protected boolean isEligibleProperty(String key, Object value) {
468468
}
469469
return true;
470470
}
471-
472471
return false;
473472
}
474473

@@ -504,7 +503,7 @@ protected String urlEncode(String input, String encodingScheme) throws Unsupport
504503
* @return the updated URL or the same as URL as the one passed in
505504
*/
506505
protected String updateTargetUrl(String targetUrl, Map<String, Object> model,
507-
HttpServletRequest request, HttpServletResponse response) {
506+
HttpServletRequest request, HttpServletResponse response) {
508507

509508
RequestContext requestContext = null;
510509
if (getWebApplicationContext() != null) {
@@ -516,14 +515,12 @@ protected String updateTargetUrl(String targetUrl, Map<String, Object> model,
516515
requestContext = new RequestContext(request, response, wac.getServletContext(), model);
517516
}
518517
}
519-
520518
if (requestContext != null) {
521519
RequestDataValueProcessor processor = requestContext.getRequestDataValueProcessor();
522520
if (processor != null) {
523521
targetUrl = processor.processUrl(request, targetUrl);
524522
}
525523
}
526-
527524
return targetUrl;
528525
}
529526

@@ -535,12 +532,10 @@ protected String updateTargetUrl(String targetUrl, Map<String, Object> model,
535532
* @param http10Compatible whether to stay compatible with HTTP 1.0 clients
536533
* @throws IOException if thrown by response methods
537534
*/
538-
protected void sendRedirect(
539-
HttpServletRequest request, HttpServletResponse response, String targetUrl, boolean http10Compatible)
540-
throws IOException {
535+
protected void sendRedirect(HttpServletRequest request, HttpServletResponse response,
536+
String targetUrl, boolean http10Compatible) throws IOException {
541537

542538
String encodedRedirectURL = response.encodeRedirectURL(targetUrl);
543-
544539
if (http10Compatible) {
545540
if (this.statusCode != null) {
546541
response.setStatus(this.statusCode.value());

0 commit comments

Comments
 (0)