- 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.ConditionalHandlercan 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
Handleronly when the request contains a header namefooand acceptstext/plain, otherwise return a response with404code.RequestPredicate.create() .containsHeader("foo") .accepts(MediaType.TEXT_PLAIN) .thenApply((req, resp) -> { // handler logic });Invoke a
Handleronly when the request contains a header namedfoootherwise 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 classRequestPredicate.ConditionalHandler
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description RequestPredicateaccepts(MediaType... contentType)Only accept request that accepts any of the given content types.RequestPredicateaccepts(String... contentType)Accept requests only when it accepts any of the given content types.RequestPredicateand(Predicate<ServerRequest> predicate)Returns a composed predicate that represents a logical AND expression between this predicate and another predicate.RequestPredicatecontainsCookie(String name)Accept request only when the specified cookie exists.RequestPredicatecontainsCookie(String name, String value)Accept requests only when the specified cookie contains a given value.RequestPredicatecontainsCookie(String name, Predicate<String> predicate)Accept requests only when the specified cookie is valid.RequestPredicatecontainsHeader(String name)Accept requests only when the specified header name exists.RequestPredicatecontainsHeader(String name, String value)Accept requests only when the specified header contains a given value.RequestPredicatecontainsHeader(String name, Predicate<String> predicate)Accept requests only when the specified header is valid.RequestPredicatecontainsQueryParameter(String name)Accept requests only when the specified query parameter exists.RequestPredicatecontainsQueryParameter(String name, String value)Accept requests only when the specified query parameter contains a given value.RequestPredicatecontainsQueryParameter(String name, Predicate<String> predicate)Accept requests only when the specified query parameter is valid.static RequestPredicatecreate()Creates new emptyRequestPredicateinstance.RequestPredicatehasContentType(MediaType... contentType)Only accept requests with any of the given content types.RequestPredicatehasContentType(String... contentType)Only accept requests with any of the given content types.RequestPredicateisOfMethod(Http.Method... methods)Accepts only requests with one of specified HTTP methods.RequestPredicateisOfMethod(String... methods)Accepts only requests with one of specified HTTP methods.RequestPredicatenegate()Return a predicate that represents the logical negation of this predicate.RequestPredicateor(Predicate<ServerRequest> predicate)Returns a composed predicate that represents a logical OR expression between this predicate and another predicate.booleantest(ServerRequest request)Evaluate this predicate.RequestPredicate.ConditionalHandlerthenApply(Handler handler)Set theHandlerto use when this predicate matches the request.
-
-
-
Method Detail
-
thenApply
public RequestPredicate.ConditionalHandler thenApply(Handler handler)
Set theHandlerto use when this predicate matches the request.- Parameters:
handler- handler to use this predicate instance matches- Returns:
- instance of
RequestPredicate.ConditionalHandlerthat can be used to specify anotherHandlerto 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 emptyRequestPredicateinstance.- Returns:
- new empty predicate (accepts all requests).
-
-