44
44
*
45
45
* @author Arjen Poutsma
46
46
* @since 5.0
47
- *
48
47
*/
49
48
public abstract class RouterFunctions {
50
49
51
- private static final HandlerFunction <Void > NOT_FOUND_HANDLER = request -> Response .notFound ().build ();
52
-
53
50
/**
54
51
* Name of the {@link ServerWebExchange} attribute that contains the {@link Request}.
55
52
*/
@@ -59,15 +56,19 @@ public abstract class RouterFunctions {
59
56
* Name of the {@link ServerWebExchange} attribute that contains the URI
60
57
* templates map, mapping variable names to values.
61
58
*/
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
+
63
64
64
65
/**
65
66
* 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
68
68
* @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}
71
72
* @see RequestPredicates
72
73
*/
73
74
public static <T > RouterFunction <T > route (RequestPredicate predicate , HandlerFunction <T > handlerFunction ) {
@@ -79,11 +80,11 @@ public static <T> RouterFunction<T> route(RequestPredicate predicate, HandlerFun
79
80
80
81
/**
81
82
* 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
84
84
* @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}
87
88
* @see RequestPredicates
88
89
*/
89
90
public static <T > RouterFunction <T > subroute (RequestPredicate predicate , RouterFunction <T > routerFunction ) {
@@ -102,9 +103,8 @@ public static <T> RouterFunction<T> subroute(RequestPredicate predicate, RouterF
102
103
}
103
104
104
105
/**
105
- * Converts the given {@linkplain RouterFunction routing function} into a {@link HttpHandler}.
106
+ * Convert the given {@linkplain RouterFunction routing function} into a {@link HttpHandler}.
106
107
* This conversion uses {@linkplain StrategiesSupplier#builder() default strategies}.
107
- *
108
108
* <p>The returned {@code HttpHandler} can be adapted to run in
109
109
* <ul>
110
110
* <li>Servlet 3.1+ using the
@@ -116,18 +116,16 @@ public static <T> RouterFunction<T> subroute(RequestPredicate predicate, RouterF
116
116
* <li>Undertow using the
117
117
* {@link org.springframework.http.server.reactive.UndertowHttpHandlerAdapter}.</li>
118
118
* </ul>
119
- *
120
119
* @param routerFunction the routing function to convert
121
120
* @return an http handler that handles HTTP request using the given routing function
122
121
*/
123
122
public static HttpHandler toHttpHandler (RouterFunction <?> routerFunction ) {
124
- return toHttpHandler (routerFunction , defaultStrategies ());
123
+ return toHttpHandler (routerFunction , StrategiesSupplier . withDefaults ());
125
124
}
126
125
127
126
/**
128
- * Converts the given {@linkplain RouterFunction routing function} into a {@link HttpHandler},
127
+ * Convert the given {@linkplain RouterFunction routing function} into a {@link HttpHandler},
129
128
* using the given strategies.
130
- *
131
129
* <p>The returned {@code HttpHandler} can be adapted to run in
132
130
* <ul>
133
131
* <li>Servlet 3.1+ using the
@@ -139,70 +137,60 @@ public static HttpHandler toHttpHandler(RouterFunction<?> routerFunction) {
139
137
* <li>Undertow using the
140
138
* {@link org.springframework.http.server.reactive.UndertowHttpHandlerAdapter}.</li>
141
139
* </ul>
142
- *
143
140
* @param routerFunction the routing function to convert
144
- * @param strategies the strategies to use
141
+ * @param strategies the strategies to use
145
142
* @return an http handler that handles HTTP request using the given routing function
146
143
*/
147
144
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" );
150
147
151
148
return new HttpWebHandlerAdapter (exchange -> {
152
149
Request request = new DefaultRequest (exchange , strategies );
153
150
addAttributes (exchange , request );
154
-
155
151
HandlerFunction <?> handlerFunction = routerFunction .route (request ).orElse (notFound ());
156
152
Response <?> response = handlerFunction .handle (request );
157
153
return response .writeTo (exchange , strategies );
158
154
});
159
155
}
160
156
161
157
/**
162
- * Converts the given {@code RouterFunction} into a {@code HandlerMapping}.
158
+ * Convert the given {@code RouterFunction} into a {@code HandlerMapping}.
163
159
* This conversion uses {@linkplain StrategiesSupplier#builder() default strategies}.
164
- *
165
160
* <p>The returned {@code HandlerMapping} can be run in a
166
161
* {@link org.springframework.web.reactive.DispatcherHandler}.
167
- *
168
162
* @param routerFunction the routing function to convert
169
163
* @return an handler mapping that maps HTTP request to a handler using the given routing function
170
164
* @see org.springframework.web.reactive.function.support.HandlerFunctionAdapter
171
165
* @see org.springframework.web.reactive.function.support.ResponseResultHandler
172
166
*/
173
167
public static HandlerMapping toHandlerMapping (RouterFunction <?> routerFunction ) {
174
- return toHandlerMapping (routerFunction , defaultStrategies ());
168
+ return toHandlerMapping (routerFunction , StrategiesSupplier . withDefaults ());
175
169
}
176
170
177
171
/**
178
- * Converts the given {@linkplain RouterFunction routing function} into a {@link HandlerMapping},
172
+ * Convert the given {@linkplain RouterFunction routing function} into a {@link HandlerMapping},
179
173
* using the given strategies.
180
- *
181
174
* <p>The returned {@code HandlerMapping} can be run in a
182
175
* {@link org.springframework.web.reactive.DispatcherHandler}.
183
- *
184
176
* @param routerFunction the routing function to convert
185
- * @param strategies the strategies to use
177
+ * @param strategies the strategies to use
186
178
* @return an handler mapping that maps HTTP request to a handler using the given routing function
187
179
* @see org.springframework.web.reactive.function.support.HandlerFunctionAdapter
188
180
* @see org.springframework.web.reactive.function.support.ResponseResultHandler
189
181
*/
190
182
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" );
193
185
194
186
return exchange -> {
195
187
Request request = new DefaultRequest (exchange , strategies );
196
188
addAttributes (exchange , request );
197
-
198
189
Optional <? extends HandlerFunction <?>> route = routerFunction .route (request );
199
190
return Mono .justOrEmpty (route );
200
191
};
201
192
}
202
193
203
- private static StrategiesSupplier defaultStrategies () {
204
- return StrategiesSupplier .builder ().build ();
205
- }
206
194
207
195
private static void addAttributes (ServerWebExchange exchange , Request request ) {
208
196
Map <String , Object > attributes = exchange .getAttributes ();
@@ -218,4 +206,5 @@ private static <T> HandlerFunction<T> notFound() {
218
206
static <T > HandlerFunction <T > cast (HandlerFunction <?> handlerFunction ) {
219
207
return (HandlerFunction <T >) handlerFunction ;
220
208
}
209
+
221
210
}
0 commit comments