Skip to content

Commit d0f13b5

Browse files
committed
SPR-7490 - Reverting RedirectView to it's 3.0.3 version
1 parent 4504ab8 commit d0f13b5

File tree

1 file changed

+35
-34
lines changed
  • org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view

1 file changed

+35
-34
lines changed

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/RedirectView.java

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.io.UnsupportedEncodingException;
2121
import java.lang.reflect.Array;
22+
import java.net.URLEncoder;
2223
import java.util.Arrays;
2324
import java.util.Collection;
2425
import java.util.Collections;
@@ -32,7 +33,6 @@
3233
import org.springframework.http.HttpStatus;
3334
import org.springframework.util.ObjectUtils;
3435
import org.springframework.web.servlet.View;
35-
import org.springframework.web.util.UriUtils;
3636
import org.springframework.web.util.WebUtils;
3737

3838
/**
@@ -207,36 +207,27 @@ protected void renderMergedOutputModel(
207207
Map<String, Object> model, HttpServletRequest request, HttpServletResponse response)
208208
throws IOException {
209209

210-
String encoding = getEncoding(request);
211-
212210
// Prepare target URL.
213211
StringBuilder targetUrl = new StringBuilder();
214212
if (this.contextRelative && getUrl().startsWith("/")) {
215213
// 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());
221215
}
216+
targetUrl.append(getUrl());
222217
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);
224226
}
225227

226228
sendRedirect(request, response, targetUrl.toString(), this.http10Compatible);
227229
}
228230

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-
240231
/**
241232
* Append query properties to the redirect URL.
242233
* Stringifies, URL-encodes and formats model attributes as query properties.
@@ -261,7 +252,7 @@ protected void appendQueryProperties(StringBuilder targetUrl, Map<String, Object
261252
boolean first = (getUrl().indexOf('?') < 0);
262253
for (Map.Entry<String, Object> entry : queryProperties(model).entrySet()) {
263254
Object rawValue = entry.getValue();
264-
Iterator valueIter;
255+
Iterator valueIter = null;
265256
if (rawValue != null && rawValue.getClass().isArray()) {
266257
valueIter = Arrays.asList(ObjectUtils.toObjectArray(rawValue)).iterator();
267258
}
@@ -280,16 +271,16 @@ else if (rawValue instanceof Collection) {
280271
else {
281272
targetUrl.append('&');
282273
}
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) : "");
285276
targetUrl.append(encodedKey).append('=').append(encodedValue);
286277
}
287278
}
288279

289280
// Append anchor fragment, if any, to end of URL.
290281
if (fragment != null) {
291282
targetUrl.append(fragment);
292-
}
283+
}
293284
}
294285

295286
/**
@@ -372,6 +363,20 @@ protected boolean isEligibleValue(Object value) {
372363
return (value != null && BeanUtils.isSimpleValueType(value.getClass()));
373364
}
374365

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+
375380
/**
376381
* Send a redirect back to the HTTP client
377382
* @param request current HTTP request (allows for reacting to request method)
@@ -398,18 +403,14 @@ protected void sendRedirect(
398403
/**
399404
* Determines the status code to use for HTTP 1.1 compatible requests.
400405
* <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).
403408
* @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
407410
*/
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;
413414
}
414415
HttpStatus attributeStatusCode = (HttpStatus) request.getAttribute(View.RESPONSE_STATUS_ATTRIBUTE);
415416
if (attributeStatusCode != null) {

0 commit comments

Comments
 (0)