Skip to content

Commit 4ac6801

Browse files
committed
Only retrieve the FlashMapManager if a non-empty output FlashMap has been found
Issue: SPR-10937
1 parent 8f81a12 commit 4ac6801

File tree

2 files changed

+27
-30
lines changed

2 files changed

+27
-30
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
* Locale, ThemeResolver, Theme, and MultipartResolver.
4545
*
4646
* @author Juergen Hoeller
47+
* @author Rossen Stoyanchev
4748
* @since 03.03.2003
4849
* @see RequestContext
4950
* @see org.springframework.web.servlet.DispatcherServlet
@@ -191,7 +192,7 @@ public static Theme getTheme(HttpServletRequest request) {
191192
* Return a read-only {@link Map} with "input" flash attributes saved on a
192193
* previous request.
193194
* @param request the current request
194-
* @return a read-only Map, or {@code null}
195+
* @return a read-only Map, or {@code null} if not found
195196
* @see FlashMap
196197
*/
197198
@SuppressWarnings("unchecked")
@@ -201,8 +202,8 @@ public static Theme getTheme(HttpServletRequest request) {
201202

202203
/**
203204
* Return the "output" FlashMap with attributes to save for a subsequent request.
204-
* @param request current request
205-
* @return a {@link FlashMap} instance, never {@code null}
205+
* @param request the current request
206+
* @return a {@link FlashMap} instance (never {@code null} within a DispatcherServlet request)
206207
* @see FlashMap
207208
*/
208209
public static FlashMap getOutputFlashMap(HttpServletRequest request) {
@@ -213,6 +214,7 @@ public static FlashMap getOutputFlashMap(HttpServletRequest request) {
213214
* Return the FlashMapManager instance to save flash attributes with
214215
* before a redirect.
215216
* @param request the current request
217+
* @return a {@link FlashMapManager} instance (never {@code null} within a DispatcherServlet request)
216218
*/
217219
public static FlashMapManager getFlashMapManager(HttpServletRequest request) {
218220
return (FlashMapManager) request.getAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE);

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

Lines changed: 22 additions & 27 deletions
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;
@@ -251,15 +250,15 @@ protected boolean isContextRequired() {
251250
return false;
252251
}
253252

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

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

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

@@ -305,7 +306,6 @@ protected final String createTargetUrl(Map<String, Object> model, HttpServletReq
305306
Map<String, String> variables = getCurrentRequestUriVariables(request);
306307
targetUrl = replaceUriTemplateVariables(targetUrl.toString(), model, variables, enc);
307308
}
308-
309309
if (this.exposeModelAttributes) {
310310
appendQueryProperties(targetUrl, model, enc);
311311
}
@@ -328,15 +328,17 @@ protected StringBuilder replaceUriTemplateVariables(
328328
throws UnsupportedEncodingException {
329329

330330
StringBuilder result = new StringBuilder();
331-
Matcher m = URI_TEMPLATE_VARIABLE_PATTERN.matcher(targetUrl);
331+
Matcher matcher = URI_TEMPLATE_VARIABLE_PATTERN.matcher(targetUrl);
332332
int endLastMatch = 0;
333-
while (m.find()) {
334-
String name = m.group(1);
335-
Object value = model.containsKey(name) ? model.remove(name) : currentUriVariables.get(name);
336-
Assert.notNull(value, "Model has no value for '" + name + "'");
337-
result.append(targetUrl.substring(endLastMatch, m.start()));
333+
while (matcher.find()) {
334+
String name = matcher.group(1);
335+
Object value = (model.containsKey(name) ? model.remove(name) : currentUriVariables.get(name));
336+
if (value == null) {
337+
throw new IllegalArgumentException("Model has no value for key '" + name + "'");
338+
}
339+
result.append(targetUrl.substring(endLastMatch, matcher.start()));
338340
result.append(UriUtils.encodePathSegment(value.toString(), encodingScheme));
339-
endLastMatch = m.end();
341+
endLastMatch = matcher.end();
340342
}
341343
result.append(targetUrl.substring(endLastMatch, targetUrl.length()));
342344
return result;
@@ -345,7 +347,7 @@ protected StringBuilder replaceUriTemplateVariables(
345347
@SuppressWarnings("unchecked")
346348
private Map<String, String> getCurrentRequestUriVariables(HttpServletRequest request) {
347349
Map<String, String> uriVars =
348-
(Map<String, String>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
350+
(Map<String, String>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
349351
return (uriVars != null) ? uriVars : Collections.<String, String> emptyMap();
350352
}
351353

@@ -442,7 +444,6 @@ protected boolean isEligibleProperty(String key, Object value) {
442444
if (isEligibleValue(value)) {
443445
return true;
444446
}
445-
446447
if (value.getClass().isArray()) {
447448
int length = Array.getLength(value);
448449
if (length == 0) {
@@ -456,7 +457,6 @@ protected boolean isEligibleProperty(String key, Object value) {
456457
}
457458
return true;
458459
}
459-
460460
if (value instanceof Collection) {
461461
Collection coll = (Collection) value;
462462
if (coll.isEmpty()) {
@@ -469,7 +469,6 @@ protected boolean isEligibleProperty(String key, Object value) {
469469
}
470470
return true;
471471
}
472-
473472
return false;
474473
}
475474

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

510509
RequestContext requestContext = null;
511510
if (getWebApplicationContext() != null) {
@@ -517,14 +516,12 @@ protected String updateTargetUrl(String targetUrl, Map<String, Object> model,
517516
requestContext = new RequestContext(request, response, wac.getServletContext(), model);
518517
}
519518
}
520-
521519
if (requestContext != null) {
522520
RequestDataValueProcessor processor = requestContext.getRequestDataValueProcessor();
523521
if (processor != null) {
524522
targetUrl = processor.processUrl(request, targetUrl);
525523
}
526524
}
527-
528525
return targetUrl;
529526
}
530527

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

543539
String encodedRedirectURL = response.encodeRedirectURL(targetUrl);
544-
545540
if (http10Compatible) {
546541
if (this.statusCode != null) {
547542
response.setStatus(this.statusCode.value());

0 commit comments

Comments
 (0)