Skip to content

Commit 4ca7d89

Browse files
committed
Revert resource handling changes
This change reverts recent commits made to expand the resource handling mechanism in Spring MVC. The changes will move into a separate branch and likely into a separate project allowing it to mature and evolve without being tied to the main framework release schedule. Issue: SPR-10933, SPR-10310
2 parents 4bf5a02 + 6a32329 commit 4ca7d89

30 files changed

+38
-1838
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistration.java

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2012 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.
@@ -23,10 +23,7 @@
2323
import org.springframework.core.io.ResourceLoader;
2424
import org.springframework.util.Assert;
2525
import org.springframework.util.CollectionUtils;
26-
import org.springframework.web.servlet.resource.PathResourceResolver;
2726
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
28-
import org.springframework.web.servlet.resource.ResourceResolver;
29-
import org.springframework.web.servlet.resource.ResourceTransformer;
3027

3128
/**
3229
* Encapsulates information required to create a resource handlers.
@@ -46,11 +43,6 @@ public class ResourceHandlerRegistration {
4643

4744
private Integer cachePeriod;
4845

49-
private List<ResourceResolver> resourceResolvers;
50-
51-
private List<ResourceTransformer> resourceTransformers;
52-
53-
5446
/**
5547
* Create a {@link ResourceHandlerRegistration} instance.
5648
* @param resourceLoader a resource loader for turning a String location into a {@link Resource}
@@ -78,23 +70,6 @@ public ResourceHandlerRegistration addResourceLocations(String...resourceLocatio
7870
return this;
7971
}
8072

81-
/**
82-
* Configure the list of {@link ResourceResolver}s to use.
83-
* <p>
84-
* By default {@link PathResourceResolver} is configured. If using this property, it
85-
* is recommended to add {@link PathResourceResolver} as the last resolver.
86-
*/
87-
public void setResourceResolvers(List<ResourceResolver> resourceResolvers) {
88-
this.resourceResolvers = resourceResolvers;
89-
}
90-
91-
/**
92-
* Configure the list of {@link ResourceTransformer}s to use.
93-
*/
94-
public void setResourceTransformers(List<ResourceTransformer> transformers) {
95-
this.resourceTransformers = transformers;
96-
}
97-
9873
/**
9974
* Specify the cache period for the resources served by the resource handler, in seconds. The default is to not
10075
* send any cache headers but to rely on last-modified timestamps only. Set to 0 in order to send cache headers
@@ -111,15 +86,7 @@ public ResourceHandlerRegistration setCachePeriod(Integer cachePeriod) {
11186
* Returns the URL path patterns for the resource handler.
11287
*/
11388
protected String[] getPathPatterns() {
114-
return this.pathPatterns;
115-
}
116-
117-
protected List<ResourceResolver> getResourceResolvers() {
118-
return this.resourceResolvers;
119-
}
120-
121-
protected List<ResourceTransformer> getResourceTransformers() {
122-
return this.resourceTransformers;
89+
return pathPatterns;
12390
}
12491

