Allow custom instances of WebJarAssetLocator

This commit allows to configure a custom WebJarAssetLocator in
WebJarResourceResolvers.

Issue: SPR-14092
This commit is contained in:
Brian Clozel
2016-03-30 11:12:10 +02:00
parent 55dae618a6
commit 977840884b
3 changed files with 30 additions and 6 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -56,6 +56,8 @@ public class ResourceChainRegistration {
private boolean hasCssLinkTransformer;
private boolean hasWebjarsResolver;
public ResourceChainRegistration(boolean cacheResources) {
this(cacheResources, cacheResources ? new ConcurrentMapCache(DEFAULT_CACHE_NAME) : null);
@@ -84,6 +86,9 @@ public class ResourceChainRegistration {
else if (resolver instanceof PathResourceResolver) {
this.hasPathResolver = true;
}
else if (resolver instanceof WebJarsResourceResolver) {
this.hasWebjarsResolver = true;
}
return this;
}
@@ -104,7 +109,7 @@ public class ResourceChainRegistration {
protected List<ResourceResolver> getResourceResolvers() {
if (!this.hasPathResolver) {
List<ResourceResolver> result = new ArrayList<ResourceResolver>(this.resolvers);
if (isWebJarsAssetLocatorPresent) {
if (isWebJarsAssetLocatorPresent && !this.hasWebjarsResolver) {
result.add(new WebJarsResourceResolver());
}
result.add(new PathResourceResolver());
@@ -114,7 +119,7 @@ public class ResourceChainRegistration {
}
protected List<ResourceTransformer> getResourceTransformers() {
if (this.hasVersionResolver && !this.hasCssLinkTransformer) {
if (this.hasVersionResolver && !this.hasCssLinkTransformer) {
List<ResourceTransformer> result = new ArrayList<ResourceTransformer>(this.transformers);
boolean hasTransformers = !this.transformers.isEmpty();
boolean hasCaching = hasTransformers && this.transformers.get(0) instanceof CachingResourceTransformer;

View File

@@ -17,6 +17,7 @@
package org.springframework.web.servlet.resource;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.webjars.MultipleMatchesException;
@@ -49,8 +50,23 @@ public class WebJarsResourceResolver extends AbstractResourceResolver {
private final static int WEBJARS_LOCATION_LENGTH = WEBJARS_LOCATION.length();
private final WebJarAssetLocator webJarAssetLocator = new WebJarAssetLocator();
private final WebJarAssetLocator webJarAssetLocator;
/**
* Create a {@code WebJarsResourceResolver} with a default {@code WebJarAssetLocator} instance.
*/
public WebJarsResourceResolver() {
this(new WebJarAssetLocator());
}
/**
* Create a {@code WebJarsResourceResolver} with a custom {@code WebJarAssetLocator} instance,
* e.g. with a custom index.
* @since 4.3.0
*/
public WebJarsResourceResolver(WebJarAssetLocator webJarAssetLocator) {
this.webJarAssetLocator = webJarAssetLocator;
}
@Override
protected Resource resolveResourceInternal(HttpServletRequest request, String requestPath,