Skip to content

Commit c076f44

Browse files
committed
Allow ETag generation configuration on ResourceHandlerRegistration
This commit surfaces the ETag generation feature for both `ResourceHttpRequestHandler` and `ResourceWebHandler` on their respective `ResourceHandlerRegistration` for easier configuration. See gh-29031
1 parent 9ac5eb0 commit c076f44

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/config/ResourceHandlerRegistration.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.HashMap;
2222
import java.util.List;
2323
import java.util.Map;
24+
import java.util.function.Function;
2425

2526
import org.springframework.cache.Cache;
2627
import org.springframework.core.io.Resource;
@@ -31,6 +32,7 @@
3132
import org.springframework.lang.Nullable;
3233
import org.springframework.util.Assert;
3334
import org.springframework.web.reactive.resource.ResourceWebHandler;
35+
import org.springframework.web.server.ServerWebExchange;
3436

3537
/**
3638
* Assist with creating and configuring a static resources handler.
@@ -54,6 +56,9 @@ public class ResourceHandlerRegistration {
5456

5557
private boolean useLastModified = true;
5658

59+
@Nullable
60+
private Function<Resource, String> etagGenerator;
61+
5762
private boolean optimizeLocations = false;
5863

5964
@Nullable
@@ -117,6 +122,22 @@ public ResourceHandlerRegistration setUseLastModified(boolean useLastModified) {
117122
return this;
118123
}
119124

125+
126+
/**
127+
* Configure a generator function that will be used to create the ETag information,
128+
* given a {@link Resource} that is about to be written to the response.
129+
* <p>This function should return a String that will be used as an argument in
130+
* {@link ServerWebExchange#checkNotModified(String)}, or {@code null} if no value
131+
* can be generated for the given resource.
132+
* @param etagGenerator the HTTP ETag generator function to use.
133+
* @since 6.1
134+
* @see ResourceWebHandler#setEtagGenerator(Function)
135+
*/
136+
public ResourceHandlerRegistration setEtagGenerator(@Nullable Function<Resource, String> etagGenerator) {
137+
this.etagGenerator = etagGenerator;
138+
return this;
139+
}
140+
120141
/**
121142
* Set whether to optimize the specified locations through an existence check on startup,
122143
* filtering non-existing directories upfront so that they do not have to be checked
@@ -211,6 +232,7 @@ protected ResourceWebHandler getRequestHandler() {
211232
handler.setCacheControl(this.cacheControl);
212233
}
213234
handler.setUseLastModified(this.useLastModified);
235+
handler.setEtagGenerator(this.etagGenerator);
214236
handler.setOptimizeLocations(this.optimizeLocations);
215237
if (this.mediaTypes != null) {
216238
handler.setMediaTypes(this.mediaTypes);

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistration.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,12 +19,14 @@
1919
import java.util.ArrayList;
2020
import java.util.Arrays;
2121
import java.util.List;
22+
import java.util.function.Function;
2223

2324
import org.springframework.cache.Cache;
2425
import org.springframework.core.io.Resource;
2526
import org.springframework.http.CacheControl;
2627
import org.springframework.lang.Nullable;
2728
import org.springframework.util.Assert;
29+
import org.springframework.web.server.ServerWebExchange;
2830
import org.springframework.web.servlet.resource.PathResourceResolver;
2931
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
3032

@@ -55,6 +57,9 @@ public class ResourceHandlerRegistration {
5557

5658
private boolean useLastModified = true;
5759

60+
@Nullable
61+
private Function<Resource, String> etagGenerator;
62+
5863
private boolean optimizeLocations = false;
5964

6065

@@ -142,6 +147,21 @@ public ResourceHandlerRegistration setUseLastModified(boolean useLastModified) {
142147
return this;
143148
}
144149

150+
/**
151+
* Configure a generator function that will be used to create the ETag information,
152+
* given a {@link Resource} that is about to be written to the response.
153+
* <p>This function should return a String that will be used as an argument in
154+
* {@link ServerWebExchange#checkNotModified(String)}, or {@code null} if no value
155+
* can be generated for the given resource.
156+
* @param etagGenerator the HTTP ETag generator function to use.
157+
* @since 6.1
158+
* @see ResourceHttpRequestHandler#setEtagGenerator(Function)
159+
*/
160+
public ResourceHandlerRegistration setEtagGenerator(@Nullable Function<Resource, String> etagGenerator) {
161+
this.etagGenerator = etagGenerator;
162+
return this;
163+
}
164+
145165
/**
146166
* Set whether to optimize the specified locations through an existence check on startup,
147167
* filtering non-existing directories upfront so that they do not have to be checked
@@ -224,6 +244,7 @@ else if (this.cachePeriod != null) {
224244
handler.setCacheSeconds(this.cachePeriod);
225245
}
226246
handler.setUseLastModified(this.useLastModified);
247+
handler.setEtagGenerator(this.etagGenerator);
227248
handler.setOptimizeLocations(this.optimizeLocations);
228249
return handler;
229250
}

0 commit comments

Comments
 (0)