Browse Source

Restore StringUtils.hasLength check

Update `MimeTypeUtils` so that the  StringUtils.hasLength check is
performed immediately on the incoming argument, rather than in
`parseMimeTypeInternal`. This restores the `IllegalArgumentException`
rather than the `NullPointerException` which is thrown by the
`ConcurrentHashMap`.

Closes gh-23215
See gh-23211
pull/23223/head
Phillip Webb 7 years ago
parent
commit
b3d56ebf3b
  1. 7
      spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java
  2. 6
      spring-core/src/test/java/org/springframework/util/MimeTypeTests.java

7
spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java

@ -189,14 +189,13 @@ public abstract class MimeTypeUtils { @@ -189,14 +189,13 @@ public abstract class MimeTypeUtils {
* @throws InvalidMimeTypeException if the string cannot be parsed
*/
public static MimeType parseMimeType(String mimeType) {
return cachedMimeTypes.get(mimeType);
}
private static MimeType parseMimeTypeInternal(String mimeType) {
if (!StringUtils.hasLength(mimeType)) {
throw new InvalidMimeTypeException(mimeType, "'mimeType' must not be empty");
}
return cachedMimeTypes.get(mimeType);
}
private static MimeType parseMimeTypeInternal(String mimeType) {
int index = mimeType.indexOf(';');
String fullType = (index >= 0 ? mimeType.substring(0, index) : mimeType).trim();
if (fullType.isEmpty()) {

6
spring-core/src/test/java/org/springframework/util/MimeTypeTests.java

@ -285,6 +285,12 @@ public class MimeTypeTests { @@ -285,6 +285,12 @@ public class MimeTypeTests {
MimeTypeUtils.parseMimeType("audio/*;attr=\""));
}
@Test
public void parseMimeTypeNull() {
assertThatExceptionOfType(InvalidMimeTypeException.class).isThrownBy(() ->
MimeTypeUtils.parseMimeType(null));
}
@Test
public void parseMimeTypes() {
String s = "text/plain, text/html, text/x-dvi, text/x-c";

Loading…
Cancel
Save