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:
parent
3d68c496f1
commit
fd1db57e05
|
|
@ -263,14 +263,12 @@ public class MockServletContext implements ServletContext {
|
|||
@Override
|
||||
public String getMimeType(String filePath) {
|
||||
String extension = StringUtils.getFilenameExtension(filePath);
|
||||
MediaType result;
|
||||
if (this.mimeTypes.containsKey(extension)) {
|
||||
result = this.mimeTypes.get(extension);
|
||||
return this.mimeTypes.get(extension).toString();
|
||||
}
|
||||
else {
|
||||
result = MediaTypeFactory.getMediaType(filePath);
|
||||
return MediaTypeFactory.getMediaType(filePath).orElse(MediaType.APPLICATION_OCTET_STREAM).toString();
|
||||
}
|
||||
return result != null ? result.toString() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -103,9 +103,12 @@ public class MediaTypeFactory {
|
|||
* @param resource the resource to introspect
|
||||
* @return the corresponding media type, or {@code null} if none found
|
||||
*/
|
||||
public static MediaType getMediaType(Resource resource) {
|
||||
public static Optional<MediaType> getMediaType(Resource resource) {
|
||||
if (resource == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
String filename = resource.getFilename();
|
||||
return (filename != null ? getMediaType(filename) : null);
|
||||
return (filename != null ? getMediaType(filename) : Optional.empty());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -113,9 +116,9 @@ public class MediaTypeFactory {
|
|||
* @param filename the file name plus extension
|
||||
* @return the corresponding media type, or {@code null} if none found
|
||||
*/
|
||||
public static MediaType getMediaType(String filename) {
|
||||
public static Optional<MediaType> getMediaType(String filename) {
|
||||
List<MediaType> mediaTypes = getMediaTypes(filename);
|
||||
return (!mediaTypes.isEmpty() ? mediaTypes.get(0) : null);
|
||||
return (!mediaTypes.isEmpty() ? Optional.of(mediaTypes.get(0)) : Optional.empty());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -136,8 +136,7 @@ public class ResourceHttpMessageWriter implements ServerHttpMessageWriter<Resour
|
|||
if (type != null && type.isConcrete() && !type.equals(MediaType.APPLICATION_OCTET_STREAM)) {
|
||||
return type;
|
||||
}
|
||||
type = MediaTypeFactory.getMediaType(resource);
|
||||
return type != null ? type : MediaType.APPLICATION_OCTET_STREAM;
|
||||
return MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM);
|
||||
}
|
||||
|
||||
private static OptionalLong lengthOf(Resource resource) {
|
||||
|
|
|
|||
|
|
@ -102,8 +102,7 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
|
|||
|
||||
@Override
|
||||
protected MediaType getDefaultContentType(Resource resource) {
|
||||
MediaType mediaType = MediaTypeFactory.getMediaType(resource);
|
||||
return mediaType != null ? mediaType : MediaType.APPLICATION_OCTET_STREAM;
|
||||
return MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -87,14 +87,7 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa
|
|||
resource = regions.iterator().next().getResource();
|
||||
}
|
||||
}
|
||||
MediaType result = null;
|
||||
if (resource != null) {
|
||||
result = MediaTypeFactory.getMediaType(resource);
|
||||
}
|
||||
if (result == null) {
|
||||
return MediaType.APPLICATION_OCTET_STREAM;
|
||||
}
|
||||
return result;
|
||||
return MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ package org.springframework.web.accept;
|
|||
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
|
|
@ -112,9 +113,9 @@ public class PathExtensionContentNegotiationStrategy extends AbstractMappingCont
|
|||
protected MediaType handleNoMatch(NativeWebRequest webRequest, String extension)
|
||||
throws HttpMediaTypeNotAcceptableException {
|
||||
|
||||
MediaType mediaType = MediaTypeFactory.getMediaType("file." + extension);
|
||||
if (mediaType != null) {
|
||||
return mediaType;
|
||||
Optional<MediaType> mediaType = MediaTypeFactory.getMediaType("file." + extension);
|
||||
if (mediaType.isPresent()) {
|
||||
return mediaType.get();
|
||||
}
|
||||
if (this.ignoreUnknownExtensions) {
|
||||
return null;
|
||||
|
|
@ -140,7 +141,7 @@ public class PathExtensionContentNegotiationStrategy extends AbstractMappingCont
|
|||
mediaType = lookupMediaType(extension);
|
||||
}
|
||||
if (mediaType == null) {
|
||||
mediaType = MediaTypeFactory.getMediaType(filename);
|
||||
mediaType = MediaTypeFactory.getMediaType(filename).orElse(null);
|
||||
}
|
||||
return mediaType;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,9 @@ package org.springframework.http;
|
|||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
|
|
@ -27,9 +29,17 @@ public class MediaTypeFactoryTests {
|
|||
|
||||
@Test
|
||||
public void getMediaType() {
|
||||
assertEquals(MediaType.APPLICATION_XML, MediaTypeFactory.getMediaType("file.xml"));
|
||||
assertEquals(MediaType.parseMediaType("application/javascript"), MediaTypeFactory.getMediaType("file.js"));
|
||||
assertEquals(MediaType.parseMediaType("text/css"), MediaTypeFactory.getMediaType("file.css"));
|
||||
assertEquals(MediaType.APPLICATION_XML, MediaTypeFactory.getMediaType("file.xml").get());
|
||||
assertEquals(MediaType.parseMediaType("application/javascript"), MediaTypeFactory.getMediaType("file.js").get());
|
||||
assertEquals(MediaType.parseMediaType("text/css"), MediaTypeFactory.getMediaType("file.css").get());
|
||||
assertFalse(MediaTypeFactory.getMediaType("file.foobar").isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nullParameter() {
|
||||
assertFalse(MediaTypeFactory.getMediaType((String) null).isPresent());
|
||||
assertFalse(MediaTypeFactory.getMediaType((Resource) null).isPresent());
|
||||
assertTrue(MediaTypeFactory.getMediaTypes(null).isEmpty());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -262,14 +262,12 @@ public class MockServletContext implements ServletContext {
|
|||
@Override
|
||||
public String getMimeType(String filePath) {
|
||||
String extension = StringUtils.getFilenameExtension(filePath);
|
||||
MediaType result;
|
||||
if (this.mimeTypes.containsKey(extension)) {
|
||||
result = this.mimeTypes.get(extension);
|
||||
return this.mimeTypes.get(extension).toString();
|
||||
}
|
||||
else {
|
||||
result = MediaTypeFactory.getMediaType(filePath);
|
||||
return MediaTypeFactory.getMediaType(filePath).orElse(MediaType.APPLICATION_OCTET_STREAM).toString();
|
||||
}
|
||||
return result != null ? result.toString() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue