23
23
import java .util .Map ;
24
24
import java .util .Properties ;
25
25
26
- import jakarta .servlet .ServletContext ;
27
-
28
26
import org .springframework .beans .factory .FactoryBean ;
29
27
import org .springframework .beans .factory .InitializingBean ;
30
28
import org .springframework .http .MediaType ;
31
29
import org .springframework .http .MediaTypeFactory ;
32
30
import org .springframework .lang .Nullable ;
33
31
import org .springframework .util .Assert ;
34
32
import org .springframework .util .CollectionUtils ;
35
- import org .springframework .web .context .ServletContextAware ;
36
33
37
34
/**
38
35
* Factory to create a {@code ContentNegotiationManager} and configure it with
56
53
* <td>Off</td>
57
54
* </tr>
58
55
* <tr>
59
- * <td>{@link #setFavorPathExtension favorPathExtension}</td>
60
- * <td>false (as of 5.3)</td>
61
- * <td>{@link PathExtensionContentNegotiationStrategy}</td>
62
- * <td>Off</td>
63
- * </tr>
64
- * <tr>
65
56
* <td>{@link #setIgnoreAcceptHeader ignoreAcceptHeader}</td>
66
57
* <td>false</td>
67
58
* <td>{@link HeaderContentNegotiationStrategy}</td>
85
76
* methods and set the exact strategies to use via
86
77
* {@link #setStrategies(List)}.
87
78
*
88
- * <p><strong>Deprecation Note:</strong> As of 5.2.4,
89
- * {@link #setFavorPathExtension(boolean) favorPathExtension} and
90
- * {@link #setIgnoreUnknownPathExtensions(boolean) ignoreUnknownPathExtensions}
91
- * are deprecated in order to discourage using path extensions for content
92
- * negotiation and for request mapping with similar deprecations on
93
- * {@link org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
94
- * RequestMappingHandlerMapping}. For further context, please read issue
95
- * <a href="https://github.com/spring-projects/spring-framework/issues/24179">#24719</a>.
96
79
* @author Rossen Stoyanchev
97
80
* @author Brian Clozel
98
81
* @since 3.2
99
82
*/
100
- public class ContentNegotiationManagerFactoryBean
101
- implements FactoryBean <ContentNegotiationManager >, ServletContextAware , InitializingBean {
83
+ public class ContentNegotiationManagerFactoryBean implements FactoryBean <ContentNegotiationManager >, InitializingBean {
102
84
103
85
@ Nullable
104
86
private List <ContentNegotiationStrategy > strategies ;
@@ -108,12 +90,8 @@ public class ContentNegotiationManagerFactoryBean
108
90
109
91
private String parameterName = "format" ;
110
92
111
- private boolean favorPathExtension = false ;
112
-
113
93
private final Map <String , MediaType > mediaTypes = new HashMap <>();
114
94
115
- private boolean ignoreUnknownPathExtensions = true ;
116
-
117
95
@ Nullable
118
96
private Boolean useRegisteredExtensionsOnly ;
119
97
@@ -125,9 +103,6 @@ public class ContentNegotiationManagerFactoryBean
125
103
@ Nullable
126
104
private ContentNegotiationManager contentNegotiationManager ;
127
105
128
- @ Nullable
129
- private ServletContext servletContext ;
130
-
131
106
132
107
/**
133
108
* Set the exact list of strategies to use.
@@ -161,30 +136,12 @@ public void setParameterName(String parameterName) {
161
136
this .parameterName = parameterName ;
162
137
}
163
138
164
- /**
165
- * Whether the path extension in the URL path should be used to determine
166
- * the requested media type.
167
- * <p>By default this is set to {@code false} in which case path extensions
168
- * have no impact on content negotiation.
169
- * @deprecated as of 5.2.4. See class-level note on the deprecation of path
170
- * extension config options. As there is no replacement for this method,
171
- * in 5.2.x it is necessary to set it to {@code false}. In 5.3 the default
172
- * changes to {@code false} and use of this property becomes unnecessary.
173
- */
174
- @ Deprecated
175
- public void setFavorPathExtension (boolean favorPathExtension ) {
176
- this .favorPathExtension = favorPathExtension ;
177
- }
178
-
179
139
/**
180
140
* Add a mapping from a key to a MediaType where the key are normalized to
181
141
* lowercase and may have been extracted from a path extension, a filename
182
142
* extension, or passed as a query parameter.
183
143
* <p>The {@link #setFavorParameter(boolean) parameter strategy} requires
184
- * such mappings in order to work while the {@link #setFavorPathExtension(boolean)
185
- * path extension strategy} can fall back on lookups via
186
- * {@link ServletContext#getMimeType} and
187
- * {@link org.springframework.http.MediaTypeFactory}.
144
+ * such mappings in order to work.
188
145
* <p><strong>Note:</strong> Mappings registered here may be accessed via
189
146
* {@link ContentNegotiationManager#getMediaTypeMappings()} and may be used
190
147
* not only in the parameter and path extension strategies. For example,
@@ -227,35 +184,10 @@ public void addMediaTypes(@Nullable Map<String, MediaType> mediaTypes) {
227
184
}
228
185
229
186
/**
230
- * Whether to ignore requests with path extension that cannot be resolved
231
- * to any media type. Setting this to {@code false} will result in an
232
- * {@code HttpMediaTypeNotAcceptableException} if there is no match.
233
- * <p>By default this is set to {@code true}.
234
- * @deprecated as of 5.2.4. See class-level note on the deprecation of path
235
- * extension config options.
236
- */
237
- @ Deprecated
238
- public void setIgnoreUnknownPathExtensions (boolean ignore ) {
239
- this .ignoreUnknownPathExtensions = ignore ;
240
- }
241
-
242
- /**
243
- * Indicate whether to use the Java Activation Framework as a fallback option
244
- * to map from file extensions to media types.
245
- * @deprecated as of 5.0, in favor of {@link #setUseRegisteredExtensionsOnly(boolean)},
246
- * which has reverse behavior.
247
- */
248
- @ Deprecated
249
- public void setUseJaf (boolean useJaf ) {
250
- setUseRegisteredExtensionsOnly (!useJaf );
251
- }
252
-
253
- /**
254
- * When {@link #setFavorPathExtension favorPathExtension} or
255
- * {@link #setFavorParameter(boolean)} is set, this property determines
187
+ * When {@link #setFavorParameter(boolean)} is set, this property determines
256
188
* whether to use only registered {@code MediaType} mappings or to allow
257
189
* dynamic resolution, for example, via {@link MediaTypeFactory}.
258
- * <p>By default this is not set in which case dynamic resolution is on.
190
+ * <p>By default, this is not set in which case dynamic resolution is on.
259
191
*/
260
192
public void setUseRegisteredExtensionsOnly (boolean useRegisteredExtensionsOnly ) {
261
193
this .useRegisteredExtensionsOnly = useRegisteredExtensionsOnly ;
@@ -303,14 +235,6 @@ public void setDefaultContentTypeStrategy(ContentNegotiationStrategy strategy) {
303
235
this .defaultNegotiationStrategy = strategy ;
304
236
}
305
237
306
- /**
307
- * Invoked by Spring to inject the ServletContext.
308
- */
309
- @ Override
310
- public void setServletContext (ServletContext servletContext ) {
311
- this .servletContext = servletContext ;
312
- }
313
-
314
238
315
239
@ Override
316
240
public void afterPropertiesSet () {
@@ -321,28 +245,13 @@ public void afterPropertiesSet() {
321
245
* Create and initialize a {@link ContentNegotiationManager} instance.
322
246
* @since 5.0
323
247
*/
324
- @ SuppressWarnings ("deprecation" )
325
248
public ContentNegotiationManager build () {
326
249
List <ContentNegotiationStrategy > strategies = new ArrayList <>();
327
250
328
251
if (this .strategies != null ) {
329
252
strategies .addAll (this .strategies );
330
253
}
331
254
else {
332
- if (this .favorPathExtension ) {
333
- PathExtensionContentNegotiationStrategy strategy ;
334
- if (this .servletContext != null && !useRegisteredExtensionsOnly ()) {
335
- strategy = new ServletPathExtensionContentNegotiationStrategy (this .servletContext , this .mediaTypes );
336
- }
337
- else {
338
- strategy = new PathExtensionContentNegotiationStrategy (this .mediaTypes );
339
- }
340
- strategy .setIgnoreUnknownExtensions (this .ignoreUnknownPathExtensions );
341
- if (this .useRegisteredExtensionsOnly != null ) {
342
- strategy .setUseRegisteredExtensionsOnly (this .useRegisteredExtensionsOnly );
343
- }
344
- strategies .add (strategy );
345
- }
346
255
if (this .favorParameter ) {
347
256
ParameterContentNegotiationStrategy strategy = new ParameterContentNegotiationStrategy (this .mediaTypes );
348
257
strategy .setParameterName (this .parameterName );
@@ -367,7 +276,7 @@ public ContentNegotiationManager build() {
367
276
// Ensure media type mappings are available via ContentNegotiationManager#getMediaTypeMappings()
368
277
// independent of path extension or parameter strategies.
369
278
370
- if (!CollectionUtils .isEmpty (this .mediaTypes ) && !this .favorPathExtension && ! this . favorParameter ) {
279
+ if (!CollectionUtils .isEmpty (this .mediaTypes ) && !this .favorParameter ) {
371
280
this .contentNegotiationManager .addFileExtensionResolvers (
372
281
new MappingMediaTypeFileExtensionResolver (this .mediaTypes ));
373
282
}
@@ -387,9 +296,4 @@ public Class<?> getObjectType() {
387
296
return ContentNegotiationManager .class ;
388
297
}
389
298
390
- @ Override
391
- public boolean isSingleton () {
392
- return true ;
393
- }
394
-
395
299
}
0 commit comments