|
|
|
|
@@ -17,31 +17,24 @@
|
|
|
|
|
package org.springframework.http;
|
|
|
|
|
|
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
|
import java.nio.charset.UnsupportedCharsetException;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.BitSet;
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.Comparator;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Locale;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.TreeSet;
|
|
|
|
|
|
|
|
|
|
import org.springframework.util.Assert;
|
|
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
import org.springframework.util.LinkedCaseInsensitiveMap;
|
|
|
|
|
import org.springframework.util.InvalidMimeTypeException;
|
|
|
|
|
import org.springframework.util.MimeType;
|
|
|
|
|
import org.springframework.util.MimeTypeUtils;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
import org.springframework.util.comparator.CompoundComparator;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Represents an Internet Media Type, as defined in the HTTP specification.
|
|
|
|
|
*
|
|
|
|
|
* <p>Consists of a {@linkplain #getType() type} and a {@linkplain #getSubtype() subtype}.
|
|
|
|
|
* Also has functionality to parse media types from a string using {@link #parseMediaType(String)},
|
|
|
|
|
* or multiple comma-separated media types using {@link #parseMediaTypes(String)}.
|
|
|
|
|
* A sub-class of {@link MimeType} that adds support for quality parameters as defined
|
|
|
|
|
* in the HTTP specification.
|
|
|
|
|
*
|
|
|
|
|
* @author Arjen Poutsma
|
|
|
|
|
* @author Juergen Hoeller
|
|
|
|
|
@@ -49,10 +42,10 @@ import org.springframework.util.comparator.CompoundComparator;
|
|
|
|
|
* @since 3.0
|
|
|
|
|
* @see <a href="http://tools.ietf.org/html/rfc2616#section-3.7">HTTP 1.1, section 3.7</a>
|
|
|
|
|
*/
|
|
|
|
|
public class MediaType implements Comparable<MediaType> {
|
|
|
|
|
public class MediaType extends MimeType {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Public constant media type that includes all media ranges (i.e. {@code */*}).
|
|
|
|
|
* Public constant media type that includes all media ranges (i.e. "*/*").
|
|
|
|
|
*/
|
|
|
|
|
public static final MediaType ALL;
|
|
|
|
|
|
|
|
|
|
@@ -192,81 +185,35 @@ public class MediaType implements Comparable<MediaType> {
|
|
|
|
|
public final static String TEXT_XML_VALUE = "text/xml";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final BitSet TOKEN;
|
|
|
|
|
|
|
|
|
|
private static final String WILDCARD_TYPE = "*";
|
|
|
|
|
|
|
|
|
|
private static final String PARAM_QUALITY_FACTOR = "q";
|
|
|
|
|
|
|
|
|
|
private static final String PARAM_CHARSET = "charset";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final String type;
|
|
|
|
|
|
|
|
|
|
private final String subtype;
|
|
|
|
|
|
|
|
|
|
private final Map<String, String> parameters;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
// variable names refer to RFC 2616, section 2.2
|
|
|
|
|
BitSet ctl = new BitSet(128);
|
|
|
|
|
for (int i=0; i <= 31; i++) {
|
|
|
|
|
ctl.set(i);
|
|
|
|
|
}
|
|
|
|
|
ctl.set(127);
|
|
|
|
|
|
|
|
|
|
BitSet separators = new BitSet(128);
|
|
|
|
|
separators.set('(');
|
|
|
|
|
separators.set(')');
|
|
|
|
|
separators.set('<');
|
|
|
|
|
separators.set('>');
|
|
|
|
|
separators.set('@');
|
|
|
|
|
separators.set(',');
|
|
|
|
|
separators.set(';');
|
|
|
|
|
separators.set(':');
|
|
|
|
|
separators.set('\\');
|
|
|
|
|
separators.set('\"');
|
|
|
|
|
separators.set('/');
|
|
|
|
|
separators.set('[');
|
|
|
|
|
separators.set(']');
|
|
|
|
|
separators.set('?');
|
|
|
|
|
separators.set('=');
|
|
|
|
|
separators.set('{');
|
|
|
|
|
separators.set('}');
|
|
|
|
|
separators.set(' ');
|
|
|
|
|
separators.set('\t');
|
|
|
|
|
|
|
|
|
|
TOKEN = new BitSet(128);
|
|
|
|
|
TOKEN.set(0, 128);
|
|
|
|
|
TOKEN.andNot(ctl);
|
|
|
|
|
TOKEN.andNot(separators);
|
|
|
|
|
|
|
|
|
|
ALL = MediaType.valueOf(ALL_VALUE);
|
|
|
|
|
APPLICATION_ATOM_XML = MediaType.valueOf(APPLICATION_ATOM_XML_VALUE);
|
|
|
|
|
APPLICATION_FORM_URLENCODED = MediaType.valueOf(APPLICATION_FORM_URLENCODED_VALUE);
|
|
|
|
|
APPLICATION_JSON = MediaType.valueOf(APPLICATION_JSON_VALUE);
|
|
|
|
|
APPLICATION_OCTET_STREAM = MediaType.valueOf(APPLICATION_OCTET_STREAM_VALUE);
|
|
|
|
|
APPLICATION_XHTML_XML = MediaType.valueOf(APPLICATION_XHTML_XML_VALUE);
|
|
|
|
|
APPLICATION_XML = MediaType.valueOf(APPLICATION_XML_VALUE);
|
|
|
|
|
IMAGE_GIF = MediaType.valueOf(IMAGE_GIF_VALUE);
|
|
|
|
|
IMAGE_JPEG = MediaType.valueOf(IMAGE_JPEG_VALUE);
|
|
|
|
|
IMAGE_PNG = MediaType.valueOf(IMAGE_PNG_VALUE);
|
|
|
|
|
MULTIPART_FORM_DATA = MediaType.valueOf(MULTIPART_FORM_DATA_VALUE);
|
|
|
|
|
TEXT_HTML = MediaType.valueOf(TEXT_HTML_VALUE);
|
|
|
|
|
TEXT_PLAIN = MediaType.valueOf(TEXT_PLAIN_VALUE);
|
|
|
|
|
TEXT_XML = MediaType.valueOf(TEXT_XML_VALUE);
|
|
|
|
|
ALL = valueOf(ALL_VALUE);
|
|
|
|
|
APPLICATION_ATOM_XML = valueOf(APPLICATION_ATOM_XML_VALUE);
|
|
|
|
|
APPLICATION_FORM_URLENCODED = valueOf(APPLICATION_FORM_URLENCODED_VALUE);
|
|
|
|
|
APPLICATION_JSON = valueOf(APPLICATION_JSON_VALUE);
|
|
|
|
|
APPLICATION_OCTET_STREAM = valueOf(APPLICATION_OCTET_STREAM_VALUE);
|
|
|
|
|
APPLICATION_XHTML_XML = valueOf(APPLICATION_XHTML_XML_VALUE);
|
|
|
|
|
APPLICATION_XML = valueOf(APPLICATION_XML_VALUE);
|
|
|
|
|
IMAGE_GIF = valueOf(IMAGE_GIF_VALUE);
|
|
|
|
|
IMAGE_JPEG = valueOf(IMAGE_JPEG_VALUE);
|
|
|
|
|
IMAGE_PNG = valueOf(IMAGE_PNG_VALUE);
|
|
|
|
|
MULTIPART_FORM_DATA = valueOf(MULTIPART_FORM_DATA_VALUE);
|
|
|
|
|
TEXT_HTML = valueOf(TEXT_HTML_VALUE);
|
|
|
|
|
TEXT_PLAIN = valueOf(TEXT_PLAIN_VALUE);
|
|
|
|
|
TEXT_XML = valueOf(TEXT_XML_VALUE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new {@code MediaType} for the given primary type.
|
|
|
|
|
* <p>The {@linkplain #getSubtype() subtype} is set to {@code *}, parameters empty.
|
|
|
|
|
* <p>The {@linkplain #getSubtype() subtype} is set to "*", parameters empty.
|
|
|
|
|
* @param type the primary type
|
|
|
|
|
* @throws IllegalArgumentException if any of the parameters contain illegal characters
|
|
|
|
|
*/
|
|
|
|
|
public MediaType(String type) {
|
|
|
|
|
this(type, WILDCARD_TYPE);
|
|
|
|
|
super(type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@@ -277,7 +224,7 @@ public class MediaType implements Comparable<MediaType> {
|
|
|
|
|
* @throws IllegalArgumentException if any of the parameters contain illegal characters
|
|
|
|
|
*/
|
|
|
|
|
public MediaType(String type, String subtype) {
|
|
|
|
|
this(type, subtype, Collections.<String, String>emptyMap());
|
|
|
|
|
super(type, subtype, Collections.<String, String>emptyMap());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@@ -288,7 +235,7 @@ public class MediaType implements Comparable<MediaType> {
|
|
|
|
|
* @throws IllegalArgumentException if any of the parameters contain illegal characters
|
|
|
|
|
*/
|
|
|
|
|
public MediaType(String type, String subtype, Charset charSet) {
|
|
|
|
|
this(type, subtype, Collections.singletonMap(PARAM_CHARSET, charSet.name()));
|
|
|
|
|
super(type, subtype, charSet);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@@ -311,7 +258,7 @@ public class MediaType implements Comparable<MediaType> {
|
|
|
|
|
* @throws IllegalArgumentException if any of the parameters contain illegal characters
|
|
|
|
|
*/
|
|
|
|
|
public MediaType(MediaType other, Map<String, String> parameters) {
|
|
|
|
|
this(other.getType(), other.getSubtype(), parameters);
|
|
|
|
|
super(other.getType(), other.getSubtype(), parameters);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@@ -322,122 +269,17 @@ public class MediaType implements Comparable<MediaType> {
|
|
|
|
|
* @throws IllegalArgumentException if any of the parameters contain illegal characters
|
|
|
|
|
*/
|
|
|
|
|
public MediaType(String type, String subtype, Map<String, String> parameters) {
|
|
|
|
|
Assert.hasLength(type, "type must not be empty");
|
|
|
|
|
Assert.hasLength(subtype, "subtype must not be empty");
|
|
|
|
|
checkToken(type);
|
|
|
|
|
checkToken(subtype);
|
|
|
|
|
this.type = type.toLowerCase(Locale.ENGLISH);
|
|
|
|
|
this.subtype = subtype.toLowerCase(Locale.ENGLISH);
|
|
|
|
|
if (!CollectionUtils.isEmpty(parameters)) {
|
|
|
|
|
Map<String, String> m = new LinkedCaseInsensitiveMap<String>(parameters.size(), Locale.ENGLISH);
|
|
|
|
|
for (Map.Entry<String, String> entry : parameters.entrySet()) {
|
|
|
|
|
String attribute = entry.getKey();
|
|
|
|
|
String value = entry.getValue();
|
|
|
|
|
checkParameters(attribute, value);
|
|
|
|
|
m.put(attribute, value);
|
|
|
|
|
}
|
|
|
|
|
this.parameters = Collections.unmodifiableMap(m);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
this.parameters = Collections.emptyMap();
|
|
|
|
|
}
|
|
|
|
|
super(type, subtype, parameters);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks the given token string for illegal characters, as defined in RFC 2616, section 2.2.
|
|
|
|
|
* @throws IllegalArgumentException in case of illegal characters
|
|
|
|
|
* @see <a href="http://tools.ietf.org/html/rfc2616#section-2.2">HTTP 1.1, section 2.2</a>
|
|
|
|
|
*/
|
|
|
|
|
private void checkToken(String token) {
|
|
|
|
|
for (int i=0; i < token.length(); i++ ) {
|
|
|
|
|
char ch = token.charAt(i);
|
|
|
|
|
if (!TOKEN.get(ch)) {
|
|
|
|
|
throw new IllegalArgumentException("Invalid token character '" + ch + "' in token \"" + token + "\"");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void checkParameters(String attribute, String value) {
|
|
|
|
|
Assert.hasLength(attribute, "parameter attribute must not be empty");
|
|
|
|
|
Assert.hasLength(value, "parameter value must not be empty");
|
|
|
|
|
checkToken(attribute);
|
|
|
|
|
protected void checkParameters(String attribute, String value) {
|
|
|
|
|
super.checkParameters(attribute, value);
|
|
|
|
|
if (PARAM_QUALITY_FACTOR.equals(attribute)) {
|
|
|
|
|
value = unquote(value);
|
|
|
|
|
double d = Double.parseDouble(value);
|
|
|
|
|
Assert.isTrue(d >= 0D && d <= 1D,
|
|
|
|
|
"Invalid quality value \"" + value + "\": should be between 0.0 and 1.0");
|
|
|
|
|
}
|
|
|
|
|
else if (PARAM_CHARSET.equals(attribute)) {
|
|
|
|
|
value = unquote(value);
|
|
|
|
|
Charset.forName(value);
|
|
|
|
|
}
|
|
|
|
|
else if (!isQuotedString(value)) {
|
|
|
|
|
checkToken(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean isQuotedString(String s) {
|
|
|
|
|
if (s.length() < 2) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return ((s.startsWith("\"") && s.endsWith("\"")) || (s.startsWith("'") && s.endsWith("'")));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String unquote(String s) {
|
|
|
|
|
if (s == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return isQuotedString(s) ? s.substring(1, s.length() - 1) : s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the primary type.
|
|
|
|
|
*/
|
|
|
|
|
public String getType() {
|
|
|
|
|
return this.type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Indicates whether the {@linkplain #getType() type} is the wildcard character {@code *} or not.
|
|
|
|
|
*/
|
|
|
|
|
public boolean isWildcardType() {
|
|
|
|
|
return WILDCARD_TYPE.equals(this.type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the subtype.
|
|
|
|
|
*/
|
|
|
|
|
public String getSubtype() {
|
|
|
|
|
return this.subtype;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Indicates whether the {@linkplain #getSubtype() subtype} is the wildcard character {@code *}
|
|
|
|
|
* or the wildcard character followed by a sufiix (e.g. {@code *+xml}), or not.
|
|
|
|
|
* @return whether the subtype is {@code *}
|
|
|
|
|
*/
|
|
|
|
|
public boolean isWildcardSubtype() {
|
|
|
|
|
return WILDCARD_TYPE.equals(this.subtype) || this.subtype.startsWith("*+");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Indicates whether this media type is concrete, i.e. whether neither the type or subtype is a wildcard
|
|
|
|
|
* character {@code *}.
|
|
|
|
|
* @return whether this media type is concrete
|
|
|
|
|
*/
|
|
|
|
|
public boolean isConcrete() {
|
|
|
|
|
return !isWildcardType() && !isWildcardSubtype();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the character set, as indicated by a {@code charset} parameter, if any.
|
|
|
|
|
* @return the character set; or {@code null} if not available
|
|
|
|
|
*/
|
|
|
|
|
public Charset getCharSet() {
|
|
|
|
|
String charSet = getParameter(PARAM_CHARSET);
|
|
|
|
|
return (charSet != null ? Charset.forName(unquote(charSet)) : null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@@ -450,119 +292,16 @@ public class MediaType implements Comparable<MediaType> {
|
|
|
|
|
return (qualityFactory != null ? Double.parseDouble(unquote(qualityFactory)) : 1D);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a generic parameter value, given a parameter name.
|
|
|
|
|
* @param name the parameter name
|
|
|
|
|
* @return the parameter value; or {@code null} if not present
|
|
|
|
|
*/
|
|
|
|
|
public String getParameter(String name) {
|
|
|
|
|
return this.parameters.get(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return all generic parameter values.
|
|
|
|
|
* @return a read-only map, possibly empty, never {@code null}
|
|
|
|
|
*/
|
|
|
|
|
public Map<String, String> getParameters() {
|
|
|
|
|
return parameters;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Indicate whether this {@code MediaType} includes the given media type.
|
|
|
|
|
* <p>For instance, {@code text/*} includes {@code text/plain} and {@code text/html}, and {@code application/*+xml}
|
|
|
|
|
* includes {@code application/soap+xml}, etc. This method is <b>not</b> symmetric.
|
|
|
|
|
* @param other the reference media type with which to compare
|
|
|
|
|
* @return {@code true} if this media type includes the given media type; {@code false} otherwise
|
|
|
|
|
*/
|
|
|
|
|
public boolean includes(MediaType other) {
|
|
|
|
|
if (other == null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (this.isWildcardType()) {
|
|
|
|
|
// */* includes anything
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (this.type.equals(other.type)) {
|
|
|
|
|
if (this.subtype.equals(other.subtype)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Indicate whether this {@code MediaType} is compatible with the given media type.
|
|
|
|
|
* <p>For instance, {@code text/*} is compatible with {@code text/plain}, {@code text/html}, and vice versa.
|
|
|
|
|
* In effect, this method is similar to {@link #includes(MediaType)}, except that it <b>is</b> symmetric.
|
|
|
|
|
* @param other the reference media type with which to compare
|
|
|
|
|
* @return {@code true} if this media type is compatible with the given media type; {@code false} otherwise
|
|
|
|
|
*/
|
|
|
|
|
public boolean isCompatibleWith(MediaType other) {
|
|
|
|
|
if (other == null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (isWildcardType() || other.isWildcardType()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (this.type.equals(other.type)) {
|
|
|
|
|
if (this.subtype.equals(other.subtype)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a replica of this instance with the quality value of the given MediaType.
|
|
|
|
|
* @return the same instance if the given MediaType doesn't have a quality value, or a new one otherwise
|
|
|
|
|
*/
|
|
|
|
|
public MediaType copyQualityValue(MediaType mediaType) {
|
|
|
|
|
if (!mediaType.parameters.containsKey(PARAM_QUALITY_FACTOR)) {
|
|
|
|
|
if (!mediaType.getParameters().containsKey(PARAM_QUALITY_FACTOR)) {
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
Map<String, String> params = new LinkedHashMap<String, String>(this.parameters);
|
|
|
|
|
params.put(PARAM_QUALITY_FACTOR, mediaType.parameters.get(PARAM_QUALITY_FACTOR));
|
|
|
|
|
Map<String, String> params = new LinkedHashMap<String, String>(getParameters());
|
|
|
|
|
params.put(PARAM_QUALITY_FACTOR, mediaType.getParameters().get(PARAM_QUALITY_FACTOR));
|
|
|
|
|
return new MediaType(this, params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -571,104 +310,14 @@ public class MediaType implements Comparable<MediaType> {
|
|
|
|
|
* @return the same instance if the media type doesn't contain a quality value, or a new one otherwise
|
|
|
|
|
*/
|
|
|
|
|
public MediaType removeQualityValue() {
|
|
|
|
|
if (!this.parameters.containsKey(PARAM_QUALITY_FACTOR)) {
|
|
|
|
|
if (!getParameters().containsKey(PARAM_QUALITY_FACTOR)) {
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
Map<String, String> params = new LinkedHashMap<String, String>(this.parameters);
|
|
|
|
|
Map<String, String> params = new LinkedHashMap<String, String>(getParameters());
|
|
|
|
|
params.remove(PARAM_QUALITY_FACTOR);
|
|
|
|
|
return new MediaType(this, params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Compares this {@code MediaType} to another alphabetically.
|
|
|
|
|
* @param other media type to compare to
|
|
|
|
|
* @see #sortBySpecificity(List)
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public int compareTo(MediaType other) {
|
|
|
|
|
int comp = this.type.compareToIgnoreCase(other.type);
|
|
|
|
|
if (comp != 0) {
|
|
|
|
|
return comp;
|
|
|
|
|
}
|
|
|
|
|
comp = this.subtype.compareToIgnoreCase(other.subtype);
|
|
|
|
|
if (comp != 0) {
|
|
|
|
|
return comp;
|
|
|
|
|
}
|
|
|
|
|
comp = this.parameters.size() - other.parameters.size();
|
|
|
|
|
if (comp != 0) {
|
|
|
|
|
return comp;
|
|
|
|
|
}
|
|
|
|
|
TreeSet<String> thisAttributes = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
|
|
|
|
|
thisAttributes.addAll(this.parameters.keySet());
|
|
|
|
|
TreeSet<String> otherAttributes = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
|
|
|
|
|
otherAttributes.addAll(other.parameters.keySet());
|
|
|
|
|
Iterator<String> thisAttributesIterator = thisAttributes.iterator();
|
|
|
|
|
Iterator<String> 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 = this.parameters.get(thisAttribute);
|
|
|
|
|
String otherValue = other.parameters.get(otherAttribute);
|
|
|
|
|
if (otherValue == null) {
|
|
|
|
|
otherValue = "";
|
|
|
|
|
}
|
|
|
|
|
comp = thisValue.compareTo(otherValue);
|
|
|
|
|
if (comp != 0) {
|
|
|
|
|
return comp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean equals(Object other) {
|
|
|
|
|
if (this == other) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (!(other instanceof MediaType)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
MediaType otherType = (MediaType) other;
|
|
|
|
|
return (this.type.equalsIgnoreCase(otherType.type) && this.subtype.equalsIgnoreCase(otherType.subtype) &&
|
|
|
|
|
this.parameters.equals(otherType.parameters));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private 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 MediaType} object,
|
|
|
|
|
* with this method name following the 'valueOf' naming convention
|
|
|
|
|
@@ -686,48 +335,17 @@ public class MediaType implements Comparable<MediaType> {
|
|
|
|
|
* @throws InvalidMediaTypeException if the string cannot be parsed
|
|
|
|
|
*/
|
|
|
|
|
public static MediaType parseMediaType(String mediaType) {
|
|
|
|
|
if (!StringUtils.hasLength(mediaType)) {
|
|
|
|
|
throw new InvalidMediaTypeException(mediaType, "'mediaType' must not be empty");
|
|
|
|
|
}
|
|
|
|
|
String[] parts = StringUtils.tokenizeToStringArray(mediaType, ";");
|
|
|
|
|
|
|
|
|
|
String fullType = parts[0].trim();
|
|
|
|
|
// java.net.HttpURLConnection returns a *; q=.2 Accept header
|
|
|
|
|
if (WILDCARD_TYPE.equals(fullType)) {
|
|
|
|
|
fullType = "*/*";
|
|
|
|
|
MimeType type;
|
|
|
|
|
try {
|
|
|
|
|
type = MimeTypeUtils.parseMimeType(mediaType);
|
|
|
|
|
}
|
|
|
|
|
int subIndex = fullType.indexOf('/');
|
|
|
|
|
if (subIndex == -1) {
|
|
|
|
|
throw new InvalidMediaTypeException(mediaType, "does not contain '/'");
|
|
|
|
|
}
|
|
|
|
|
if (subIndex == fullType.length() - 1) {
|
|
|
|
|
throw new InvalidMediaTypeException(mediaType, "does not contain subtype after '/'");
|
|
|
|
|
}
|
|
|
|
|
String type = fullType.substring(0, subIndex);
|
|
|
|
|
String subtype = fullType.substring(subIndex + 1, fullType.length());
|
|
|
|
|
if (WILDCARD_TYPE.equals(type) && !WILDCARD_TYPE.equals(subtype)) {
|
|
|
|
|
throw new InvalidMediaTypeException(mediaType, "wildcard type is legal only in '*/*' (all media types)");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Map<String, String> parameters = null;
|
|
|
|
|
if (parts.length > 1) {
|
|
|
|
|
parameters = new LinkedHashMap<String, String>(parts.length - 1);
|
|
|
|
|
for (int i = 1; i < parts.length; i++) {
|
|
|
|
|
String parameter = parts[i];
|
|
|
|
|
int eqIndex = parameter.indexOf('=');
|
|
|
|
|
if (eqIndex != -1) {
|
|
|
|
|
String attribute = parameter.substring(0, eqIndex);
|
|
|
|
|
String value = parameter.substring(eqIndex + 1, parameter.length());
|
|
|
|
|
parameters.put(attribute, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (InvalidMimeTypeException ex) {
|
|
|
|
|
throw new InvalidMediaTypeException(ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
return new MediaType(type, subtype, parameters);
|
|
|
|
|
}
|
|
|
|
|
catch (UnsupportedCharsetException ex) {
|
|
|
|
|
throw new InvalidMediaTypeException(mediaType, "unsupported charset '" + ex.getCharsetName() + "'");
|
|
|
|
|
return new MediaType(type.getType(), type.getSubtype(), type.getParameters());
|
|
|
|
|
}
|
|
|
|
|
catch (IllegalArgumentException ex) {
|
|
|
|
|
throw new InvalidMediaTypeException(mediaType, ex.getMessage());
|
|
|
|
|
@@ -762,15 +380,7 @@ public class MediaType implements Comparable<MediaType> {
|
|
|
|
|
* @throws IllegalArgumentException if the String cannot be parsed
|
|
|
|
|
*/
|
|
|
|
|
public static String toString(Collection<MediaType> mediaTypes) {
|
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
|
for (Iterator<MediaType> iterator = mediaTypes.iterator(); iterator.hasNext();) {
|
|
|
|
|
MediaType mediaType = iterator.next();
|
|
|
|
|
mediaType.appendTo(builder);
|
|
|
|
|
if (iterator.hasNext()) {
|
|
|
|
|
builder.append(", ");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return builder.toString();
|
|
|
|
|
return MimeTypeUtils.toString(mediaTypes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@@ -848,50 +458,6 @@ public class MediaType implements Comparable<MediaType> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Comparator used by {@link #sortBySpecificity(List)}.
|
|
|
|
|
*/
|
|
|
|
|
public static final Comparator<MediaType> SPECIFICITY_COMPARATOR = new Comparator<MediaType>() {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int compare(MediaType mediaType1, MediaType mediaType2) {
|
|
|
|
|
if (mediaType1.isWildcardType() && !mediaType2.isWildcardType()) { // */* < audio/*
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
else if (mediaType2.isWildcardType() && !mediaType1.isWildcardType()) { // audio/* > */*
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
else if (!mediaType1.getType().equals(mediaType2.getType())) { // audio/basic == text/html
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else { // mediaType1.getType().equals(mediaType2.getType())
|
|
|
|
|
if (mediaType1.isWildcardSubtype() && !mediaType2.isWildcardSubtype()) { // audio/* < audio/basic
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
else if (mediaType2.isWildcardSubtype() && !mediaType1.isWildcardSubtype()) { // audio/basic > audio/*
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
else if (!mediaType1.getSubtype().equals(mediaType2.getSubtype())) { // audio/basic == audio/wave
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else { // mediaType2.getSubtype().equals(mediaType2.getSubtype())
|
|
|
|
|
double quality1 = mediaType1.getQualityValue();
|
|
|
|
|
double quality2 = mediaType2.getQualityValue();
|
|
|
|
|
int qualityComparison = Double.compare(quality2, quality1);
|
|
|
|
|
if (qualityComparison != 0) {
|
|
|
|
|
return qualityComparison; // audio/*;q=0.7 < audio/*;q=0.3
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
int paramsSize1 = mediaType1.parameters.size();
|
|
|
|
|
int paramsSize2 = mediaType2.parameters.size();
|
|
|
|
|
return (paramsSize2 < paramsSize1 ? -1 : (paramsSize2 == paramsSize1 ? 0 : 1)); // audio/basic;level=1 < audio/basic
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Comparator used by {@link #sortByQualityValue(List)}.
|
|
|
|
|
*/
|
|
|
|
|
@@ -925,12 +491,29 @@ public class MediaType implements Comparable<MediaType> {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
int paramsSize1 = mediaType1.parameters.size();
|
|
|
|
|
int paramsSize2 = mediaType2.parameters.size();
|
|
|
|
|
int paramsSize1 = mediaType1.getParameters().size();
|
|
|
|
|
int paramsSize2 = mediaType2.getParameters().size();
|
|
|
|
|
return (paramsSize2 < paramsSize1 ? -1 : (paramsSize2 == paramsSize1 ? 0 : 1)); // audio/basic;level=1 < audio/basic
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Comparator used by {@link #sortBySpecificity(List)}.
|
|
|
|
|
*/
|
|
|
|
|
public static final Comparator<MediaType> SPECIFICITY_COMPARATOR = new SpecificityComparator<MediaType>() {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected int compareParameters(MediaType mediaType1, MediaType mediaType2) {
|
|
|
|
|
double quality1 = mediaType1.getQualityValue();
|
|
|
|
|
double quality2 = mediaType2.getQualityValue();
|
|
|
|
|
int qualityComparison = Double.compare(quality2, quality1);
|
|
|
|
|
if (qualityComparison != 0) {
|
|
|
|
|
return qualityComparison; // audio/*;q=0.7 < audio/*;q=0.3
|
|
|
|
|
}
|
|
|
|
|
return super.compareParameters(mediaType1, mediaType2);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|