Update MediaType's includes method
An additional update (after the last commit) of the "includes" and "isCompatibleWith" methods of MediaType to accomodate wildcards in media types with a suffix. Issue: SPR-9841
This commit is contained in:
@@ -482,19 +482,27 @@ public class MediaType implements Comparable<MediaType> {
|
||||
return true;
|
||||
}
|
||||
else if (this.type.equals(other.type)) {
|
||||
if (this.subtype.equals(other.subtype) || this.isWildcardSubtype()) {
|
||||
if (this.subtype.equals(other.subtype)) {
|
||||
return true;
|
||||
}
|
||||
// application/*+xml includes application/soap+xml
|
||||
int thisPlusIdx = this.subtype.indexOf('+');
|
||||
int otherPlusIdx = other.subtype.indexOf('+');
|
||||
if (thisPlusIdx != -1 && otherPlusIdx != -1) {
|
||||
String thisSubtypeNoSuffix = this.subtype.substring(0, thisPlusIdx);
|
||||
String thisSubtypeSuffix = this.subtype.substring(thisPlusIdx + 1);
|
||||
String otherSubtypeSuffix = other.subtype.substring(otherPlusIdx + 1);
|
||||
if (thisSubtypeSuffix.equals(otherSubtypeSuffix) && WILDCARD_TYPE.equals(thisSubtypeNoSuffix)) {
|
||||
if (this.isWildcardSubtype()) {
|
||||
// wildcard with suffix, e.g. application/*+xml
|
||||
int thisPlusIdx = this.subtype.indexOf('+');
|
||||
if (thisPlusIdx == -1) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
// application/*+xml includes application/soap+xml
|
||||
int otherPlusIdx = other.subtype.indexOf('+');
|
||||
if (otherPlusIdx != -1) {
|
||||
String thisSubtypeNoSuffix = this.subtype.substring(0, thisPlusIdx);
|
||||
String thisSubtypeSuffix = this.subtype.substring(thisPlusIdx + 1);
|
||||
String otherSubtypeSuffix = other.subtype.substring(otherPlusIdx + 1);
|
||||
if (thisSubtypeSuffix.equals(otherSubtypeSuffix) && WILDCARD_TYPE.equals(thisSubtypeNoSuffix)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
@@ -515,23 +523,30 @@ public class MediaType implements Comparable<MediaType> {
|
||||
return true;
|
||||
}
|
||||
else if (this.type.equals(other.type)) {
|
||||
if (this.subtype.equals(other.subtype) || this.isWildcardSubtype() || other.isWildcardSubtype()) {
|
||||
if (this.subtype.equals(other.subtype)) {
|
||||
return true;
|
||||
}
|
||||
// application/*+xml is compatible with application/soap+xml, and vice-versa
|
||||
int thisPlusIdx = this.subtype.indexOf('+');
|
||||
int otherPlusIdx = other.subtype.indexOf('+');
|
||||
if (thisPlusIdx != -1 && otherPlusIdx != -1) {
|
||||
String thisSubtypeNoSuffix = this.subtype.substring(0, thisPlusIdx);
|
||||
String otherSubtypeNoSuffix = other.subtype.substring(0, otherPlusIdx);
|
||||
// wildcard with suffix? e.g. application/*+xml
|
||||
if (this.isWildcardSubtype() || other.isWildcardSubtype()) {
|
||||
|
||||
String thisSubtypeSuffix = this.subtype.substring(thisPlusIdx + 1);
|
||||
String otherSubtypeSuffix = other.subtype.substring(otherPlusIdx + 1);
|
||||
int thisPlusIdx = this.subtype.indexOf('+');
|
||||
int otherPlusIdx = other.subtype.indexOf('+');
|
||||
|
||||
if (thisSubtypeSuffix.equals(otherSubtypeSuffix) &&
|
||||
(WILDCARD_TYPE.equals(thisSubtypeNoSuffix) || WILDCARD_TYPE.equals(otherSubtypeNoSuffix))) {
|
||||
if (thisPlusIdx == -1 && otherPlusIdx == -1) {
|
||||
return true;
|
||||
}
|
||||
else if (thisPlusIdx != -1 && otherPlusIdx != -1) {
|
||||
String thisSubtypeNoSuffix = this.subtype.substring(0, thisPlusIdx);
|
||||
String otherSubtypeNoSuffix = other.subtype.substring(0, otherPlusIdx);
|
||||
|
||||
String thisSubtypeSuffix = this.subtype.substring(thisPlusIdx + 1);
|
||||
String otherSubtypeSuffix = other.subtype.substring(otherPlusIdx + 1);
|
||||
|
||||
if (thisSubtypeSuffix.equals(otherSubtypeSuffix) &&
|
||||
(WILDCARD_TYPE.equals(thisSubtypeNoSuffix) || WILDCARD_TYPE.equals(otherSubtypeNoSuffix))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -59,6 +59,8 @@ public class MediaTypeTests {
|
||||
|
||||
assertTrue(applicationWildcardXml.includes(applicationSoapXml));
|
||||
assertFalse(applicationSoapXml.includes(applicationWildcardXml));
|
||||
|
||||
assertFalse(applicationWildcardXml.includes(MediaType.APPLICATION_JSON));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -84,6 +86,8 @@ public class MediaTypeTests {
|
||||
|
||||
assertTrue(applicationWildcardXml.isCompatibleWith(applicationSoapXml));
|
||||
assertTrue(applicationSoapXml.isCompatibleWith(applicationWildcardXml));
|
||||
|
||||
assertFalse(applicationWildcardXml.isCompatibleWith(MediaType.APPLICATION_JSON));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user