1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2013 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
33
33
34
34
import org .springframework .beans .BeanUtils ;
35
35
import org .springframework .http .HttpStatus ;
36
- import org .springframework .util .Assert ;
37
36
import org .springframework .util .CollectionUtils ;
38
37
import org .springframework .util .ObjectUtils ;
39
38
import org .springframework .util .StringUtils ;
@@ -250,15 +249,15 @@ protected boolean isContextRequired() {
250
249
return false ;
251
250
}
252
251
252
+
253
253
/**
254
254
* Convert model to request parameters and redirect to the given URL.
255
255
* @see #appendQueryProperties
256
256
* @see #sendRedirect
257
257
*/
258
258
@ 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 {
262
261
263
262
String targetUrl = createTargetUrl (model , request );
264
263
targetUrl = updateTargetUrl (targetUrl , model , request , response );
@@ -268,11 +267,13 @@ protected void renderMergedOutputModel(
268
267
UriComponents uriComponents = UriComponentsBuilder .fromUriString (targetUrl ).build ();
269
268
flashMap .setTargetRequestPath (uriComponents .getPath ());
270
269
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 );
271
275
}
272
276
273
- FlashMapManager flashMapManager = RequestContextUtils .getFlashMapManager (request );
274
- flashMapManager .saveOutputFlashMap (flashMap , request , response );
275
-
276
277
sendRedirect (request , response , targetUrl , this .http10Compatible );
277
278
}
278
279
@@ -304,7 +305,6 @@ protected final String createTargetUrl(Map<String, Object> model, HttpServletReq
304
305
Map <String , String > variables = getCurrentRequestUriVariables (request );
305
306
targetUrl = replaceUriTemplateVariables (targetUrl .toString (), model , variables , enc );
306
307
}
307
-
308
308
if (this .exposeModelAttributes ) {
309
309
appendQueryProperties (targetUrl , model , enc );
310
310
}
@@ -327,15 +327,17 @@ protected StringBuilder replaceUriTemplateVariables(
327
327
throws UnsupportedEncodingException {
328
328
329
329
StringBuilder result = new StringBuilder ();
330
- Matcher m = URI_TEMPLATE_VARIABLE_PATTERN .matcher (targetUrl );
330
+ Matcher matcher = URI_TEMPLATE_VARIABLE_PATTERN .matcher (targetUrl );
331
331
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 ()));
337
339
result .append (UriUtils .encodePathSegment (value .toString (), encodingScheme ));
338
- endLastMatch = m .end ();
340
+ endLastMatch = matcher .end ();
339
341
}
340
342
result .append (targetUrl .substring (endLastMatch , targetUrl .length ()));
341
343
return result ;
@@ -344,7 +346,7 @@ protected StringBuilder replaceUriTemplateVariables(
344
346
@ SuppressWarnings ("unchecked" )
345
347
private Map <String , String > getCurrentRequestUriVariables (HttpServletRequest request ) {
346
348
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 );
348
350
return (uriVars != null ) ? uriVars : Collections .<String , String > emptyMap ();
349
351
}
350
352
@@ -441,7 +443,6 @@ protected boolean isEligibleProperty(String key, Object value) {
441
443
if (isEligibleValue (value )) {
442
444
return true ;
443
445
}
444
-
445
446
if (value .getClass ().isArray ()) {
446
447
int length = Array .getLength (value );
447
448
if (length == 0 ) {
@@ -455,7 +456,6 @@ protected boolean isEligibleProperty(String key, Object value) {
455
456
}
456
457
return true ;
457
458
}
458
-
459
459
if (value instanceof Collection ) {
460
460
Collection coll = (Collection ) value ;
461
461
if (coll .isEmpty ()) {
@@ -468,7 +468,6 @@ protected boolean isEligibleProperty(String key, Object value) {
468
468
}
469
469
return true ;
470
470
}
471
-
472
471
return false ;
473
472
}
474
473
@@ -504,7 +503,7 @@ protected String urlEncode(String input, String encodingScheme) throws Unsupport
504
503
* @return the updated URL or the same as URL as the one passed in
505
504
*/
506
505
protected String updateTargetUrl (String targetUrl , Map <String , Object > model ,
507
- HttpServletRequest request , HttpServletResponse response ) {
506
+ HttpServletRequest request , HttpServletResponse response ) {
508
507
509
508
RequestContext requestContext = null ;
510
509
if (getWebApplicationContext () != null ) {
@@ -516,14 +515,12 @@ protected String updateTargetUrl(String targetUrl, Map<String, Object> model,
516
515
requestContext = new RequestContext (request , response , wac .getServletContext (), model );
517
516
}
518
517
}
519
-
520
518
if (requestContext != null ) {
521
519
RequestDataValueProcessor processor = requestContext .getRequestDataValueProcessor ();
522
520
if (processor != null ) {
523
521
targetUrl = processor .processUrl (request , targetUrl );
524
522
}
525
523
}
526
-
527
524
return targetUrl ;
528
525
}
529
526
@@ -535,12 +532,10 @@ protected String updateTargetUrl(String targetUrl, Map<String, Object> model,
535
532
* @param http10Compatible whether to stay compatible with HTTP 1.0 clients
536
533
* @throws IOException if thrown by response methods
537
534
*/
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 {
541
537
542
538
String encodedRedirectURL = response .encodeRedirectURL (targetUrl );
543
-
544
539
if (http10Compatible ) {
545
540
if (this .statusCode != null ) {
546
541
response .setStatus (this .statusCode .value ());
0 commit comments