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
This commit is contained in:
@@ -123,14 +123,35 @@ public abstract class RouterFunctions {
|
||||
* For instance
|
||||
* <pre class="code">
|
||||
* Resource location = new FileSystemResource("public-resources/");
|
||||
* RoutingFunction<ServerResponse> resources = RouterFunctions.resources("/resources/**", location);
|
||||
* RouterFunction<ServerResponse> resources = RouterFunctions.resources("/resources/**", location);
|
||||
* </pre>
|
||||
* @param pattern the pattern to match
|
||||
* @param location the location directory relative to which resources should be resolved
|
||||
* @return a router function that routes to resources
|
||||
* @see #resourceLookupFunction(String, Resource)
|
||||
*/
|
||||
public static RouterFunction<ServerResponse> resources(String pattern, Resource location) {
|
||||
return resources(new PathResourceLookupFunction(pattern, location));
|
||||
return resources(resourceLookupFunction(pattern, location));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the resource lookup function used by {@link #resources(String, Resource)}.
|
||||
* The returned function can be {@linkplain Function#andThen(Function) composed} on, for
|
||||
* instance to return a default resource when the lookup function does not match:
|
||||
* <pre class="code">
|
||||
* Mono<Resource> defaultResource = Mono.just(new ClassPathResource("index.html"));
|
||||
* Function<ServerRequest, Mono<Resource>> lookupFunction =
|
||||
* RouterFunctions.resourceLookupFunction("/resources/**", new FileSystemResource("public-resources/"))
|
||||
* .andThen(resourceMono -> resourceMono.switchIfEmpty(defaultResource));
|
||||
*
|
||||
* RouterFunction<ServerResponse> resources = RouterFunctions.resources(lookupFunction);
|
||||
* </pre>
|
||||
* @param pattern the pattern to match
|
||||
* @param location the location directory relative to which resources should be resolved
|
||||
* @return the default resource lookup function for the given parameters.
|
||||
*/
|
||||
public static Function<ServerRequest, Mono<Resource>> resourceLookupFunction(String pattern, Resource location) {
|
||||
return new PathResourceLookupFunction(pattern, location);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user