Move RouterFunction WebHandler to inner class

This commit is contained in:
Arjen Poutsma 2019-06-18 16:04:20 +02:00
parent 7f1d5d790e
commit edd86e5dd5
1 changed files with 55 additions and 39 deletions

View File

@ -77,8 +77,6 @@ public abstract class RouterFunctions {
RouterFunctions.class.getName() + ".matchingPattern";
private static final HandlerFunction<ServerResponse> NOT_FOUND_HANDLER =
request -> ServerResponse.notFound().build();
/**
@ -253,43 +251,9 @@ public abstract class RouterFunctions {
Assert.notNull(routerFunction, "RouterFunction must not be null");
Assert.notNull(strategies, "HandlerStrategies must not be null");
return exchange -> {
ServerRequest request = new DefaultServerRequest(exchange, strategies.messageReaders());
addAttributes(exchange, request);
return routerFunction.route(request)
.defaultIfEmpty(notFound())
.flatMap(handlerFunction -> wrapException(() -> handlerFunction.handle(request)))
.flatMap(response -> wrapException(() -> response.writeTo(exchange,
new HandlerStrategiesResponseContext(strategies))));
};
return new RouterFunctionWebHandler(strategies, routerFunction);
}
private static <T> Mono<T> wrapException(Supplier<Mono<T>> supplier) {
try {
return supplier.get();
}
catch (Throwable ex) {
return Mono.error(ex);
}
}
private static void addAttributes(ServerWebExchange exchange, ServerRequest request) {
Map<String, Object> attributes = exchange.getAttributes();
attributes.put(REQUEST_ATTRIBUTE, request);
}
@SuppressWarnings("unchecked")
private static <T extends ServerResponse> HandlerFunction<T> notFound() {
return (HandlerFunction<T>) NOT_FOUND_HANDLER;
}
@SuppressWarnings("unchecked")
static <T extends ServerResponse> HandlerFunction<T> cast(HandlerFunction<?> handlerFunction) {
return (HandlerFunction<T>) handlerFunction;
}
/**
* Represents a discoverable builder for router functions.
* Obtained via {@link RouterFunctions#route()}.
@ -846,8 +810,13 @@ public abstract class RouterFunctions {
@Override
public Mono<HandlerFunction<ServerResponse>> route(ServerRequest request) {
return this.first.route(request)
.map(RouterFunctions::cast)
.switchIfEmpty(Mono.defer(() -> this.second.route(request).map(RouterFunctions::cast)));
.map(this::cast)
.switchIfEmpty(Mono.defer(() -> this.second.route(request).map(this::cast)));
}
@SuppressWarnings("unchecked")
private <T extends ServerResponse> HandlerFunction<T> cast(HandlerFunction<?> handlerFunction) {
return (HandlerFunction<T>) handlerFunction;
}
@Override
@ -1012,4 +981,51 @@ public abstract class RouterFunctions {
}
}
private static class RouterFunctionWebHandler implements WebHandler {
private static final HandlerFunction<ServerResponse> NOT_FOUND_HANDLER =
request -> ServerResponse.notFound().build();
private final HandlerStrategies strategies;
private final RouterFunction<?> routerFunction;
public RouterFunctionWebHandler(HandlerStrategies strategies, RouterFunction<?> routerFunction) {
this.strategies = strategies;
this.routerFunction = routerFunction;
}
@Override
public Mono<Void> handle(ServerWebExchange exchange) {
return Mono.defer(() -> {
ServerRequest request = new DefaultServerRequest(exchange, this.strategies.messageReaders());
addAttributes(exchange, request);
return this.routerFunction.route(request)
.defaultIfEmpty(notFound())
.flatMap(handlerFunction -> wrapException(() -> handlerFunction.handle(request)))
.flatMap(response -> wrapException(() -> response.writeTo(exchange,
new HandlerStrategiesResponseContext(this.strategies))));
});
}
private void addAttributes(ServerWebExchange exchange, ServerRequest request) {
Map<String, Object> attributes = exchange.getAttributes();
attributes.put(REQUEST_ATTRIBUTE, request);
}
@SuppressWarnings("unchecked")
private static <T extends ServerResponse> HandlerFunction<T> notFound() {
return (HandlerFunction<T>) NOT_FOUND_HANDLER;
}
private static <T> Mono<T> wrapException(Supplier<Mono<T>> supplier) {
try {
return supplier.get();
}
catch (Throwable ex) {
return Mono.error(ex);
}
}
}
}