Skip to content

Commit 307a2c7

Browse files
committed
Polishing external contribution
See gh-29985
1 parent ad50de1 commit 307a2c7

File tree

14 files changed

+236
-122
lines changed

14 files changed

+236
-122
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultEntityResponseBuilder.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 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.
@@ -115,6 +115,13 @@ public EntityResponse.Builder<T> headers(HttpHeaders headers) {
115115
return this;
116116
}
117117

118+
@Override
119+
public EntityResponse.Builder<T> headers(Consumer<HttpHeaders> headersConsumer) {
120+
Assert.notNull(headersConsumer, "HeadersConsumer must not be null");
121+
headersConsumer.accept(this.headers);
122+
return this;
123+
}
124+
118125
@Override
119126
public EntityResponse.Builder<T> allow(HttpMethod... allowedMethods) {
120127
this.headers.setAllow(new LinkedHashSet<>(Arrays.asList(allowedMethods)));

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultResourceCacheLookupStrategy.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/EntityResponse.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 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.
@@ -149,6 +149,18 @@ interface Builder<T> {
149149
*/
150150
Builder<T> headers(HttpHeaders headers);
151151

152+
/**
153+
* Manipulate this entity's headers with the given consumer. The
154+
* headers provided to the consumer are "live", so that the consumer can be used to
155+
* {@linkplain HttpHeaders#set(String, String) overwrite} existing header values,
156+
* {@linkplain HttpHeaders#remove(Object) remove} values, or use any of the other
157+
* {@link HttpHeaders} methods.
158+
* @param headersConsumer a function that consumes the {@code HttpHeaders}
159+
* @return this builder
160+
* @since 6.1
161+
*/
162+
Builder<T> headers(Consumer<HttpHeaders> headersConsumer);
163+
152164
/**
153165
* Set the HTTP status.
154166
* @param status the response status

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ResourceCacheLookupStrategy.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ResourceHandlerFunction.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 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.
@@ -23,11 +23,12 @@
2323
import java.net.URI;
2424
import java.net.URL;
2525
import java.util.Set;
26+
import java.util.function.BiConsumer;
2627

2728
import reactor.core.publisher.Mono;
2829

2930
import org.springframework.core.io.Resource;
30-
import org.springframework.http.CacheControl;
31+
import org.springframework.http.HttpHeaders;
3132
import org.springframework.http.HttpMethod;
3233
import org.springframework.http.HttpStatus;
3334
import org.springframework.lang.Nullable;
@@ -46,18 +47,13 @@ class ResourceHandlerFunction implements HandlerFunction<ServerResponse> {
4647

4748

4849
private final Resource resource;
49-
private final CacheControl cacheControl;
5050

51-
52-
public ResourceHandlerFunction(Resource resource) {
53-
this.resource = resource;
54-
this.cacheControl = CacheControl.empty();
55-
}
51+
private final BiConsumer<Resource, HttpHeaders> headersConsumer;
5652

5753

58-
public ResourceHandlerFunction(Resource resource, ResourceCacheLookupStrategy strategy) {
54+
public ResourceHandlerFunction(Resource resource, BiConsumer<Resource, HttpHeaders> headersConsumer) {
5955
this.resource = resource;
60-
this.cacheControl = strategy.lookupCacheControl(resource);
56+
this.headersConsumer = headersConsumer;
6157
}
6258

6359

@@ -66,14 +62,14 @@ public Mono<ServerResponse> handle(ServerRequest request) {
6662
HttpMethod method = request.method();
6763
if (HttpMethod.GET.equals(method)) {
6864
return EntityResponse.fromObject(this.resource)
69-
.cacheControl(this.cacheControl)
65+
.headers(headers -> this.headersConsumer.accept(this.resource, headers))
7066
.build()
7167
.map(response -> response);
7268
}
7369
else if (HttpMethod.HEAD.equals(method)) {
7470
Resource headResource = new HeadMethodResource(this.resource);
7571
return EntityResponse.fromObject(headResource)
76-
.cacheControl(this.cacheControl)
72+
.headers(headers -> this.headersConsumer.accept(this.resource, headers))
7773
.build()
7874
.map(response -> response);
7975
}

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RouterFunctionBuilder.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 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,6 +19,7 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121
import java.util.Map;
22+
import java.util.function.BiConsumer;
2223
import java.util.function.BiFunction;
2324
import java.util.function.Consumer;
2425
import java.util.function.Function;
@@ -30,6 +31,7 @@
3031
import reactor.core.publisher.Mono;
3132

3233
import org.springframework.core.io.Resource;
34+
import org.springframework.http.HttpHeaders;
3335
import org.springframework.http.HttpMethod;
3436
import org.springframework.util.Assert;
3537

@@ -242,8 +244,10 @@ public RouterFunctions.Builder resources(String pattern, Resource location) {
242244
}
243245

244246
@Override
245-
public RouterFunctions.Builder resources(String pattern, Resource location, ResourceCacheLookupStrategy resourceCacheLookupStrategy) {
246-
return add(RouterFunctions.resources(pattern,location,resourceCacheLookupStrategy));
247+
public RouterFunctions.Builder resources(String pattern, Resource location,
248+
BiConsumer<Resource, HttpHeaders> headersConsumer) {
249+
250+
return add(RouterFunctions.resources(pattern, location, headersConsumer));
247251
}
248252

249253
@Override
@@ -252,8 +256,10 @@ public RouterFunctions.Builder resources(Function<ServerRequest, Mono<Resource>>
252256
}
253257

254258
@Override
255-
public RouterFunctions.Builder resources(Function<ServerRequest, Mono<Resource>> lookupFunction, ResourceCacheLookupStrategy resourceCacheLookupStrategy) {
256-
return add(RouterFunctions.resources(lookupFunction,resourceCacheLookupStrategy));
259+
public RouterFunctions.Builder resources(Function<ServerRequest, Mono<Resource>> lookupFunction,
260+
BiConsumer<Resource, HttpHeaders> headersConsumer) {
261+
262+
return add(RouterFunctions.resources(lookupFunction, headersConsumer));
257263
}
258264

259265
@Override

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RouterFunctions.java

Lines changed: 68 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 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.
@@ -20,6 +20,7 @@
2020
import java.util.LinkedHashMap;
2121
import java.util.List;
2222
import java.util.Map;
23+
import java.util.function.BiConsumer;
2324
import java.util.function.BiFunction;
2425
import java.util.function.Consumer;
2526
import java.util.function.Function;
@@ -32,6 +33,7 @@
3233
import reactor.core.publisher.Mono;
3334

3435
import org.springframework.core.io.Resource;
36+
import org.springframework.http.HttpHeaders;
3537
import org.springframework.http.HttpStatus;
3638
import org.springframework.http.codec.HttpMessageWriter;
3739
import org.springframework.http.server.reactive.HttpHandler;
@@ -156,10 +158,26 @@ public static <T extends ServerResponse> RouterFunction<T> nest(
156158
* @see #resourceLookupFunction(String, Resource)
157159
*/
158160
public static RouterFunction<ServerResponse> resources(String pattern, Resource location) {
159-
return resources(resourceLookupFunction(pattern, location), ResourceCacheLookupStrategy.noCaching());
161+
return resources(resourceLookupFunction(pattern, location), (resource, httpHeaders) -> {});
160162
}
161-
public static RouterFunction<ServerResponse> resources(String pattern, Resource location, ResourceCacheLookupStrategy lookupStrategy) {
162-
return resources(resourceLookupFunction(pattern, location), lookupStrategy);
163+
164+
/**
165+
* Route requests that match the given pattern to resources relative to the given root location.
166+
* For instance
167+
* <pre class="code">
168+
* Resource location = new FileSystemResource("public-resources/");
169+
* RouterFunction&lt;ServerResponse&gt; resources = RouterFunctions.resources("/resources/**", location);
170+
* </pre>
171+
* @param pattern the pattern to match
172+
* @param location the location directory relative to which resources should be resolved
173+
* @param headersConsumer provides access to the HTTP headers for served resources
174+
* @return a router function that routes to resources
175+
* @since 6.1
176+
* @see #resourceLookupFunction(String, Resource)
177+
*/
178+
public static RouterFunction<ServerResponse> resources(String pattern, Resource location,
179+
BiConsumer<Resource, HttpHeaders> headersConsumer) {
180+
return resources(resourceLookupFunction(pattern, location), headersConsumer);
163181
}
164182

165183
/**
@@ -189,10 +207,21 @@ public static Function<ServerRequest, Mono<Resource>> resourceLookupFunction(Str
189207
* @return a router function that routes to resources
190208
*/
191209
public static RouterFunction<ServerResponse> resources(Function<ServerRequest, Mono<Resource>> lookupFunction) {
192-
return new ResourcesRouterFunction(lookupFunction, ResourceCacheLookupStrategy.noCaching());
210+
return new ResourcesRouterFunction(lookupFunction, (resource, httpHeaders) -> {});
193211
}
194-
public static RouterFunction<ServerResponse> resources(Function<ServerRequest, Mono<Resource>> lookupFunction, ResourceCacheLookupStrategy lookupStrategy) {
195-
return new ResourcesRouterFunction(lookupFunction, lookupStrategy);
212+
213+
/**
214+
* Route to resources using the provided lookup function. If the lookup function provides a
215+
* {@link Resource} for the given request, it will be it will be exposed using a
216+
* {@link HandlerFunction} that handles GET, HEAD, and OPTIONS requests.
217+
* @param lookupFunction the function to provide a {@link Resource} given the {@link ServerRequest}
218+
* @param headersConsumer provides access to the HTTP headers for served resources
219+
* @return a router function that routes to resources
220+
* @since 6.1
221+
*/
222+
public static RouterFunction<ServerResponse> resources(Function<ServerRequest, Mono<Resource>> lookupFunction,
223+
BiConsumer<Resource, HttpHeaders> headersConsumer) {
224+
return new ResourcesRouterFunction(lookupFunction, headersConsumer);
196225
}
197226

198227
/**
@@ -658,7 +687,21 @@ public interface Builder {
658687
* @return this builder
659688
*/
660689
Builder resources(String pattern, Resource location);
661-
Builder resources(String pattern, Resource location, ResourceCacheLookupStrategy resourceCacheLookupStrategy);
690+
691+
/**
692+
* Route requests that match the given pattern to resources relative to the given root location.
693+
* For instance
694+
* <pre class="code">
695+
* Resource location = new FileSystemResource("public-resources/");
696+
* RouterFunction&lt;ServerResponse&gt; resources = RouterFunctions.resources("/resources/**", location);
697+
* </pre>
698+
* @param pattern the pattern to match
699+
* @param location the location directory relative to which resources should be resolved
700+
* @param headersConsumer provides access to the HTTP headers for served resources
701+
* @return this builder
702+
* @since 6.1
703+
*/
704+
Builder resources(String pattern, Resource location, BiConsumer<Resource, HttpHeaders> headersConsumer);
662705

663706
/**
664707
* Route to resources using the provided lookup function. If the lookup function provides a
@@ -668,7 +711,17 @@ public interface Builder {
668711
* @return this builder
669712
*/
670713
Builder resources(Function<ServerRequest, Mono<Resource>> lookupFunction);
671-
Builder resources(Function<ServerRequest, Mono<Resource>> lookupFunction, ResourceCacheLookupStrategy resourceCacheLookupStrategy);
714+
715+
/**
716+
* Route to resources using the provided lookup function. If the lookup function provides a
717+
* {@link Resource} for the given request, it will be it will be exposed using a
718+
* {@link HandlerFunction} that handles GET, HEAD, and OPTIONS requests.
719+
* @param lookupFunction the function to provide a {@link Resource} given the {@link ServerRequest}
720+
* @param headersConsumer provides access to the HTTP headers for served resources
721+
* @return this builder
722+
* @since 6.1
723+
*/
724+
Builder resources(Function<ServerRequest, Mono<Resource>> lookupFunction, BiConsumer<Resource, HttpHeaders> headersConsumer);
672725

673726
/**
674727
* Route to the supplied router function if the given request predicate applies. This method
@@ -1150,22 +1203,20 @@ private static class ResourcesRouterFunction extends AbstractRouterFunction<Ser
11501203

11511204
private final Function<ServerRequest, Mono<Resource>> lookupFunction;
11521205

1153-
private final ResourceCacheLookupStrategy lookupStrategy;
1206+
private final BiConsumer<Resource, HttpHeaders> headersConsumer;
11541207

1155-
public ResourcesRouterFunction(Function<ServerRequest, Mono<Resource>> lookupFunction) {
1156-
this(lookupFunction, ResourceCacheLookupStrategy.noCaching());
1157-
}
11581208

1159-
public ResourcesRouterFunction(Function<ServerRequest, Mono<Resource>> lookupFunction, ResourceCacheLookupStrategy lookupStrategy) {
1209+
public ResourcesRouterFunction(Function<ServerRequest, Mono<Resource>> lookupFunction,
1210+
BiConsumer<Resource, HttpHeaders> headersConsumer) {
11601211
Assert.notNull(lookupFunction, "Function must not be null");
1161-
Assert.notNull(lookupStrategy, "Strategy must not be null");
1212+
Assert.notNull(headersConsumer, "HeadersConsumer must not be null");
11621213
this.lookupFunction = lookupFunction;
1163-
this.lookupStrategy = lookupStrategy;
1214+
this.headersConsumer = headersConsumer;
11641215
}
11651216

11661217
@Override
11671218
public Mono<HandlerFunction<ServerResponse>> route(ServerRequest request) {
1168-
return this.lookupFunction.apply(request).map(resource -> new ResourceHandlerFunction(resource, this.lookupStrategy));
1219+
return this.lookupFunction.apply(request).map(resource -> new ResourceHandlerFunction(resource, this.headersConsumer));
11691220
}
11701221

11711222
@Override

spring-webflux/src/test/java/org/springframework/web/reactive/function/server/ResourceHandlerFunctionTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 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.
@@ -47,7 +47,7 @@ public class ResourceHandlerFunctionTests {
4747

4848
private final Resource resource = new ClassPathResource("response.txt", getClass());
4949

50-
private final ResourceHandlerFunction handlerFunction = new ResourceHandlerFunction(this.resource);
50+
private final ResourceHandlerFunction handlerFunction = new ResourceHandlerFunction(this.resource, (r, h) -> {});
5151

5252
private ServerResponse.Context context;
5353

0 commit comments

Comments
 (0)