@@ -266,10 +266,17 @@ public static RequestPredicate OPTIONS(String pattern) {
266
266
*/
267
267
public static RequestPredicate pathExtension (String extension ) {
268
268
Assert .notNull (extension , "'extension' must not be null" );
269
- return pathExtension (pathExtension -> {
270
- boolean match = extension .equalsIgnoreCase (pathExtension );
271
- traceMatch ("Extension" , extension , pathExtension , match );
272
- return match ;
269
+ return pathExtension (new Predicate <String >() {
270
+ @ Override
271
+ public boolean test (String pathExtension ) {
272
+ boolean match = extension .equalsIgnoreCase (pathExtension );
273
+ traceMatch ("Extension" , extension , pathExtension , match );
274
+ return match ;
275
+ }
276
+
277
+ public String toString () {
278
+ return String .format ("*.%s" , extension );
279
+ }
273
280
});
274
281
}
275
282
@@ -281,11 +288,30 @@ public static RequestPredicate pathExtension(String extension) {
281
288
* file extension
282
289
*/
283
290
public static RequestPredicate pathExtension (Predicate <String > extensionPredicate ) {
284
- Assert .notNull (extensionPredicate , "'extensionPredicate' must not be null" );
285
- return request -> {
286
- String pathExtension = UriUtils .extractFileExtension (request .path ());
287
- return extensionPredicate .test (pathExtension );
288
- };
291
+ return new PathExtensionPredicate (extensionPredicate );
292
+ }
293
+
294
+ /**
295
+ * Return a {@code RequestPredicate} that matches if the request's query parameter of the given name
296
+ * has the given value
297
+ * @param name the name of the query parameter to test against
298
+ * @param value the value of the query parameter to test against
299
+ * @return a predicate that matches if the query parameter has the given value
300
+ * @see ServerRequest#queryParam(String)
301
+ * @since 5.0.7
302
+ */
303
+ public static RequestPredicate queryParam (String name , String value ) {
304
+ return queryParam (name , new Predicate <String >() {
305
+ @ Override
306
+ public boolean test (String s ) {
307
+ return s .equals (value );
308
+ }
309
+
310
+ @ Override
311
+ public String toString () {
312
+ return String .format ("== %s" , value );
313
+ }
314
+ });
289
315
}
290
316
291
317
/**
@@ -297,10 +323,7 @@ public static RequestPredicate pathExtension(Predicate<String> extensionPredicat
297
323
* @see ServerRequest#queryParam(String)
298
324
*/
299
325
public static RequestPredicate queryParam (String name , Predicate <String > predicate ) {
300
- return request -> {
301
- Optional <String > s = request .queryParam (name );
302
- return s .filter (predicate ).isPresent ();
303
- };
326
+ return new QueryParamPredicate (name , predicate );
304
327
}
305
328
306
329
@@ -402,6 +425,56 @@ public String toString() {
402
425
}
403
426
}
404
427
428
+
429
+ private static class PathExtensionPredicate implements RequestPredicate {
430
+
431
+ private final Predicate <String > extensionPredicate ;
432
+
433
+ public PathExtensionPredicate (Predicate <String > extensionPredicate ) {
434
+ Assert .notNull (extensionPredicate , "Predicate must not be null" );
435
+ this .extensionPredicate = extensionPredicate ;
436
+ }
437
+
438
+ @ Override
439
+ public boolean test (ServerRequest request ) {
440
+ String pathExtension = UriUtils .extractFileExtension (request .path ());
441
+ return this .extensionPredicate .test (pathExtension );
442
+ }
443
+
444
+ @ Override
445
+ public String toString () {
446
+ return this .extensionPredicate .toString ();
447
+ }
448
+
449
+ }
450
+
451
+
452
+ private static class QueryParamPredicate implements RequestPredicate {
453
+
454
+ private final String name ;
455
+
456
+ private final Predicate <String > predicate ;
457
+
458
+ public QueryParamPredicate (String name , Predicate <String > predicate ) {
459
+ Assert .notNull (name , "Name must not be null" );
460
+ Assert .notNull (predicate , "Predicate must not be null" );
461
+ this .name = name ;
462
+ this .predicate = predicate ;
463
+ }
464
+
465
+ @ Override
466
+ public boolean test (ServerRequest request ) {
467
+ Optional <String > s = request .queryParam (this .name );
468
+ return s .filter (this .predicate ).isPresent ();
469
+ }
470
+
471
+ @ Override
472
+ public String toString () {
473
+ return String .format ("?%s %s" , this .name , this .predicate );
474
+ }
475
+ }
476
+
477
+
405
478
static class AndRequestPredicate implements RequestPredicate {
406
479
407
480
private final RequestPredicate left ;
@@ -431,6 +504,7 @@ public String toString() {
431
504
}
432
505
}
433
506
507
+
434
508
static class OrRequestPredicate implements RequestPredicate {
435
509
436
510
private final RequestPredicate left ;
@@ -466,6 +540,7 @@ public String toString() {
466
540
}
467
541
}
468
542
543
+
469
544
private static class SubPathServerRequestWrapper implements ServerRequest {
470
545
471
546
private final ServerRequest request ;
0 commit comments