Skip to content

Commit 0aaa652

Browse files
poutsmarstoyanchev
authored andcommitted
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
1 parent f0a43e5 commit 0aaa652

File tree

9 files changed

+67
-136
lines changed

9 files changed

+67
-136
lines changed

spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,14 @@
2727
import org.springframework.http.HttpOutputMessage;
2828
import org.springframework.http.MediaType;
2929
import org.springframework.http.MediaTypeFactory;
30-
import org.springframework.util.ClassUtils;
3130
import org.springframework.util.StreamUtils;
3231

3332
/**
3433
* Implementation of {@link HttpMessageConverter} that can read/write {@link Resource Resources}
3534
* and supports byte range requests.
3635
*
37-
* <p>By default, this converter can read all media types. The Java Activation Framework (JAF) -
38-
* if available - is used to determine the {@code Content-Type} of written resources.
39-
* If JAF is not available, {@code application/octet-stream} is used.
36+
* <p>By default, this converter can read all media types. The {@link MediaTypeFactory} is used
37+
* to determine the {@code Content-Type} of written resources.
4038
*
4139
* @author Arjen Poutsma
4240
* @author Juergen Hoeller
@@ -45,9 +43,6 @@
4543
*/
4644
public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<Resource> {
4745

48-
private static final boolean jafPresent = ClassUtils.isPresent(
49-
"javax.activation.FileTypeMap", ResourceHttpMessageConverter.class.getClassLoader());
50-
5146
private final boolean supportsReadStreaming;
5247

5348

@@ -107,12 +102,8 @@ public String getFilename() {
107102

108103
@Override
109104
protected MediaType getDefaultContentType(Resource resource) {
110-
if (jafPresent) {
111-
return MediaTypeFactory.getMediaType(resource);
112-
}
113-
else {
114-
return MediaType.APPLICATION_OCTET_STREAM;
115-
}
105+
MediaType mediaType = MediaTypeFactory.getMediaType(resource);
106+
return mediaType != null ? mediaType : MediaType.APPLICATION_OCTET_STREAM;
116107
}
117108

118109
@Override

spring-web/src/main/java/org/springframework/http/converter/ResourceRegionHttpMessageConverter.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424
import java.nio.charset.StandardCharsets;
2525
import java.util.Collection;
2626

27+
import org.springframework.core.io.Resource;
2728
import org.springframework.core.io.support.ResourceRegion;
2829
import org.springframework.http.HttpHeaders;
2930
import org.springframework.http.HttpInputMessage;
3031
import org.springframework.http.HttpOutputMessage;
3132
import org.springframework.http.MediaType;
3233
import org.springframework.http.MediaTypeFactory;
3334
import org.springframework.util.Assert;
34-
import org.springframework.util.ClassUtils;
3535
import org.springframework.util.MimeTypeUtils;
3636
import org.springframework.util.StreamUtils;
3737

@@ -44,9 +44,6 @@
4444
*/
4545
public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessageConverter<Object> {
4646

47-
private static final boolean jafPresent = ClassUtils.isPresent(
48-
"javax.activation.FileTypeMap", ResourceHttpMessageConverter.class.getClassLoader());
49-
5047
public ResourceRegionHttpMessageConverter() {
5148
super(MediaType.ALL);
5249
}
@@ -80,18 +77,24 @@ protected ResourceRegion readInternal(Class<?> clazz, HttpInputMessage inputMess
8077
@Override
8178
@SuppressWarnings("unchecked")
8279
protected MediaType getDefaultContentType(Object object) {
83-
if (jafPresent) {
84-
if(object instanceof ResourceRegion) {
85-
return MediaTypeFactory.getMediaType(((ResourceRegion) object).getResource());
86-
}
87-
else {
88-
Collection<ResourceRegion> regions = (Collection<ResourceRegion>) object;
89-
if(regions.size() > 0) {
90-
return MediaTypeFactory.getMediaType(regions.iterator().next().getResource());
91-
}
80+
Resource resource = null;
81+
if (object instanceof ResourceRegion) {
82+
resource = ((ResourceRegion) object).getResource();
83+
}
84+
else {
85+
Collection<ResourceRegion> regions = (Collection<ResourceRegion>) object;
86+
if (regions.size() > 0) {
87+
resource = regions.iterator().next().getResource();
9288
}
9389
}
94-
return MediaType.APPLICATION_OCTET_STREAM;
90+
MediaType result = null;
91+
if (resource != null) {
92+
result = MediaTypeFactory.getMediaType(resource);
93+
}
94+
if (result == null) {
95+
return MediaType.APPLICATION_OCTET_STREAM;
96+
}
97+
return result;
9598
}
9699

97100
@Override

spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManager.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,9 @@ public List<String> resolveFileExtensions(MediaType mediaType) {
143143
* <p>At startup this method returns extensions explicitly registered with
144144
* either {@link PathExtensionContentNegotiationStrategy} or
145145
* {@link ParameterContentNegotiationStrategy}. At runtime if there is a
146-
* "path extension" strategy and its
147-
* {@link PathExtensionContentNegotiationStrategy#setUseJaf(boolean)
148-
* useJaf} property is set to "true", the list of extensions may
149-
* increase as file extensions are resolved via JAF and cached.
146+
* "path extension" strategy, the list of extensions may
147+
* increase as file extensions are resolved via
148+
* {@link org.springframework.http.MediaTypeFactory} and cached.
150149
*/
151150
@Override
152151
public List<String> getAllFileExtensions() {

spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBean.java

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828
import org.springframework.beans.factory.FactoryBean;
2929
import org.springframework.beans.factory.InitializingBean;
3030
import org.springframework.http.MediaType;
31+
import org.springframework.http.MediaTypeFactory;
3132
import org.springframework.util.Assert;
3233
import org.springframework.util.CollectionUtils;
3334
import org.springframework.web.context.ServletContextAware;
@@ -81,8 +82,7 @@
8182
* "application/json".
8283
*
8384
* <p>The path extension strategy will also use {@link ServletContext#getMimeType}
84-
* and the Java Activation framework (JAF), if available, to resolve a path
85-
* extension to a MediaType. You may {@link #setUseJaf suppress} the use of JAF.
85+
* and {@link MediaTypeFactory} to resolve a path extension to a MediaType.
8686
*
8787
* @author Rossen Stoyanchev
8888
* @since 3.2
@@ -100,8 +100,6 @@ public class ContentNegotiationManagerFactoryBean
100100

101101
private boolean ignoreUnknownPathExtensions = true;
102102

103-
private Boolean useJaf;
104-
105103
private String parameterName = "format";
106104

107105
private ContentNegotiationStrategy defaultNegotiationStrategy;
@@ -130,8 +128,8 @@ public void setFavorPathExtension(boolean favorPathExtension) {
130128
* (see Spring Framework reference documentation for more details on RFD
131129
* attack protection).
132130
* <p>The path extension strategy will also try to use
133-
* {@link ServletContext#getMimeType} and JAF (if present) to resolve path
134-
* extensions. To change this behavior see the {@link #useJaf} property.
131+
* {@link ServletContext#getMimeType} and
132+
* {@link org.springframework.http.MediaTypeFactory} to resolve path extensions.
135133
* @param mediaTypes media type mappings
136134
* @see #addMediaType(String, MediaType)
137135
* @see #addMediaTypes(Map)
@@ -177,18 +175,10 @@ public void setIgnoreUnknownPathExtensions(boolean ignore) {
177175
}
178176

179177
/**
180-
* When {@link #setFavorPathExtension favorPathExtension} is set, this
181-
* property determines whether to allow use of JAF (Java Activation Framework)
182-
* to resolve a path extension to a specific MediaType.
183-
* <p>By default this is not set in which case
184-
* {@code PathExtensionContentNegotiationStrategy} will use JAF if available.
178+
* @deprecated as 5.0, in favor of {@link MediaTypeFactory}, which has no JAF dependency.
185179
*/
180+
@Deprecated
186181
public void setUseJaf(boolean useJaf) {
187-
this.useJaf = useJaf;
188-
}
189-
190-
private boolean isUseJafTurnedOff() {
191-
return (this.useJaf != null && !this.useJaf);
192182
}
193183

194184
/**
@@ -254,17 +244,14 @@ public void afterPropertiesSet() {
254244

255245
if (this.favorPathExtension) {
256246
PathExtensionContentNegotiationStrategy strategy;
257-
if (this.servletContext != null && !isUseJafTurnedOff()) {
247+
if (this.servletContext != null) {
258248
strategy = new ServletPathExtensionContentNegotiationStrategy(
259249
this.servletContext, this.mediaTypes);
260250
}
261251
else {
262252
strategy = new PathExtensionContentNegotiationStrategy(this.mediaTypes);
263253
}
264254
strategy.setIgnoreUnknownExtensions(this.ignoreUnknownPathExtensions);
265-
if (this.useJaf != null) {
266-
strategy.setUseJaf(this.useJaf);
267-
}
268255
strategies.add(strategy);
269256
}
270257

spring-web/src/main/java/org/springframework/web/accept/PathExtensionContentNegotiationStrategy.java

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.springframework.http.MediaType;
2828
import org.springframework.http.MediaTypeFactory;
2929
import org.springframework.util.Assert;
30-
import org.springframework.util.ClassUtils;
3130
import org.springframework.util.StringUtils;
3231
import org.springframework.web.HttpMediaTypeNotAcceptableException;
3332
import org.springframework.web.context.request.NativeWebRequest;
@@ -39,26 +38,18 @@
3938
* request path to a key to be used to look up a media type.
4039
*
4140
* <p>If the file extension is not found in the explicit registrations provided
42-
* to the constructor, the Java Activation Framework (JAF) is used as a fallback
41+
* to the constructor, the {@link MediaTypeFactory} is used as a fallback
4342
* mechanism.
4443
*
45-
* <p>The presence of the JAF is detected and enabled automatically but the
46-
* {@link #setUseJaf(boolean)} property may be set to false.
47-
*
4844
* @author Rossen Stoyanchev
4945
* @since 3.2
5046
*/
5147
public class PathExtensionContentNegotiationStrategy extends AbstractMappingContentNegotiationStrategy {
5248

53-
private static final boolean JAF_PRESENT = ClassUtils.isPresent("javax.activation.FileTypeMap",
54-
PathExtensionContentNegotiationStrategy.class.getClassLoader());
55-
5649
private static final Log logger = LogFactory.getLog(PathExtensionContentNegotiationStrategy.class);
5750

5851
private UrlPathHelper urlPathHelper = new UrlPathHelper();
5952

60-
private boolean useJaf = true;
61-
6253
private boolean ignoreUnknownExtensions = true;
6354

6455

@@ -89,11 +80,10 @@ public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
8980
}
9081

9182
/**
92-
* Whether to use the Java Activation Framework to look up file extensions.
93-
* <p>By default this is set to "true" but depends on JAF being present.
83+
* @deprecated as 5.0, in favor of {@link MediaTypeFactory}, which has no JAF dependency.
9484
*/
85+
@Deprecated
9586
public void setUseJaf(boolean useJaf) {
96-
this.useJaf = useJaf;
9787
}
9888

9989
/**
@@ -122,11 +112,9 @@ protected String getMediaTypeKey(NativeWebRequest webRequest) {
122112
protected MediaType handleNoMatch(NativeWebRequest webRequest, String extension)
123113
throws HttpMediaTypeNotAcceptableException {
124114

125-
if (this.useJaf && JAF_PRESENT) {
126-
MediaType mediaType = MediaTypeFactory.getMediaType("file." + extension);
127-
if (mediaType != null && !MediaType.APPLICATION_OCTET_STREAM.equals(mediaType)) {
128-
return mediaType;
129-
}
115+
MediaType mediaType = MediaTypeFactory.getMediaType("file." + extension);
116+
if (mediaType != null) {
117+
return mediaType;
130118
}
131119
if (this.ignoreUnknownExtensions) {
132120
return null;
@@ -138,7 +126,7 @@ protected MediaType handleNoMatch(NativeWebRequest webRequest, String extension)
138126
* A public method exposing the knowledge of the path extension strategy to
139127
* resolve file extensions to a {@link MediaType} in this case for a given
140128
* {@link Resource}. The method first looks up any explicitly registered
141-
* file extensions first and then falls back on JAF if available.
129+
* file extensions first and then falls back on {@link MediaTypeFactory} if available.
142130
* @param resource the resource to look up
143131
* @return the MediaType for the extension, or {@code null} if none found
144132
* @since 4.3
@@ -151,12 +139,9 @@ public MediaType getMediaTypeForResource(Resource resource) {
151139
if (extension != null) {
152140
mediaType = lookupMediaType(extension);
153141
}
154-
if (mediaType == null && JAF_PRESENT) {
142+
if (mediaType == null) {
155143
mediaType = MediaTypeFactory.getMediaType(filename);
156144
}
157-
if (MediaType.APPLICATION_OCTET_STREAM.equals(mediaType)) {
158-
mediaType = null;
159-
}
160145
return mediaType;
161146
}
162147

spring-web/src/main/java/org/springframework/web/accept/ServletPathExtensionContentNegotiationStrategy.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,7 +52,8 @@ public ServletPathExtensionContentNegotiationStrategy(
5252
/**
5353
* Create an instance without any mappings to start with. Mappings may be
5454
* added later when extensions are resolved through
55-
* {@link ServletContext#getMimeType(String)} or via JAF.
55+
* {@link ServletContext#getMimeType(String)} or via
56+
* {@link org.springframework.http.MediaTypeFactory}.
5657
*/
5758
public ServletPathExtensionContentNegotiationStrategy(ServletContext context) {
5859
this(context, null);
@@ -61,7 +62,8 @@ public ServletPathExtensionContentNegotiationStrategy(ServletContext context) {
6162

6263
/**
6364
* Resolve file extension via {@link ServletContext#getMimeType(String)}
64-
* and also delegate to base class for a potential JAF lookup.
65+
* and also delegate to base class for a potential
66+
* {@link org.springframework.http.MediaTypeFactory} lookup.
6567
*/
6668
@Override
6769
protected MediaType handleNoMatch(NativeWebRequest webRequest, String extension)

spring-webflux/src/main/java/org/springframework/web/reactive/accept/PathExtensionContentTypeResolver.java

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,17 +33,14 @@
3333
* the request path and uses that as the media type lookup key.
3434
*
3535
* <p>If the file extension is not found in the explicit registrations provided
36-
* to the constructor, the Java Activation Framework (JAF) is used as a fallback
37-
* mechanism. The presence of the JAF is detected and enabled automatically but
38-
* the {@link #setUseJaf(boolean)} property may be set to false.
36+
* to the constructor, the {@link MediaTypeFactory} is used as a fallback
37+
* mechanism.
3938
*
4039
* @author Rossen Stoyanchev
4140
* @since 5.0
4241
*/
4342
public class PathExtensionContentTypeResolver extends AbstractMappingContentTypeResolver {
4443

45-
private boolean useJaf = true;
46-
4744
private boolean ignoreUnknownExtensions = true;
4845

4946

@@ -63,14 +60,6 @@ public PathExtensionContentTypeResolver() {
6360
}
6461

6562

66-
/**
67-
* Whether to use the Java Activation Framework to look up file extensions.
68-
* <p>By default this is set to "true" but depends on JAF being present.
69-
*/
70-
public void setUseJaf(boolean useJaf) {
71-
this.useJaf = useJaf;
72-
}
73-
7463
/**
7564
* Whether to ignore requests with unknown file extension. Setting this to
7665
* {@code false} results in {@code HttpMediaTypeNotAcceptableException}.
@@ -90,11 +79,9 @@ protected String extractKey(ServerWebExchange exchange) {
9079

9180
@Override
9281
protected MediaType handleNoMatch(String key) throws NotAcceptableStatusException {
93-
if (this.useJaf) {
94-
MediaType mediaType = MediaTypeFactory.getMediaType("file." + key);
95-
if (mediaType != null && !MediaType.APPLICATION_OCTET_STREAM.equals(mediaType)) {
96-
return mediaType;
97-
}
82+
MediaType mediaType = MediaTypeFactory.getMediaType("file." + key);
83+
if (mediaType != null) {
84+
return mediaType;
9885
}
9986
if (!this.ignoreUnknownExtensions) {
10087
throw new NotAcceptableStatusException(getAllMediaTypes());
@@ -105,7 +92,7 @@ protected MediaType handleNoMatch(String key) throws NotAcceptableStatusExceptio
10592
/**
10693
* A public method exposing the knowledge of the path extension resolver to
10794
* determine the media type for a given {@link Resource}. First it checks
108-
* the explicitly registered mappings and then falls back on JAF.
95+
* the explicitly registered mappings and then falls back on {@link MediaTypeFactory}.
10996
* @param resource the resource
11097
* @return the MediaType for the extension, or {@code null} if none determined
11198
*/
@@ -120,9 +107,6 @@ public MediaType resolveMediaTypeForResource(Resource resource) {
120107
if (mediaType == null) {
121108
mediaType = MediaTypeFactory.getMediaType(filename);
122109
}
123-
if (MediaType.APPLICATION_OCTET_STREAM.equals(mediaType)) {
124-
mediaType = null;
125-
}
126110
return mediaType;
127111
}
128112

0 commit comments

Comments
 (0)