Browse Source

Merge branch '5.3.x' into main

pull/27550/head
Rossen Stoyanchev 5 years ago
parent
commit
7957b9cc0f
  1. 10
      spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java
  2. 20
      spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java

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

@ -25,6 +25,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
import jakarta.servlet.ServletException; import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
@ -423,6 +424,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
} }
private void resolveResourceLocations() { private void resolveResourceLocations() {
List<Resource> result = new ArrayList<>();
if (!this.locationValues.isEmpty()) { if (!this.locationValues.isEmpty()) {
ApplicationContext applicationContext = obtainApplicationContext(); ApplicationContext applicationContext = obtainApplicationContext();
for (String location : this.locationValues) { for (String location : this.locationValues) {
@ -451,7 +453,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
"but resolved to a Resource of type: " + resource.getClass() + ". " + "but resolved to a Resource of type: " + resource.getClass() + ". " +
"If this is intentional, please pass it as a pre-configured Resource via setLocations."); "If this is intentional, please pass it as a pre-configured Resource via setLocations.");
} }
this.locationsToUse.add(resource); result.add(resource);
if (charset != null) { if (charset != null) {
if (!(resource instanceof UrlResource)) { if (!(resource instanceof UrlResource)) {
throw new IllegalArgumentException("Unexpected charset for non-UrlResource: " + resource); throw new IllegalArgumentException("Unexpected charset for non-UrlResource: " + resource);
@ -460,7 +462,11 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
} }
} }
} }
this.locationsToUse.addAll(this.locationResources);
result.addAll(this.locationResources);
result = result.stream().filter(Resource::exists).collect(Collectors.toList());
this.locationsToUse.addAll(result);
} }
/** /**

20
spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -311,6 +311,24 @@ public class ResourceHttpRequestHandlerTests {
assertThat(this.response.getContentAsString()).isEqualTo("h1 { color:red; }"); assertThat(this.response.getContentAsString()).isEqualTo("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()));
ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
handler.setServletContext(new MockServletContext());
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 @Test
public void testInvalidPath() throws Exception { public void testInvalidPath() throws Exception {

Loading…
Cancel
Save