Browse Source

Avoid use of Optional wrapper to get List<MediaType>

See gh-26170
pull/26197/head
Rossen Stoyanchev 5 years ago
parent
commit
0c825621b8
  1. 12
      spring-web/src/main/java/org/springframework/http/MediaTypeFactory.java
  2. 5
      spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -112,10 +112,12 @@ public final class MediaTypeFactory { @@ -112,10 +112,12 @@ public final class MediaTypeFactory {
* @return the corresponding media types, or an empty list if none found
*/
public static List<MediaType> getMediaTypes(@Nullable String filename) {
return Optional.ofNullable(StringUtils.getFilenameExtension(filename))
.map(s -> s.toLowerCase(Locale.ENGLISH))
.map(fileExtensionToMediaTypes::get)
.orElse(Collections.emptyList());
List<MediaType> mediaTypes = null;
String ext = StringUtils.getFilenameExtension(filename);
if (ext != null) {
mediaTypes = fileExtensionToMediaTypes.get(ext.toLowerCase(Locale.ENGLISH));
}
return (mediaTypes != null ? mediaTypes : Collections.emptyList());
}
}

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

@ -736,7 +736,10 @@ public class ResourceHttpRequestHandler extends WebContentGenerator @@ -736,7 +736,10 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
mediaType = this.mediaTypes.get(ext.toLowerCase(Locale.ENGLISH));
}
if (mediaType == null) {
mediaType = MediaTypeFactory.getMediaType(filename).orElse(null);
List<MediaType> mediaTypes = MediaTypeFactory.getMediaTypes(filename);
if (!CollectionUtils.isEmpty(mediaTypes)) {
mediaType = mediaTypes.get(0);
}
}
if (mediaType != null) {
result = mediaType;

Loading…
Cancel
Save