12592
/**
@@ -128,15 +95,9 @@ protected List<ResourceTransformer> getResourceTransformers() {
12895
protected ResourceHttpRequestHandler getRequestHandler() {
12996
Assert.isTrue(!CollectionUtils.isEmpty(locations), "At least one location is required for resource handling.");
13097
ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
131-
if (this.resourceResolvers != null) {
132-
requestHandler.setResourceResolvers(this.resourceResolvers);
133-
}
134-
if (this.resourceTransformers != null) {
135-
requestHandler.setResourceTransformers(this.resourceTransformers);
136-
}
137-
requestHandler.setLocations(this.locations);
138-
if (this.cachePeriod != null) {
139-
requestHandler.setCacheSeconds(this.cachePeriod);
98+
requestHandler.setLocations(locations);
99+
if (cachePeriod != null) {
100+
requestHandler.setCacheSeconds(cachePeriod);
140101
}
141102
return requestHandler;
142103
}

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistry.java

Lines changed: 5 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2012 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.
@@ -17,23 +17,19 @@
1717
package org.springframework.web.servlet.config.annotation;
1818

1919
import java.util.ArrayList;
20-
import java.util.Arrays;
2120
import java.util.LinkedHashMap;
2221
import java.util.List;
2322
import java.util.Map;
2423

2524
import javax.servlet.ServletContext;
2625

27-
import org.springframework.beans.factory.BeanInitializationException;
2826
import org.springframework.context.ApplicationContext;
2927
import org.springframework.util.Assert;
3028
import org.springframework.web.HttpRequestHandler;
3129
import org.springframework.web.servlet.HandlerMapping;
3230
import org.springframework.web.servlet.handler.AbstractHandlerMapping;
3331
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
3432
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
35-
import org.springframework.web.servlet.resource.ResourceResolver;
36-
import org.springframework.web.servlet.resource.ResourceTransformer;
3733

3834
/**
3935
* Stores registrations of resource handlers for serving static resources such as images, css files and others
@@ -60,13 +56,8 @@ public class ResourceHandlerRegistry {
6056

6157
private final List<ResourceHandlerRegistration> registrations = new ArrayList<ResourceHandlerRegistration>();
6258

63-
private List<ResourceResolver> resourceResolvers;
64-
65-
private List<ResourceTransformer> resourceTransformers;
66-
6759
private int order = Integer.MAX_VALUE -1;
6860

69-
7061
public ResourceHandlerRegistry(ApplicationContext applicationContext, ServletContext servletContext) {
7162
Assert.notNull(applicationContext, "ApplicationContext is required");
7263
this.applicationContext = applicationContext;
@@ -84,18 +75,6 @@ public ResourceHandlerRegistration addResourceHandler(String... pathPatterns) {
8475
return registration;
8576
}
8677

87-
/**
88-
* Whether a resource handler has already been registered for the given pathPattern.
89-
*/
90-
public boolean hasMappingForPattern(String pathPattern) {
91-
for (ResourceHandlerRegistration registration : registrations) {
92-
if (Arrays.asList(registration.getPathPatterns()).contains(pathPattern)) {
93-
return true;
94-
}
95-
}
96-
return false;
97-
}
98-
9978
/**
10079
* Specify the order to use for resource handling relative to other {@link HandlerMapping}s configured in
10180
* the Spring MVC application context. The default value used is {@code Integer.MAX_VALUE-1}.
@@ -105,22 +84,6 @@ public ResourceHandlerRegistry setOrder(int order) {
10584
return this;
10685
}
10786

108-
/**
109-
* Configure the {@link ResourceResolver}s to use by default in resource handlers that
110-
* don't have them set.
111-
*/
112-
public void setResourceResolvers(List<ResourceResolver> resourceResolvers) {
113-
this.resourceResolvers = resourceResolvers;
114-
}
115-
116-
/**
117-
* Configure the {@link ResourceTransformer}s to use by default in resource handlers
118-
* that don't have them set.
119-
*/
120-
public void setResourceTransformers(List<ResourceTransformer> transformers) {
121-
this.resourceTransformers = transformers;
122-
}
123-
12487
/**
12588
* Return a handler mapping with the mapped resource handlers; or {@code null} in case of no registrations.
12689
*/
@@ -132,22 +95,10 @@ protected AbstractHandlerMapping getHandlerMapping() {
13295
Map<String, HttpRequestHandler> urlMap = new LinkedHashMap<String, HttpRequestHandler>();
13396
for (ResourceHandlerRegistration registration : registrations) {
13497
for (String pathPattern : registration.getPathPatterns()) {
135-
ResourceHttpRequestHandler handler = registration.getRequestHandler();
136-
handler.setServletContext(servletContext);
137-
handler.setApplicationContext(applicationContext);
138-
if ((this.resourceResolvers != null) && (registration.getResourceResolvers() == null)) {
139-
handler.setResourceResolvers(this.resourceResolvers);
140-
}
141-
if ((this.resourceTransformers != null) && (registration.getResourceTransformers() == null)) {
142-
handler.setResourceTransformers(this.resourceTransformers);
143-
}
144-
try {
145-
handler.afterPropertiesSet();
146-
}
147-
catch (Exception e) {
148-
throw new BeanInitializationException("Failed to init ResourceHttpRequestHandler", e);
149-
}
150-
urlMap.put(pathPattern, handler);
98+
ResourceHttpRequestHandler requestHandler = registration.getRequestHandler();
99+
requestHandler.setServletContext(servletContext);
100+
requestHandler.setApplicationContext(applicationContext);
101+
urlMap.put(pathPattern, requestHandler);
151102
}
152103
}
153104

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2012 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.
@@ -17,7 +17,6 @@
1717
package org.springframework.web.servlet.config.annotation;
1818

1919
import java.util.ArrayList;
20-
import java.util.Collections;
2120
import java.util.HashMap;
2221
import java.util.List;
2322
import java.util.Map;
@@ -71,7 +70,6 @@
7170
import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
7271
import org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor;
7372
import org.springframework.web.servlet.handler.HandlerExceptionResolverComposite;
74-
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
7573
import org.springframework.web.servlet.mvc.Controller;
7674
import org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter;
7775
import org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter;
@@ -80,7 +78,6 @@
8078
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
8179
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
8280
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;
83-
import org.springframework.web.servlet.resource.ResourceUrlGenerator;
8481

8582
/**
8683
* This is the main class providing the configuration behind the MVC Java config.
@@ -309,26 +306,13 @@ public BeanNameUrlHandlerMapping beanNameHandlerMapping() {
309306
*/
310307
@Bean
311308
public HandlerMapping resourceHandlerMapping() {
312-
ResourceHandlerRegistry registry = new ResourceHandlerRegistry(
313-
this.applicationContext, this.servletContext);
309+
ResourceHandlerRegistry registry = new ResourceHandlerRegistry(applicationContext, servletContext);
314310
addResourceHandlers(registry);
315311
AbstractHandlerMapping handlerMapping = registry.getHandlerMapping();
316312
handlerMapping = handlerMapping != null ? handlerMapping : new EmptyHandlerMapping();
317313
return handlerMapping;
318314
}
319315

320-
/**
321-
* Return a {@link ResourceUrlGenerator} to use to generate URLs for resources.
322-
*/
323-
@Bean
324-
public ResourceUrlGenerator resourceUrlGenerator() {
325-
Map<String, ?> handlerMap = Collections.<String, Object>emptyMap();
326-
if (resourceHandlerMapping() instanceof SimpleUrlHandlerMapping) {
327-
handlerMap = ((SimpleUrlHandlerMapping) resourceHandlerMapping()).getUrlMap();
328-
}
329-
return new ResourceUrlGenerator(handlerMap);
330-
}
331-
332316
/**
333317
* Override this method to add resource handlers for serving static resources.
334318
* @see ResourceHandlerRegistry

spring-webmvc/src/main/java/org/springframework/web/servlet/resource/DefaultResourceResolverChain.java

Lines changed: 0 additions & 113 deletions
This file was deleted.

0 commit comments

Comments
 (0)