Skip to content

Commit

Permalink
Filter non-existing static resource locations
Browse files Browse the repository at this point in the history
Same as a2c52a9, on the WebFlux side.

See gh-27538
  • Loading branch information
rstoyanchev committed Oct 13, 2021
1 parent 0436dd0 commit 4dac833
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 21 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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.
Expand Down Expand Up @@ -96,7 +96,9 @@ public class ResourceWebHandler implements WebHandler, InitializingBean {

private final List<String> locationValues = new ArrayList<>(4);

private final List<Resource> locations = new ArrayList<>(4);
private final List<Resource> locationResources = new ArrayList<>(4);

private final List<Resource> locationsToUse = new ArrayList<>(4);

private final List<ResourceResolver> resourceResolvers = new ArrayList<>(4);

Expand Down Expand Up @@ -147,9 +149,9 @@ public List<String> getLocationValues() {
* for serving static resources.
*/
public void setLocations(@Nullable List<Resource> locations) {
this.locations.clear();
this.locationResources.clear();
if (locations != null) {
this.locations.addAll(locations);
this.locationResources.addAll(locations);
}
}

Expand All @@ -159,11 +161,18 @@ public void setLocations(@Nullable List<Resource> locations) {
* <p>Note that if {@link #setLocationValues(List) locationValues} are provided,
* instead of loaded Resource-based locations, this method will return
* empty until after initialization via {@link #afterPropertiesSet()}.
* <p><strong>Note:</strong> As of 5.3.11 the list of locations is filtered
* to exclude those that don't actually exist and therefore the list returned
* from this method may be a subset of all given locations.
* @see #setLocationValues
* @see #setLocations
*/
public List<Resource> getLocations() {
return this.locations;
if (this.locationsToUse.isEmpty()) {
// Possibly not yet initialized, return only what we have so far
return this.locationResources;
}
return this.locationsToUse;
}

/**
Expand Down Expand Up @@ -295,7 +304,7 @@ public void setUseLastModified(boolean useLastModified) {
public void afterPropertiesSet() throws Exception {
resolveResourceLocations();

if (logger.isWarnEnabled() && CollectionUtils.isEmpty(this.locations)) {
if (logger.isWarnEnabled() && CollectionUtils.isEmpty(getLocations())) {
logger.warn("Locations list is empty. No resources will be served unless a " +
"custom ResourceResolver is configured as an alternative to PathResourceResolver.");
}
Expand All @@ -316,21 +325,22 @@ public void afterPropertiesSet() throws Exception {
}

private void resolveResourceLocations() {
if (CollectionUtils.isEmpty(this.locationValues)) {
return;
}
else if (!CollectionUtils.isEmpty(this.locations)) {
throw new IllegalArgumentException("Please set either Resource-based \"locations\" or " +
"String-based \"locationValues\", but not both.");
List<Resource> result = new ArrayList<>(this.locationResources);

if (!this.locationValues.isEmpty()) {
Assert.notNull(this.resourceLoader,
"ResourceLoader is required when \"locationValues\" are configured.");
Assert.isTrue(CollectionUtils.isEmpty(this.locationResources), "Please set " +
"either Resource-based \"locations\" or String-based \"locationValues\", but not both.");
for (String location : this.locationValues) {
result.add(this.resourceLoader.getResource(location));
}
}

Assert.notNull(this.resourceLoader,
"ResourceLoader is required when \"locationValues\" are configured.");
result = result.stream().filter(Resource::exists).collect(Collectors.toList());

for (String location : this.locationValues) {
Resource resource = this.resourceLoader.getResource(location);
this.locations.add(resource);
}
this.locationsToUse.clear();
this.locationsToUse.addAll(result);
}

/**
Expand All @@ -339,7 +349,7 @@ else if (!CollectionUtils.isEmpty(this.locations)) {
* match the {@link #setLocations locations} configured on this class.
*/
protected void initAllowedLocations() {
if (CollectionUtils.isEmpty(this.locations)) {
if (CollectionUtils.isEmpty(getLocations())) {
if (logger.isInfoEnabled()) {
logger.info("Locations list is empty. No resources will be served unless a " +
"custom ResourceResolver is configured as an alternative to PathResourceResolver.");
Expand Down Expand Up @@ -618,8 +628,8 @@ private Object formatLocations() {
if (!this.locationValues.isEmpty()) {
return this.locationValues.stream().collect(Collectors.joining("\", \"", "[\"", "\"]"));
}
else if (!this.locations.isEmpty()) {
return "[" + this.locations.toString()
if (!getLocations().isEmpty()) {
return "[" + getLocations().toString()
.replaceAll("class path resource", "Classpath")
.replaceAll("ServletContext resource", "ServletContext") + "]";
}
Expand Down
Expand Up @@ -253,6 +253,23 @@ public void getResourceFromFileSystem() throws Exception {
assertResponseBody(exchange, "h1 { color:red; }");
}

@Test // gh-27538
public void filterNonExistingLocations() throws Exception {
List<Resource> inputLocations = Arrays.asList(
new ClassPathResource("test/", getClass()),
new ClassPathResource("testalternatepath/", getClass()),
new ClassPathResource("nosuchpath/", getClass()));

ResourceWebHandler handler = new ResourceWebHandler();
handler.setLocations(inputLocations);
handler.afterPropertiesSet();

List<Resource> actual = handler.getLocations();
assertThat(actual).hasSize(2);
assertThat(actual.get(0).getURL().toString()).endsWith("test/");
assertThat(actual.get(1).getURL().toString()).endsWith("testalternatepath/");
}

@Test // SPR-14577
public void getMediaTypeWithFavorPathExtensionOff() throws Exception {
List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
Expand Down
Expand Up @@ -189,6 +189,9 @@ public void setLocations(List<Resource> locations) {
* locations provided via {@link #setLocations(List) setLocations}.
* <p>Note that the returned list is fully initialized only after
* initialization via {@link #afterPropertiesSet()}.
* <p><strong>Note:</strong> As of 5.3.11 the list of locations is filtered
* to exclude those that don't actually exist and therefore the list returned
* from this method may be a subset of all given locations.
* @see #setLocationValues
* @see #setLocations
*/
Expand Down Expand Up @@ -467,6 +470,7 @@ private void resolveResourceLocations() {
result.addAll(this.locationResources);
result = result.stream().filter(Resource::exists).collect(Collectors.toList());

this.locationsToUse.clear();
this.locationsToUse.addAll(result);
}

Expand Down

0 comments on commit 4dac833

Please sign in to comment.