Support for MediaType mappings in ResourceWebHandler
Closes gh-26170
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -18,12 +18,16 @@ package org.springframework.web.reactive.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.http.CacheControl;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.MediaTypeFactory;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.reactive.resource.ResourceWebHandler;
|
||||
@@ -50,6 +54,10 @@ public class ResourceHandlerRegistration {
|
||||
|
||||
private boolean useLastModified = true;
|
||||
|
||||
@Nullable
|
||||
private Map<String, MediaType> mediaTypes;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Create a {@link ResourceHandlerRegistration} instance.
|
||||
@@ -146,6 +154,23 @@ public class ResourceHandlerRegistration {
|
||||
return this.resourceChainRegistration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add mappings between file extensions extracted from the filename of static
|
||||
* {@link Resource}s and the media types to use for the response.
|
||||
* <p>Use of this method is typically not necessary since mappings can be
|
||||
* also determined via {@link MediaTypeFactory#getMediaType(Resource)}.
|
||||
* @param mediaTypes media type mappings
|
||||
* @since 5.3.2
|
||||
*/
|
||||
public void setMediaTypes(Map<String, MediaType> mediaTypes) {
|
||||
if (this.mediaTypes == null) {
|
||||
this.mediaTypes = new HashMap<>(mediaTypes.size());
|
||||
}
|
||||
this.mediaTypes.clear();
|
||||
this.mediaTypes.putAll(mediaTypes);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the URL path patterns for the resource handler.
|
||||
*/
|
||||
@@ -168,6 +193,9 @@ public class ResourceHandlerRegistration {
|
||||
handler.setCacheControl(this.cacheControl);
|
||||
}
|
||||
handler.setUseLastModified(this.useLastModified);
|
||||
if (this.mediaTypes != null) {
|
||||
handler.setMediaTypes(this.mediaTypes);
|
||||
}
|
||||
return handler;
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,10 @@ import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -111,6 +114,9 @@ public class ResourceWebHandler implements WebHandler, InitializingBean {
|
||||
@Nullable
|
||||
private ResourceHttpMessageWriter resourceHttpMessageWriter;
|
||||
|
||||
@Nullable
|
||||
private Map<String, MediaType> mediaTypes;
|
||||
|
||||
@Nullable
|
||||
private ResourceLoader resourceLoader;
|
||||
|
||||
@@ -230,6 +236,30 @@ public class ResourceWebHandler implements WebHandler, InitializingBean {
|
||||
return this.resourceHttpMessageWriter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add mappings between file extensions extracted from the filename of static
|
||||
* {@link Resource}s and the media types to use for the response.
|
||||
* <p>Use of this method is typically not necessary since mappings can be
|
||||
* also determined via {@link MediaTypeFactory#getMediaType(Resource)}.
|
||||
* @param mediaTypes media type mappings
|
||||
* @since 5.3.2
|
||||
*/
|
||||
public void setMediaTypes(Map<String, MediaType> mediaTypes) {
|
||||
if (this.mediaTypes == null) {
|
||||
this.mediaTypes = new HashMap<>(mediaTypes.size());
|
||||
}
|
||||
mediaTypes.forEach((ext, type) ->
|
||||
this.mediaTypes.put(ext.toLowerCase(Locale.ENGLISH), type));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link #setMediaTypes(Map) configured} media type mappings.
|
||||
* @since 5.3.2
|
||||
*/
|
||||
public Map<String, MediaType> getMediaTypes() {
|
||||
return (this.mediaTypes != null ? this.mediaTypes : Collections.emptyMap());
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide the ResourceLoader to load {@link #setLocationValues(List)
|
||||
* location values} with.
|
||||
@@ -374,7 +404,7 @@ public class ResourceWebHandler implements WebHandler, InitializingBean {
|
||||
}
|
||||
|
||||
// Check the media type for the resource
|
||||
MediaType mediaType = MediaTypeFactory.getMediaType(resource).orElse(null);
|
||||
MediaType mediaType = getMediaType(resource);
|
||||
setHeaders(exchange, resource, mediaType);
|
||||
|
||||
// Content phase
|
||||
@@ -535,6 +565,25 @@ public class ResourceWebHandler implements WebHandler, InitializingBean {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private MediaType getMediaType(Resource resource) {
|
||||
MediaType mediaType = null;
|
||||
String filename = resource.getFilename();
|
||||
if (!CollectionUtils.isEmpty(this.mediaTypes)) {
|
||||
String ext = StringUtils.getFilenameExtension(filename);
|
||||
if (ext != null) {
|
||||
mediaType = this.mediaTypes.get(ext.toLowerCase(Locale.ENGLISH));
|
||||
}
|
||||
}
|
||||
if (mediaType == null) {
|
||||
List<MediaType> mediaTypes = MediaTypeFactory.getMediaTypes(filename);
|
||||
if (!CollectionUtils.isEmpty(mediaTypes)) {
|
||||
mediaType = mediaTypes.get(0);
|
||||
}
|
||||
}
|
||||
return mediaType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set headers on the response. Called for both GET and HEAD requests.
|
||||
* @param exchange current exchange
|
||||
|
||||
Reference in New Issue
Block a user