Uses Charset instead of String in MimeType.equals()

Prior to this commit, Spring's MimeType checked for equality between
two MIME types based on the equality of their properties maps; however,
the properties maps contain string representations of the "charset"
values. Thus, "UTF-8" is never equal to "utf-8" which breaks the
contract for character set names which must be compared in a
case-insensitive manner.

This commit addresses this issue by ensuring that "charset" properties
in MimeType instances are compared as Java Charset instances, thereby
ignoring case when checking for equality between charset names.

Issue: SPR-13157
This commit is contained in:
Sam Brannen
2015-06-23 22:30:25 +02:00
parent f2f58f1677
commit 89e504c2f1
3 changed files with 61 additions and 14 deletions

View File

@@ -25,6 +25,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeSet;
/**
@@ -430,7 +431,37 @@ public class MimeType implements Comparable<MimeType>, Serializable {
MimeType otherType = (MimeType) other;
return (this.type.equalsIgnoreCase(otherType.type) &&
this.subtype.equalsIgnoreCase(otherType.subtype) &&
this.parameters.equals(otherType.parameters));
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 (Entry<String, String> entry : this.parameters.entrySet()) {
String key = entry.getKey();
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