Remove JAF references

This commit updates the main code base to conform to the dropped JAF
dependency in MediaTypeFactory. Specifically, it

 - Removes JAF detection (JAF_PRESENT constants)
 - Deprecated useJaf properties, with no direct replacement.
 - Updated docs to remove JAF references, in favor of MediaTypeFactory.

Issue: SPR-14908
This commit is contained in:
Arjen Poutsma 2017-03-14 14:56:01 +01:00 committed by Rossen Stoyanchev
parent f0a43e5d5c
commit 0aaa6528dc
9 changed files with 67 additions and 136 deletions

View File

@ -27,16 +27,14 @@ import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage; import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory; import org.springframework.http.MediaTypeFactory;
import org.springframework.util.ClassUtils;
import org.springframework.util.StreamUtils; import org.springframework.util.StreamUtils;
/** /**
* Implementation of {@link HttpMessageConverter} that can read/write {@link Resource Resources} * Implementation of {@link HttpMessageConverter} that can read/write {@link Resource Resources}
* and supports byte range requests. * and supports byte range requests.
* *
* <p>By default, this converter can read all media types. The Java Activation Framework (JAF) - * <p>By default, this converter can read all media types. The {@link MediaTypeFactory} is used
* if available - is used to determine the {@code Content-Type} of written resources. * to determine the {@code Content-Type} of written resources.
* If JAF is not available, {@code application/octet-stream} is used.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @author Juergen Hoeller * @author Juergen Hoeller
@ -45,9 +43,6 @@ import org.springframework.util.StreamUtils;
*/ */
public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<Resource> { public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<Resource> {
private static final boolean jafPresent = ClassUtils.isPresent(
"javax.activation.FileTypeMap", ResourceHttpMessageConverter.class.getClassLoader());
private final boolean supportsReadStreaming; private final boolean supportsReadStreaming;
@ -107,12 +102,8 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
@Override @Override
protected MediaType getDefaultContentType(Resource resource) { protected MediaType getDefaultContentType(Resource resource) {
if (jafPresent) { MediaType mediaType = MediaTypeFactory.getMediaType(resource);
return MediaTypeFactory.getMediaType(resource); return mediaType != null ? mediaType : MediaType.APPLICATION_OCTET_STREAM;
}
else {
return MediaType.APPLICATION_OCTET_STREAM;
}
} }
@Override @Override

View File

@ -24,6 +24,7 @@ import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Collection; import java.util.Collection;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourceRegion; import org.springframework.core.io.support.ResourceRegion;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpInputMessage;
@ -31,7 +32,6 @@ import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory; import org.springframework.http.MediaTypeFactory;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.MimeTypeUtils; import org.springframework.util.MimeTypeUtils;
import org.springframework.util.StreamUtils; import org.springframework.util.StreamUtils;
@ -44,9 +44,6 @@ import org.springframework.util.StreamUtils;
*/ */
public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessageConverter<Object> { public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessageConverter<Object> {
private static final boolean jafPresent = ClassUtils.isPresent(
"javax.activation.FileTypeMap", ResourceHttpMessageConverter.class.getClassLoader());
public ResourceRegionHttpMessageConverter() { public ResourceRegionHttpMessageConverter() {
super(MediaType.ALL); super(MediaType.ALL);
} }
@ -80,18 +77,24 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected MediaType getDefaultContentType(Object object) { protected MediaType getDefaultContentType(Object object) {
if (jafPresent) { Resource resource = null;
if(object instanceof ResourceRegion) { if (object instanceof ResourceRegion) {
return MediaTypeFactory.getMediaType(((ResourceRegion) object).getResource()); resource = ((ResourceRegion) object).getResource();
} }
else { else {
Collection<ResourceRegion> regions = (Collection<ResourceRegion>) object; Collection<ResourceRegion> regions = (Collection<ResourceRegion>) object;
if(regions.size() > 0) { if (regions.size() > 0) {
return MediaTypeFactory.getMediaType(regions.iterator().next().getResource()); resource = regions.iterator().next().getResource();
}
} }
} }
return MediaType.APPLICATION_OCTET_STREAM; MediaType result = null;
if (resource != null) {
result = MediaTypeFactory.getMediaType(resource);
}
if (result == null) {
return MediaType.APPLICATION_OCTET_STREAM;
}
return result;
} }
@Override @Override

View File

@ -143,10 +143,9 @@ public class ContentNegotiationManager implements ContentNegotiationStrategy, Me
* <p>At startup this method returns extensions explicitly registered with * <p>At startup this method returns extensions explicitly registered with
* either {@link PathExtensionContentNegotiationStrategy} or * either {@link PathExtensionContentNegotiationStrategy} or
* {@link ParameterContentNegotiationStrategy}. At runtime if there is a * {@link ParameterContentNegotiationStrategy}. At runtime if there is a
* "path extension" strategy and its * "path extension" strategy, the list of extensions may
* {@link PathExtensionContentNegotiationStrategy#setUseJaf(boolean) * increase as file extensions are resolved via
* useJaf} property is set to "true", the list of extensions may * {@link org.springframework.http.MediaTypeFactory} and cached.
* increase as file extensions are resolved via JAF and cached.
*/ */
@Override @Override
public List<String> getAllFileExtensions() { public List<String> getAllFileExtensions() {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -28,6 +28,7 @@ import javax.servlet.ServletContext;
import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.web.context.ServletContextAware; import org.springframework.web.context.ServletContextAware;
@ -81,8 +82,7 @@ import org.springframework.web.context.ServletContextAware;
* "application/json". * "application/json".
* *
* <p>The path extension strategy will also use {@link ServletContext#getMimeType} * <p>The path extension strategy will also use {@link ServletContext#getMimeType}
* and the Java Activation framework (JAF), if available, to resolve a path * and {@link MediaTypeFactory} to resolve a path extension to a MediaType.
* extension to a MediaType. You may {@link #setUseJaf suppress} the use of JAF.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 3.2 * @since 3.2
@ -100,8 +100,6 @@ public class ContentNegotiationManagerFactoryBean
private boolean ignoreUnknownPathExtensions = true; private boolean ignoreUnknownPathExtensions = true;
private Boolean useJaf;
private String parameterName = "format"; private String parameterName = "format";
private ContentNegotiationStrategy defaultNegotiationStrategy; private ContentNegotiationStrategy defaultNegotiationStrategy;
@ -130,8 +128,8 @@ public class ContentNegotiationManagerFactoryBean
* (see Spring Framework reference documentation for more details on RFD * (see Spring Framework reference documentation for more details on RFD
* attack protection). * attack protection).
* <p>The path extension strategy will also try to use * <p>The path extension strategy will also try to use
* {@link ServletContext#getMimeType} and JAF (if present) to resolve path * {@link ServletContext#getMimeType} and
* extensions. To change this behavior see the {@link #useJaf} property. * {@link org.springframework.http.MediaTypeFactory} to resolve path extensions.
* @param mediaTypes media type mappings * @param mediaTypes media type mappings
* @see #addMediaType(String, MediaType) * @see #addMediaType(String, MediaType)
* @see #addMediaTypes(Map) * @see #addMediaTypes(Map)
@ -177,18 +175,10 @@ public class ContentNegotiationManagerFactoryBean
} }
/** /**
* When {@link #setFavorPathExtension favorPathExtension} is set, this * @deprecated as 5.0, in favor of {@link MediaTypeFactory}, which has no JAF dependency.
* property determines whether to allow use of JAF (Java Activation Framework)
* to resolve a path extension to a specific MediaType.
* <p>By default this is not set in which case
* {@code PathExtensionContentNegotiationStrategy} will use JAF if available.
*/ */
@Deprecated
public void setUseJaf(boolean useJaf) { public void setUseJaf(boolean useJaf) {
this.useJaf = useJaf;
}
private boolean isUseJafTurnedOff() {
return (this.useJaf != null && !this.useJaf);
} }
/** /**
@ -254,7 +244,7 @@ public class ContentNegotiationManagerFactoryBean
if (this.favorPathExtension) { if (this.favorPathExtension) {
PathExtensionContentNegotiationStrategy strategy; PathExtensionContentNegotiationStrategy strategy;
if (this.servletContext != null && !isUseJafTurnedOff()) { if (this.servletContext != null) {
strategy = new ServletPathExtensionContentNegotiationStrategy( strategy = new ServletPathExtensionContentNegotiationStrategy(
this.servletContext, this.mediaTypes); this.servletContext, this.mediaTypes);
} }
@ -262,9 +252,6 @@ public class ContentNegotiationManagerFactoryBean
strategy = new PathExtensionContentNegotiationStrategy(this.mediaTypes); strategy = new PathExtensionContentNegotiationStrategy(this.mediaTypes);
} }
strategy.setIgnoreUnknownExtensions(this.ignoreUnknownPathExtensions); strategy.setIgnoreUnknownExtensions(this.ignoreUnknownPathExtensions);
if (this.useJaf != null) {
strategy.setUseJaf(this.useJaf);
}
strategies.add(strategy); strategies.add(strategy);
} }

View File

@ -27,7 +27,6 @@ import org.springframework.core.io.Resource;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory; import org.springframework.http.MediaTypeFactory;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.HttpMediaTypeNotAcceptableException; import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.context.request.NativeWebRequest;
@ -39,26 +38,18 @@ import org.springframework.web.util.UrlPathHelper;
* request path to a key to be used to look up a media type. * request path to a key to be used to look up a media type.
* *
* <p>If the file extension is not found in the explicit registrations provided * <p>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 * to the constructor, the {@link MediaTypeFactory} is used as a fallback
* mechanism. * mechanism.
* *
* <p>The presence of the JAF is detected and enabled automatically but the
* {@link #setUseJaf(boolean)} property may be set to false.
*
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 3.2 * @since 3.2
*/ */
public class PathExtensionContentNegotiationStrategy extends AbstractMappingContentNegotiationStrategy { public class PathExtensionContentNegotiationStrategy extends AbstractMappingContentNegotiationStrategy {
private static final boolean JAF_PRESENT = ClassUtils.isPresent("javax.activation.FileTypeMap",
PathExtensionContentNegotiationStrategy.class.getClassLoader());
private static final Log logger = LogFactory.getLog(PathExtensionContentNegotiationStrategy.class); private static final Log logger = LogFactory.getLog(PathExtensionContentNegotiationStrategy.class);
private UrlPathHelper urlPathHelper = new UrlPathHelper(); private UrlPathHelper urlPathHelper = new UrlPathHelper();
private boolean useJaf = true;
private boolean ignoreUnknownExtensions = true; private boolean ignoreUnknownExtensions = true;
@ -89,11 +80,10 @@ public class PathExtensionContentNegotiationStrategy extends AbstractMappingCont
} }
/** /**
* Whether to use the Java Activation Framework to look up file extensions. * @deprecated as 5.0, in favor of {@link MediaTypeFactory}, which has no JAF dependency.
* <p>By default this is set to "true" but depends on JAF being present.
*/ */
@Deprecated
public void setUseJaf(boolean useJaf) { public void setUseJaf(boolean useJaf) {
this.useJaf = useJaf;
} }
/** /**
@ -122,11 +112,9 @@ public class PathExtensionContentNegotiationStrategy extends AbstractMappingCont
protected MediaType handleNoMatch(NativeWebRequest webRequest, String extension) protected MediaType handleNoMatch(NativeWebRequest webRequest, String extension)
throws HttpMediaTypeNotAcceptableException { throws HttpMediaTypeNotAcceptableException {
if (this.useJaf && JAF_PRESENT) { MediaType mediaType = MediaTypeFactory.getMediaType("file." + extension);
MediaType mediaType = MediaTypeFactory.getMediaType("file." + extension); if (mediaType != null) {
if (mediaType != null && !MediaType.APPLICATION_OCTET_STREAM.equals(mediaType)) { return mediaType;
return mediaType;
}
} }
if (this.ignoreUnknownExtensions) { if (this.ignoreUnknownExtensions) {
return null; return null;
@ -138,7 +126,7 @@ public class PathExtensionContentNegotiationStrategy extends AbstractMappingCont
* A public method exposing the knowledge of the path extension strategy to * A public method exposing the knowledge of the path extension strategy to
* resolve file extensions to a {@link MediaType} in this case for a given * resolve file extensions to a {@link MediaType} in this case for a given
* {@link Resource}. The method first looks up any explicitly registered * {@link Resource}. The method first looks up any explicitly registered
* file extensions first and then falls back on JAF if available. * file extensions first and then falls back on {@link MediaTypeFactory} if available.
* @param resource the resource to look up * @param resource the resource to look up
* @return the MediaType for the extension, or {@code null} if none found * @return the MediaType for the extension, or {@code null} if none found
* @since 4.3 * @since 4.3
@ -151,12 +139,9 @@ public class PathExtensionContentNegotiationStrategy extends AbstractMappingCont
if (extension != null) { if (extension != null) {
mediaType = lookupMediaType(extension); mediaType = lookupMediaType(extension);
} }
if (mediaType == null && JAF_PRESENT) { if (mediaType == null) {
mediaType = MediaTypeFactory.getMediaType(filename); mediaType = MediaTypeFactory.getMediaType(filename);
} }
if (MediaType.APPLICATION_OCTET_STREAM.equals(mediaType)) {
mediaType = null;
}
return mediaType; return mediaType;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -52,7 +52,8 @@ public class ServletPathExtensionContentNegotiationStrategy extends PathExtensio
/** /**
* Create an instance without any mappings to start with. Mappings may be * Create an instance without any mappings to start with. Mappings may be
* added later when extensions are resolved through * added later when extensions are resolved through
* {@link ServletContext#getMimeType(String)} or via JAF. * {@link ServletContext#getMimeType(String)} or via
* {@link org.springframework.http.MediaTypeFactory}.
*/ */
public ServletPathExtensionContentNegotiationStrategy(ServletContext context) { public ServletPathExtensionContentNegotiationStrategy(ServletContext context) {
this(context, null); this(context, null);
@ -61,7 +62,8 @@ public class ServletPathExtensionContentNegotiationStrategy extends PathExtensio
/** /**
* Resolve file extension via {@link ServletContext#getMimeType(String)} * Resolve file extension via {@link ServletContext#getMimeType(String)}
* and also delegate to base class for a potential JAF lookup. * and also delegate to base class for a potential
* {@link org.springframework.http.MediaTypeFactory} lookup.
*/ */
@Override @Override
protected MediaType handleNoMatch(NativeWebRequest webRequest, String extension) protected MediaType handleNoMatch(NativeWebRequest webRequest, String extension)

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -33,17 +33,14 @@ import org.springframework.web.util.UriUtils;
* the request path and uses that as the media type lookup key. * the request path and uses that as the media type lookup key.
* *
* <p>If the file extension is not found in the explicit registrations provided * <p>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 * to the constructor, the {@link MediaTypeFactory} is used as a fallback
* mechanism. The presence of the JAF is detected and enabled automatically but * mechanism.
* the {@link #setUseJaf(boolean)} property may be set to false.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 5.0 * @since 5.0
*/ */
public class PathExtensionContentTypeResolver extends AbstractMappingContentTypeResolver { public class PathExtensionContentTypeResolver extends AbstractMappingContentTypeResolver {
private boolean useJaf = true;
private boolean ignoreUnknownExtensions = true; private boolean ignoreUnknownExtensions = true;
@ -63,14 +60,6 @@ public class PathExtensionContentTypeResolver extends AbstractMappingContentType
} }
/**
* Whether to use the Java Activation Framework to look up file extensions.
* <p>By default this is set to "true" but depends on JAF being present.
*/
public void setUseJaf(boolean useJaf) {
this.useJaf = useJaf;
}
/** /**
* Whether to ignore requests with unknown file extension. Setting this to * Whether to ignore requests with unknown file extension. Setting this to
* {@code false} results in {@code HttpMediaTypeNotAcceptableException}. * {@code false} results in {@code HttpMediaTypeNotAcceptableException}.
@ -90,11 +79,9 @@ public class PathExtensionContentTypeResolver extends AbstractMappingContentType
@Override @Override
protected MediaType handleNoMatch(String key) throws NotAcceptableStatusException { protected MediaType handleNoMatch(String key) throws NotAcceptableStatusException {
if (this.useJaf) { MediaType mediaType = MediaTypeFactory.getMediaType("file." + key);
MediaType mediaType = MediaTypeFactory.getMediaType("file." + key); if (mediaType != null) {
if (mediaType != null && !MediaType.APPLICATION_OCTET_STREAM.equals(mediaType)) { return mediaType;
return mediaType;
}
} }
if (!this.ignoreUnknownExtensions) { if (!this.ignoreUnknownExtensions) {
throw new NotAcceptableStatusException(getAllMediaTypes()); throw new NotAcceptableStatusException(getAllMediaTypes());
@ -105,7 +92,7 @@ public class PathExtensionContentTypeResolver extends AbstractMappingContentType
/** /**
* A public method exposing the knowledge of the path extension resolver to * A public method exposing the knowledge of the path extension resolver to
* determine the media type for a given {@link Resource}. First it checks * determine the media type for a given {@link Resource}. First it checks
* the explicitly registered mappings and then falls back on JAF. * the explicitly registered mappings and then falls back on {@link MediaTypeFactory}.
* @param resource the resource * @param resource the resource
* @return the MediaType for the extension, or {@code null} if none determined * @return the MediaType for the extension, or {@code null} if none determined
*/ */
@ -120,9 +107,6 @@ public class PathExtensionContentTypeResolver extends AbstractMappingContentType
if (mediaType == null) { if (mediaType == null) {
mediaType = MediaTypeFactory.getMediaType(filename); mediaType = MediaTypeFactory.getMediaType(filename);
} }
if (MediaType.APPLICATION_OCTET_STREAM.equals(mediaType)) {
mediaType = null;
}
return mediaType; return mediaType;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -74,9 +74,9 @@ import org.springframework.util.CollectionUtils;
* {@link #mediaTypes(Map)}. This will be used to resolve path extensions or a * {@link #mediaTypes(Map)}. This will be used to resolve path extensions or a
* parameter value such as "json" to a media type such as "application/json". * parameter value such as "json" to a media type such as "application/json".
* *
* <p>The path extension strategy will also use the Java Activation framework * <p>The path extension strategy will also use
* (JAF), if available, to resolve a path extension to a MediaType. You may * {@link org.springframework.http.MediaTypeFactory} to resolve a path extension
* {@link #useJaf suppress} the use of JAF. * to a MediaType.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 5.0 * @since 5.0
@ -93,8 +93,6 @@ public class RequestedContentTypeResolverBuilder {
private boolean ignoreUnknownPathExtensions = true; private boolean ignoreUnknownPathExtensions = true;
private Boolean useJaf;
private String parameterName = "format"; private String parameterName = "format";
private RequestedContentTypeResolver contentTypeResolver; private RequestedContentTypeResolver contentTypeResolver;
@ -119,8 +117,9 @@ public class RequestedContentTypeResolverBuilder {
* whitelisted for the purpose of Reflected File Download attack detection * whitelisted for the purpose of Reflected File Download attack detection
* (see Spring Framework reference documentation for more details on RFD * (see Spring Framework reference documentation for more details on RFD
* attack protection). * attack protection).
* <p>The path extension strategy will also try to use JAF (if present) to * <p>The path extension strategy will also use the
* resolve path extensions. To change this behavior see {@link #useJaf}. * {@link org.springframework.http.MediaTypeFactory} to resolve path
* extensions.
* @param mediaTypes media type mappings * @param mediaTypes media type mappings
*/ */
public RequestedContentTypeResolverBuilder mediaTypes(Map<String, MediaType> mediaTypes) { public RequestedContentTypeResolverBuilder mediaTypes(Map<String, MediaType> mediaTypes) {
@ -153,18 +152,6 @@ public class RequestedContentTypeResolverBuilder {
return this; return this;
} }
/**
* When {@link #favorPathExtension favorPathExtension} is set, this
* property determines whether to allow use of JAF (Java Activation Framework)
* to resolve a path extension to a specific MediaType.
* <p>By default this is not set in which case
* {@code PathExtensionContentNegotiationStrategy} will use JAF if available.
*/
public RequestedContentTypeResolverBuilder useJaf(boolean useJaf) {
this.useJaf = useJaf;
return this;
}
/** /**
* Whether a request parameter ("format" by default) should be used to * Whether a request parameter ("format" by default) should be used to
* determine the requested media type. For this option to work you must * determine the requested media type. For this option to work you must
@ -224,9 +211,6 @@ public class RequestedContentTypeResolverBuilder {
if (this.favorPathExtension) { if (this.favorPathExtension) {
PathExtensionContentTypeResolver resolver = new PathExtensionContentTypeResolver(this.mediaTypes); PathExtensionContentTypeResolver resolver = new PathExtensionContentTypeResolver(this.mediaTypes);
resolver.setIgnoreUnknownExtensions(this.ignoreUnknownPathExtensions); resolver.setIgnoreUnknownExtensions(this.ignoreUnknownPathExtensions);
if (this.useJaf != null) {
resolver.setUseJaf(this.useJaf);
}
resolvers.add(resolver); resolvers.add(resolver);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,6 +20,7 @@ import java.util.Map;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory;
import org.springframework.web.accept.ContentNegotiationManager; import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.accept.ContentNegotiationManagerFactoryBean; import org.springframework.web.accept.ContentNegotiationManagerFactoryBean;
import org.springframework.web.accept.ContentNegotiationStrategy; import org.springframework.web.accept.ContentNegotiationStrategy;
@ -76,9 +77,8 @@ import org.springframework.web.accept.PathExtensionContentNegotiationStrategy;
* type such as "application/json". * type such as "application/json".
* *
* <p>The path extension strategy will also use {@link ServletContext#getMimeType} * <p>The path extension strategy will also use {@link ServletContext#getMimeType}
* and the Java Activation framework (JAF), if available, to resolve a path * and the {@link MediaTypeFactory} to resolve a path
* extension to a MediaType. You may however {@link #useJaf suppress} the use * extension to a MediaType.
* of JAF.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 3.2 * @since 3.2
@ -119,8 +119,8 @@ public class ContentNegotiationConfigurer {
* (see Spring Framework reference documentation for more details on RFD * (see Spring Framework reference documentation for more details on RFD
* attack protection). * attack protection).
* <p>The path extension strategy will also try to use * <p>The path extension strategy will also try to use
* {@link ServletContext#getMimeType} and JAF (if present) to resolve path * {@link ServletContext#getMimeType} and {@link MediaTypeFactory} to resolve path
* extensions. To change this behavior see the {@link #useJaf} property. * extensions.
* @param extension the key to look up * @param extension the key to look up
* @param mediaType the media type * @param mediaType the media type
* @see #mediaTypes(Map) * @see #mediaTypes(Map)
@ -166,14 +166,10 @@ public class ContentNegotiationConfigurer {
} }
/** /**
* When {@link #favorPathExtension} is set, this property determines whether * @deprecated as 5.0, in favor of {@link MediaTypeFactory}, which has no JAF dependency.
* to allow use of JAF (Java Activation Framework) to resolve a path
* extension to a specific MediaType.
* <p>By default this is not set in which case
* {@code PathExtensionContentNegotiationStrategy} will use JAF if available.
*/ */
@Deprecated
public ContentNegotiationConfigurer useJaf(boolean useJaf) { public ContentNegotiationConfigurer useJaf(boolean useJaf) {
this.factory.setUseJaf(useJaf);
return this; return this;
} }