Moved encodeHttpHeaderFieldParam method to HttpHeaders itself (including tests)

This commit also sets the test source encoding to UTF-8.

Issue: SPR-14547
This commit is contained in:
Juergen Hoeller
2016-08-26 11:14:02 +02:00
parent bbd5993945
commit a8f7f75f64
8 changed files with 96 additions and 114 deletions

View File

@@ -16,8 +16,6 @@
package org.springframework.util;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -1196,44 +1194,4 @@ public abstract class StringUtils {
return arrayToDelimitedString(arr, ",");
}
/**
* Encode the given header field param as describe in the rfc5987.
* @param input the header field param
* @param charset the charset of the header field param string
* @return the encoded header field param
* @see <a href="https://tools.ietf.org/html/rfc5987">rfc5987</a>
* @since 5.0
*/
public static String encodeHttpHeaderFieldParam(String input, Charset charset) {
Assert.notNull(charset, "charset should not be null");
if(StandardCharsets.US_ASCII.equals(charset)) {
return input;
}
Assert.isTrue(StandardCharsets.UTF_8.equals(charset) || StandardCharsets.ISO_8859_1.equals(charset),
"charset should be UTF-8 or ISO-8859-1");
final byte[] source = input.getBytes(charset);
final int len = source.length;
final StringBuilder sb = new StringBuilder(len << 1);
sb.append(charset.name());
sb.append("''");
for (byte b : source) {
if (isRFC5987AttrChar(b)) {
sb.append((char) b);
}
else {
sb.append('%');
char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, 16));
char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, 16));
sb.append(hex1);
sb.append(hex2);
}
}
return sb.toString();
}
private static boolean isRFC5987AttrChar(byte c) {
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
|| c == '!' || c == '#' || c == '$' || c == '&' || c == '+' || c == '-'
|| c == '.' || c == '^' || c == '_' || c == '`' || c == '|' || c == '~';
}
}