|
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.
|
|
59 | 59 | * environments, Reactor, or Undertow.
|
60 | 60 | *
|
61 | 61 | * @author Arjen Poutsma
|
| 62 | + * @author Sebastien Deleuze |
62 | 63 | * @since 5.0
|
63 | 64 | */
|
64 | 65 | public abstract class RouterFunctions {
|
@@ -145,6 +146,40 @@ public static <T extends ServerResponse> RouterFunction<T> nest(
|
145 | 146 | return new DefaultNestedRouterFunction<>(predicate, routerFunction);
|
146 | 147 | }
|
147 | 148 |
|
| 149 | + /** |
| 150 | + * Route requests that match the given predicate to the given resource. |
| 151 | + * For instance |
| 152 | + * <pre class="code"> |
| 153 | + * Resource resource = new ClassPathResource("static/index.html") |
| 154 | + * RouterFunction<ServerResponse> resources = RouterFunctions.resource(path("/api/**").negate(), resource); |
| 155 | + * </pre> |
| 156 | + * @param predicate predicate to match |
| 157 | + * @param resource the resources to serve |
| 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 | + return resources(new PredicateResourceLookupFunction(predicate, resource), (consumerResource, httpHeaders) -> {}); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Route requests that match the given predicate to the given resource. |
| 167 | + * For instance |
| 168 | + * <pre class="code"> |
| 169 | + * Resource resource = new ClassPathResource("static/index.html") |
| 170 | + * RouterFunction<ServerResponse> resources = RouterFunctions.resource(path("/api/**").negate(), resource); |
| 171 | + * </pre> |
| 172 | + * @param predicate predicate to match |
| 173 | + * @param resource the resources to serve |
| 174 | + * @param headersConsumer provides access to the HTTP headers for served resources |
| 175 | + * @return a router function that routes to a resource |
| 176 | + * @since 6.1.4 |
| 177 | + */ |
| 178 | + public static RouterFunction<ServerResponse> resource(RequestPredicate predicate, Resource resource, |
| 179 | + BiConsumer<Resource, HttpHeaders> headersConsumer) { |
| 180 | + return resources(new PredicateResourceLookupFunction(predicate, resource), headersConsumer); |
| 181 | + } |
| 182 | + |
148 | 183 | /**
|
149 | 184 | * Route requests that match the given pattern to resources relative to the given root location.
|
150 | 185 | * For instance
|
@@ -692,6 +727,35 @@ public interface Builder {
|
692 | 727 | */
|
693 | 728 | Builder add(RouterFunction<ServerResponse> routerFunction);
|
694 | 729 |
|
| 730 | + /** |
| 731 | + * Route requests that match the given predicate to the given resource. |
| 732 | + * For instance |
| 733 | + * <pre class="code"> |
| 734 | + * Resource resource = new ClassPathResource("static/index.html") |
| 735 | + * RouterFunction<ServerResponse> resources = RouterFunctions.resource(path("/api/**").negate(), resource); |
| 736 | + * </pre> |
| 737 | + * @param predicate predicate to match |
| 738 | + * @param resource the resources to serve |
| 739 | + * @return a router function that routes to a resource |
| 740 | + * @since 6.1.4 |
| 741 | + */ |
| 742 | + Builder resource(RequestPredicate predicate, Resource resource); |
| 743 | + |
| 744 | + /** |
| 745 | + * Route requests that match the given predicate to the given resource. |
| 746 | + * For instance |
| 747 | + * <pre class="code"> |
| 748 | + * Resource resource = new ClassPathResource("static/index.html") |
| 749 | + * RouterFunction<ServerResponse> resources = RouterFunctions.resource(path("/api/**").negate(), resource); |
| 750 | + * </pre> |
| 751 | + * @param predicate predicate to match |
| 752 | + * @param resource the resources to serve |
| 753 | + * @param headersConsumer provides access to the HTTP headers for served resources |
| 754 | + * @return a router function that routes to a resource |
| 755 | + * @since 6.1.4 |
| 756 | + */ |
| 757 | + Builder resource(RequestPredicate predicate, Resource resource, BiConsumer<Resource, HttpHeaders> headersConsumer); |
| 758 | + |
695 | 759 | /**
|
696 | 760 | * Route requests that match the given pattern to resources relative to the given root location.
|
697 | 761 | * For instance
|
|
0 commit comments