Revise encoding steps towards use of JDK Charset and StandardCharsets

Issue: SPR-14492
This commit is contained in:
Juergen Hoeller
2016-07-19 23:43:05 +02:00
parent 79d30d8c8a
commit 99be15f58b
95 changed files with 480 additions and 569 deletions

View File

@@ -17,7 +17,7 @@
package org.springframework.http.client.support;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
@@ -34,8 +34,6 @@ import org.springframework.util.Base64Utils;
*/
public class BasicAuthorizationInterceptor implements ClientHttpRequestInterceptor {
private static final Charset UTF_8 = Charset.forName("UTF-8");
private final String username;
private final String password;
@@ -58,7 +56,8 @@ public class BasicAuthorizationInterceptor implements ClientHttpRequestIntercept
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
ClientHttpRequestExecution execution) throws IOException {
String token = Base64Utils.encodeToString((this.username + ":" + this.password).getBytes(UTF_8));
String token = Base64Utils.encodeToString(
(this.username + ":" + this.password).getBytes(StandardCharsets.UTF_8));
request.getHeaders().add("Authorization", "Basic " + token);
return execution.execute(request, body);
}

View File

@@ -22,6 +22,7 @@ import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
@@ -88,7 +89,7 @@ import org.springframework.util.StringUtils;
*/
public class FormHttpMessageConverter implements HttpMessageConverter<MultiValueMap<String, ?>> {
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
private List<MediaType> supportedMediaTypes = new ArrayList<>();
@@ -303,7 +304,7 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
builder.append('&');
}
}
final byte[] bytes = builder.toString().getBytes(charset.name());
final byte[] bytes = builder.toString().getBytes(charset);
outputMessage.getHeaders().setContentLength(bytes.length);
if (outputMessage instanceof StreamingHttpOutputMessage) {
@@ -494,13 +495,7 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
}
private byte[] getAsciiBytes(String name) {
try {
return name.getBytes("US-ASCII");
}
catch (UnsupportedEncodingException ex) {
// Should not happen - US-ASCII is always supported.
throw new IllegalStateException(ex);
}
return name.getBytes(StandardCharsets.US_ASCII);
}
}

View File

@@ -21,6 +21,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import org.springframework.core.io.support.ResourceRegion;
@@ -184,7 +185,7 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa
}
private static void print(OutputStream os, String buf) throws IOException {
os.write(buf.getBytes("US-ASCII"));
os.write(buf.getBytes(StandardCharsets.US_ASCII));
}
}

View File

@@ -17,7 +17,6 @@
package org.springframework.http.converter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
@@ -89,13 +88,7 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
@Override
protected Long getContentLength(String str, MediaType contentType) {
Charset charset = getContentTypeCharset(contentType);
try {
return (long) str.getBytes(charset.name()).length;
}
catch (UnsupportedEncodingException ex) {
// should not occur
throw new IllegalStateException(ex);
}
return (long) str.getBytes(charset).length;
}
@Override

View File

