26
26
27
27
import reactor .core .publisher .Mono ;
28
28
29
- import org .springframework .http . HttpMethod ;
29
+ import org .springframework .core . io . Resource ;
30
30
import org .springframework .util .Assert ;
31
31
32
32
/**
@@ -40,89 +40,111 @@ class RouterFunctionBuilder implements RouterFunctions.Builder {
40
40
41
41
private List <HandlerFilterFunction <ServerResponse , ServerResponse >> filterFunctions = new ArrayList <>();
42
42
43
-
44
43
@ Override
45
- public RouterFunctions .Builder route (RequestPredicate predicate ,
44
+ public RouterFunctions .Builder add (RouterFunction <ServerResponse > routerFunction ) {
45
+ Assert .notNull (routerFunction , "RouterFunction must not be null" );
46
+ this .routerFunctions .add (routerFunction );
47
+ return this ;
48
+ }
49
+
50
+ private RouterFunctions .Builder add (RequestPredicate predicate ,
46
51
HandlerFunction <ServerResponse > handlerFunction ) {
47
52
this .routerFunctions .add (RouterFunctions .route (predicate , handlerFunction ));
48
53
return this ;
49
54
}
50
55
51
56
@ Override
52
- public RouterFunctions .Builder routeGet (HandlerFunction <ServerResponse > handlerFunction ) {
53
- return route (RequestPredicates .method (HttpMethod .GET ), handlerFunction );
57
+ public RouterFunctions .Builder GET (String pattern , HandlerFunction <ServerResponse > handlerFunction ) {
58
+ return add (RequestPredicates .GET (pattern ), handlerFunction );
59
+ }
60
+
61
+ @ Override
62
+ public RouterFunctions .Builder GET (String pattern , RequestPredicate predicate ,
63
+ HandlerFunction <ServerResponse > handlerFunction ) {
64
+ return add (RequestPredicates .GET (pattern ).and (predicate ), handlerFunction );
65
+ }
66
+
67
+ @ Override
68
+ public RouterFunctions .Builder HEAD (String pattern , HandlerFunction <ServerResponse > handlerFunction ) {
69
+ return add (RequestPredicates .HEAD (pattern ), handlerFunction );
54
70
}
55
71
56
72
@ Override
57
- public RouterFunctions .Builder routeGet (String pattern , HandlerFunction <ServerResponse > handlerFunction ) {
58
- return route (RequestPredicates .GET (pattern ), handlerFunction );
73
+ public RouterFunctions .Builder HEAD (String pattern , RequestPredicate predicate ,
74
+ HandlerFunction <ServerResponse > handlerFunction ) {
75
+ return add (RequestPredicates .HEAD (pattern ).and (predicate ), handlerFunction );
59
76
}
60
77
61
78
@ Override
62
- public RouterFunctions .Builder routeHead ( HandlerFunction <ServerResponse > handlerFunction ) {
63
- return route (RequestPredicates .method ( HttpMethod . HEAD ), handlerFunction );
79
+ public RouterFunctions .Builder POST ( String pattern , HandlerFunction <ServerResponse > handlerFunction ) {
80
+ return add (RequestPredicates .POST ( pattern ), handlerFunction );
64
81
}
65
82
66
83
@ Override
67
- public RouterFunctions .Builder routeHead (String pattern , HandlerFunction <ServerResponse > handlerFunction ) {
68
- return route (RequestPredicates .HEAD (pattern ), handlerFunction );
84
+ public RouterFunctions .Builder POST (String pattern , RequestPredicate predicate ,
85
+ HandlerFunction <ServerResponse > handlerFunction ) {
86
+ return add (RequestPredicates .POST (pattern ).and (predicate ), handlerFunction );
69
87
}
70
88
71
89
@ Override
72
- public RouterFunctions .Builder routePost ( HandlerFunction <ServerResponse > handlerFunction ) {
73
- return route (RequestPredicates .method ( HttpMethod . POST ), handlerFunction );
90
+ public RouterFunctions .Builder PUT ( String pattern , HandlerFunction <ServerResponse > handlerFunction ) {
91
+ return add (RequestPredicates .PUT ( pattern ), handlerFunction );
74
92
}
75
93
76
94
@ Override
77
- public RouterFunctions .Builder routePost (String pattern , HandlerFunction <ServerResponse > handlerFunction ) {
78
- return route (RequestPredicates .POST (pattern ), handlerFunction );
95
+ public RouterFunctions .Builder PUT (String pattern , RequestPredicate predicate ,
96
+ HandlerFunction <ServerResponse > handlerFunction ) {
97
+ return add (RequestPredicates .PUT (pattern ).and (predicate ), handlerFunction );
79
98
}
80
99
81
100
@ Override
82
- public RouterFunctions .Builder routePut ( HandlerFunction <ServerResponse > handlerFunction ) {
83
- return route (RequestPredicates .method ( HttpMethod . PUT ), handlerFunction );
101
+ public RouterFunctions .Builder PATCH ( String pattern , HandlerFunction <ServerResponse > handlerFunction ) {
102
+ return add (RequestPredicates .PATCH ( pattern ), handlerFunction );
84
103
}
85
104
86
105
@ Override
87
- public RouterFunctions .Builder routePut (String pattern , HandlerFunction <ServerResponse > handlerFunction ) {
88
- return route (RequestPredicates .PUT (pattern ), handlerFunction );
106
+ public RouterFunctions .Builder PATCH (String pattern , RequestPredicate predicate ,
107
+ HandlerFunction <ServerResponse > handlerFunction ) {
108
+ return add (RequestPredicates .PATCH (pattern ).and (predicate ), handlerFunction );
89
109
}
90
110
91
111
@ Override
92
- public RouterFunctions .Builder routePatch ( HandlerFunction <ServerResponse > handlerFunction ) {
93
- return route (RequestPredicates .method ( HttpMethod . PATCH ), handlerFunction );
112
+ public RouterFunctions .Builder DELETE ( String pattern , HandlerFunction <ServerResponse > handlerFunction ) {
113
+ return add (RequestPredicates .DELETE ( pattern ), handlerFunction );
94
114
}
95
115
96
116
@ Override
97
- public RouterFunctions .Builder routePatch (String pattern , HandlerFunction <ServerResponse > handlerFunction ) {
98
- return route (RequestPredicates .PATCH (pattern ), handlerFunction );
117
+ public RouterFunctions .Builder DELETE (String pattern , RequestPredicate predicate ,
118
+ HandlerFunction <ServerResponse > handlerFunction ) {
119
+ return add (RequestPredicates .DELETE (pattern ).and (predicate ), handlerFunction );
99
120
}
100
121
101
122
@ Override
102
- public RouterFunctions .Builder routeDelete ( HandlerFunction <ServerResponse > handlerFunction ) {
103
- return route (RequestPredicates .method ( HttpMethod . DELETE ), handlerFunction );
123
+ public RouterFunctions .Builder OPTIONS ( String pattern , HandlerFunction <ServerResponse > handlerFunction ) {
124
+ return add (RequestPredicates .OPTIONS ( pattern ), handlerFunction );
104
125
}
105
126
106
127
@ Override
107
- public RouterFunctions .Builder routeDelete (String pattern , HandlerFunction <ServerResponse > handlerFunction ) {
108
- return route (RequestPredicates .DELETE (pattern ), handlerFunction );
128
+ public RouterFunctions .Builder OPTIONS (String pattern , RequestPredicate predicate ,
129
+ HandlerFunction <ServerResponse > handlerFunction ) {
130
+ return add (RequestPredicates .OPTIONS (pattern ).and (predicate ), handlerFunction );
109
131
}
110
132
111
133
@ Override
112
- public RouterFunctions .Builder routeOptions ( HandlerFunction < ServerResponse > handlerFunction ) {
113
- return route ( RequestPredicates . method ( HttpMethod . OPTIONS ), handlerFunction );
134
+ public RouterFunctions .Builder resources ( String pattern , Resource location ) {
135
+ return add ( RouterFunctions . resources ( pattern , location ) );
114
136
}
115
137
116
138
@ Override
117
- public RouterFunctions .Builder routeOptions ( String pattern , HandlerFunction < ServerResponse > handlerFunction ) {
118
- return route ( RequestPredicates . OPTIONS ( pattern ), handlerFunction );
139
+ public RouterFunctions .Builder resources ( Function < ServerRequest , Mono < Resource >> lookupFunction ) {
140
+ return add ( RouterFunctions . resources ( lookupFunction ) );
119
141
}
120
142
121
143
@ Override
122
144
public RouterFunctions .Builder nest (RequestPredicate predicate ,
123
145
Consumer <RouterFunctions .Builder > builderConsumer ) {
124
146
125
- Assert .notNull (builderConsumer , "'builderConsumer' must not be null" );
147
+ Assert .notNull (builderConsumer , "Consumer must not be null" );
126
148
127
149
RouterFunctionBuilder nestedBuilder = new RouterFunctionBuilder ();
128
150
builderConsumer .accept (nestedBuilder );
@@ -136,7 +158,7 @@ public RouterFunctions.Builder nest(RequestPredicate predicate,
136
158
public RouterFunctions .Builder nest (RequestPredicate predicate ,
137
159
Supplier <RouterFunction <ServerResponse >> routerFunctionSupplier ) {
138
160
139
- Assert .notNull (routerFunctionSupplier , "'routerFunctionSupplier' must not be null" );
161
+ Assert .notNull (routerFunctionSupplier , "RouterFunction Supplier must not be null" );
140
162
141
163
RouterFunction <ServerResponse > nestedRoute = routerFunctionSupplier .get ();
142
164
@@ -145,57 +167,56 @@ public RouterFunctions.Builder nest(RequestPredicate predicate,
145
167
}
146
168
147
169
@ Override
148
- public RouterFunctions .Builder nestPath (String pattern ,
170
+ public RouterFunctions .Builder path (String pattern ,
149
171
Consumer <RouterFunctions .Builder > builderConsumer ) {
150
172
return nest (RequestPredicates .path (pattern ), builderConsumer );
151
173
}
152
174
153
175
@ Override
154
- public RouterFunctions .Builder nestPath (String pattern ,
176
+ public RouterFunctions .Builder path (String pattern ,
155
177
Supplier <RouterFunction <ServerResponse >> routerFunctionSupplier ) {
156
178
return nest (RequestPredicates .path (pattern ), routerFunctionSupplier );
157
179
}
158
180
159
181
@ Override
160
182
public RouterFunctions .Builder filter (HandlerFilterFunction <ServerResponse , ServerResponse > filterFunction ) {
161
- Assert .notNull (filterFunction , "'filterFunction' must not be null" );
183
+ Assert .notNull (filterFunction , "HandlerFilterFunction must not be null" );
162
184
163
185
this .filterFunctions .add (filterFunction );
164
186
return this ;
165
187
}
166
188
167
189
@ Override
168
- public RouterFunctions .Builder filterBefore (
169
- Function <ServerRequest , Mono <ServerRequest >> requestProcessor ) {
170
-
171
- Assert .notNull (requestProcessor , "Function must not be null" );
172
- return filter ((request , next ) -> requestProcessor .apply (request ).flatMap (next ::handle ));
190
+ public RouterFunctions .Builder before (Function <ServerRequest , ServerRequest > requestProcessor ) {
191
+ Assert .notNull (requestProcessor , "RequestProcessor must not be null" );
192
+ return filter ((request , next ) -> next .handle (requestProcessor .apply (request )));
173
193
}
174
194
175
195
@ Override
176
- public RouterFunctions .Builder filterAfter (
177
- BiFunction <ServerRequest , ServerResponse , Mono <ServerResponse >> responseProcessor ) {
196
+ public RouterFunctions .Builder after (
197
+ BiFunction <ServerRequest , ServerResponse , ServerResponse > responseProcessor ) {
198
+ Assert .notNull (responseProcessor , "ResponseProcessor must not be null" );
178
199
return filter ((request , next ) -> next .handle (request )
179
- .flatMap (serverResponse -> responseProcessor .apply (request , serverResponse )));
200
+ .map (serverResponse -> responseProcessor .apply (request , serverResponse )));
180
201
}
181
202
182
203
@ Override
183
- public RouterFunctions .Builder filterException (Predicate <? super Throwable > predicate ,
204
+ public RouterFunctions .Builder onError (Predicate <? super Throwable > predicate ,
184
205
BiFunction <? super Throwable , ServerRequest , Mono <ServerResponse >> responseProvider ) {
185
206
186
- Assert .notNull (predicate , "'exceptionType' must not be null" );
187
- Assert .notNull (responseProvider , "'fallback' must not be null" );
207
+ Assert .notNull (predicate , "Predicate must not be null" );
208
+ Assert .notNull (responseProvider , "ResponseProvider must not be null" );
188
209
189
210
return filter ((request , next ) -> next .handle (request )
190
211
.onErrorResume (predicate , t -> responseProvider .apply (t , request )));
191
212
}
192
213
193
214
@ Override
194
- public <T extends Throwable > RouterFunctions .Builder filterException (
215
+ public <T extends Throwable > RouterFunctions .Builder onError (
195
216
Class <T > exceptionType ,
196
217
BiFunction <? super T , ServerRequest , Mono <ServerResponse >> responseProvider ) {
197
- Assert .notNull (exceptionType , "'exceptionType' must not be null" );
198
- Assert .notNull (responseProvider , "'fallback' must not be null" );
218
+ Assert .notNull (exceptionType , "ExceptionType must not be null" );
219
+ Assert .notNull (responseProvider , "ResponseProvider must not be null" );
199
220
200
221
return filter ((request , next ) -> next .handle (request )
201
222
.onErrorResume (exceptionType , t -> responseProvider .apply (t , request )));
0 commit comments