|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2023 the original author or authors. |
| 2 | + * Copyright 2002-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
45 | 45 | * function.
|
46 | 46 | *
|
47 | 47 | * @author Arjen Poutsma
|
| 48 | + * @author Sebastien Deleuze |
48 | 49 | * @since 5.2
|
49 | 50 | */
|
50 | 51 | public abstract class RouterFunctions {
|
@@ -128,6 +129,40 @@ public static <T extends ServerResponse> RouterFunction<T> nest(
|
128 | 129 | return new DefaultNestedRouterFunction<>(predicate, routerFunction);
|
129 | 130 | }
|
130 | 131 |
|
| 132 | + /** |
| 133 | + * Route requests that match the given predicate to the given resource. |
| 134 | + * For instance |
| 135 | + * <pre class="code"> |
| 136 | + * Resource resource = new ClassPathResource("static/index.html") |
| 137 | + * RouterFunction<ServerResponse> resources = RouterFunctions.resource(path("/api/**").negate(), resource); |
| 138 | + * </pre> |
| 139 | + * @param predicate predicate to match |
| 140 | + * @param resource the resources to serve |
| 141 | + * @return a router function that routes to a resource |
| 142 | + * @since 6.1.4 |
| 143 | + */ |
| 144 | + public static RouterFunction<ServerResponse> resource(RequestPredicate predicate, Resource resource) { |
| 145 | + return resources(new PredicateResourceLookupFunction(predicate, resource), (consumerResource, httpHeaders) -> {}); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Route requests that match the given predicate to the given resource. |
| 150 | + * For instance |
| 151 | + * <pre class="code"> |
| 152 | + * Resource resource = new ClassPathResource("static/index.html") |
| 153 | + * RouterFunction<ServerResponse> resources = RouterFunctions.resource(path("/api/**").negate(), resource); |
| 154 | + * </pre> |
| 155 | + * @param predicate predicate to match |
| 156 | + * @param resource the resources to serve |
| 157 | + * @param headersConsumer provides access to the HTTP headers for served resources |
| 158 | + * @return a router function that routes to a resource |
| 159 | + * @since 6.1.4 |
| 160 | + */ |
| 161 | + public static RouterFunction<ServerResponse> resource(RequestPredicate predicate, Resource resource, |
| 162 | + BiConsumer<Resource, HttpHeaders> headersConsumer) { |
| 163 | + return resources(new PredicateResourceLookupFunction(predicate, resource), headersConsumer); |
| 164 | + } |
| 165 | + |
131 | 166 | /**
|
132 | 167 | * Route requests that match the given pattern to resources relative to the given root location.
|
133 | 168 | * For instance
|
@@ -602,6 +637,36 @@ public interface Builder {
|
602 | 637 | */
|
603 | 638 | Builder add(RouterFunction<ServerResponse> routerFunction);
|
604 | 639 |
|
| 640 | + /** |
| 641 | + * Route requests that match the given predicate to the given resource. |
| 642 | + * For instance |
| 643 | + * <pre class="code"> |
| 644 | + * Resource resource = new ClassPathResource("static/index.html") |
| 645 | + * RouterFunction<ServerResponse> resources = RouterFunctions.resource(path("/api/**").negate(), resource); |
| 646 | + * </pre> |
| 647 | + * @param predicate predicate to match |
| 648 | + * @param resource the resources to serve |
| 649 | + * @return a router function that routes to a resource |
| 650 | + * @since 6.1.4 |
| 651 | + */ |
| 652 | + Builder resource(RequestPredicate predicate, Resource resource); |
| 653 | + |
| 654 | + /** |
| 655 | + * Route requests that match the given predicate to the given resource. |
| 656 | + * For instance |
| 657 | + * <pre class="code"> |
| 658 | + * Resource resource = new ClassPathResource("static/index.html") |
| 659 | + * RouterFunction<ServerResponse> resources = RouterFunctions.resource(path("/api/**").negate(), resource); |
| 660 | + * </pre> |
| 661 | + * @param predicate predicate to match |
| 662 | + * @param resource the resources to serve |
| 663 | + * @param headersConsumer provides access to the HTTP headers for served resources |
| 664 | + * @return a router function that routes to a resource |
| 665 | + * @since 6.1.4 |
| 666 | + */ |
| 667 | + Builder resource(RequestPredicate predicate, Resource resource, BiConsumer<Resource, HttpHeaders> headersConsumer); |
| 668 | + |
| 669 | + |
605 | 670 | /**
|
606 | 671 | * Route requests that match the given pattern to resources relative to the given root location.
|
607 | 672 | * For instance
|
|
0 commit comments