Package org.springframework.web.accept
Class PathExtensionContentNegotiationStrategy
- java.lang.Object
- org.springframework.web.accept.MappingMediaTypeFileExtensionResolver
- org.springframework.web.accept.AbstractMappingContentNegotiationStrategy
- org.springframework.web.accept.PathExtensionContentNegotiationStrategy
- All Implemented Interfaces:
ContentNegotiationStrategy
,MediaTypeFileExtensionResolver
- Direct Known Subclasses:
ServletPathExtensionContentNegotiationStrategy
public class PathExtensionContentNegotiationStrategy extends AbstractMappingContentNegotiationStrategy
AContentNegotiationStrategy
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.- Since:
- 3.2
- Author:
- Rossen Stoyanchev
Constructor Summary
Constructors Constructor Description PathExtensionContentNegotiationStrategy()
Create an instance without any mappings to start with.PathExtensionContentNegotiationStrategy(Map<String,MediaType> mediaTypes)
Create an instance with the given map of file extensions and media types.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description MediaType
getMediaTypeForResource(Resource resource)
protected String
getMediaTypeKey(NativeWebRequest webRequest)
Extract a key from the request to use to look up media types.protected MediaType
handleNoMatch(NativeWebRequest webRequest, 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 aUrlPathHelper
to use ingetMediaTypeKey(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.Methods inherited from class org.springframework.web.accept.AbstractMappingContentNegotiationStrategy
handleMatch, resolveMediaTypeKey, resolveMediaTypes
Methods inherited from class org.springframework.web.accept.MappingMediaTypeFileExtensionResolver
addMapping, getAllFileExtensions, getAllMediaTypes, getMediaTypes, lookupMediaType, resolveFileExtensions
Constructor Detail
PathExtensionContentNegotiationStrategy
public PathExtensionContentNegotiationStrategy()
Create an instance without any mappings to start with. Mappings may be added later on if any extensions are resolved through the Java Activation framework.
PathExtensionContentNegotiationStrategy
public PathExtensionContentNegotiationStrategy(Map<String,MediaType> mediaTypes)
Create an instance with the given map of file extensions and media types.
Method Detail
setUrlPathHelper
public void setUrlPathHelper(UrlPathHelper urlPathHelper)
Configure aUrlPathHelper
to use ingetMediaTypeKey(org.springframework.web.context.request.NativeWebRequest)
in order to derive the lookup path for a target request URL path.- Since:
- 4.2.8
setUseJaf
public void setUseJaf(boolean useJaf)
Whether to use the Java Activation Framework to look up file extensions.By default this is set to "true" but depends on JAF being present.
setIgnoreUnknownExtensions
public void setIgnoreUnknownExtensions(boolean ignoreUnknownExtensions)
Whether to ignore requests with unknown file extension. Setting this tofalse
results inHttpMediaTypeNotAcceptableException
.By default this is set to
true
.
getMediaTypeKey
protected String getMediaTypeKey(NativeWebRequest webRequest)
Description copied from class:AbstractMappingContentNegotiationStrategy
Extract a key from the request to use to look up media types.- Specified by:
getMediaTypeKey
in classAbstractMappingContentNegotiationStrategy
- Returns:
- the lookup key, or
null
if none
handleNoMatch
protected MediaType handleNoMatch(NativeWebRequest webRequest, String extension) throws HttpMediaTypeNotAcceptableException
Description copied from class:AbstractMappingContentNegotiationStrategy
Override to provide handling when a key is not resolved via.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.- Overrides:
handleNoMatch
in classAbstractMappingContentNegotiationStrategy
- Throws:
HttpMediaTypeNotAcceptableException
getMediaTypeForResource
public MediaType getMediaTypeForResource(Resource resource)
A public method exposing the knowledge of the path extension strategy to resolve file extensions to aMediaType
in this case for a givenResource
. The method first looks up any explicitly registered file extensions first and then falls back on JAF if available.- Parameters:
resource
- the resource to look up- Returns:
- the MediaType for the extension, or
null
if none found - Since:
- 4.3