@@ -22,6 +22,7 @@ import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import com.rometools.rome.feed.WireFeed;
import com.rometools.rome.io.FeedException;
@@ -50,7 +51,7 @@ import org.springframework.util.StringUtils;
*/
public abstract class AbstractWireFeedHttpMessageConverter<T extends WireFeed> extends AbstractHttpMessageConverter<T> {
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
protected AbstractWireFeedHttpMessageConverter(MediaType supportedMediaType) {

View File

@@ -20,6 +20,7 @@ import java.io.IOException;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicReference;
import com.fasterxml.jackson.core.JsonEncoding;
@@ -60,7 +61,7 @@ import org.springframework.util.TypeUtils;
*/
public abstract class AbstractJackson2HttpMessageConverter extends AbstractGenericHttpMessageConverter<Object> {
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
protected ObjectMapper objectMapper;

View File

@@ -22,6 +22,7 @@ import java.io.OutputStreamWriter;
import java.io.Reader;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import com.google.gson.Gson;
import com.google.gson.JsonIOException;
@@ -56,7 +57,7 @@ import org.springframework.util.Assert;
*/
public class GsonHttpMessageConverter extends AbstractGenericHttpMessageConverter<Object> {
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
private Gson gson = new Gson();

View File

@@ -21,6 +21,7 @@ import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ConcurrentHashMap;
import com.google.protobuf.ExtensionRegistry;
@@ -39,7 +40,6 @@ import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.util.FileCopyUtils;
/**
* An {@code HttpMessageConverter} that reads and writes {@link com.google.protobuf.Message}s
* using <a href="https://developers.google.com/protocol-buffers/">Google Protocol Buffers</a>.
@@ -58,7 +58,7 @@ import org.springframework.util.FileCopyUtils;
*/
public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<Message> {
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
public static final MediaType PROTOBUF = new MediaType("application", "x-protobuf", DEFAULT_CHARSET);

View File

@@ -27,6 +27,7 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.Principal;
import java.util.Arrays;
import java.util.Enumeration;
@@ -54,7 +55,7 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
protected static final String FORM_CONTENT_TYPE = "application/x-www-form-urlencoded";
protected static final String FORM_CHARSET = "UTF-8";
protected static final Charset FORM_CHARSET = StandardCharsets.UTF_8;
private final HttpServletRequest servletRequest;
@@ -210,10 +211,10 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
List<String> values = Arrays.asList(form.get(name));
for (Iterator<String> valueIterator = values.iterator(); valueIterator.hasNext();) {
String value = valueIterator.next();
writer.write(URLEncoder.encode(name, FORM_CHARSET));
writer.write(URLEncoder.encode(name, FORM_CHARSET.name()));
if (value != null) {
writer.write('=');
writer.write(URLEncoder.encode(value, FORM_CHARSET));
writer.write(URLEncoder.encode(value, FORM_CHARSET.name()));
if (valueIterator.hasNext()) {
writer.write('&');
}

View File

@@ -17,7 +17,7 @@
package org.springframework.web.cors;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
@@ -52,8 +52,6 @@ import org.springframework.web.util.WebUtils;
*/
public class DefaultCorsProcessor implements CorsProcessor {
private static final Charset UTF8_CHARSET = Charset.forName("UTF-8");
private static final Log logger = LogFactory.getLog(DefaultCorsProcessor.class);
@@ -109,7 +107,7 @@ public class DefaultCorsProcessor implements CorsProcessor {
*/
protected void rejectRequest(ServerHttpResponse response) throws IOException {
response.setStatusCode(HttpStatus.FORBIDDEN);
response.getBody().write("Invalid CORS request".getBytes(UTF8_CHARSET));
response.getBody().write("Invalid CORS request".getBytes(StandardCharsets.UTF_8));
}
/**

View File

@@ -95,21 +95,21 @@ public class RequestPartServletServerHttpRequest extends ServletServerHttpReques
}
else {
String paramValue = this.multipartRequest.getParameter(this.partName);
return new ByteArrayInputStream(paramValue.getBytes(determineEncoding()));
return new ByteArrayInputStream(paramValue.getBytes(determineCharset()));
}
}
}
private String determineEncoding() {
private Charset determineCharset() {
MediaType contentType = getHeaders().getContentType();
if (contentType != null) {
Charset charset = contentType.getCharset();
if (charset != null) {
return charset.name();
return charset;
}
}
String encoding = this.multipartRequest.getCharacterEncoding();
return (encoding != null ? encoding : FORM_CHARSET);
return (encoding != null ? Charset.forName(encoding) : FORM_CHARSET);
}
}

View File

@@ -21,6 +21,8 @@ import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -61,6 +63,7 @@ final class HierarchicalUriComponents extends UriComponents {
private final boolean encoded;
/**
* Package-private constructor. All arguments are optional, and can be {@code null}.
* @param scheme the scheme
@@ -178,34 +181,33 @@ final class HierarchicalUriComponents extends UriComponents {
/**
* Encode all URI components using their specific encoding rules and return
* the result as a new {@code UriComponents} instance.
* @param encoding the encoding of the values contained in this map
* @param charset the encoding of the values contained in this map
* @return the encoded uri components
* @throws UnsupportedEncodingException if the given encoding is not supported
*/
@Override
public HierarchicalUriComponents encode(String encoding) throws UnsupportedEncodingException {
public HierarchicalUriComponents encode(Charset charset) throws UnsupportedEncodingException {
if (this.encoded) {
return this;
}
Assert.hasLength(encoding, "Encoding must not be empty");
String schemeTo = encodeUriComponent(getScheme(), encoding, Type.SCHEME);
String userInfoTo = encodeUriComponent(this.userInfo, encoding, Type.USER_INFO);
String hostTo = encodeUriComponent(this.host, encoding, getHostType());
PathComponent pathTo = this.path.encode(encoding);
MultiValueMap<String, String> paramsTo = encodeQueryParams(encoding);
String fragmentTo = encodeUriComponent(this.getFragment(), encoding, Type.FRAGMENT);
String schemeTo = encodeUriComponent(getScheme(), charset, Type.SCHEME);
String userInfoTo = encodeUriComponent(this.userInfo, charset, Type.USER_INFO);
String hostTo = encodeUriComponent(this.host, charset, getHostType());
PathComponent pathTo = this.path.encode(charset);
MultiValueMap<String, String> paramsTo = encodeQueryParams(charset);
String fragmentTo = encodeUriComponent(this.getFragment(), charset, Type.FRAGMENT);
return new HierarchicalUriComponents(schemeTo, userInfoTo, hostTo, this.port,
pathTo, paramsTo, fragmentTo, true, false);
}
private MultiValueMap<String, String> encodeQueryParams(String encoding) throws UnsupportedEncodingException {
private MultiValueMap<String, String> encodeQueryParams(Charset charset) throws UnsupportedEncodingException {
int size = this.queryParams.size();
MultiValueMap<String, String> result = new LinkedMultiValueMap<>(size);
for (Map.Entry<String, List<String>> entry : this.queryParams.entrySet()) {
String name = encodeUriComponent(entry.getKey(), encoding, Type.QUERY_PARAM);
String name = encodeUriComponent(entry.getKey(), charset, Type.QUERY_PARAM);
List<String> values = new ArrayList<>(entry.getValue().size());
for (String value : entry.getValue()) {
values.add(encodeUriComponent(value, encoding, Type.QUERY_PARAM));
values.add(encodeUriComponent(value, charset, Type.QUERY_PARAM));
}
result.put(name, values);
}
@@ -221,15 +223,25 @@ final class HierarchicalUriComponents extends UriComponents {
* @return the encoded URI
* @throws IllegalArgumentException when the given uri parameter is not a valid URI
*/
static String encodeUriComponent(String source, String encoding, Type type)
throws UnsupportedEncodingException {
static String encodeUriComponent(String source, String encoding, Type type) {
return encodeUriComponent(source, Charset.forName(encoding), type);
}
/**
* Encode the given source into an encoded String using the rules specified
* by the given component and with the given options.
* @param source the source string
* @param charset the encoding of the source string
* @param type the URI component for the source
* @return the encoded URI
* @throws IllegalArgumentException when the given uri parameter is not a valid URI
*/
static String encodeUriComponent(String source, Charset charset, Type type) {
if (source == null) {
return null;
}
Assert.hasLength(encoding, "Encoding must not be empty");
byte[] bytes = encodeBytes(source.getBytes(encoding), type);
return new String(bytes, "US-ASCII");
byte[] bytes = encodeBytes(source.getBytes(charset), type);
return new String(bytes, StandardCharsets.US_ASCII);
}
private static byte[] encodeBytes(byte[] source, Type type) {
@@ -637,7 +649,7 @@ final class HierarchicalUriComponents extends UriComponents {
List<String> getPathSegments();
PathComponent encode(String encoding) throws UnsupportedEncodingException;
PathComponent encode(Charset charset) throws UnsupportedEncodingException;
void verify();
@@ -666,15 +678,16 @@ final class HierarchicalUriComponents extends UriComponents {
@Override
public List<String> getPathSegments() {
String delimiter = new String(new char[]{PATH_DELIMITER});
String delimiter = new String(new char[] {PATH_DELIMITER});
String[] pathSegments = StringUtils.tokenizeToStringArray(path, delimiter);
return Collections.unmodifiableList(Arrays.asList(pathSegments));
}
@Override
public PathComponent encode(String encoding) throws UnsupportedEncodingException {
String encodedPath = encodeUriComponent(getPath(),encoding, Type.PATH);
return new FullPathComponent(encodedPath); }
public PathComponent encode(Charset charset) throws UnsupportedEncodingException {
String encodedPath = encodeUriComponent(getPath(), charset, Type.PATH);
return new FullPathComponent(encodedPath);
}
@Override
public void verify() {
@@ -737,11 +750,11 @@ final class HierarchicalUriComponents extends UriComponents {
}
@Override
public PathComponent encode(String encoding) throws UnsupportedEncodingException {
public PathComponent encode(Charset charset) throws UnsupportedEncodingException {
List<String> pathSegments = getPathSegments();
List<String> encodedPathSegments = new ArrayList<>(pathSegments.size());
for (String pathSegment : pathSegments) {
String encodedPathSegment = encodeUriComponent(pathSegment, encoding, Type.PATH_SEGMENT);
String encodedPathSegment = encodeUriComponent(pathSegment, charset, Type.PATH_SEGMENT);
encodedPathSegments.add(encodedPathSegment);
}
return new PathSegmentComponent(encodedPathSegments);
@@ -814,10 +827,10 @@ final class HierarchicalUriComponents extends UriComponents {
}
@Override
public PathComponent encode(String encoding) throws UnsupportedEncodingException {
public PathComponent encode(Charset charset) throws UnsupportedEncodingException {
List<PathComponent> encodedComponents = new ArrayList<>(this.pathComponents.size());
for (PathComponent pathComponent : this.pathComponents) {
encodedComponents.add(pathComponent.encode(encoding));
encodedComponents.add(pathComponent.encode(charset));
}
return new PathComponentComposite(encodedComponents);
}
@@ -860,7 +873,7 @@ final class HierarchicalUriComponents extends UriComponents {
return Collections.emptyList();
}
@Override
public PathComponent encode(String encoding) throws UnsupportedEncodingException {
public PathComponent encode(Charset charset) throws UnsupportedEncodingException {
return this;
}
@Override

View File

@@ -19,6 +19,7 @@ package org.springframework.web.util;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.List;
@@ -89,7 +90,7 @@ final class OpaqueUriComponents extends UriComponents {
}
@Override
public UriComponents encode(String encoding) throws UnsupportedEncodingException {
public UriComponents encode(Charset charset) throws UnsupportedEncodingException {
return this;
}

View File

@@ -19,6 +19,8 @@ package org.springframework.web.util;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
@@ -42,8 +44,6 @@ import org.springframework.util.MultiValueMap;
@SuppressWarnings("serial")
public abstract class UriComponents implements Serializable {
private static final String DEFAULT_ENCODING = "UTF-8";
/** Captures URI template variable names. */
private static final Pattern NAMES_PATTERN = Pattern.compile("\\{([^/]+?)\\}");
@@ -123,7 +123,7 @@ public abstract class UriComponents implements Serializable {
*/
public final UriComponents encode() {
try {
return encode(DEFAULT_ENCODING);
return encode(StandardCharsets.UTF_8);
}
catch (UnsupportedEncodingException ex) {
// should not occur
@@ -134,11 +134,11 @@ public abstract class UriComponents implements Serializable {
/**
* Encode all URI components using their specific encoding rules, and
* returns the result as a new {@code UriComponents} instance.
* @param encoding the encoding of the values contained in this map
* @param charset the encoding of the values contained in this map
* @return the encoded URI components
* @throws UnsupportedEncodingException if the given encoding is not supported
*/
public abstract UriComponents encode(String encoding) throws UnsupportedEncodingException;
public abstract UriComponents encode(Charset charset) throws UnsupportedEncodingException;
/**
* Replace all URI template variables with the values from a given map.