public class PathExtensionContentNegotiationStrategy extends AbstractMappingContentNegotiationStrategy
ContentNegotiationStrategy
that resolves the file extension in the
request path to a key to be used to look up a media type.
If the file extension is not found in the explicit registrations provided to the constructor, the Java Activation Framework (JAF) is used as a fallback mechanism.
The presence of the JAF is detected and enabled automatically but the
setUseJaf(boolean)
property may be set to false.
Modifier and Type | Class and Description |
---|---|
private static class |
PathExtensionContentNegotiationStrategy.ActivationMediaTypeFactory
Inner class to avoid hard-coded dependency on JAF.
|
Modifier and Type | Field and Description |
---|---|
private boolean |
ignoreUnknownExtensions |
private static boolean |
JAF_PRESENT |
private static Log |
logger |
private UrlPathHelper |
urlPathHelper |
private boolean |
useJaf |
Constructor and Description |
---|
PathExtensionContentNegotiationStrategy()
Create an instance without any mappings to start with.
|
PathExtensionContentNegotiationStrategy(java.util.Map<java.lang.String,MediaType> mediaTypes)
Create an instance with the given map of file extensions and media types.
|
Modifier and Type | Method and Description |
---|---|
MediaType |
getMediaTypeForResource(Resource resource)
|
protected java.lang.String |
getMediaTypeKey(NativeWebRequest webRequest)
Extract a key from the request to use to look up media types.
|
protected MediaType |
handleNoMatch(NativeWebRequest webRequest,
java.lang.String extension)
Override to provide handling when a key is not resolved via.
|
void |
setIgnoreUnknownExtensions(boolean ignoreUnknownExtensions)
Whether to ignore requests with unknown file extension.
|
void |
setUrlPathHelper(UrlPathHelper urlPathHelper)
Configure a
UrlPathHelper to use in getMediaTypeKey(org.springframework.web.context.request.NativeWebRequest)
in order to derive the lookup path for a target request URL path. |
void |
setUseJaf(boolean useJaf)
Whether to use the Java Activation Framework to look up file extensions.
|
handleMatch, resolveMediaTypeKey, resolveMediaTypes
addMapping, getAllFileExtensions, getAllMediaTypes, getMediaTypes, lookupMediaType, resolveFileExtensions
private static final boolean JAF_PRESENT
private static final Log logger
private UrlPathHelper urlPathHelper
private boolean useJaf
private boolean ignoreUnknownExtensions
public PathExtensionContentNegotiationStrategy()
public PathExtensionContentNegotiationStrategy(java.util.Map<java.lang.String,MediaType> mediaTypes)
public void setUrlPathHelper(UrlPathHelper urlPathHelper)
UrlPathHelper
to use in getMediaTypeKey(org.springframework.web.context.request.NativeWebRequest)
in order to derive the lookup path for a target request URL path.public void setUseJaf(boolean useJaf)
By default this is set to "true" but depends on JAF being present.
public void setIgnoreUnknownExtensions(boolean ignoreUnknownExtensions)
false
results in HttpMediaTypeNotAcceptableException
.
By default this is set to true
.
protected java.lang.String getMediaTypeKey(NativeWebRequest webRequest)
AbstractMappingContentNegotiationStrategy
getMediaTypeKey
in class AbstractMappingContentNegotiationStrategy
null
.protected MediaType handleNoMatch(NativeWebRequest webRequest, java.lang.String extension) throws HttpMediaTypeNotAcceptableException
AbstractMappingContentNegotiationStrategy
MappingMediaTypeFileExtensionResolver.lookupMediaType(java.lang.String)
. Sub-classes can take further steps to
determine the media type(s). If a MediaType is returned from
this method it will be added to the cache in the base class.handleNoMatch
in class AbstractMappingContentNegotiationStrategy
HttpMediaTypeNotAcceptableException
public MediaType getMediaTypeForResource(Resource resource)
MediaType
in this case for a given
Resource
. The method first looks up any explicitly registered
file extensions first and then falls back on JAF if available.resource
- the resource to look upnull
if none found