@@ -77,8 +77,6 @@ public abstract class RouterFunctions {
7777 RouterFunctions .class .getName () + ".matchingPattern" ;
7878
7979
80- private static final HandlerFunction <ServerResponse > NOT_FOUND_HANDLER =
81- request -> ServerResponse .notFound ().build ();
8280
8381
8482 /**
@@ -253,43 +251,9 @@ public static WebHandler toWebHandler(RouterFunction<?> routerFunction, HandlerS
253251 Assert .notNull (routerFunction , "RouterFunction must not be null" );
254252 Assert .notNull (strategies , "HandlerStrategies must not be null" );
255253
256- return exchange -> {
257- ServerRequest request = new DefaultServerRequest (exchange , strategies .messageReaders ());
258- addAttributes (exchange , request );
259- return routerFunction .route (request )
260- .defaultIfEmpty (notFound ())
261- .flatMap (handlerFunction -> wrapException (() -> handlerFunction .handle (request )))
262- .flatMap (response -> wrapException (() -> response .writeTo (exchange ,
263- new HandlerStrategiesResponseContext (strategies ))));
264- };
254+ return new RouterFunctionWebHandler (strategies , routerFunction );
265255 }
266256
267-
268- private static <T > Mono <T > wrapException (Supplier <Mono <T >> supplier ) {
269- try {
270- return supplier .get ();
271- }
272- catch (Throwable ex ) {
273- return Mono .error (ex );
274- }
275- }
276-
277- private static void addAttributes (ServerWebExchange exchange , ServerRequest request ) {
278- Map <String , Object > attributes = exchange .getAttributes ();
279- attributes .put (REQUEST_ATTRIBUTE , request );
280- }
281-
282- @ SuppressWarnings ("unchecked" )
283- private static <T extends ServerResponse > HandlerFunction <T > notFound () {
284- return (HandlerFunction <T >) NOT_FOUND_HANDLER ;
285- }
286-
287- @ SuppressWarnings ("unchecked" )
288- static <T extends ServerResponse > HandlerFunction <T > cast (HandlerFunction <?> handlerFunction ) {
289- return (HandlerFunction <T >) handlerFunction ;
290- }
291-
292-
293257 /**
294258 * Represents a discoverable builder for router functions.
295259 * Obtained via {@link RouterFunctions#route()}.
@@ -846,8 +810,13 @@ public DifferentComposedRouterFunction(RouterFunction<?> first, RouterFunction<?
846810 @ Override
847811 public Mono <HandlerFunction <ServerResponse >> route (ServerRequest request ) {
848812 return this .first .route (request )
849- .map (RouterFunctions ::cast )
850- .switchIfEmpty (Mono .defer (() -> this .second .route (request ).map (RouterFunctions ::cast )));
813+ .map (this ::cast )
814+ .switchIfEmpty (Mono .defer (() -> this .second .route (request ).map (this ::cast )));
815+ }
816+
817+ @ SuppressWarnings ("unchecked" )
818+ private <T extends ServerResponse > HandlerFunction <T > cast (HandlerFunction <?> handlerFunction ) {
819+ return (HandlerFunction <T >) handlerFunction ;
851820 }
852821
853822 @ Override
@@ -1012,4 +981,51 @@ public List<ViewResolver> viewResolvers() {
1012981 }
1013982 }
1014983
984+
985+ private static class RouterFunctionWebHandler implements WebHandler {
986+
987+ private static final HandlerFunction <ServerResponse > NOT_FOUND_HANDLER =
988+ request -> ServerResponse .notFound ().build ();
989+
990+ private final HandlerStrategies strategies ;
991+
992+ private final RouterFunction <?> routerFunction ;
993+
994+ public RouterFunctionWebHandler (HandlerStrategies strategies , RouterFunction <?> routerFunction ) {
995+ this .strategies = strategies ;
996+ this .routerFunction = routerFunction ;
997+ }
998+
999+ @ Override
1000+ public Mono <Void > handle (ServerWebExchange exchange ) {
1001+ return Mono .defer (() -> {
1002+ ServerRequest request = new DefaultServerRequest (exchange , this .strategies .messageReaders ());
1003+ addAttributes (exchange , request );
1004+ return this .routerFunction .route (request )
1005+ .defaultIfEmpty (notFound ())
1006+ .flatMap (handlerFunction -> wrapException (() -> handlerFunction .handle (request )))
1007+ .flatMap (response -> wrapException (() -> response .writeTo (exchange ,
1008+ new HandlerStrategiesResponseContext (this .strategies ))));
1009+ });
1010+ }
1011+
1012+ private void addAttributes (ServerWebExchange exchange , ServerRequest request ) {
1013+ Map <String , Object > attributes = exchange .getAttributes ();
1014+ attributes .put (REQUEST_ATTRIBUTE , request );
1015+ }
1016+
1017+ @ SuppressWarnings ("unchecked" )
1018+ private static <T extends ServerResponse > HandlerFunction <T > notFound () {
1019+ return (HandlerFunction <T >) NOT_FOUND_HANDLER ;
1020+ }
1021+
1022+ private static <T > Mono <T > wrapException (Supplier <Mono <T >> supplier ) {
1023+ try {
1024+ return supplier .get ();
1025+ }
1026+ catch (Throwable ex ) {
1027+ return Mono .error (ex );
1028+ }
1029+ }
1030+ }
10151031}
0 commit comments