Skip to content

Commit bfb2eff

Browse files
committed
Expose resource lookup function
This commit exposes the resource lookup function used by `RouterFunctions.resources(String, Resource)`, so that it can be composed upon. Issue: SPR-16788
1 parent cdf483f commit bfb2eff

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,35 @@ public static <T extends ServerResponse> RouterFunction<T> nest(
123123
* For instance
124124
* <pre class="code">
125125
* Resource location = new FileSystemResource("public-resources/");
126-
* RoutingFunction&lt;ServerResponse&gt; resources = RouterFunctions.resources("/resources/**", location);
126+
* RouterFunction&lt;ServerResponse&gt; resources = RouterFunctions.resources("/resources/**", location);
127127
* </pre>
128128
* @param pattern the pattern to match
129129
* @param location the location directory relative to which resources should be resolved
130130
* @return a router function that routes to resources
131+
* @see #resourceLookupFunction(String, Resource)
131132
*/
132133
public static RouterFunction<ServerResponse> resources(String pattern, Resource location) {
133-
return resources(new PathResourceLookupFunction(pattern, location));
134+
return resources(resourceLookupFunction(pattern, location));
135+
}
136+
137+
/**
138+
* Returns the resource lookup function used by {@link #resources(String, Resource)}.
139+
* The returned function can be {@linkplain Function#andThen(Function) composed} on, for
140+
* instance to return a default resource when the lookup function does not match:
141+
* <pre class="code">
142+
* Mono&lt;Resource&gt; defaultResource = Mono.just(new ClassPathResource("index.html"));
143+
* Function&lt;ServerRequest, Mono&lt;Resource&gt;&gt; lookupFunction =
144+
* RouterFunctions.resourceLookupFunction("/resources/**", new FileSystemResource("public-resources/"))
145+
* .andThen(resourceMono -&gt; resourceMono.switchIfEmpty(defaultResource));
146+
*
147+
* RouterFunction&lt;ServerResponse&gt; resources = RouterFunctions.resources(lookupFunction);
148+
* </pre>
149+
* @param pattern the pattern to match
150+
* @param location the location directory relative to which resources should be resolved
151+
* @return the default resource lookup function for the given parameters.
152+
*/
153+
public static Function<ServerRequest, Mono<Resource>> resourceLookupFunction(String pattern, Resource location) {
154+
return new PathResourceLookupFunction(pattern, location);
134155
}
135156

136157
/**

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.io.File;
2020
import java.io.IOException;
2121
import java.net.URI;
22+
import java.util.function.Function;
2223

2324
import org.junit.Test;
2425
import reactor.core.publisher.Mono;
@@ -95,4 +96,37 @@ public void notFound() throws Exception {
9596
.verify();
9697
}
9798

99+
@Test
100+
public void composeResourceLookupFunction() throws Exception {
101+
102+
Function<ServerRequest, Mono<Resource>> lookupFunction =
103+
new PathResourceLookupFunction("/resources/**",
104+
new ClassPathResource("org/springframework/web/reactive/function/server/"));
105+
106+
ClassPathResource defaultResource = new ClassPathResource("response.txt", getClass());
107+
108+
Function<ServerRequest, Mono<Resource>> customLookupFunction =
109+
lookupFunction.andThen(resourceMono -> resourceMono
110+
.switchIfEmpty(Mono.just(defaultResource)));
111+
112+
MockServerRequest request = MockServerRequest.builder()
113+
.uri(new URI("http://localhost/resources/foo"))
114+
.build();
115+
116+
Mono<Resource> result = customLookupFunction.apply(request);
117+
StepVerifier.create(result)
118+
.expectNextMatches(resource -> {
119+
try {
120+
return defaultResource.getFile().equals(resource.getFile());
121+
}
122+
catch (IOException ex) {
123+
return false;
124+
}
125+
})
126+
.expectComplete()
127+
.verify();
128+
129+
}
130+
131+
98132
}

0 commit comments

Comments
 (0)