|
|
|
@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
|
|
|
|
|
/* |
|
|
|
|
* Copyright 2002-2022 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. |
|
|
|
@ -20,6 +20,7 @@ import java.util.Collections;
@@ -20,6 +20,7 @@ import java.util.Collections;
|
|
|
|
|
import java.util.LinkedHashMap; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.function.BiConsumer; |
|
|
|
|
import java.util.function.BiFunction; |
|
|
|
|
import java.util.function.Consumer; |
|
|
|
|
import java.util.function.Function; |
|
|
|
@ -32,6 +33,7 @@ import reactor.core.publisher.Flux;
@@ -32,6 +33,7 @@ import reactor.core.publisher.Flux;
|
|
|
|
|
import reactor.core.publisher.Mono; |
|
|
|
|
|
|
|
|
|
import org.springframework.core.io.Resource; |
|
|
|
|
import org.springframework.http.HttpHeaders; |
|
|
|
|
import org.springframework.http.HttpStatus; |
|
|
|
|
import org.springframework.http.codec.HttpMessageWriter; |
|
|
|
|
import org.springframework.http.server.reactive.HttpHandler; |
|
|
|
@ -156,10 +158,26 @@ public abstract class RouterFunctions {
@@ -156,10 +158,26 @@ public abstract class RouterFunctions {
|
|
|
|
|
* @see #resourceLookupFunction(String, Resource) |
|
|
|
|
*/ |
|
|
|
|
public static RouterFunction<ServerResponse> resources(String pattern, Resource location) { |
|
|
|
|
return resources(resourceLookupFunction(pattern, location), ResourceCacheLookupStrategy.noCaching()); |
|
|
|
|
return resources(resourceLookupFunction(pattern, location), (resource, httpHeaders) -> {}); |
|
|
|
|
} |
|
|
|
|
public static RouterFunction<ServerResponse> resources(String pattern, Resource location, ResourceCacheLookupStrategy lookupStrategy) { |
|
|
|
|
return resources(resourceLookupFunction(pattern, location), lookupStrategy); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Route requests that match the given pattern to resources relative to the given root location. |
|
|
|
|
* For instance |
|
|
|
|
* <pre class="code"> |
|
|
|
|
* Resource location = new FileSystemResource("public-resources/"); |
|
|
|
|
* 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 |
|
|
|
|
* @param headersConsumer provides access to the HTTP headers for served resources |
|
|
|
|
* @return a router function that routes to resources |
|
|
|
|
* @since 6.1 |
|
|
|
|
* @see #resourceLookupFunction(String, Resource) |
|
|
|
|
*/ |
|
|
|
|
public static RouterFunction<ServerResponse> resources(String pattern, Resource location, |
|
|
|
|
BiConsumer<Resource, HttpHeaders> headersConsumer) { |
|
|
|
|
return resources(resourceLookupFunction(pattern, location), headersConsumer); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -189,10 +207,21 @@ public abstract class RouterFunctions {
@@ -189,10 +207,21 @@ public abstract class RouterFunctions {
|
|
|
|
|
* @return a router function that routes to resources |
|
|
|
|
*/ |
|
|
|
|
public static RouterFunction<ServerResponse> resources(Function<ServerRequest, Mono<Resource>> lookupFunction) { |
|
|
|
|
return new ResourcesRouterFunction(lookupFunction, ResourceCacheLookupStrategy.noCaching()); |
|
|
|
|
return new ResourcesRouterFunction(lookupFunction, (resource, httpHeaders) -> {}); |
|
|
|
|
} |
|
|
|
|
public static RouterFunction<ServerResponse> resources(Function<ServerRequest, Mono<Resource>> lookupFunction, ResourceCacheLookupStrategy lookupStrategy) { |
|
|
|
|
return new ResourcesRouterFunction(lookupFunction, lookupStrategy); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Route to resources using the provided lookup function. If the lookup function provides a |
|
|
|
|
* {@link Resource} for the given request, it will be it will be exposed using a |
|
|
|
|
* {@link HandlerFunction} that handles GET, HEAD, and OPTIONS requests. |
|
|
|
|
* @param lookupFunction the function to provide a {@link Resource} given the {@link ServerRequest} |
|
|
|
|
* @param headersConsumer provides access to the HTTP headers for served resources |
|
|
|
|
* @return a router function that routes to resources |
|
|
|
|
* @since 6.1 |
|
|
|
|
*/ |
|
|
|
|
public static RouterFunction<ServerResponse> resources(Function<ServerRequest, Mono<Resource>> lookupFunction, |
|
|
|
|
BiConsumer<Resource, HttpHeaders> headersConsumer) { |
|
|
|
|
return new ResourcesRouterFunction(lookupFunction, headersConsumer); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -658,7 +687,21 @@ public abstract class RouterFunctions {
@@ -658,7 +687,21 @@ public abstract class RouterFunctions {
|
|
|
|
|
* @return this builder |
|
|
|
|
*/ |
|
|
|
|
Builder resources(String pattern, Resource location); |
|
|
|
|
Builder resources(String pattern, Resource location, ResourceCacheLookupStrategy resourceCacheLookupStrategy); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Route requests that match the given pattern to resources relative to the given root location. |
|
|
|
|
* For instance |
|
|
|
|
* <pre class="code"> |
|
|
|
|
* Resource location = new FileSystemResource("public-resources/"); |
|
|
|
|
* 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 |
|
|
|
|
* @param headersConsumer provides access to the HTTP headers for served resources |
|
|
|
|
* @return this builder |
|
|
|
|
* @since 6.1 |
|
|
|
|
*/ |
|
|
|
|
Builder resources(String pattern, Resource location, BiConsumer<Resource, HttpHeaders> headersConsumer); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Route to resources using the provided lookup function. If the lookup function provides a |
|
|
|
@ -668,7 +711,17 @@ public abstract class RouterFunctions {
@@ -668,7 +711,17 @@ public abstract class RouterFunctions {
|
|
|
|
|
* @return this builder |
|
|
|
|
*/ |
|
|
|
|
Builder resources(Function<ServerRequest, Mono<Resource>> lookupFunction); |
|
|
|
|
Builder resources(Function<ServerRequest, Mono<Resource>> lookupFunction, ResourceCacheLookupStrategy resourceCacheLookupStrategy); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Route to resources using the provided lookup function. If the lookup function provides a |
|
|
|
|
* {@link Resource} for the given request, it will be it will be exposed using a |
|
|
|
|
* {@link HandlerFunction} that handles GET, HEAD, and OPTIONS requests. |
|
|
|
|
* @param lookupFunction the function to provide a {@link Resource} given the {@link ServerRequest} |
|
|
|
|
* @param headersConsumer provides access to the HTTP headers for served resources |
|
|
|
|
* @return this builder |
|
|
|
|
* @since 6.1 |
|
|
|
|
*/ |
|
|
|
|
Builder resources(Function<ServerRequest, Mono<Resource>> lookupFunction, BiConsumer<Resource, HttpHeaders> headersConsumer); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Route to the supplied router function if the given request predicate applies. This method |
|
|
|
@ -1150,22 +1203,20 @@ public abstract class RouterFunctions {
@@ -1150,22 +1203,20 @@ public abstract class RouterFunctions {
|
|
|
|
|
|
|
|
|
|
private final Function<ServerRequest, Mono<Resource>> lookupFunction; |
|
|
|
|
|
|
|
|
|
private final ResourceCacheLookupStrategy lookupStrategy; |
|
|
|
|
private final BiConsumer<Resource, HttpHeaders> headersConsumer; |
|
|
|
|
|
|
|
|
|
public ResourcesRouterFunction(Function<ServerRequest, Mono<Resource>> lookupFunction) { |
|
|
|
|
this(lookupFunction, ResourceCacheLookupStrategy.noCaching()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public ResourcesRouterFunction(Function<ServerRequest, Mono<Resource>> lookupFunction, ResourceCacheLookupStrategy lookupStrategy) { |
|
|
|
|
public ResourcesRouterFunction(Function<ServerRequest, Mono<Resource>> lookupFunction, |
|
|
|
|
BiConsumer<Resource, HttpHeaders> headersConsumer) { |
|
|
|
|
Assert.notNull(lookupFunction, "Function must not be null"); |
|
|
|
|
Assert.notNull(lookupStrategy, "Strategy must not be null"); |
|
|
|
|
Assert.notNull(headersConsumer, "HeadersConsumer must not be null"); |
|
|
|
|
this.lookupFunction = lookupFunction; |
|
|
|
|
this.lookupStrategy = lookupStrategy; |
|
|
|
|
this.headersConsumer = headersConsumer; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Mono<HandlerFunction<ServerResponse>> route(ServerRequest request) { |
|
|
|
|
return this.lookupFunction.apply(request).map(resource -> new ResourceHandlerFunction(resource, this.lookupStrategy)); |
|
|
|
|
return this.lookupFunction.apply(request).map(resource -> new ResourceHandlerFunction(resource, this.headersConsumer)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|