Refactor media types parsing improvements
Issue: SPR-17459
This commit is contained in:
@@ -28,6 +28,7 @@ import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.MimeType.SpecificityComparator;
|
||||
@@ -249,32 +250,53 @@ public abstract class MimeTypeUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the given, comma-separated string into a list of {@code MimeType} objects.
|
||||
* Parse the comma-separated string into a list of {@code MimeType} objects.
|
||||
* @param mimeTypes the string to parse
|
||||
* @return the list of mime types
|
||||
* @throws IllegalArgumentException if the string cannot be parsed
|
||||
* @throws InvalidMimeTypeException if the string cannot be parsed
|
||||
*/
|
||||
public static List<MimeType> parseMimeTypes(String mimeTypes) {
|
||||
if (!StringUtils.hasLength(mimeTypes)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
boolean isQuoted = false;
|
||||
int nextBeginIndex = 0;
|
||||
List<MimeType> tokens = new ArrayList<>();
|
||||
for(int i = 0; i < mimeTypes.length() - 1; i++) {
|
||||
//tokenizing on commas that are not within double quotes
|
||||
if(mimeTypes.charAt(i) == ',' && !isQuoted) {
|
||||
tokens.add(parseMimeType(mimeTypes.substring(nextBeginIndex,i)));
|
||||
nextBeginIndex = i + 1;
|
||||
//ignoring escaped double quote within double quotes
|
||||
} else if(isQuoted && mimeTypes.charAt(i) == '"' && mimeTypes.charAt(i-1) == '\\') {
|
||||
continue;
|
||||
} else if(mimeTypes.charAt(i) == '"') {
|
||||
isQuoted = !isQuoted;
|
||||
}
|
||||
return tokenize(mimeTypes).stream()
|
||||
.map(MimeTypeUtils::parseMimeType).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tokenize the given comma-separated string of {@code MimeType} objects
|
||||
* into a {@code List<String>}. Unlike simple tokenization by ",", this
|
||||
* method takes into account quoted parameters.
|
||||
* @param mimeTypes the string to tokenize
|
||||
* @return the list of tokens
|
||||
* @since 5.1.3
|
||||
*/
|
||||
public static List<String> tokenize(String mimeTypes) {
|
||||
if (!StringUtils.hasLength(mimeTypes)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
//either the last part of the tokenization or the original string
|
||||
tokens.add(parseMimeType(mimeTypes.substring(nextBeginIndex)));
|
||||
List<String> tokens = new ArrayList<>();
|
||||
boolean inQuotes = false;
|
||||
int startIndex = 0;
|
||||
int i = 0;
|
||||
while (i < mimeTypes.length()) {
|
||||
switch (mimeTypes.charAt(i)) {
|
||||
case '"':
|
||||
inQuotes = !inQuotes;
|
||||
break;
|
||||
case ',':
|
||||
if (!inQuotes) {
|
||||
tokens.add(mimeTypes.substring(startIndex, i));
|
||||
startIndex = i + 1;
|
||||
}
|
||||
break;
|
||||
case '\\':
|
||||
i++;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
tokens.add(mimeTypes.substring(startIndex));
|
||||
return tokens;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user