public final class RequestPredicate extends Object
A new expression can be created using the create() method. This method
will initialize an expression with an empty condition that will match any
request.
Conditions are added to the expression by chaining method invocations and form
a logical AND expression. Each method invocation will return a different
instance representing the last condition in the expression. Each instance can
represent only one condition, invoking a method representing a condition more
than once per instance will throw an IllegalStateException.
The expression can be evaluated against a request using the
test(ServerRequest) method, or a RequestPredicate.ConditionalHandler can be
used to evaluate the expression and delegate to other handlers based on the
result of the evaluation.
The thenApply(Handler) method can be invoked on an expression to create
a RequestPredicate.ConditionalHandler.
The handler to be used for matching requests is passed as parameter to
thenApply(Handler) and the handler to be used for requests that do not
match can be specified using RequestPredicate.ConditionalHandler.otherwise(Handler).
Invoke a Handler only when the request contains a header name foo
and accepts text/plain, otherwise return a response with 404 code.
RequestPredicate.create()
.containsHeader("foo")
.accepts(MediaType.TEXT_PLAIN)
.thenApply((req, resp) -> {
// handler logic
});
Invoke a Handler only when the request contains a header named foo
otherwise invoke another handler that throws an exception.
RequestPredicate.create()
.containsHeader("foo")
.thenApply((req, resp) -> {
// handler logic
})
.otherwise(req, resp) -> {
throw new RuntimeException("Missing 'foo' header!");
});
| Modifier and Type | Class and Description |
|---|---|
static class |
RequestPredicate.ConditionalHandler
|
| Modifier and Type | Method and Description |
|---|---|
RequestPredicate |
accepts(MediaType... contentType)
Only accept request that accepts any of the given content types.
|
RequestPredicate |
accepts(String... contentType)
Accept requests only when it accepts any of the given content types.
|
RequestPredicate |
and(Predicate<ServerRequest> predicate)
Returns a composed predicate that represents a logical AND expression
between this predicate and another predicate.
|
RequestPredicate |
containsCookie(String name)
Accept request only when the specified cookie exists.
|
RequestPredicate |
containsCookie(String name,
Predicate<String> predicate)
Accept requests only when the specified cookie is valid.
|
RequestPredicate |
containsCookie(String name,
String value)
Accept requests only when the specified cookie contains a given value.
|
RequestPredicate |
containsHeader(String name)
Accept requests only when the specified header name exists.
|
RequestPredicate |
containsHeader(String name,
Predicate<String> predicate)
Accept requests only when the specified header is valid.
|
RequestPredicate |
containsHeader(String name,
String value)
Accept requests only when the specified header contains a given value.
|
RequestPredicate |
containsQueryParameter(String name)
Accept requests only when the specified query parameter exists.
|
RequestPredicate |
containsQueryParameter(String name,
Predicate<String> predicate)
Accept requests only when the specified query parameter is valid.
|
RequestPredicate |
containsQueryParameter(String name,
String value)
Accept requests only when the specified query parameter contains a given
value.
|
static RequestPredicate |
create()
Creates new empty
RequestPredicate instance. |
RequestPredicate |
hasContentType(MediaType... contentType)
Only accept requests with any of the given content types.
|
RequestPredicate |
hasContentType(String... contentType)
Only accept requests with any of the given content types.
|
RequestPredicate |
isOfMethod(Http.Method... methods)
Accepts only requests with one of specified HTTP methods.
|
RequestPredicate |
isOfMethod(String... methods)
Accepts only requests with one of specified HTTP methods.
|
RequestPredicate |
negate()
Return a predicate that represents the logical negation of this predicate.
|
RequestPredicate |
or(Predicate<ServerRequest> predicate)
Returns a composed predicate that represents a logical OR expression
between this predicate and another predicate.
|
boolean |
test(ServerRequest request)
Evaluate this predicate.
|
RequestPredicate.ConditionalHandler |
thenApply(Handler handler)
Set the
Handler to use when this predicate matches the request. |
public RequestPredicate.ConditionalHandler thenApply(Handler handler)
Handler to use when this predicate matches the request.handler - handler to use this predicate instance matchesRequestPredicate.ConditionalHandler that can be used to
specify another Handler to use when this predicates does not
match the requestRequestPredicate.ConditionalHandler.otherwise(Handler)public boolean test(ServerRequest request)
request - the server requestpublic RequestPredicate and(Predicate<ServerRequest> predicate)
predicate - predicate to compose withpublic RequestPredicate or(Predicate<ServerRequest> predicate)
predicate - predicate that compute the new valuepublic RequestPredicate negate()
public RequestPredicate isOfMethod(String... methods)
methods - Acceptable method namesNullPointerException - if the specified methods array is nullpublic RequestPredicate isOfMethod(Http.Method... methods)
methods - Acceptable method namesNullPointerException - if the methods type array is nullpublic RequestPredicate containsHeader(String name)
name - header nameNullPointerException - if the specified name is nullpublic RequestPredicate containsHeader(String name, String value)
name - header namevalue - the expected header valueNullPointerException - if the specified name or value is nullpublic RequestPredicate containsHeader(String name, Predicate<String> predicate)
name - header namepredicate - predicate to match the header valueNullPointerException - if the specified name or predicate is nullpublic RequestPredicate containsQueryParameter(String name)
name - query parameter nameNullPointerException - if the specified name is nullpublic RequestPredicate containsQueryParameter(String name, String value)
name - query parameter namevalue - expected query parameter valueNullPointerException - if the specified name or value is nullpublic RequestPredicate containsQueryParameter(String name, Predicate<String> predicate)
name - query parameter namepredicate - to match the query parameter valueNullPointerException - if the specified name or predicate is nullpublic RequestPredicate containsCookie(String name)
name - cookie nameNullPointerException - if the specified name is nullpublic RequestPredicate containsCookie(String name, String value)
name - cookie namevalue - expected cookie valueNullPointerException - if the specified name or value is nullpublic RequestPredicate containsCookie(String name, Predicate<String> predicate)
name - cookie namepredicate - predicate to match the cookie valueNullPointerException - if the specified name or predicate is nullpublic RequestPredicate accepts(String... contentType)
contentType - the content types to testNullPointerException - if the specified content type array is nullpublic RequestPredicate accepts(MediaType... contentType)
contentType - the content types to testNullPointerException - if the specified content type array is nullpublic RequestPredicate hasContentType(String... contentType)
contentType - Content typeNullPointerException - if the specified content type array is nullpublic RequestPredicate hasContentType(MediaType... contentType)
contentType - Content typeNullPointerException - if the specified content type array is nullpublic static RequestPredicate create()
RequestPredicate instance.Copyright © 2018–2019 Oracle and/or its affiliates. All rights reserved. Use is subject to license terms.