001/*
002 * Copyright 2002-2016 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.servlet.mvc.condition;
018
019import javax.servlet.http.HttpServletRequest;
020
021/**
022 * Contract for request mapping conditions.
023 *
024 * <p>Request conditions can be combined via {@link #combine(Object)}, matched to
025 * a request via {@link #getMatchingCondition(HttpServletRequest)}, and compared
026 * to each other via {@link #compareTo(Object, HttpServletRequest)} to determine
027 * which is a closer match for a given request.
028 *
029 * @author Rossen Stoyanchev
030 * @author Arjen Poutsma
031 * @since 3.1
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        T getMatchingCondition(HttpServletRequest request);
059
060        /**
061         * Compare this condition to another condition in the context of
062         * a specific request. This method assumes both instances have
063         * been obtained via {@link #getMatchingCondition(HttpServletRequest)}
064         * to ensure they have content relevant to current request only.
065         */
066        int compareTo(T other, HttpServletRequest request);
067
068}