Class 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 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!");
                     });
     
    • Method Detail

      • 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 name
        value - 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 name
        predicate - 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 name
        value - 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 name
        predicate - 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 name
        value - 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 name
        predicate - 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