diff --git a/spring-core/src/main/java/org/springframework/util/MimeType.java b/spring-core/src/main/java/org/springframework/util/MimeType.java index 4b0fa6ac646..e73602d2c7f 100644 --- a/spring-core/src/main/java/org/springframework/util/MimeType.java +++ b/spring-core/src/main/java/org/springframework/util/MimeType.java @@ -51,19 +51,12 @@ public class MimeType implements Comparable, 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 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, Serializable { } + private final String type; + + private final String subtype; + + private final Map parameters; + + /** * Create a new {@code MimeType} for the given primary type. *

The {@linkplain #getSubtype() subtype} is set to "*", @@ -140,17 +140,12 @@ public class MimeType implements Comparable, 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 addCharsetParameter(Charset charset, Map parameters) { - Map map = new LinkedHashMap(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, 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,50 +399,6 @@ public class MimeType implements Comparable, Serializable { return false; } - /** - * Compares this {@code MediaType} to another alphabetically. - * @param other media type to compare to - * @see MimeTypeUtils#sortBySpecificity(List) - */ - @Override - public int compareTo(MimeType other) { - int comp = getType().compareToIgnoreCase(other.getType()); - if (comp != 0) { - return comp; - } - comp = getSubtype().compareToIgnoreCase(other.getSubtype()); - if (comp != 0) { - return comp; - } - comp = getParameters().size() - other.getParameters().size(); - if (comp != 0) { - return comp; - } - TreeSet thisAttributes = new TreeSet(String.CASE_INSENSITIVE_ORDER); - thisAttributes.addAll(getParameters().keySet()); - TreeSet otherAttributes = new TreeSet(String.CASE_INSENSITIVE_ORDER); - otherAttributes.addAll(other.getParameters().keySet()); - Iterator thisAttributesIterator = thisAttributes.iterator(); - Iterator otherAttributesIterator = otherAttributes.iterator(); - while (thisAttributesIterator.hasNext()) { - String thisAttribute = thisAttributesIterator.next(); - String otherAttribute = otherAttributesIterator.next(); - comp = thisAttribute.compareToIgnoreCase(otherAttribute); - if (comp != 0) { - return comp; - } - String thisValue = getParameters().get(thisAttribute); - String otherValue = other.getParameters().get(otherAttribute); - if (otherValue == null) { - otherValue = ""; - } - comp = thisValue.compareTo(otherValue); - if (comp != 0) { - return comp; - } - } - return 0; - } @Override public boolean equals(Object other) { @@ -457,22 +420,22 @@ public class MimeType implements Comparable, Serializable { * for {@link Charset}s. * @since 4.2 */ - private boolean parametersAreEqual(MimeType that) { - if (this.parameters.size() != that.parameters.size()) { + private boolean parametersAreEqual(MimeType other) { + if (this.parameters.size() != other.parameters.size()) { return false; } for (String key : this.parameters.keySet()) { - if (!that.parameters.containsKey(key)) { + if (!other.parameters.containsKey(key)) { return false; } if (PARAM_CHARSET.equals(key)) { - if (!ObjectUtils.nullSafeEquals(this.getCharSet(), that.getCharSet())) { + if (!ObjectUtils.nullSafeEquals(getCharset(), other.getCharset())) { return false; } } - else if (!ObjectUtils.nullSafeEquals(this.parameters.get(key), that.parameters.get(key))) { + else if (!ObjectUtils.nullSafeEquals(this.parameters.get(key), other.parameters.get(key))) { return false; } } @@ -511,6 +474,52 @@ public class MimeType implements Comparable, Serializable { } } + /** + * Compares this {@code MediaType} to another alphabetically. + * @param other media type to compare to + * @see MimeTypeUtils#sortBySpecificity(List) + */ + @Override + public int compareTo(MimeType other) { + int comp = getType().compareToIgnoreCase(other.getType()); + if (comp != 0) { + return comp; + } + comp = getSubtype().compareToIgnoreCase(other.getSubtype()); + if (comp != 0) { + return comp; + } + comp = getParameters().size() - other.getParameters().size(); + if (comp != 0) { + return comp; + } + TreeSet thisAttributes = new TreeSet(String.CASE_INSENSITIVE_ORDER); + thisAttributes.addAll(getParameters().keySet()); + TreeSet otherAttributes = new TreeSet(String.CASE_INSENSITIVE_ORDER); + otherAttributes.addAll(other.getParameters().keySet()); + Iterator thisAttributesIterator = thisAttributes.iterator(); + Iterator otherAttributesIterator = otherAttributes.iterator(); + while (thisAttributesIterator.hasNext()) { + String thisAttribute = thisAttributesIterator.next(); + String otherAttribute = otherAttributesIterator.next(); + comp = thisAttribute.compareToIgnoreCase(otherAttribute); + if (comp != 0) { + return comp; + } + String thisValue = getParameters().get(thisAttribute); + String otherValue = other.getParameters().get(otherAttribute); + if (otherValue == null) { + otherValue = ""; + } + comp = thisValue.compareTo(otherValue); + if (comp != 0) { + return comp; + } + } + return 0; + } + + /** * Parse the given String value into a {@code MimeType} object, * with this method name following the 'valueOf' naming convention @@ -521,6 +530,12 @@ public class MimeType implements Comparable, Serializable { return MimeTypeUtils.parseMimeType(value); } + private static Map addCharsetParameter(Charset charset, Map parameters) { + Map map = new LinkedHashMap(parameters); + map.put(PARAM_CHARSET, charset.name()); + return map; + } + public static class SpecificityComparator implements Comparator { diff --git a/spring-core/src/test/java/org/springframework/util/MimeTypeTests.java b/spring-core/src/test/java/org/springframework/util/MimeTypeTests.java index b9d3480fd72..5508fa26fe8 100644 --- a/spring-core/src/test/java/org/springframework/util/MimeTypeTests.java +++ b/spring-core/src/test/java/org/springframework/util/MimeTypeTests.java @@ -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 diff --git a/spring-messaging/src/main/java/org/springframework/messaging/converter/MappingJackson2MessageConverter.java b/spring-messaging/src/main/java/org/springframework/messaging/converter/MappingJackson2MessageConverter.java index d553fe1edbd..490b6f6a915 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/converter/MappingJackson2MessageConverter.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/converter/MappingJackson2MessageConverter.java @@ -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; diff --git a/spring-messaging/src/main/java/org/springframework/messaging/converter/StringMessageConverter.java b/spring-messaging/src/main/java/org/springframework/messaging/converter/StringMessageConverter.java index a0d094aa1b7..f511f77f2ba 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/converter/StringMessageConverter.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/converter/StringMessageConverter.java @@ -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; diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaderAccessor.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaderAccessor.java index 78db72b10e9..76971b265f6 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaderAccessor.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaderAccessor.java @@ -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) : diff --git a/spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java b/spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java index 8a84f6f8728..4c3141c42b2 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java @@ -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); } diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java index 01840d66ce1..d1a8c37c7f3 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java @@ -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) { diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java index 5fc724b8c3b..1d5b838dfe2 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java @@ -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; } } diff --git a/spring-web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java index 331324a5c3d..09c6e0e711c 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java @@ -246,7 +246,7 @@ public abstract class AbstractHttpMessageConverter 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); diff --git a/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java index 44491be00f8..3a5584a9485 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java @@ -202,7 +202,7 @@ public class FormHttpMessageConverter implements HttpMessageConverter 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); diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java index 342a708c04b..546aca46989 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java @@ -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; diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/GsonHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/json/GsonHttpMessageConverter.java index bc209af8f23..d82bd28d452 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/GsonHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/GsonHttpMessageConverter.java @@ -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 diff --git a/spring-web/src/main/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverter.java index 9958d6ba531..c73e6129139 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverter.java @@ -116,7 +116,7 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter supportedMediaTypes = messageConverter.getSupportedMediaTypes(); List result = new ArrayList(supportedMediaTypes.size()); for (MediaType supportedMediaType : supportedMediaTypes) { - if (supportedMediaType.getCharSet() != null) { + if (supportedMediaType.getCharset() != null) { supportedMediaType = new MediaType(supportedMediaType.getType(), supportedMediaType.getSubtype()); } diff --git a/spring-web/src/main/java/org/springframework/web/multipart/commons/CommonsFileUploadSupport.java b/spring-web/src/main/java/org/springframework/web/multipart/commons/CommonsFileUploadSupport.java index 622b1844fce..727bc92c5f9 100644 --- a/spring-web/src/main/java/org/springframework/web/multipart/commons/CommonsFileUploadSupport.java +++ b/spring-web/src/main/java/org/springframework/web/multipart/commons/CommonsFileUploadSupport.java @@ -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); } diff --git a/spring-web/src/main/java/org/springframework/web/multipart/support/RequestPartServletServerHttpRequest.java b/spring-web/src/main/java/org/springframework/web/multipart/support/RequestPartServletServerHttpRequest.java index b69e2fa2305..ae817e4b403 100644 --- a/spring-web/src/main/java/org/springframework/web/multipart/support/RequestPartServletServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/web/multipart/support/RequestPartServletServerHttpRequest.java @@ -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(); } diff --git a/spring-web/src/test/java/org/springframework/http/converter/FormHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/FormHttpMessageConverterTests.java index 488e1826f86..aaa03b596d0 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/FormHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/FormHttpMessageConverterTests.java @@ -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 diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java index 48b03112e14..da21c6e27d4 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java @@ -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) { diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java index 413b3321855..c0986b29557 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java @@ -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; } }