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)
.
Examples
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!");
});
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
-
Method Summary
Modifier and TypeMethodDescriptionOnly accept request that accepts any of the given content types.Accept requests only when it accepts any of the given content types.and
(Predicate<ServerRequest> predicate) Returns a composed predicate that represents a logical AND expression between this predicate and another predicate.containsCookie
(String name) Accept request only when the specified cookie exists.containsCookie
(String name, String value) Accept requests only when the specified cookie contains a given value.containsCookie
(String name, Predicate<String> predicate) Accept requests only when the specified cookie is valid.containsHeader
(String name) Accept requests only when the specified header name exists.containsHeader
(String name, String value) Accept requests only when the specified header contains a given value.containsHeader
(String name, Predicate<String> predicate) Accept requests only when the specified header is valid.containsQueryParameter
(String name) Accept requests only when the specified query parameter exists.containsQueryParameter
(String name, String value) Accept requests only when the specified query parameter contains a given value.containsQueryParameter
(String name, Predicate<String> predicate) Accept requests only when the specified query parameter is valid.static RequestPredicate
create()
Creates new emptyRequestPredicate
instance.hasContentType
(MediaType... contentType) Only accept requests with any of the given content types.hasContentType
(String... contentType) Only accept requests with any of the given content types.isOfMethod
(Http.Method... methods) Accepts only requests with one of specified HTTP methods.isOfMethod
(String... methods) Accepts only requests with one of specified HTTP methods.negate()
Return a predicate that represents the logical negation of this predicate.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.Set theHandler
to use when this predicate matches the request.
-
Method Details
-
thenApply
Set theHandler
to use when this predicate matches the request.- Parameters:
handler
- handler to use this predicate instance matches- Returns:
- instance of
RequestPredicate.ConditionalHandler
that can be used to specify anotherHandler
to use when this predicates does not match the request - See Also:
-
test
Evaluate this predicate.- Parameters:
request
- the server request- Returns:
- the computed value
-
and
Returns a composed predicate that represents a logical AND expression between this predicate and another predicate.- Parameters:
predicate
- predicate to compose with- Returns:
- composed predicate representing the logical expression between this predicate AND the provided predicate
-
or
Returns a composed predicate that represents a logical OR expression between this predicate and another predicate.- Parameters:
predicate
- predicate that compute the new value- Returns:
- composed predicate representing the logical expression between this predicate OR the provided predicate
-
negate
Return a predicate that represents the logical negation of this predicate.- Returns:
- new predicate that represents the logical negation of this predicate.
-
isOfMethod
Accepts only requests with one of specified HTTP methods.- Parameters:
methods
- Acceptable method names- Returns:
- composed predicate representing the logical expression between this predicate AND the provided predicate
- Throws:
NullPointerException
- if the specified methods array is null
-
isOfMethod
Accepts only requests with one of specified HTTP methods.- Parameters:
methods
- Acceptable method names- Returns:
- composed predicate representing the logical expression between this predicate AND the provided predicate
- Throws:
NullPointerException
- if the methods type array is null
-
containsHeader
Accept requests only when the specified header name exists.- Parameters:
name
- header name- Returns:
- composed predicate representing the logical expression between this predicate AND the provided predicate
- Throws:
NullPointerException
- if the specified name is null
-
containsHeader
Accept requests only when the specified header contains a given value. If the request contains more then one header, it will be accepted if any of the values is equal to the provided value.- Parameters:
name
- header namevalue
- the expected header value- Returns:
- composed predicate representing the logical expression between this predicate AND the provided predicate
- Throws:
NullPointerException
- if the specified name or value is null
-
containsHeader
Accept requests only when the specified header is valid. A header is valid when the supplied predicate matches the header value. If the request contains more than one header, it will be accepted if the predicate matches any of the values.- Parameters:
name
- header namepredicate
- predicate to match the header value- Returns:
- composed predicate representing the logical expression between this predicate AND the provided predicate
- Throws:
NullPointerException
- if the specified name or predicate is null
-
containsQueryParameter
Accept requests only when the specified query parameter exists.- Parameters:
name
- query parameter name- Returns:
- composed predicate representing the logical expression between this predicate AND the provided predicate
- Throws:
NullPointerException
- if the specified name is null
-
containsQueryParameter
Accept requests only when the specified query parameter contains a given value.- Parameters:
name
- query parameter namevalue
- expected query parameter value- Returns:
- composed predicate representing the logical expression between this predicate AND the provided predicate
- Throws:
NullPointerException
- if the specified name or value is null
-
containsQueryParameter
Accept requests only when the specified query parameter is valid.- Parameters:
name
- query parameter namepredicate
- to match the query parameter value- Returns:
- composed predicate representing the logical expression between this predicate AND the provided predicate
- Throws:
NullPointerException
- if the specified name or predicate is null
-
containsCookie
Accept request only when the specified cookie exists.- Parameters:
name
- cookie name- Returns:
- composed predicate representing the logical expression between this predicate AND the provided predicate
- Throws:
NullPointerException
- if the specified name is null
-
containsCookie
Accept requests only when the specified cookie contains a given value.- Parameters:
name
- cookie namevalue
- expected cookie value- Returns:
- composed predicate representing the logical expression between this predicate AND the provided predicate
- Throws:
NullPointerException
- if the specified name or value is null
-
containsCookie
Accept requests only when the specified cookie is valid.- Parameters:
name
- cookie namepredicate
- predicate to match the cookie value- Returns:
- composed predicate representing the logical expression between this predicate AND the provided predicate
- Throws:
NullPointerException
- if the specified name or predicate is null
-
accepts
Accept requests only when it accepts any of the given content types.- Parameters:
contentType
- the content types to test- Returns:
- composed predicate representing the logical expression between this predicate AND the provided predicate
- Throws:
NullPointerException
- if the specified content type array is null
-
accepts
Only accept request that accepts any of the given content types.- Parameters:
contentType
- the content types to test- Returns:
- composed predicate representing the logical expression between this predicate AND the provided predicate
- Throws:
NullPointerException
- if the specified content type array is null
-
hasContentType
Only accept requests with any of the given content types.- Parameters:
contentType
- Content type- Returns:
- composed predicate representing the logical expression between this predicate AND the provided predicate
- Throws:
NullPointerException
- if the specified content type array is null
-
hasContentType
Only accept requests with any of the given content types.- Parameters:
contentType
- Content type- Returns:
- composed predicate representing the logical expression between this predicate AND the provided predicate
- Throws:
NullPointerException
- if the specified content type array is null
-
create
Creates new emptyRequestPredicate
instance.- Returns:
- new empty predicate (accepts all requests).
-