Accept only ClassPathResource in ResourceHints#registerResource()

This commit throws an exception in registerResource() if the supplied
resource is not a ClassPathResource.

See gh-29083
This commit is contained in:
Sam Brannen 2022-09-11 23:33:39 +02:00
parent 7605ed7862
commit 649c2f56fd
2 changed files with 10 additions and 10 deletions

View File

@ -27,7 +27,6 @@ import java.util.stream.Stream;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/** /**
* Gather the need for resources available at runtime. * Gather the need for resources available at runtime.
@ -115,19 +114,19 @@ public class ResourceHints {
/** /**
* Register that the supplied resource should be made available at runtime. * Register that the supplied resource should be made available at runtime.
* <p>If the supplied resource is not a {@link ClassPathResource}, it will
* not be registered.
* @param resource the resource to register * @param resource the resource to register
* @throws IllegalArgumentException if the supplied class path resource does * @throws IllegalArgumentException if the supplied resource is not a
* not {@linkplain Resource#exists() exist} * {@link ClassPathResource} or does not {@linkplain Resource#exists() exist}
* @see #registerPattern(String) * @see #registerPattern(String)
* @see ClassPathResource#getAbsolutePath() * @see ClassPathResource#getAbsolutePath()
*/ */
public void registerResource(Resource resource) { public void registerResource(Resource resource) {
if (resource instanceof ClassPathResource classPathResource) { if (resource instanceof ClassPathResource classPathResource && classPathResource.exists()) {
Assert.isTrue(classPathResource.exists(), () -> "Resource does not exist: " + classPathResource);
registerPattern(classPathResource.getAbsolutePath()); registerPattern(classPathResource.getAbsolutePath());
} }
else {
throw new IllegalArgumentException("Resource must be a ClassPathResource that exists: " + resource);
}
} }
/** /**

View File

@ -117,8 +117,9 @@ class ResourceHintsTests {
@Test @Test
void registerResourceWithUnsupportedResourceType() { void registerResourceWithUnsupportedResourceType() {
DescriptiveResource resource = new DescriptiveResource("bogus"); DescriptiveResource resource = new DescriptiveResource("bogus");
this.resourceHints.registerResource(resource); assertThatIllegalArgumentException()
assertThat(this.resourceHints.resourcePatterns()).isEmpty(); .isThrownBy(() -> this.resourceHints.registerResource(resource))
.withMessage("Resource must be a ClassPathResource that exists: %s", resource);
} }
@Test @Test
@ -126,7 +127,7 @@ class ResourceHintsTests {
ClassPathResource resource = new ClassPathResource("bogus", getClass()); ClassPathResource resource = new ClassPathResource("bogus", getClass());
assertThatIllegalArgumentException() assertThatIllegalArgumentException()
.isThrownBy(() -> this.resourceHints.registerResource(resource)) .isThrownBy(() -> this.resourceHints.registerResource(resource))
.withMessage("Resource does not exist: %s", resource); .withMessage("Resource must be a ClassPathResource that exists: %s", resource);
} }
@Test @Test