Skip to content

Commit

Permalink
Allow ETag generation configuration on ResourceHandlerRegistration
Browse files Browse the repository at this point in the history
This commit surfaces the ETag generation feature for both
`ResourceHttpRequestHandler` and `ResourceWebHandler` on their
respective `ResourceHandlerRegistration` for easier configuration.

See gh-29031
  • Loading branch information
bclozel committed Oct 25, 2023
1 parent 9ac5eb0 commit c076f44
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;
Expand All @@ -31,6 +32,7 @@
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.
Expand All @@ -54,6 +56,9 @@ public class ResourceHandlerRegistration {

private boolean useLastModified = true;

@Nullable
private Function<Resource, String> etagGenerator;

private boolean optimizeLocations = false;

@Nullable
Expand Down Expand Up @@ -117,6 +122,22 @@ public ResourceHandlerRegistration setUseLastModified(boolean useLastModified) {
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,
* filtering non-existing directories upfront so that they do not have to be checked
Expand Down Expand Up @@ -211,6 +232,7 @@ protected ResourceWebHandler getRequestHandler() {
handler.setCacheControl(this.cacheControl);
}
handler.setUseLastModified(this.useLastModified);
handler.setEtagGenerator(this.etagGenerator);
handler.setOptimizeLocations(this.optimizeLocations);
if (this.mediaTypes != null) {
handler.setMediaTypes(this.mediaTypes);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -19,12 +19,14 @@
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;

Expand Down Expand Up @@ -55,6 +57,9 @@ public class ResourceHandlerRegistration {

private boolean useLastModified = true;

@Nullable
private Function<Resource, String> etagGenerator;

private boolean optimizeLocations = false;


Expand Down Expand Up @@ -142,6 +147,21 @@ public ResourceHandlerRegistration setUseLastModified(boolean useLastModified) {
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,
* filtering non-existing directories upfront so that they do not have to be checked
Expand Down Expand Up @@ -224,6 +244,7 @@ else if (this.cachePeriod != null) {
handler.setCacheSeconds(this.cachePeriod);
}
handler.setUseLastModified(this.useLastModified);
handler.setEtagGenerator(this.etagGenerator);
handler.setOptimizeLocations(this.optimizeLocations);
return handler;
}
Expand Down

0 comments on commit c076f44

Please sign in to comment.