Skip to content

Commit 0c82562

Browse files
committed
Avoid use of Optional wrapper to get List<MediaType>
See gh-26170
1 parent e7b0b65 commit 0c82562

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

spring-web/src/main/java/org/springframework/http/MediaTypeFactory.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -112,10 +112,12 @@ public static Optional<MediaType> getMediaType(@Nullable String filename) {
112112
* @return the corresponding media types, or an empty list if none found
113113
*/
114114
public static List<MediaType> getMediaTypes(@Nullable String filename) {
115-
return Optional.ofNullable(StringUtils.getFilenameExtension(filename))
116-
.map(s -> s.toLowerCase(Locale.ENGLISH))
117-
.map(fileExtensionToMediaTypes::get)
118-
.orElse(Collections.emptyList());
115+
List<MediaType> mediaTypes = null;
116+
String ext = StringUtils.getFilenameExtension(filename);
117+
if (ext != null) {
118+
mediaTypes = fileExtensionToMediaTypes.get(ext.toLowerCase(Locale.ENGLISH));
119+
}
120+
return (mediaTypes != null ? mediaTypes : Collections.emptyList());
119121
}
120122

121123
}

spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,10 @@ protected MediaType getMediaType(HttpServletRequest request, Resource resource)
736736
mediaType = this.mediaTypes.get(ext.toLowerCase(Locale.ENGLISH));
737737
}
738738
if (mediaType == null) {
739-
mediaType = MediaTypeFactory.getMediaType(filename).orElse(null);
739+
List<MediaType> mediaTypes = MediaTypeFactory.getMediaTypes(filename);
740+
if (!CollectionUtils.isEmpty(mediaTypes)) {
741+
mediaType = mediaTypes.get(0);
742+
}
740743
}
741744
if (mediaType != null) {
742745
result = mediaType;

0 commit comments

Comments
 (0)