4444 *
4545 * @author Arjen Poutsma
4646 * @since 5.0
47- *
4847 */
4948public abstract class RouterFunctions {
5049
51- private static final HandlerFunction <Void > NOT_FOUND_HANDLER = request -> Response .notFound ().build ();
52-
5350 /**
5451 * Name of the {@link ServerWebExchange} attribute that contains the {@link Request}.
5552 */
@@ -59,15 +56,19 @@ public abstract class RouterFunctions {
5956 * Name of the {@link ServerWebExchange} attribute that contains the URI
6057 * templates map, mapping variable names to values.
6158 */
62- public static final String URI_TEMPLATE_VARIABLES_ATTRIBUTE = RouterFunctions .class .getName () + ".uriTemplateVariables" ;
59+ public static final String URI_TEMPLATE_VARIABLES_ATTRIBUTE =
60+ RouterFunctions .class .getName () + ".uriTemplateVariables" ;
61+
62+ private static final HandlerFunction <Void > NOT_FOUND_HANDLER = request -> Response .notFound ().build ();
63+
6364
6465 /**
6566 * Route to the given handler function if the given request predicate applies.
66- *
67- * @param predicate the predicate to test
67+ * @param predicate the predicate to test
6868 * @param handlerFunction the handler function to route to
69- * @param <T> the type of the handler function
70- * @return a routing function that routes to {@code handlerFunction} if {@code predicate} evaluates to {@code true}
69+ * @param <T> the type of the handler function
70+ * @return a routing function that routes to {@code handlerFunction} if
71+ * {@code predicate} evaluates to {@code true}
7172 * @see RequestPredicates
7273 */
7374 public static <T > RouterFunction <T > route (RequestPredicate predicate , HandlerFunction <T > handlerFunction ) {
@@ -79,11 +80,11 @@ public static <T> RouterFunction<T> route(RequestPredicate predicate, HandlerFun
7980
8081 /**
8182 * Route to the given routing function if the given request predicate applies.
82- *
83- * @param predicate the predicate to test
83+ * @param predicate the predicate to test
8484 * @param routerFunction the routing function to route to
85- * @param <T> the type of the handler function
86- * @return a routing function that routes to {@code routerFunction} if {@code predicate} evaluates to {@code true}
85+ * @param <T> the type of the handler function
86+ * @return a routing function that routes to {@code routerFunction} if
87+ * {@code predicate} evaluates to {@code true}
8788 * @see RequestPredicates
8889 */
8990 public static <T > RouterFunction <T > subroute (RequestPredicate predicate , RouterFunction <T > routerFunction ) {
@@ -102,9 +103,8 @@ public static <T> RouterFunction<T> subroute(RequestPredicate predicate, RouterF
102103 }
103104
104105 /**
105- * Converts the given {@linkplain RouterFunction routing function} into a {@link HttpHandler}.
106+ * Convert the given {@linkplain RouterFunction routing function} into a {@link HttpHandler}.
106107 * This conversion uses {@linkplain StrategiesSupplier#builder() default strategies}.
107- *
108108 * <p>The returned {@code HttpHandler} can be adapted to run in
109109 * <ul>
110110 * <li>Servlet 3.1+ using the
@@ -116,18 +116,16 @@ public static <T> RouterFunction<T> subroute(RequestPredicate predicate, RouterF
116116 * <li>Undertow using the
117117 * {@link org.springframework.http.server.reactive.UndertowHttpHandlerAdapter}.</li>
118118 * </ul>
119- *
120119 * @param routerFunction the routing function to convert
121120 * @return an http handler that handles HTTP request using the given routing function
122121 */
123122 public static HttpHandler toHttpHandler (RouterFunction <?> routerFunction ) {
124- return toHttpHandler (routerFunction , defaultStrategies ());
123+ return toHttpHandler (routerFunction , StrategiesSupplier . withDefaults ());
125124 }
126125
127126 /**
128- * Converts the given {@linkplain RouterFunction routing function} into a {@link HttpHandler},
127+ * Convert the given {@linkplain RouterFunction routing function} into a {@link HttpHandler},
129128 * using the given strategies.
130- *
131129 * <p>The returned {@code HttpHandler} can be adapted to run in
132130 * <ul>
133131 * <li>Servlet 3.1+ using the
@@ -139,70 +137,60 @@ public static HttpHandler toHttpHandler(RouterFunction<?> routerFunction) {
139137 * <li>Undertow using the
140138 * {@link org.springframework.http.server.reactive.UndertowHttpHandlerAdapter}.</li>
141139 * </ul>
142- *
143140 * @param routerFunction the routing function to convert
144- * @param strategies the strategies to use
141+ * @param strategies the strategies to use
145142 * @return an http handler that handles HTTP request using the given routing function
146143 */
147144 public static HttpHandler toHttpHandler (RouterFunction <?> routerFunction , StrategiesSupplier strategies ) {
148- Assert .notNull (routerFunction , "'routerFunction' must not be null" );
149- Assert .notNull (strategies , "'strategies' must not be null" );
145+ Assert .notNull (routerFunction , "RouterFunction must not be null" );
146+ Assert .notNull (strategies , "StrategiesSupplier must not be null" );
150147
151148 return new HttpWebHandlerAdapter (exchange -> {
152149 Request request = new DefaultRequest (exchange , strategies );
153150 addAttributes (exchange , request );
154-
155151 HandlerFunction <?> handlerFunction = routerFunction .route (request ).orElse (notFound ());
156152 Response <?> response = handlerFunction .handle (request );
157153 return response .writeTo (exchange , strategies );
158154 });
159155 }
160156
161157 /**
162- * Converts the given {@code RouterFunction} into a {@code HandlerMapping}.
158+ * Convert the given {@code RouterFunction} into a {@code HandlerMapping}.
163159 * This conversion uses {@linkplain StrategiesSupplier#builder() default strategies}.
164- *
165160 * <p>The returned {@code HandlerMapping} can be run in a
166161 * {@link org.springframework.web.reactive.DispatcherHandler}.
167- *
168162 * @param routerFunction the routing function to convert
169163 * @return an handler mapping that maps HTTP request to a handler using the given routing function
170164 * @see org.springframework.web.reactive.function.support.HandlerFunctionAdapter
171165 * @see org.springframework.web.reactive.function.support.ResponseResultHandler
172166 */
173167 public static HandlerMapping toHandlerMapping (RouterFunction <?> routerFunction ) {
174- return toHandlerMapping (routerFunction , defaultStrategies ());
168+ return toHandlerMapping (routerFunction , StrategiesSupplier . withDefaults ());
175169 }
176170
177171 /**
178- * Converts the given {@linkplain RouterFunction routing function} into a {@link HandlerMapping},
172+ * Convert the given {@linkplain RouterFunction routing function} into a {@link HandlerMapping},
179173 * using the given strategies.
180- *
181174 * <p>The returned {@code HandlerMapping} can be run in a
182175 * {@link org.springframework.web.reactive.DispatcherHandler}.
183- *
184176 * @param routerFunction the routing function to convert
185- * @param strategies the strategies to use
177+ * @param strategies the strategies to use
186178 * @return an handler mapping that maps HTTP request to a handler using the given routing function
187179 * @see org.springframework.web.reactive.function.support.HandlerFunctionAdapter
188180 * @see org.springframework.web.reactive.function.support.ResponseResultHandler
189181 */
190182 public static HandlerMapping toHandlerMapping (RouterFunction <?> routerFunction , StrategiesSupplier strategies ) {
191- Assert .notNull (routerFunction , "'routerFunction' must not be null" );
192- Assert .notNull (strategies , "'strategies' must not be null" );
183+ Assert .notNull (routerFunction , "RouterFunction must not be null" );
184+ Assert .notNull (strategies , "StrategiesSupplier must not be null" );
193185
194186 return exchange -> {
195187 Request request = new DefaultRequest (exchange , strategies );
196188 addAttributes (exchange , request );
197-
198189 Optional <? extends HandlerFunction <?>> route = routerFunction .route (request );
199190 return Mono .justOrEmpty (route );
200191 };
201192 }
202193
203- private static StrategiesSupplier defaultStrategies () {
204- return StrategiesSupplier .builder ().build ();
205- }
206194
207195 private static void addAttributes (ServerWebExchange exchange , Request request ) {
208196 Map <String , Object > attributes = exchange .getAttributes ();
@@ -218,4 +206,5 @@ private static <T> HandlerFunction<T> notFound() {
218206 static <T > HandlerFunction <T > cast (HandlerFunction <?> handlerFunction ) {
219207 return (HandlerFunction <T >) handlerFunction ;
220208 }
209+
221210}
0 commit comments