001/* 002 * Copyright 2002-2019 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.util; 018 019import java.util.Comparator; 020import java.util.Map; 021 022import org.springframework.lang.Nullable; 023 024/** 025 * {@code RouteMatcher} that delegates to a {@link PathMatcher}. 026 * 027 * <p><strong>Note:</strong> This implementation is not efficient since 028 * {@code PathMatcher} treats paths and patterns as Strings. For more optimized 029 * performance use the {@code PathPatternRouteMatcher} from {@code spring-web} 030 * which enables use of parsed routes and patterns. 031 * 032 * @author Rossen Stoyanchev 033 * @since 5.2 034 */ 035public class SimpleRouteMatcher implements RouteMatcher { 036 037 private final PathMatcher pathMatcher; 038 039 040 /** 041 * Create a new {@code SimpleRouteMatcher} for the given 042 * {@link PathMatcher} delegate. 043 */ 044 public SimpleRouteMatcher(PathMatcher pathMatcher) { 045 Assert.notNull(pathMatcher, "PathMatcher is required"); 046 this.pathMatcher = pathMatcher; 047 } 048 049 /** 050 * Return the underlying {@link PathMatcher} delegate. 051 */ 052 public PathMatcher getPathMatcher() { 053 return this.pathMatcher; 054 } 055 056 057 @Override 058 public Route parseRoute(String route) { 059 return new DefaultRoute(route); 060 } 061 062 @Override 063 public boolean isPattern(String route) { 064 return this.pathMatcher.isPattern(route); 065 } 066 067 @Override 068 public String combine(String pattern1, String pattern2) { 069 return this.pathMatcher.combine(pattern1, pattern2); 070 } 071 072 @Override 073 public boolean match(String pattern, Route route) { 074 return this.pathMatcher.match(pattern, route.value()); 075 } 076 077 @Override 078 @Nullable 079 public Map<String, String> matchAndExtract(String pattern, Route route) { 080 if (!match(pattern, route)) { 081 return null; 082 } 083 return this.pathMatcher.extractUriTemplateVariables(pattern, route.value()); 084 } 085 086 @Override 087 public Comparator<String> getPatternComparator(Route route) { 088 return this.pathMatcher.getPatternComparator(route.value()); 089 } 090 091 092 private static class DefaultRoute implements Route { 093 094 private final String path; 095 096 DefaultRoute(String path) { 097 this.path = path; 098 } 099 100 @Override 101 public String value() { 102 return this.path; 103 } 104 105 @Override 106 public String toString() { 107 return value(); 108 } 109 } 110 111}