|
|
|
@ -67,7 +67,8 @@ public abstract class RouterFunctions { |
|
|
|
public static final String URI_TEMPLATE_VARIABLES_ATTRIBUTE = |
|
|
|
public static final String URI_TEMPLATE_VARIABLES_ATTRIBUTE = |
|
|
|
RouterFunctions.class.getName() + ".uriTemplateVariables"; |
|
|
|
RouterFunctions.class.getName() + ".uriTemplateVariables"; |
|
|
|
|
|
|
|
|
|
|
|
private static final HandlerFunction<ServerResponse> NOT_FOUND_HANDLER = request -> ServerResponse.notFound().build(); |
|
|
|
private static final HandlerFunction<ServerResponse> NOT_FOUND_HANDLER = |
|
|
|
|
|
|
|
request -> ServerResponse.notFound().build(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -103,9 +104,8 @@ public abstract class RouterFunctions { |
|
|
|
* RouterFunction<ServerResponse> userRoutes = |
|
|
|
* RouterFunction<ServerResponse> userRoutes = |
|
|
|
* RouterFunctions.route(RequestPredicates.method(HttpMethod.GET), this::listUsers) |
|
|
|
* RouterFunctions.route(RequestPredicates.method(HttpMethod.GET), this::listUsers) |
|
|
|
* .andRoute(RequestPredicates.method(HttpMethod.POST), this::createUser); |
|
|
|
* .andRoute(RequestPredicates.method(HttpMethod.POST), this::createUser); |
|
|
|
* |
|
|
|
|
|
|
|
* RouterFunction<ServerResponse> nestedRoute = |
|
|
|
* RouterFunction<ServerResponse> nestedRoute = |
|
|
|
* RouterFunctions.nest(RequestPredicates.path("/user"),userRoutes); |
|
|
|
* RouterFunctions.nest(RequestPredicates.path("/user"), userRoutes); |
|
|
|
* </pre> |
|
|
|
* </pre> |
|
|
|
* @param predicate the predicate to test |
|
|
|
* @param predicate the predicate to test |
|
|
|
* @param routerFunction the nested router function to delegate to if the predicate applies |
|
|
|
* @param routerFunction the nested router function to delegate to if the predicate applies |
|
|
|
@ -125,7 +125,7 @@ public abstract class RouterFunctions { |
|
|
|
* For instance |
|
|
|
* For instance |
|
|
|
* <pre class="code"> |
|
|
|
* <pre class="code"> |
|
|
|
* Resource location = new FileSystemResource("public-resources/"); |
|
|
|
* Resource location = new FileSystemResource("public-resources/"); |
|
|
|
* RoutingFunction<ServerResponse> resources = RouterFunctions.resources("/resources/**", location); |
|
|
|
* RouterFunction<ServerResponse> resources = RouterFunctions.resources("/resources/**", location); |
|
|
|
* </pre> |
|
|
|
* </pre> |
|
|
|
* @param pattern the pattern to match |
|
|
|
* @param pattern the pattern to match |
|
|
|
* @param location the location directory relative to which resources should be resolved |
|
|
|
* @param location the location directory relative to which resources should be resolved |
|
|
|
@ -225,12 +225,13 @@ public abstract class RouterFunctions { |
|
|
|
}; |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static <T> Mono<T> wrapException(Supplier<Mono<T>> supplier) { |
|
|
|
private static <T> Mono<T> wrapException(Supplier<Mono<T>> supplier) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
return supplier.get(); |
|
|
|
return supplier.get(); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (Throwable t) { |
|
|
|
catch (Throwable ex) { |
|
|
|
return Mono.error(t); |
|
|
|
return Mono.error(ex); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -303,6 +304,7 @@ public abstract class RouterFunctions { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static final class SameComposedRouterFunction<T extends ServerResponse> extends AbstractRouterFunction<T> { |
|
|
|
static final class SameComposedRouterFunction<T extends ServerResponse> extends AbstractRouterFunction<T> { |
|
|
|
|
|
|
|
|
|
|
|
private final RouterFunction<T> first; |
|
|
|
private final RouterFunction<T> first; |
|
|
|
@ -350,9 +352,9 @@ public abstract class RouterFunctions { |
|
|
|
this.first.accept(visitor); |
|
|
|
this.first.accept(visitor); |
|
|
|
this.second.accept(visitor); |
|
|
|
this.second.accept(visitor); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static final class FilteredRouterFunction<T extends ServerResponse, S extends ServerResponse> |
|
|
|
static final class FilteredRouterFunction<T extends ServerResponse, S extends ServerResponse> |
|
|
|
implements RouterFunction<S> { |
|
|
|
implements RouterFunction<S> { |
|
|
|
|
|
|
|
|
|
|
|
@ -383,8 +385,8 @@ public abstract class RouterFunctions { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static final class DefaultRouterFunction<T extends ServerResponse> |
|
|
|
|
|
|
|
extends AbstractRouterFunction<T> { |
|
|
|
private static final class DefaultRouterFunction<T extends ServerResponse> extends AbstractRouterFunction<T> { |
|
|
|
|
|
|
|
|
|
|
|
private final RequestPredicate predicate; |
|
|
|
private final RequestPredicate predicate; |
|
|
|
|
|
|
|
|
|
|
|
@ -414,11 +416,10 @@ public abstract class RouterFunctions { |
|
|
|
public void accept(Visitor visitor) { |
|
|
|
public void accept(Visitor visitor) { |
|
|
|
visitor.route(this.predicate, this.handlerFunction); |
|
|
|
visitor.route(this.predicate, this.handlerFunction); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static final class DefaultNestedRouterFunction<T extends ServerResponse> |
|
|
|
|
|
|
|
extends AbstractRouterFunction<T> { |
|
|
|
private static final class DefaultNestedRouterFunction<T extends ServerResponse> extends AbstractRouterFunction<T> { |
|
|
|
|
|
|
|
|
|
|
|
private final RequestPredicate predicate; |
|
|
|
private final RequestPredicate predicate; |
|
|
|
|
|
|
|
|
|
|
|
@ -446,15 +447,15 @@ public abstract class RouterFunctions { |
|
|
|
mergeTemplateVariables(serverRequest, nestedRequest.pathVariables()); |
|
|
|
mergeTemplateVariables(serverRequest, nestedRequest.pathVariables()); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
) |
|
|
|
).orElseGet(Mono::empty); |
|
|
|
.orElseGet(Mono::empty); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
private void mergeTemplateVariables(ServerRequest request, Map<String, String> variables) { |
|
|
|
private void mergeTemplateVariables(ServerRequest request, Map<String, String> variables) { |
|
|
|
if (!variables.isEmpty()) { |
|
|
|
if (!variables.isEmpty()) { |
|
|
|
Map<String, Object> attributes = request.attributes(); |
|
|
|
Map<String, Object> attributes = request.attributes(); |
|
|
|
Map<String, String> oldVariables = (Map<String, String>)request.attribute(RouterFunctions.URI_TEMPLATE_VARIABLES_ATTRIBUTE) |
|
|
|
Map<String, String> oldVariables = |
|
|
|
|
|
|
|
(Map<String, String>) request.attribute(RouterFunctions.URI_TEMPLATE_VARIABLES_ATTRIBUTE) |
|
|
|
.orElseGet(LinkedHashMap::new); |
|
|
|
.orElseGet(LinkedHashMap::new); |
|
|
|
Map<String, String> mergedVariables = new LinkedHashMap<>(oldVariables); |
|
|
|
Map<String, String> mergedVariables = new LinkedHashMap<>(oldVariables); |
|
|
|
mergedVariables.putAll(variables); |
|
|
|
mergedVariables.putAll(variables); |
|
|
|
@ -469,9 +470,9 @@ public abstract class RouterFunctions { |
|
|
|
this.routerFunction.accept(visitor); |
|
|
|
this.routerFunction.accept(visitor); |
|
|
|
visitor.endNested(this.predicate); |
|
|
|
visitor.endNested(this.predicate); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class ResourcesRouterFunction extends AbstractRouterFunction<ServerResponse> { |
|
|
|
private static class ResourcesRouterFunction extends AbstractRouterFunction<ServerResponse> { |
|
|
|
|
|
|
|
|
|
|
|
private final Function<ServerRequest, Mono<Resource>> lookupFunction; |
|
|
|
private final Function<ServerRequest, Mono<Resource>> lookupFunction; |
|
|
|
@ -492,6 +493,7 @@ public abstract class RouterFunctions { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class HandlerStrategiesResponseContext implements ServerResponse.Context { |
|
|
|
private static class HandlerStrategiesResponseContext implements ServerResponse.Context { |
|
|
|
|
|
|
|
|
|
|
|
private final HandlerStrategies strategies; |
|
|
|
private final HandlerStrategies strategies; |
|
|
|
|