Sort handler matches in ResourceUrlProvider

Prior to this change, the `ResourceUrlProvider.getForLookupPath` method
would try to match handlers using the keySet order in the
handlerMappings Map. In case of several matches, the handler used for
the return value could vary, since the registration order in the
handlerMappings can't be guaranteed in the configuration.

This commit now collects all matching handlers and sort them using a
`PatternComparator`, in order to try each handler from the most specific
mapping to the least.

Issue: SPR-12647
This commit is contained in:
Brian Clozel
2015-02-03 11:20:28 +01:00
parent e0d407dad0
commit 13cdd22f5e
2 changed files with 53 additions and 19 deletions

View File

@@ -44,6 +44,8 @@ import static org.junit.Assert.*;
*/
public class ResourceUrlProviderTests {
private List<Resource> locations;
private ResourceUrlProvider translator;
private ResourceHttpRequestHandler handler;
@@ -53,9 +55,9 @@ public class ResourceUrlProviderTests {
@Before
public void setUp() {
List<Resource> locations = new ArrayList<Resource>();
locations.add(new ClassPathResource("test/", getClass()));
locations.add(new ClassPathResource("testalternatepath/", getClass()));
this.locations = new ArrayList<Resource>();
this.locations.add(new ClassPathResource("test/", getClass()));
this.locations.add(new ClassPathResource("testalternatepath/", getClass()));
this.handler = new ResourceHttpRequestHandler();
this.handler.setLocations(locations);
@@ -94,6 +96,28 @@ public class ResourceUrlProviderTests {
this.translator.setHandlerMap(this.handlerMap);
}
// SPR-12647
@Test
public void bestPatternMatch() throws Exception {
ResourceHttpRequestHandler otherHandler = new ResourceHttpRequestHandler();
otherHandler.setLocations(this.locations);
Map<String, VersionStrategy> versionStrategyMap = new HashMap<>();
versionStrategyMap.put("/**", new ContentVersionStrategy());
VersionResourceResolver versionResolver = new VersionResourceResolver();
versionResolver.setStrategyMap(versionStrategyMap);
List<ResourceResolver> resolvers = new ArrayList<ResourceResolver>();
resolvers.add(versionResolver);
resolvers.add(new PathResourceResolver());
otherHandler.setResourceResolvers(resolvers);
this.handlerMap.put("/resources/*.css", otherHandler);
initTranslator();
String url = this.translator.getForLookupPath("/resources/foo.css");
assertEquals("/resources/foo-e36d2e05253c6c7085a91522ce43a0b4.css", url);
}
// SPR-12592
@Test
public void initializeOnce() throws Exception {