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 ;
@@ -251,15 +250,15 @@ protected boolean isContextRequired() {
251
250
return false ;
252
251
}
253
252
253
+
254
254
/**
255
255
* Convert model to request parameters and redirect to the given URL.
256
256
* @see #appendQueryProperties
257
257
* @see #sendRedirect
258
258
*/
259
259
@ 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 {
263
262
264
263
String targetUrl = createTargetUrl (model , request );
265
264
targetUrl = updateTargetUrl (targetUrl , model , request , response );
@@ -269,11 +268,13 @@ protected void renderMergedOutputModel(
269
268
UriComponents uriComponents = UriComponentsBuilder .fromUriString (targetUrl ).build ();
270
269
flashMap .setTargetRequestPath (uriComponents .getPath ());
271
270
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 );
272
276
}
273
277
274
- FlashMapManager flashMapManager = RequestContextUtils .getFlashMapManager (request );
275
- flashMapManager .saveOutputFlashMap (flashMap , request , response );
276
-
277
278
sendRedirect (request , response , targetUrl , this .http10Compatible );
278
279
}
279
280
@@ -305,7 +306,6 @@ protected final String createTargetUrl(Map<String, Object> model, HttpServletReq
305
306
Map <String , String > variables = getCurrentRequestUriVariables (request );
306
307
targetUrl = replaceUriTemplateVariables (targetUrl .toString (), model , variables , enc );
307
308
}
308
-
309
309
if (this .exposeModelAttributes ) {
310
310
appendQueryProperties (targetUrl , model , enc );
311
311
}
@@ -328,15 +328,17 @@ protected StringBuilder replaceUriTemplateVariables(
328
328
throws UnsupportedEncodingException {
329
329
330
330
StringBuilder result = new StringBuilder ();
331
- Matcher m = URI_TEMPLATE_VARIABLE_PATTERN .matcher (targetUrl );
331
+ Matcher matcher = URI_TEMPLATE_VARIABLE_PATTERN .matcher (targetUrl );
332
332
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 ()));
338
340
result .append (UriUtils .encodePathSegment (value .toString (), encodingScheme ));
339
- endLastMatch = m .end ();
341
+ endLastMatch = matcher .end ();
340
342
}
341
343
result .append (targetUrl .substring (endLastMatch , targetUrl .length ()));
342
344
return result ;
@@ -345,7 +347,7 @@ protected StringBuilder replaceUriTemplateVariables(
345
347
@ SuppressWarnings ("unchecked" )
346
348
private Map <String , String > getCurrentRequestUriVariables (HttpServletRequest request ) {
347
349
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 );
349
351
return (uriVars != null ) ? uriVars : Collections .<String , String > emptyMap ();
350
352
}
351
353
@@ -442,7 +444,6 @@ protected boolean isEligibleProperty(String key, Object value) {
442
444
if (isEligibleValue (value )) {
443
445
return true ;
444
446
}
445
-
446
447
if (value .getClass ().isArray ()) {
447
448
int length = Array .getLength (value );
448
449
if (length == 0 ) {
@@ -456,7 +457,6 @@ protected boolean isEligibleProperty(String key, Object value) {
456
457
}
457
458
return true ;
458
459
}
459
-
460
460
if (value instanceof Collection ) {
461
461
Collection coll = (Collection ) value ;
462
462
if (coll .isEmpty ()) {
@@ -469,7 +469,6 @@ protected boolean isEligibleProperty(String key, Object value) {
469
469
}
470
470
return true ;
471
471
}
472
-
473
472
return false ;
474
473
}
475
474
@@ -505,7 +504,7 @@ protected String urlEncode(String input, String encodingScheme) throws Unsupport
505
504
* @return the updated URL or the same as URL as the one passed in
506
505
*/
507
506
protected String updateTargetUrl (String targetUrl , Map <String , Object > model ,
508
- HttpServletRequest request , HttpServletResponse response ) {
507
+ HttpServletRequest request , HttpServletResponse response ) {
509
508
510
509
RequestContext requestContext = null ;
511
510
if (getWebApplicationContext () != null ) {
@@ -517,14 +516,12 @@ protected String updateTargetUrl(String targetUrl, Map<String, Object> model,
517
516
requestContext = new RequestContext (request , response , wac .getServletContext (), model );
518
517
}
519
518
}
520
-
521
519
if (requestContext != null ) {
522
520
RequestDataValueProcessor processor = requestContext .getRequestDataValueProcessor ();
523
521
if (processor != null ) {
524
522
targetUrl = processor .processUrl (request , targetUrl );
525
523
}
526
524
}
527
-
528
525
return targetUrl ;
529
526
}
530
527
@@ -536,12 +533,10 @@ protected String updateTargetUrl(String targetUrl, Map<String, Object> model,
536
533
* @param http10Compatible whether to stay compatible with HTTP 1.0 clients
537
534
* @throws IOException if thrown by response methods
538
535
*/
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 {
542
538
543
539
String encodedRedirectURL = response .encodeRedirectURL (targetUrl );
544
-
545
540
if (http10Compatible ) {
546
541
if (this .statusCode != null ) {
547
542
response .setStatus (this .statusCode .value ());
0 commit comments