|
18 | 18 |
|
19 | 19 | import java.io.UnsupportedEncodingException;
|
20 | 20 | import java.nio.charset.Charset;
|
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.LinkedHashMap; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.stream.Collectors; |
21 | 26 |
|
22 | 27 | import org.springframework.util.StringUtils;
|
23 | 28 |
|
@@ -165,6 +170,52 @@ public static String encode(String source, String encoding) throws UnsupportedEn
|
165 | 170 | return HierarchicalUriComponents.encodeUriComponent(source, encoding, type);
|
166 | 171 | }
|
167 | 172 |
|
| 173 | + /** |
| 174 | + * Encode characters outside the unreserved character set as defined in |
| 175 | + * <a href="https://tools.ietf.org/html/rfc3986#section-2">RFC 3986 Section 2</a>. |
| 176 | + * <p>This can be used to ensure the given String will not contain any |
| 177 | + * characters with reserved URI meaning regardless of URI component. |
| 178 | + * @param source the String to be encoded |
| 179 | + * @param charset the character encoding to encode to |
| 180 | + * @return the encoded String |
| 181 | + */ |
| 182 | + public static String encode(String source, Charset charset) { |
| 183 | + HierarchicalUriComponents.Type type = HierarchicalUriComponents.Type.URI; |
| 184 | + return HierarchicalUriComponents.encodeUriComponent(source, charset, type); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Apply {@link #encode(String, String)} to the values in the given URI |
| 189 | + * variables and return a new Map containing the encoded values. |
| 190 | + * @param uriVariables the URI variable values to be encoded |
| 191 | + * @return the encoded String |
| 192 | + * @since 5.0 |
| 193 | + */ |
| 194 | + public static Map<String, String> encodeUriVariables(Map<String, ?> uriVariables) { |
| 195 | + Map<String, String> result = new LinkedHashMap<>(uriVariables.size()); |
| 196 | + uriVariables.entrySet().stream().forEach(entry -> { |
| 197 | + String stringValue = (entry.getValue() != null ? entry.getValue().toString() : ""); |
| 198 | + result.put(entry.getKey(), encode(stringValue, StandardCharsets.UTF_8)); |
| 199 | + }); |
| 200 | + return result; |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Apply {@link #encode(String, String)} to the values in the given URI |
| 205 | + * variables and return a new array containing the encoded values. |
| 206 | + * @param uriVariables the URI variable values to be encoded |
| 207 | + * @return the encoded String |
| 208 | + * @since 5.0 |
| 209 | + */ |
| 210 | + public static Object[] encodeUriVariables(Object... uriVariables) { |
| 211 | + return Arrays.stream(uriVariables) |
| 212 | + .map(value -> { |
| 213 | + String stringValue = (value != null ? value.toString() : ""); |
| 214 | + return encode(stringValue, StandardCharsets.UTF_8); |
| 215 | + }) |
| 216 | + .collect(Collectors.toList()).toArray(); |
| 217 | + } |
| 218 | + |
168 | 219 | /**
|
169 | 220 | * Decode the given encoded URI component.
|
170 | 221 | * <p>See {@link StringUtils#uriDecode(String, Charset) for the decoding rules.
|
|
0 commit comments