2626import java .util .Objects ;
2727import java .util .function .Predicate ;
2828
29+ import org .springframework .cloud .gateway .filter .GatewayFilter ;
30+ import org .springframework .cloud .gateway .handler .AsyncPredicate ;
31+ import org .springframework .cloud .gateway .support .ServerWebExchangeUtils ;
2932import org .springframework .core .Ordered ;
3033import org .springframework .util .Assert ;
3134import org .springframework .web .server .ServerWebExchange ;
32- import org .springframework .cloud .gateway .filter .GatewayFilter ;
3335import org .springframework .web .util .UriComponentsBuilder ;
3436
37+ import static org .springframework .cloud .gateway .support .ServerWebExchangeUtils .toAsyncPredicate ;
38+
3539/**
3640 * @author Spencer Gibb
3741 */
@@ -43,7 +47,7 @@ public class Route implements Ordered {
4347
4448 private final int order ;
4549
46- private final Predicate <ServerWebExchange > predicate ;
50+ private final AsyncPredicate <ServerWebExchange > predicate ;
4751
4852 private final List <GatewayFilter > gatewayFilters ;
4953
@@ -58,46 +62,57 @@ public static Builder builder(RouteDefinition routeDefinition) {
5862 .order (routeDefinition .getOrder ());
5963 }
6064
61- private Route (String id , URI uri , int order , Predicate <ServerWebExchange > predicate , List <GatewayFilter > gatewayFilters ) {
65+ public static AsyncBuilder async () {
66+ return new AsyncBuilder ();
67+ }
68+
69+ public static AsyncBuilder async (RouteDefinition routeDefinition ) {
70+ return new AsyncBuilder ()
71+ .id (routeDefinition .getId ())
72+ .uri (routeDefinition .getUri ())
73+ .order (routeDefinition .getOrder ());
74+ }
75+
76+ private Route (String id , URI uri , int order , AsyncPredicate <ServerWebExchange > predicate , List <GatewayFilter > gatewayFilters ) {
6277 this .id = id ;
6378 this .uri = uri ;
6479 this .order = order ;
6580 this .predicate = predicate ;
6681 this .gatewayFilters = gatewayFilters ;
6782 }
6883
69- public static class Builder {
70- private String id ;
84+ public abstract static class AbstractBuilder < B extends AbstractBuilder < B >> {
85+ protected String id ;
7186
72- private URI uri ;
87+ protected URI uri ;
7388
74- private int order = 0 ;
89+ protected int order = 0 ;
7590
76- private Predicate < ServerWebExchange > predicate ;
91+ protected List < GatewayFilter > gatewayFilters = new ArrayList <>() ;
7792
78- private List < GatewayFilter > gatewayFilters = new ArrayList <>();
93+ protected AbstractBuilder () {}
7994
80- private Builder () {}
95+ protected abstract B getThis ();
8196
82- public Builder id (String id ) {
97+ public B id (String id ) {
8398 this .id = id ;
84- return this ;
99+ return getThis () ;
85100 }
86101
87102 public String getId () {
88103 return id ;
89104 }
90105
91- public Builder order (int order ) {
106+ public B order (int order ) {
92107 this .order = order ;
93- return this ;
108+ return getThis () ;
94109 }
95110
96- public Builder uri (String uri ) {
111+ public B uri (String uri ) {
97112 return uri (URI .create (uri ));
98113 }
99114
100- public Builder uri (URI uri ) {
115+ public B uri (URI uri ) {
101116 this .uri = uri ;
102117 if (this .uri .getPort () < 0 && this .uri .getScheme ().startsWith ("http" )) {
103118 // default known http ports
@@ -107,62 +122,115 @@ public Builder uri(URI uri) {
107122 .build (false )
108123 .toUri ();
109124 }
125+ return getThis ();
126+ }
127+
128+ public abstract AsyncPredicate <ServerWebExchange > getPredicate ();
129+
130+
131+
132+ public B replaceFilters (List <GatewayFilter > gatewayFilters ) {
133+ this .gatewayFilters = gatewayFilters ;
134+ return getThis ();
135+ }
136+
137+ public B filter (GatewayFilter gatewayFilter ) {
138+ this .gatewayFilters .add (gatewayFilter );
139+ return getThis ();
140+ }
141+
142+ public B filters (Collection <GatewayFilter > gatewayFilters ) {
143+ this .gatewayFilters .addAll (gatewayFilters );
144+ return getThis ();
145+ }
146+
147+ public B filters (GatewayFilter ... gatewayFilters ) {
148+ return filters (Arrays .asList (gatewayFilters ));
149+ }
150+
151+ public Route build () {
152+ Assert .notNull (this .id , "id can not be null" );
153+ Assert .notNull (this .uri , "uri can not be null" );
154+ AsyncPredicate <ServerWebExchange > predicate = getPredicate ();
155+ Assert .notNull (predicate , "predicate can not be null" );
156+
157+ return new Route (this .id , this .uri , this .order , predicate , this .gatewayFilters );
158+ }
159+ }
160+
161+ public static class AsyncBuilder extends AbstractBuilder <AsyncBuilder > {
162+
163+ protected AsyncPredicate <ServerWebExchange > predicate ;
164+
165+ @ Override
166+ protected AsyncBuilder getThis () {
110167 return this ;
111168 }
112169
113- public Predicate <ServerWebExchange > getPredicate () {
170+ @ Override
171+ public AsyncPredicate <ServerWebExchange > getPredicate () {
114172 return this .predicate ;
115173 }
116174
117- public Builder predicate (Predicate <ServerWebExchange > predicate ) {
175+ public AsyncBuilder predicate (Predicate <ServerWebExchange > predicate ) {
176+ return asyncPredicate (toAsyncPredicate (predicate ));
177+ }
178+
179+ public AsyncBuilder asyncPredicate (AsyncPredicate <ServerWebExchange > predicate ) {
118180 this .predicate = predicate ;
119181 return this ;
120182 }
121183
122- public Builder and (Predicate <ServerWebExchange > predicate ) {
184+ public AsyncBuilder and (AsyncPredicate <ServerWebExchange > predicate ) {
123185 Assert .notNull (this .predicate , "can not call and() on null predicate" );
124186 this .predicate = this .predicate .and (predicate );
125187 return this ;
126188 }
127189
128- public Builder or (Predicate <ServerWebExchange > predicate ) {
190+ public AsyncBuilder or (AsyncPredicate <ServerWebExchange > predicate ) {
129191 Assert .notNull (this .predicate , "can not call or() on null predicate" );
130192 this .predicate = this .predicate .or (predicate );
131193 return this ;
132194 }
133195
134- public Builder negate () {
196+ public AsyncBuilder negate () {
135197 Assert .notNull (this .predicate , "can not call negate() on null predicate" );
136198 this .predicate = this .predicate .negate ();
137199 return this ;
138200 }
201+ }
139202
140- public Builder replaceFilters (List <GatewayFilter > gatewayFilters ) {
141- this .gatewayFilters = gatewayFilters ;
203+ public static class Builder extends AbstractBuilder <Builder > {
204+ protected Predicate <ServerWebExchange > predicate ;
205+
206+ @ Override
207+ protected Builder getThis () {
142208 return this ;
143209 }
144210
145- public Builder filter ( GatewayFilter gatewayFilter ) {
146- this . gatewayFilters . add ( gatewayFilter );
147- return this ;
211+ @ Override
212+ public AsyncPredicate < ServerWebExchange > getPredicate () {
213+ return ServerWebExchangeUtils . toAsyncPredicate ( this . predicate ) ;
148214 }
149215
150- public Builder filters (Collection <GatewayFilter > gatewayFilters ) {
151- this .gatewayFilters .addAll (gatewayFilters );
216+ public Builder and (Predicate <ServerWebExchange > predicate ) {
217+ Assert .notNull (this .predicate , "can not call and() on null predicate" );
218+ this .predicate = this .predicate .and (predicate );
152219 return this ;
153220 }
154221
155- public Builder filters (GatewayFilter ... gatewayFilters ) {
156- return filters (Arrays .asList (gatewayFilters ));
222+ public Builder or (Predicate <ServerWebExchange > predicate ) {
223+ Assert .notNull (this .predicate , "can not call or() on null predicate" );
224+ this .predicate = this .predicate .or (predicate );
225+ return this ;
157226 }
158227
159- public Route build () {
160- Assert .notNull (this .id , "id can not be null" );
161- Assert .notNull (this .uri , "uri can not be null" );
162- Assert .notNull (this .predicate , "predicate can not be null" );
163-
164- return new Route (this .id , this .uri , this .order , this .predicate , this .gatewayFilters );
228+ public Builder negate () {
229+ Assert .notNull (this .predicate , "can not call negate() on null predicate" );
230+ this .predicate = this .predicate .negate ();
231+ return this ;
165232 }
233+
166234 }
167235
168236 public String getId () {
@@ -177,7 +245,7 @@ public int getOrder() {
177245 return order ;
178246 }
179247
180- public Predicate <ServerWebExchange > getPredicate () {
248+ public AsyncPredicate <ServerWebExchange > getPredicate () {
181249 return this .predicate ;
182250 }
183251
0 commit comments