|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2021 the original author or authors. |
| 2 | + * Copyright 2002-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
19 | 19 | import java.util.ArrayList;
|
20 | 20 | import java.util.Arrays;
|
21 | 21 | import java.util.List;
|
| 22 | +import java.util.function.Function; |
22 | 23 |
|
23 | 24 | import org.springframework.cache.Cache;
|
24 | 25 | import org.springframework.core.io.Resource;
|
25 | 26 | import org.springframework.http.CacheControl;
|
26 | 27 | import org.springframework.lang.Nullable;
|
27 | 28 | import org.springframework.util.Assert;
|
| 29 | +import org.springframework.web.server.ServerWebExchange; |
28 | 30 | import org.springframework.web.servlet.resource.PathResourceResolver;
|
29 | 31 | import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
|
30 | 32 |
|
@@ -55,6 +57,9 @@ public class ResourceHandlerRegistration {
|
55 | 57 |
|
56 | 58 | private boolean useLastModified = true;
|
57 | 59 |
|
| 60 | + @Nullable |
| 61 | + private Function<Resource, String> etagGenerator; |
| 62 | + |
58 | 63 | private boolean optimizeLocations = false;
|
59 | 64 |
|
60 | 65 |
|
@@ -142,6 +147,21 @@ public ResourceHandlerRegistration setUseLastModified(boolean useLastModified) {
|
142 | 147 | return this;
|
143 | 148 | }
|
144 | 149 |
|
| 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 | + |
145 | 165 | /**
|
146 | 166 | * Set whether to optimize the specified locations through an existence check on startup,
|
147 | 167 | * filtering non-existing directories upfront so that they do not have to be checked
|
@@ -224,6 +244,7 @@ else if (this.cachePeriod != null) {
|
224 | 244 | handler.setCacheSeconds(this.cachePeriod);
|
225 | 245 | }
|
226 | 246 | handler.setUseLastModified(this.useLastModified);
|
| 247 | + handler.setEtagGenerator(this.etagGenerator); |
227 | 248 | handler.setOptimizeLocations(this.optimizeLocations);
|
228 | 249 | return handler;
|
229 | 250 | }
|
|
0 commit comments