Skip to content

Commit

Permalink
Add UriUtils.encodeQueryParams
Browse files Browse the repository at this point in the history
Closes gh-24043
  • Loading branch information
rstoyanchev committed Dec 4, 2019
1 parent 797f618 commit b866d42
Showing 1 changed file with 32 additions and 2 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,9 +21,12 @@
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.lang.Nullable;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;

/**
Expand Down Expand Up @@ -225,7 +228,6 @@ public static String encodeQuery(String query, Charset charset) {
* @return the encoded query parameter
*/
public static String encodeQueryParam(String queryParam, String encoding) {

return encode(queryParam, encoding, HierarchicalUriComponents.Type.QUERY_PARAM);
}

Expand All @@ -240,6 +242,34 @@ public static String encodeQueryParam(String queryParam, Charset charset) {
return encode(queryParam, charset, HierarchicalUriComponents.Type.QUERY_PARAM);
}

/**
* Encode the query parameters from the given {@code MultiValueMap} with UTF-8.
* <p>This can be used with {@link UriComponentsBuilder#queryParams(MultiValueMap)}
* when building a URI from an already encoded template.
* <pre class="code">
* MultiValueMap&lt;String, String&gt; params = new LinkedMultiValueMap<>(2);
* // add to params...
*
* ServletUriComponentsBuilder.fromCurrentRequest()
* .queryParams(UriUtils.encodeQueryParams(params))
* .build(true)
* .toUriString();
* </pre>
* @param params the parameters to encode
* @return a new {@code MultiValueMap} with the encoded names and values
* @since 5.2.3
*/
public static MultiValueMap<String, String> encodeQueryParams(MultiValueMap<String, String> params) {
Charset charset = StandardCharsets.UTF_8;
MultiValueMap<String, String> result = new LinkedMultiValueMap<>(params.size());
for (Map.Entry<String, List<String>> entry : params.entrySet()) {
for (String value : entry.getValue()) {
result.add(encodeQueryParam(entry.getKey(), charset), encodeQueryParam(value, charset));
}
}
return result;
}

/**
* Encode the given URI fragment with the given encoding.
* @param fragment the fragment to be encoded
Expand Down

0 comments on commit b866d42

Please sign in to comment.