- java.lang.Object
-
- io.helidon.webserver.RequestPredicate
-
public final class RequestPredicate extends Object
Fluent API that allows to create chains of request conditions for composing logical expressions to match requests.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 aRequestPredicate.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 aRequestPredicate.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 usingRequestPredicate.ConditionalHandler.otherwise(Handler)
.Examples
Invoke a
Handler
only when the request contains a header namefoo
and acceptstext/plain
, otherwise return a response with404
code.RequestPredicate.create() .containsHeader("foo") .accepts(MediaType.TEXT_PLAIN) .thenApply((req, resp) -> { // handler logic });
Invoke a
Handler
only when the request contains a header namedfoo
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
Nested Classes Modifier and Type Class Description static class
RequestPredicate.ConditionalHandler
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method 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, String value)
Accept requests only when the specified cookie contains a given value.RequestPredicate
containsCookie(String name, Predicate<String> predicate)
Accept requests only when the specified cookie is valid.RequestPredicate
containsHeader(String name)
Accept requests only when the specified header name exists.RequestPredicate
containsHeader(String name, String value)
Accept requests only when the specified header contains a given value.RequestPredicate
containsHeader(String name, Predicate<String> predicate)
Accept requests only when the specified header is valid.RequestPredicate
containsQueryParameter(String name)
Accept requests only when the specified query parameter exists.RequestPredicate
containsQueryParameter(String name, String value)
Accept requests only when the specified query parameter contains a given value.RequestPredicate
containsQueryParameter(String name, Predicate<String> predicate)
Accept requests only when the specified query parameter is valid.static RequestPredicate
create()
Creates new emptyRequestPredicate
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 theHandler
to use when this predicate matches the request.
-
-
-
Method Detail
-
thenApply
public RequestPredicate.ConditionalHandler thenApply(Handler handler)
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:
RequestPredicate.ConditionalHandler.otherwise(Handler)
-
test
public boolean test(ServerRequest request)
Evaluate this predicate.- Parameters:
request
- the server request- Returns:
- the computed value
-
and
public RequestPredicate and(Predicate<ServerRequest> predicate)
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
public RequestPredicate or(Predicate<ServerRequest> predicate)
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
public RequestPredicate negate()
Return a predicate that represents the logical negation of this predicate.- Returns:
- new predicate that represents the logical negation of this predicate.
-
isOfMethod
public RequestPredicate isOfMethod(String... methods)
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
public RequestPredicate isOfMethod(Http.Method... methods)
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
public RequestPredicate containsHeader(String name)
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
public RequestPredicate containsHeader(String name, String value)
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
public RequestPredicate containsHeader(String name, Predicate<String> predicate)
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
public RequestPredicate containsQueryParameter(String name)
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
public RequestPredicate containsQueryParameter(String name, String value)
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
public RequestPredicate containsQueryParameter(String name, Predicate<String> predicate)
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
public RequestPredicate containsCookie(String name)
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
public RequestPredicate containsCookie(String name, String value)
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
public RequestPredicate containsCookie(String name, Predicate<String> predicate)
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
public RequestPredicate accepts(String... contentType)
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
public RequestPredicate accepts(MediaType... contentType)
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
public RequestPredicate hasContentType(String... contentType)
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
public RequestPredicate hasContentType(MediaType... contentType)
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
public static RequestPredicate create()
Creates new emptyRequestPredicate
instance.- Returns:
- new empty predicate (accepts all requests).
-
-