diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/config/ResourceHandlerRegistration.java b/spring-webflux/src/main/java/org/springframework/web/reactive/config/ResourceHandlerRegistration.java index 9734347cc6..9c79953011 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/config/ResourceHandlerRegistration.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/config/ResourceHandlerRegistration.java @@ -21,6 +21,7 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.function.Function; import org.springframework.cache.Cache; import org.springframework.core.io.Resource; @@ -31,6 +32,7 @@ import org.springframework.http.MediaTypeFactory; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.web.reactive.resource.ResourceWebHandler; +import org.springframework.web.server.ServerWebExchange; /** * Assist with creating and configuring a static resources handler. @@ -54,6 +56,9 @@ public class ResourceHandlerRegistration { private boolean useLastModified = true; + @Nullable + private Function etagGenerator; + private boolean optimizeLocations = false; @Nullable @@ -117,6 +122,22 @@ public class ResourceHandlerRegistration { return this; } + + /** + * Configure a generator function that will be used to create the ETag information, + * given a {@link Resource} that is about to be written to the response. + *

This function should return a String that will be used as an argument in + * {@link ServerWebExchange#checkNotModified(String)}, or {@code null} if no value + * can be generated for the given resource. + * @param etagGenerator the HTTP ETag generator function to use. + * @since 6.1 + * @see ResourceWebHandler#setEtagGenerator(Function) + */ + public ResourceHandlerRegistration setEtagGenerator(@Nullable Function etagGenerator) { + this.etagGenerator = etagGenerator; + return this; + } + /** * Set whether to optimize the specified locations through an existence check on startup, * filtering non-existing directories upfront so that they do not have to be checked @@ -211,6 +232,7 @@ public class ResourceHandlerRegistration { handler.setCacheControl(this.cacheControl); } handler.setUseLastModified(this.useLastModified); + handler.setEtagGenerator(this.etagGenerator); handler.setOptimizeLocations(this.optimizeLocations); if (this.mediaTypes != null) { handler.setMediaTypes(this.mediaTypes); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistration.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistration.java index c173f60245..97e244c079 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistration.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistration.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2023 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. @@ -19,12 +19,14 @@ package org.springframework.web.servlet.config.annotation; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.function.Function; import org.springframework.cache.Cache; import org.springframework.core.io.Resource; import org.springframework.http.CacheControl; import org.springframework.lang.Nullable; import org.springframework.util.Assert; +import org.springframework.web.server.ServerWebExchange; import org.springframework.web.servlet.resource.PathResourceResolver; import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; @@ -55,6 +57,9 @@ public class ResourceHandlerRegistration { private boolean useLastModified = true; + @Nullable + private Function etagGenerator; + private boolean optimizeLocations = false; @@ -142,6 +147,21 @@ public class ResourceHandlerRegistration { return this; } + /** + * Configure a generator function that will be used to create the ETag information, + * given a {@link Resource} that is about to be written to the response. + *

This function should return a String that will be used as an argument in + * {@link ServerWebExchange#checkNotModified(String)}, or {@code null} if no value + * can be generated for the given resource. + * @param etagGenerator the HTTP ETag generator function to use. + * @since 6.1 + * @see ResourceHttpRequestHandler#setEtagGenerator(Function) + */ + public ResourceHandlerRegistration setEtagGenerator(@Nullable Function etagGenerator) { + this.etagGenerator = etagGenerator; + return this; + } + /** * Set whether to optimize the specified locations through an existence check on startup, * filtering non-existing directories upfront so that they do not have to be checked @@ -224,6 +244,7 @@ public class ResourceHandlerRegistration { handler.setCacheSeconds(this.cachePeriod); } handler.setUseLastModified(this.useLastModified); + handler.setEtagGenerator(this.etagGenerator); handler.setOptimizeLocations(this.optimizeLocations); return handler; }