|
|
|
|
@ -47,6 +47,11 @@ public abstract class MimeTypeUtils {
@@ -47,6 +47,11 @@ public abstract class MimeTypeUtils {
|
|
|
|
|
|
|
|
|
|
private static final Random RND = new Random(); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Comparator used by {@link #sortBySpecificity(List)}. |
|
|
|
|
*/ |
|
|
|
|
public static final Comparator<MimeType> SPECIFICITY_COMPARATOR = new SpecificityComparator<>(); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Public constant mime type that includes all media ranges (i.e. "*/*"). |
|
|
|
|
*/ |
|
|
|
|
@ -216,12 +221,13 @@ public abstract class MimeTypeUtils {
@@ -216,12 +221,13 @@ public abstract class MimeTypeUtils {
|
|
|
|
|
if (!StringUtils.hasLength(mimeType)) { |
|
|
|
|
throw new InvalidMimeTypeException(mimeType, "'mimeType' must not be empty"); |
|
|
|
|
} |
|
|
|
|
String[] parts = StringUtils.tokenizeToStringArray(mimeType, ";"); |
|
|
|
|
if (parts.length == 0) { |
|
|
|
|
|
|
|
|
|
int index = mimeType.indexOf(';'); |
|
|
|
|
String fullType = (index >= 0 ? mimeType.substring(0, index) : mimeType).trim(); |
|
|
|
|
if (fullType.length() == 0) { |
|
|
|
|
throw new InvalidMimeTypeException(mimeType, "'mimeType' must not be empty"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
String fullType = parts[0].trim(); |
|
|
|
|
// java.net.HttpURLConnection returns a *; q=.2 Accept header
|
|
|
|
|
if (MimeType.WILDCARD_TYPE.equals(fullType)) { |
|
|
|
|
fullType = "*/*"; |
|
|
|
|
@ -240,18 +246,36 @@ public abstract class MimeTypeUtils {
@@ -240,18 +246,36 @@ public abstract class MimeTypeUtils {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Map<String, String> parameters = null; |
|
|
|
|
if (parts.length > 1) { |
|
|
|
|
parameters = new LinkedHashMap<>(parts.length - 1); |
|
|
|
|
for (int i = 1; i < parts.length; i++) { |
|
|
|
|
String parameter = parts[i]; |
|
|
|
|
do { |
|
|
|
|
int nextIndex = index + 1; |
|
|
|
|
boolean quoted = false; |
|
|
|
|
while (nextIndex < mimeType.length()) { |
|
|
|
|
char ch = mimeType.charAt(nextIndex); |
|
|
|
|
if (ch == ';') { |
|
|
|
|
if (!quoted) { |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else if (ch == '"') { |
|
|
|
|
quoted = !quoted; |
|
|
|
|
} |
|
|
|
|
nextIndex++; |
|
|
|
|
} |
|
|
|
|
String parameter = mimeType.substring(index + 1, nextIndex).trim(); |
|
|
|
|
if (parameter.length() > 0) { |
|
|
|
|
if (parameters == null) { |
|
|
|
|
parameters = new LinkedHashMap<>(4); |
|
|
|
|
} |
|
|
|
|
int eqIndex = parameter.indexOf('='); |
|
|
|
|
if (eqIndex != -1) { |
|
|
|
|
if (eqIndex >= 0) { |
|
|
|
|
String attribute = parameter.substring(0, eqIndex); |
|
|
|
|
String value = parameter.substring(eqIndex + 1, parameter.length()); |
|
|
|
|
parameters.put(attribute, value); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
index = nextIndex; |
|
|
|
|
} |
|
|
|
|
while (index < mimeType.length()); |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
return new MimeType(type, subtype, parameters); |
|
|
|
|
@ -350,11 +374,4 @@ public abstract class MimeTypeUtils {
@@ -350,11 +374,4 @@ public abstract class MimeTypeUtils {
|
|
|
|
|
return new String(generateMultipartBoundary(), StandardCharsets.US_ASCII); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Comparator used by {@link #sortBySpecificity(List)}. |
|
|
|
|
*/ |
|
|
|
|
public static final Comparator<MimeType> SPECIFICITY_COMPARATOR = new SpecificityComparator<>(); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|