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.handler;
018
019import java.util.Map;
020
021import org.springframework.util.Assert;
022import org.springframework.util.PathMatcher;
023
024/**
025 * Container for the result from request pattern matching via
026 * {@link MatchableHandlerMapping} with a method to further extract
027 * URI template variables from the pattern.
028 *
029 * @author Rossen Stoyanchev
030 * @since 4.3.1
031 */
032public class RequestMatchResult {
033
034        private final String matchingPattern;
035
036        private final String lookupPath;
037
038        private final PathMatcher pathMatcher;
039
040
041        /**
042         * Create an instance with a matching pattern.
043         * @param matchingPattern the matching pattern, possibly not the same as the
044         * input pattern, e.g. inputPattern="/foo" and matchingPattern="/foo/".
045         * @param lookupPath the lookup path extracted from the request
046         * @param pathMatcher the PathMatcher used
047         */
048        public RequestMatchResult(String matchingPattern, String lookupPath, PathMatcher pathMatcher) {
049                Assert.hasText(matchingPattern, "'matchingPattern' is required");
050                Assert.hasText(lookupPath, "'lookupPath' is required");
051                Assert.notNull(pathMatcher, "'pathMatcher' is required");
052                this.matchingPattern = matchingPattern;
053                this.lookupPath = lookupPath;
054                this.pathMatcher = pathMatcher;
055        }
056
057
058        /**
059         * Extract URI template variables from the matching pattern as defined in
060         * {@link PathMatcher#extractUriTemplateVariables}.
061         * @return a map with URI template variables
062         */
063        public Map<String, String> extractUriTemplateVariables() {
064                return this.pathMatcher.extractUriTemplateVariables(this.matchingPattern, this.lookupPath);
065        }
066
067}