19
19
import java .io .IOException ;
20
20
import java .io .UnsupportedEncodingException ;
21
21
import java .lang .reflect .Array ;
22
+ import java .net .URLEncoder ;
22
23
import java .util .Arrays ;
23
24
import java .util .Collection ;
24
25
import java .util .Collections ;
32
33
import org .springframework .http .HttpStatus ;
33
34
import org .springframework .util .ObjectUtils ;
34
35
import org .springframework .web .servlet .View ;
35
- import org .springframework .web .util .UriUtils ;
36
36
import org .springframework .web .util .WebUtils ;
37
37
38
38
/**
@@ -207,36 +207,27 @@ protected void renderMergedOutputModel(
207
207
Map <String , Object > model , HttpServletRequest request , HttpServletResponse response )
208
208
throws IOException {
209
209
210
- String encoding = getEncoding (request );
211
-
212
210
// Prepare target URL.
213
211
StringBuilder targetUrl = new StringBuilder ();
214
212
if (this .contextRelative && getUrl ().startsWith ("/" )) {
215
213
// Do not apply context path to relative URLs.
216
- targetUrl .append (UriUtils .encodePath (request .getContextPath (), encoding ));
217
- targetUrl .append (UriUtils .encodeUri (getUrl (), encoding ));
218
- }
219
- else {
220
- targetUrl .append (UriUtils .encodeUri (getUrl (), encoding ));
214
+ targetUrl .append (request .getContextPath ());
221
215
}
216
+ targetUrl .append (getUrl ());
222
217
if (this .exposeModelAttributes ) {
223
- appendQueryProperties (targetUrl , model , encoding );
218
+ String enc = this .encodingScheme ;
219
+ if (enc == null ) {
220
+ enc = request .getCharacterEncoding ();
221
+ }
222
+ if (enc == null ) {
223
+ enc = WebUtils .DEFAULT_CHARACTER_ENCODING ;
224
+ }
225
+ appendQueryProperties (targetUrl , model , enc );
224
226
}
225
227
226
228
sendRedirect (request , response , targetUrl .toString (), this .http10Compatible );
227
229
}
228
230
229
- private String getEncoding (HttpServletRequest request ) {
230
- String enc = this .encodingScheme ;
231
- if (enc == null ) {
232
- enc = request .getCharacterEncoding ();
233
- }
234
- if (enc == null ) {
235
- enc = WebUtils .DEFAULT_CHARACTER_ENCODING ;
236
- }
237
- return enc ;
238
- }
239
-
240
231
/**
241
232
* Append query properties to the redirect URL.
242
233
* Stringifies, URL-encodes and formats model attributes as query properties.
@@ -261,7 +252,7 @@ protected void appendQueryProperties(StringBuilder targetUrl, Map<String, Object
261
252
boolean first = (getUrl ().indexOf ('?' ) < 0 );
262
253
for (Map .Entry <String , Object > entry : queryProperties (model ).entrySet ()) {
263
254
Object rawValue = entry .getValue ();
264
- Iterator valueIter ;
255
+ Iterator valueIter = null ;
265
256
if (rawValue != null && rawValue .getClass ().isArray ()) {
266
257
valueIter = Arrays .asList (ObjectUtils .toObjectArray (rawValue )).iterator ();
267
258
}
@@ -280,16 +271,16 @@ else if (rawValue instanceof Collection) {
280
271
else {
281
272
targetUrl .append ('&' );
282
273
}
283
- String encodedKey = UriUtils . encodeQueryParam (entry .getKey (), encodingScheme );
284
- String encodedValue = (value != null ? UriUtils . encodeQueryParam (value .toString (), encodingScheme ) : "" );
274
+ String encodedKey = urlEncode (entry .getKey (), encodingScheme );
275
+ String encodedValue = (value != null ? urlEncode (value .toString (), encodingScheme ) : "" );
285
276
targetUrl .append (encodedKey ).append ('=' ).append (encodedValue );
286
277
}
287
278
}
288
279
289
280
// Append anchor fragment, if any, to end of URL.
290
281
if (fragment != null ) {
291
282
targetUrl .append (fragment );
292
- }
283
+ }
293
284
}
294
285
295
286
/**
@@ -372,6 +363,20 @@ protected boolean isEligibleValue(Object value) {
372
363
return (value != null && BeanUtils .isSimpleValueType (value .getClass ()));
373
364
}
374
365
366
+ /**
367
+ * URL-encode the given input String with the given encoding scheme.
368
+ * <p>The default implementation uses <code>URLEncoder.encode(input, enc)</code>.
369
+ * @param input the unencoded input String
370
+ * @param encodingScheme the encoding scheme
371
+ * @return the encoded output String
372
+ * @throws UnsupportedEncodingException if thrown by the JDK URLEncoder
373
+ * @see java.net.URLEncoder#encode(String, String)
374
+ * @see java.net.URLEncoder#encode(String)
375
+ */
376
+ protected String urlEncode (String input , String encodingScheme ) throws UnsupportedEncodingException {
377
+ return (input != null ? URLEncoder .encode (input , encodingScheme ) : null );
378
+ }
379
+
375
380
/**
376
381
* Send a redirect back to the HTTP client
377
382
* @param request current HTTP request (allows for reacting to request method)
@@ -398,18 +403,14 @@ protected void sendRedirect(
398
403
/**
399
404
* Determines the status code to use for HTTP 1.1 compatible requests.
400
405
* <p>The default implemenetation returns the {@link #setStatusCode(HttpStatus) statusCode}
401
- * property if set, or the value of the {@link #RESPONSE_STATUS_ATTRIBUTE} attribute.
402
- * If neither are set, it defaults to {@link HttpStatus#SEE_OTHER} (303).
406
+ * property if set, or the value of the {@link #RESPONSE_STATUS_ATTRIBUTE} attribute. If neither are
407
+ * set, it defaults to {@link HttpStatus#SEE_OTHER} (303).
403
408
* @param request the request to inspect
404
- * @param response the servlet response
405
- * @param targetUrl the target URL
406
- * @return the response status
409
+ * @return the response
407
410
*/
408
- protected HttpStatus getHttp11StatusCode (
409
- HttpServletRequest request , HttpServletResponse response , String targetUrl ) {
410
-
411
- if (this .statusCode != null ) {
412
- return this .statusCode ;
411
+ protected HttpStatus getHttp11StatusCode (HttpServletRequest request , HttpServletResponse response , String targetUrl ) {
412
+ if (statusCode != null ) {
413
+ return statusCode ;
413
414
}
414
415
HttpStatus attributeStatusCode = (HttpStatus ) request .getAttribute (View .RESPONSE_STATUS_ATTRIBUTE );
415
416
if (attributeStatusCode != null ) {
0 commit comments