Browse Source

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
pull/31518/head
Brian Clozel 1 year ago
parent
commit
c076f44144
  1. 22
      spring-webflux/src/main/java/org/springframework/web/reactive/config/ResourceHandlerRegistration.java
  2. 23
      spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistration.java

22
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.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Function;
import org.springframework.cache.Cache; import org.springframework.cache.Cache;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
@ -31,6 +32,7 @@ import org.springframework.http.MediaTypeFactory;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.web.reactive.resource.ResourceWebHandler; import org.springframework.web.reactive.resource.ResourceWebHandler;
import org.springframework.web.server.ServerWebExchange;
/** /**
* Assist with creating and configuring a static resources handler. * Assist with creating and configuring a static resources handler.
@ -54,6 +56,9 @@ public class ResourceHandlerRegistration {
private boolean useLastModified = true; private boolean useLastModified = true;
@Nullable
private Function<Resource, String> etagGenerator;
private boolean optimizeLocations = false; private boolean optimizeLocations = false;
@Nullable @Nullable
@ -117,6 +122,22 @@ public class ResourceHandlerRegistration {
return this; 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.
* <p>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<Resource, String> etagGenerator) {
this.etagGenerator = etagGenerator;
return this;
}
/** /**
* Set whether to optimize the specified locations through an existence check on startup, * 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 * 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.setCacheControl(this.cacheControl);
} }
handler.setUseLastModified(this.useLastModified); handler.setUseLastModified(this.useLastModified);
handler.setEtagGenerator(this.etagGenerator);
handler.setOptimizeLocations(this.optimizeLocations); handler.setOptimizeLocations(this.optimizeLocations);
if (this.mediaTypes != null) { if (this.mediaTypes != null) {
handler.setMediaTypes(this.mediaTypes); handler.setMediaTypes(this.mediaTypes);

23
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"); * 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.
@ -19,12 +19,14 @@ package org.springframework.web.servlet.config.annotation;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.function.Function;
import org.springframework.cache.Cache; import org.springframework.cache.Cache;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.http.CacheControl; import org.springframework.http.CacheControl;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.servlet.resource.PathResourceResolver; import org.springframework.web.servlet.resource.PathResourceResolver;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
@ -55,6 +57,9 @@ public class ResourceHandlerRegistration {
private boolean useLastModified = true; private boolean useLastModified = true;
@Nullable
private Function<Resource, String> etagGenerator;
private boolean optimizeLocations = false; private boolean optimizeLocations = false;
@ -142,6 +147,21 @@ public class ResourceHandlerRegistration {
return this; 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.
* <p>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<Resource, String> etagGenerator) {
this.etagGenerator = etagGenerator;
return this;
}
/** /**
* Set whether to optimize the specified locations through an existence check on startup, * 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 * 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.setCacheSeconds(this.cachePeriod);
} }
handler.setUseLastModified(this.useLastModified); handler.setUseLastModified(this.useLastModified);
handler.setEtagGenerator(this.etagGenerator);
handler.setOptimizeLocations(this.optimizeLocations); handler.setOptimizeLocations(this.optimizeLocations);
return handler; return handler;
} }

Loading…
Cancel
Save