Rename MimeType's getCharSet() to getCharset()

Issue: SPR-14172
This commit is contained in:
Juergen Hoeller
2016-04-14 16:38:59 +02:00
parent 977734cc59
commit f1cb793ccb
25 changed files with 144 additions and 129 deletions

View File

@@ -51,19 +51,12 @@ public class MimeType implements Comparable<MimeType>, Serializable {
private static final long serialVersionUID = 4085923477777865903L;
protected static final String WILDCARD_TYPE = "*";
private static final BitSet TOKEN;
protected static final String WILDCARD_TYPE = "*";
private static final String PARAM_CHARSET = "charset";
private final String type;
private final String subtype;
private final Map<String, String> parameters;
private static final BitSet TOKEN;
static {
// variable names refer to RFC 2616, section 2.2
@@ -101,6 +94,13 @@ public class MimeType implements Comparable<MimeType>, Serializable {
}
private final String type;
private final String subtype;
private final Map<String, String> parameters;
/**
* Create a new {@code MimeType} for the given primary type.
* <p>The {@linkplain #getSubtype() subtype} is set to <code>"&#42;"</code>,
@@ -140,17 +140,12 @@ public class MimeType implements Comparable<MimeType>, Serializable {
* @param other the other media type
* @param charset the character set
* @throws IllegalArgumentException if any of the parameters contains illegal characters
* @since 4.3
*/
public MimeType(MimeType other, Charset charset) {
this(other.getType(), other.getSubtype(), addCharsetParameter(charset, other.getParameters()));
}
private static Map<String, String> addCharsetParameter(Charset charset, Map<String, String> parameters) {
Map<String, String> map = new LinkedHashMap<String, String>(parameters);
map.put(PARAM_CHARSET, charset.name());
return map;
}
/**
* Copy-constructor that copies the type and subtype of the given {@code MimeType},
* and allows for different parameter.
@@ -279,12 +274,24 @@ public class MimeType implements Comparable<MimeType>, Serializable {
/**
* Return the character set, as indicated by a {@code charset} parameter, if any.
* @return the character set, or {@code null} if not available
* @since 4.3
*/
public Charset getCharSet() {
public Charset getCharset() {
String charSet = getParameter(PARAM_CHARSET);
return (charSet != null ? Charset.forName(unquote(charSet)) : null);
}
/**
* Return the character set, as indicated by a {@code charset} parameter, if any.
* @return the character set, or {@code null} if not available
* @deprecated as of Spring 4.3, in favor of {@link #getCharset()} with its name
* aligned with the Java return type name
*/
@Deprecated
public Charset getCharSet() {
return getCharset();
}
/**
* Return a generic parameter value, given a parameter name.
* @param name the parameter name
@@ -392,6 +399,81 @@ public class MimeType implements Comparable<MimeType>, Serializable {
return false;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof MimeType)) {
return false;
}
MimeType otherType = (MimeType) other;
return (this.type.equalsIgnoreCase(otherType.type) &&
this.subtype.equalsIgnoreCase(otherType.subtype) &&
parametersAreEqual(otherType));
}
/**
* Determine if the parameters in this {@code MimeType} and the supplied
* {@code MimeType} are equal, performing case-insensitive comparisons
* for {@link Charset}s.
* @since 4.2
*/
private boolean parametersAreEqual(MimeType other) {
if (this.parameters.size() != other.parameters.size()) {
return false;
}
for (String key : this.parameters.keySet()) {
if (!other.parameters.containsKey(key)) {
return false;
}
if (PARAM_CHARSET.equals(key)) {
if (!ObjectUtils.nullSafeEquals(getCharset(), other.getCharset())) {
return false;
}
}
else if (!ObjectUtils.nullSafeEquals(this.parameters.get(key), other.parameters.get(key))) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
int result = this.type.hashCode();
result = 31 * result + this.subtype.hashCode();
result = 31 * result + this.parameters.hashCode();
return result;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
appendTo(builder);
return builder.toString();
}
protected void appendTo(StringBuilder builder) {
builder.append(this.type);
builder.append('/');
builder.append(this.subtype);
appendTo(this.parameters, builder);
}
private void appendTo(Map<String, String> map, StringBuilder builder) {
for (Map.Entry<String, String> entry : map.entrySet()) {
builder.append(';');
builder.append(entry.getKey());
builder.append('=');
builder.append(entry.getValue());
}
}
/**
* Compares this {@code MediaType} to another alphabetically.
* @param other media type to compare to
@@ -437,79 +519,6 @@ public class MimeType implements Comparable<MimeType>, Serializable {
return 0;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof MimeType)) {
return false;
}
MimeType otherType = (MimeType) other;
return (this.type.equalsIgnoreCase(otherType.type) &&
this.subtype.equalsIgnoreCase(otherType.subtype) &&
parametersAreEqual(otherType));
}
/**
* Determine if the parameters in this {@code MimeType} and the supplied
* {@code MimeType} are equal, performing case-insensitive comparisons
* for {@link Charset}s.
* @since 4.2
*/
private boolean parametersAreEqual(MimeType that) {
if (this.parameters.size() != that.parameters.size()) {
return false;
}
for (String key : this.parameters.keySet()) {
if (!that.parameters.containsKey(key)) {
return false;
}
if (PARAM_CHARSET.equals(key)) {
if (!ObjectUtils.nullSafeEquals(this.getCharSet(), that.getCharSet())) {
return false;
}
}
else if (!ObjectUtils.nullSafeEquals(this.parameters.get(key), that.parameters.get(key))) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
int result = this.type.hashCode();
result = 31 * result + this.subtype.hashCode();
result = 31 * result + this.parameters.hashCode();
return result;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
appendTo(builder);
return builder.toString();
}
protected void appendTo(StringBuilder builder) {
builder.append(this.type);
builder.append('/');
builder.append(this.subtype);
appendTo(this.parameters, builder);
}
private void appendTo(Map<String, String> map, StringBuilder builder) {
for (Map.Entry<String, String> entry : map.entrySet()) {
builder.append(';');
builder.append(entry.getKey());
builder.append('=');
builder.append(entry.getValue());
}
}
/**
* Parse the given String value into a {@code MimeType} object,
@@ -521,6 +530,12 @@ public class MimeType implements Comparable<MimeType>, Serializable {
return MimeTypeUtils.parseMimeType(value);
}
private static Map<String, String> addCharsetParameter(Charset charset, Map<String, String> parameters) {
Map<String, String> map = new LinkedHashMap<String, String>(parameters);
map.put(PARAM_CHARSET, charset.name());
return map;
}
public static class SpecificityComparator<T extends MimeType> implements Comparator<T> {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -75,7 +75,7 @@ public class MimeTypeTests {
MimeType mimeType = MimeType.valueOf(s);
assertEquals("Invalid type", "text", mimeType.getType());
assertEquals("Invalid subtype", "html", mimeType.getSubtype());
assertEquals("Invalid charset", Charset.forName("ISO-8859-1"), mimeType.getCharSet());
assertEquals("Invalid charset", Charset.forName("ISO-8859-1"), mimeType.getCharset());
}
@Test
@@ -84,7 +84,7 @@ public class MimeTypeTests {
MimeType mimeType = MimeType.valueOf(s);
assertEquals("Invalid type", "application", mimeType.getType());
assertEquals("Invalid subtype", "xml", mimeType.getSubtype());
assertEquals("Invalid charset", Charset.forName("UTF-8"), mimeType.getCharSet());
assertEquals("Invalid charset", Charset.forName("UTF-8"), mimeType.getCharset());
}
@Test

View File

@@ -300,8 +300,8 @@ public class MappingJackson2MessageConverter extends AbstractMessageConverter {
* @return the JSON encoding to use (never {@code null})
*/
protected JsonEncoding getJsonEncoding(MimeType contentType) {
if ((contentType != null) && (contentType.getCharSet() != null)) {
Charset charset = contentType.getCharSet();
if ((contentType != null) && (contentType.getCharset() != null)) {
Charset charset = contentType.getCharset();
for (JsonEncoding encoding : JsonEncoding.values()) {
if (charset.name().equals(encoding.getJavaName())) {
return encoding;

View File

@@ -66,8 +66,8 @@ public class StringMessageConverter extends AbstractMessageConverter {
}
private Charset getContentTypeCharset(MimeType mimeType) {
if (mimeType != null && mimeType.getCharSet() != null) {
return mimeType.getCharSet();
if (mimeType != null && mimeType.getCharset() != null) {
return mimeType.getCharset();
}
else {
return this.defaultCharset;

View File

@@ -440,7 +440,7 @@ public class StompHeaderAccessor extends SimpMessageHeaderAccessor {
if (bytes.length == 0 || getContentType() == null || !isReadableContentType()) {
return contentType;
}
Charset charset = getContentType().getCharSet();
Charset charset = getContentType().getCharset();
charset = (charset != null ? charset : StompDecoder.UTF8_CHARSET);
return (bytes.length < 80) ?
contentType + " payload=" + new String(bytes, charset) :

View File

@@ -501,7 +501,7 @@ public class MessageHeaderAccessor {
else if (payload instanceof byte[]) {
byte[] bytes = (byte[]) payload;
if (isReadableContentType()) {
Charset charset = getContentType().getCharSet();
Charset charset = getContentType().getCharset();
charset = (charset != null ? charset : DEFAULT_CHARSET);
return (bytes.length < 80) ?
" payload=" + new String(bytes, charset) :
@@ -526,7 +526,7 @@ public class MessageHeaderAccessor {
else if (payload instanceof byte[]) {
byte[] bytes = (byte[]) payload;
if (isReadableContentType()) {
Charset charset = getContentType().getCharSet();
Charset charset = getContentType().getCharset();
charset = (charset != null ? charset : DEFAULT_CHARSET);
return " payload=" + new String(bytes, charset);
}

View File

@@ -390,8 +390,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
if (contentType != null) {
try {
MediaType mediaType = MediaType.parseMediaType(contentType);
if (mediaType.getCharSet() != null) {
this.characterEncoding = mediaType.getCharSet().name();
if (mediaType.getCharset() != null) {
this.characterEncoding = mediaType.getCharset().name();
}
}
catch (Exception ex) {

View File

@@ -236,8 +236,8 @@ public class MockHttpServletResponse implements HttpServletResponse {
if (contentType != null) {
try {
MediaType mediaType = MediaType.parseMediaType(contentType);
if (mediaType.getCharSet() != null) {
this.characterEncoding = mediaType.getCharSet().name();
if (mediaType.getCharset() != null) {
this.characterEncoding = mediaType.getCharset().name();
this.charset = true;
}
}

View File

@@ -246,7 +246,7 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
contentTypeToUse = (mediaType != null ? mediaType : contentTypeToUse);
}
if (contentTypeToUse != null) {
if (contentTypeToUse.getCharSet() == null && this.defaultCharset != null) {
if (contentTypeToUse.getCharset() == null && this.defaultCharset != null) {
contentTypeToUse = new MediaType(contentTypeToUse, this.defaultCharset);
}
headers.setContentType(contentTypeToUse);

View File

@@ -202,7 +202,7 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
MediaType contentType = inputMessage.getHeaders().getContentType();
Charset charset = (contentType.getCharSet() != null ? contentType.getCharSet() : this.charset);
Charset charset = (contentType.getCharset() != null ? contentType.getCharset() : this.charset);
String body = StreamUtils.copyToString(inputMessage.getBody(), charset);
String[] pairs = StringUtils.tokenizeToStringArray(body, "&");
@@ -255,7 +255,7 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
Charset charset;
if (contentType != null) {
outputMessage.getHeaders().setContentType(contentType);
charset = contentType.getCharSet() != null ? contentType.getCharSet() : this.charset;
charset = contentType.getCharset() != null ? contentType.getCharset() : this.charset;
}
else {
outputMessage.getHeaders().setContentType(MediaType.APPLICATION_FORM_URLENCODED);

View File

@@ -118,8 +118,8 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
}
private Charset getContentTypeCharset(MediaType contentType) {
if (contentType != null && contentType.getCharSet() != null) {
return contentType.getCharSet();
if (contentType != null && contentType.getCharset() != null) {
return contentType.getCharset();
}
else {
return this.getDefaultCharset();

View File

@@ -66,7 +66,7 @@ public abstract class AbstractWireFeedHttpMessageConverter<T extends WireFeed> e
WireFeedInput feedInput = new WireFeedInput();
MediaType contentType = inputMessage.getHeaders().getContentType();
Charset charset =
(contentType != null && contentType.getCharSet() != null? contentType.getCharSet() : DEFAULT_CHARSET);
(contentType != null && contentType.getCharset() != null? contentType.getCharset() : DEFAULT_CHARSET);
try {
Reader reader = new InputStreamReader(inputMessage.getBody(), charset);
return (T) feedInput.build(reader);

View File

@@ -348,8 +348,8 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener
* @return the JSON encoding to use (never {@code null})
*/
protected JsonEncoding getJsonEncoding(MediaType contentType) {
if (contentType != null && contentType.getCharSet() != null) {
Charset charset = contentType.getCharSet();
if (contentType != null && contentType.getCharset() != null) {
Charset charset = contentType.getCharset();
for (JsonEncoding encoding : JsonEncoding.values()) {
if (charset.name().equals(encoding.getJavaName())) {
return encoding;

View File

@@ -178,10 +178,10 @@ public class GsonHttpMessageConverter extends AbstractGenericHttpMessageConverte
}
private Charset getCharset(HttpHeaders headers) {
if (headers == null || headers.getContentType() == null || headers.getContentType().getCharSet() == null) {
if (headers == null || headers.getContentType() == null || headers.getContentType().getCharset() == null) {
return DEFAULT_CHARSET;
}
return headers.getContentType().getCharSet();
return headers.getContentType().getCharset();
}
@Override

View File

@@ -116,7 +116,7 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
if (contentType == null) {
contentType = PROTOBUF;
}
Charset charset = contentType.getCharSet();
Charset charset = contentType.getCharset();
if (charset == null) {
charset = DEFAULT_CHARSET;
}
@@ -160,7 +160,7 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
if (contentType == null) {
contentType = getDefaultContentType(message);
}
Charset charset = contentType.getCharSet();
Charset charset = contentType.getCharset();
if (charset == null) {
charset = DEFAULT_CHARSET;
}

View File

@@ -195,8 +195,8 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa
}
private void setCharset(MediaType contentType, Marshaller marshaller) throws PropertyException {
if (contentType != null && contentType.getCharSet() != null) {
marshaller.setProperty(Marshaller.JAXB_ENCODING, contentType.getCharSet().name());
if (contentType != null && contentType.getCharset() != null) {
marshaller.setProperty(Marshaller.JAXB_ENCODING, contentType.getCharset().name());
}
}

View File

@@ -121,7 +121,7 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
this.headers.setContentType(contentType);
}
}
if (contentType != null && contentType.getCharSet() == null) {
if (contentType != null && contentType.getCharset() == null) {
String requestEncoding = this.servletRequest.getCharacterEncoding();
if (StringUtils.hasLength(requestEncoding)) {
Charset charSet = Charset.forName(requestEncoding);

View File

@@ -114,8 +114,8 @@ public class ServletServerHttpResponse implements ServerHttpResponse {
this.servletResponse.setContentType(this.headers.getContentType().toString());
}
if (this.servletResponse.getCharacterEncoding() == null && this.headers.getContentType() != null &&
this.headers.getContentType().getCharSet() != null) {
this.servletResponse.setCharacterEncoding(this.headers.getContentType().getCharSet().name());
this.headers.getContentType().getCharset() != null) {
this.servletResponse.setCharacterEncoding(this.headers.getContentType().getCharset().name());
}
this.headersWritten = true;
}

View File

@@ -113,7 +113,7 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler {
private Charset getCharset(ClientHttpResponse response) {
HttpHeaders headers = response.getHeaders();
MediaType contentType = headers.getContentType();
return contentType != null ? contentType.getCharSet() : null;
return contentType != null ? contentType.getCharset() : null;
}
}

View File

@@ -735,7 +735,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
List<MediaType> supportedMediaTypes = messageConverter.getSupportedMediaTypes();
List<MediaType> result = new ArrayList<MediaType>(supportedMediaTypes.size());
for (MediaType supportedMediaType : supportedMediaTypes) {
if (supportedMediaType.getCharSet() != null) {
if (supportedMediaType.getCharset() != null) {
supportedMediaType =
new MediaType(supportedMediaType.getType(), supportedMediaType.getSubtype());
}

View File

@@ -304,7 +304,7 @@ public abstract class CommonsFileUploadSupport {
return defaultEncoding;
}
MediaType contentType = MediaType.parseMediaType(contentTypeHeader);
Charset charset = contentType.getCharSet();
Charset charset = contentType.getCharset();
return (charset != null ? charset.name() : defaultEncoding);
}

View File

@@ -103,7 +103,7 @@ public class RequestPartServletServerHttpRequest extends ServletServerHttpReques
private String determineEncoding() {
MediaType contentType = getHeaders().getContentType();
if (contentType != null) {
Charset charset = contentType.getCharSet();
Charset charset = contentType.getCharset();
if (charset != null) {
return charset.name();
}

View File

@@ -261,7 +261,7 @@ public class FormHttpMessageConverterTests {
@Override
public String getCharacterEncoding() {
MediaType type = this.outputMessage.getHeaders().getContentType();
return (type != null && type.getCharSet() != null ? type.getCharSet().name() : null);
return (type != null && type.getCharset() != null ? type.getCharset().name() : null);
}
@Override

View File

@@ -390,8 +390,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
if (contentType != null) {
try {
MediaType mediaType = MediaType.parseMediaType(contentType);
if (mediaType.getCharSet() != null) {
this.characterEncoding = mediaType.getCharSet().name();
if (mediaType.getCharset() != null) {
this.characterEncoding = mediaType.getCharset().name();
}
}
catch (Exception ex) {

View File

@@ -236,8 +236,8 @@ public class MockHttpServletResponse implements HttpServletResponse {
if (contentType != null) {
try {
MediaType mediaType = MediaType.parseMediaType(contentType);
if (mediaType.getCharSet() != null) {
this.characterEncoding = mediaType.getCharSet().name();
if (mediaType.getCharset() != null) {
this.characterEncoding = mediaType.getCharset().name();
this.charset = true;
}
}