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.config; 018 019import java.util.LinkedHashMap; 020import java.util.Map; 021import java.util.function.Predicate; 022 023import org.springframework.lang.Nullable; 024 025/** 026 * Assist with configuring {@code HandlerMapping}'s with path matching options. 027 * 028 * @author Rossen Stoyanchev 029 * @author Brian Clozel 030 * @since 5.0 031 */ 032public class PathMatchConfigurer { 033 034 @Nullable 035 private Boolean trailingSlashMatch; 036 037 038 @Nullable 039 private Boolean caseSensitiveMatch; 040 041 @Nullable 042 private Map<String, Predicate<Class<?>>> pathPrefixes; 043 044 045 /** 046 * Whether to match to URLs irrespective of their case. 047 * If enabled a method mapped to "/users" won't match to "/Users/". 048 * <p>The default value is {@code false}. 049 */ 050 public PathMatchConfigurer setUseCaseSensitiveMatch(Boolean caseSensitiveMatch) { 051 this.caseSensitiveMatch = caseSensitiveMatch; 052 return this; 053 } 054 055 /** 056 * Whether to match to URLs irrespective of the presence of a trailing slash. 057 * If enabled a method mapped to "/users" also matches to "/users/". 058 * <p>The default value is {@code true}. 059 */ 060 public PathMatchConfigurer setUseTrailingSlashMatch(Boolean trailingSlashMatch) { 061 this.trailingSlashMatch = trailingSlashMatch; 062 return this; 063 } 064 065 /** 066 * Configure a path prefix to apply to matching controller methods. 067 * <p>Prefixes are used to enrich the mappings of every {@code @RequestMapping} 068 * method whose controller type is matched by the corresponding 069 * {@code Predicate}. The prefix for the first matching predicate is used. 070 * <p>Consider using {@link org.springframework.web.method.HandlerTypePredicate 071 * HandlerTypePredicate} to group controllers. 072 * @param prefix the path prefix to apply 073 * @param predicate a predicate for matching controller types 074 * @since 5.1 075 */ 076 public PathMatchConfigurer addPathPrefix(String prefix, Predicate<Class<?>> predicate) { 077 if (this.pathPrefixes == null) { 078 this.pathPrefixes = new LinkedHashMap<>(); 079 } 080 this.pathPrefixes.put(prefix, predicate); 081 return this; 082 } 083 084 085 @Nullable 086 protected Boolean isUseTrailingSlashMatch() { 087 return this.trailingSlashMatch; 088 } 089 090 @Nullable 091 protected Boolean isUseCaseSensitiveMatch() { 092 return this.caseSensitiveMatch; 093 } 094 095 @Nullable 096 protected Map<String, Predicate<Class<?>>> getPathPrefixes() { 097 return this.pathPrefixes; 098 } 099}