Change MediaTypeFactory to return Optional

This commit changes the `MediaTypeFactory` to return
`Optional<MediaType>` (instead of a plain `MediaType`) for the
`getMediaType` methods.

Issue: SPR-14908
This commit is contained in:
Arjen Poutsma
2017-03-23 10:14:43 +01:00
parent 3d68c496f1
commit fd1db57e05
9 changed files with 38 additions and 36 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.web.reactive.accept;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
@@ -79,9 +80,9 @@ public class PathExtensionContentTypeResolver extends AbstractMappingContentType
@Override
protected MediaType handleNoMatch(String key) throws NotAcceptableStatusException {
MediaType mediaType = MediaTypeFactory.getMediaType("file." + key);
if (mediaType != null) {
return mediaType;
Optional<MediaType> mediaType = MediaTypeFactory.getMediaType("file." + key);
if (mediaType.isPresent()) {
return mediaType.get();
}
if (!this.ignoreUnknownExtensions) {
throw new NotAcceptableStatusException(getAllMediaTypes());
@@ -105,7 +106,7 @@ public class PathExtensionContentTypeResolver extends AbstractMappingContentType
mediaType = getMediaType(extension);
}
if (mediaType == null) {
mediaType = MediaTypeFactory.getMediaType(filename);
mediaType = MediaTypeFactory.getMediaType(filename).orElse(null);
}
return mediaType;
}