Browse Source

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
pull/1536/head
Rossen Stoyanchev 12 years ago
parent
commit
470c85ade0
  1. 59
      org.springframework.web/src/main/java/org/springframework/http/MediaType.java
  2. 4
      org.springframework.web/src/test/java/org/springframework/http/MediaTypeTests.java

59
org.springframework.web/src/main/java/org/springframework/http/MediaType.java

@ -482,19 +482,27 @@ public class MediaType implements Comparable<MediaType> { @@ -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> { @@ -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);
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))) {
// wildcard with suffix? e.g. application/*+xml
if (this.isWildcardSubtype() || other.isWildcardSubtype()) {
int thisPlusIdx = this.subtype.indexOf('+');
int otherPlusIdx = other.subtype.indexOf('+');
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;

4
org.springframework.web/src/test/java/org/springframework/http/MediaTypeTests.java

@ -59,6 +59,8 @@ public class MediaTypeTests { @@ -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 { @@ -84,6 +86,8 @@ public class MediaTypeTests {
assertTrue(applicationWildcardXml.isCompatibleWith(applicationSoapXml));
assertTrue(applicationSoapXml.isCompatibleWith(applicationWildcardXml));
assertFalse(applicationWildcardXml.isCompatibleWith(MediaType.APPLICATION_JSON));
}
@Test

Loading…
Cancel
Save