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