Consolidate matrix vars parsing in HttpRequestPathHelper

Restore matrix variable parsing code not resorting to the use of
WebUtils which brings Servlet API dependencies with it.

Instead the parsing logic is now exposed through HttpRequestPathHelper
which already contained the decoding logic and also the knowledge of
whether to decode the parsed variables or not.

Issue: SPR-15397
This commit is contained in:
Rossen Stoyanchev
2017-06-02 15:52:44 -04:00
parent d2685dfe67
commit 4244df740b
3 changed files with 129 additions and 9 deletions

View File

@@ -19,6 +19,7 @@ package org.springframework.web.server.support;
import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.StringTokenizer;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.util.LinkedMultiValueMap;
@@ -108,6 +109,43 @@ public class HttpRequestPathHelper {
return decodedVars;
}
/**
* Parse the given string with matrix variables. An example string would look
* like this {@code "q1=a;q1=b;q2=a,b,c"}. The resulting map would contain
* keys {@code "q1"} and {@code "q2"} with values {@code ["a","b"]} and
* {@code ["a","b","c"]} respectively.
* <p>The returned values are decoded unless {@link #setUrlDecode(boolean)}
* is set to {@code true} in which case it is assumed the URL path from
* which the variables were extracted is already decoded through a call to
* {@link #getLookupPathForRequest(ServerWebExchange)}.
* @param semicolonContent path parameter content to parse
* @return a map with matrix variable names and values (never {@code null})
*/
public MultiValueMap<String, String> parseMatrixVariables(ServerWebExchange exchange,
String semicolonContent) {
MultiValueMap<String, String> result = new LinkedMultiValueMap<>();
if (!StringUtils.hasText(semicolonContent)) {
return result;
}
StringTokenizer pairs = new StringTokenizer(semicolonContent, ";");
while (pairs.hasMoreTokens()) {
String pair = pairs.nextToken();
int index = pair.indexOf('=');
if (index != -1) {
String name = pair.substring(0, index);
String rawValue = pair.substring(index + 1);
for (String value : StringUtils.commaDelimitedListToStringArray(rawValue)) {
result.add(name, value);
}
}
else {
result.add(pair, "");
}
}
return decodeMatrixVariables(exchange, result);
}
/**
* Decode the given matrix variables unless {@link #setUrlDecode(boolean)}
* is set to {@code true} in which case it is assumed the URL path from
@@ -117,7 +155,7 @@ public class HttpRequestPathHelper {
* @param vars URI variables extracted from the URL path
* @return the same Map or a new Map instance
*/
public MultiValueMap<String, String> decodeMatrixVariables(ServerWebExchange exchange,
private MultiValueMap<String, String> decodeMatrixVariables(ServerWebExchange exchange,
MultiValueMap<String, String> vars) {
if (this.urlDecode) {