001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.reactive.result.condition;
018
019import org.springframework.lang.Nullable;
020import org.springframework.web.server.ServerWebExchange;
021
022/**
023 * Contract for request mapping conditions.
024 *
025 * <p>Request conditions can be combined via {@link #combine(Object)}, matched to
026 * a request via {@link #getMatchingCondition(ServerWebExchange)}, and compared
027 * to each other via {@link #compareTo(Object, ServerWebExchange)} to determine
028 * which is a closer match for a given request.
029 *
030 * @author Rossen Stoyanchev
031 * @since 5.0
032 * @param <T> the type of objects that this RequestCondition can be combined
033 * with and compared to
034 */
035public interface RequestCondition<T> {
036
037        /**
038         * Combine this condition with another such as conditions from a
039         * type-level and method-level {@code @RequestMapping} annotation.
040         * @param other the condition to combine with.
041         * @return a request condition instance that is the result of combining
042         * the two condition instances.
043         */
044        T combine(T other);
045
046        /**
047         * Check if the condition matches the request returning a potentially new
048         * instance created for the current request. For example a condition with
049         * multiple URL patterns may return a new instance only with those patterns
050         * that match the request.
051         * <p>For CORS pre-flight requests, conditions should match to the would-be,
052         * actual request (e.g. URL pattern, query parameters, and the HTTP method
053         * from the "Access-Control-Request-Method" header). If a condition cannot
054         * be matched to a pre-flight request it should return an instance with
055         * empty content thus not causing a failure to match.
056         * @return a condition instance in case of a match or {@code null} otherwise.
057         */
058        @Nullable
059        T getMatchingCondition(ServerWebExchange exchange);
060
061        /**
062         * Compare this condition to another condition in the context of
063         * a specific request. This method assumes both instances have
064         * been obtained via {@link #getMatchingCondition(ServerWebExchange)}
065         * to ensure they have content relevant to current request only.
066         */
067        int compareTo(T other, ServerWebExchange exchange);
068
069}