Skip to content

Commit

Permalink
Deprecate UrlPathHelper shortcuts on AbstractHandlerMapping
Browse files Browse the repository at this point in the history
Given the availability of two alternatives mechanisms for URL path
matching, PathPatternParser and AntPathMatcher, and now that
parsed patterns are enabled by default, it makes sense to reduce the
proliferation of options on AbstractHandlerMapping by deprecating
shortcuts related to String path matching. Most applications rely
on Boot and on the MVC config to do all this.

See gh-28607
  • Loading branch information
rstoyanchev committed Jun 29, 2022
1 parent 8d08b1d commit 14fd260
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 34 deletions.
Expand Up @@ -58,7 +58,6 @@
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.handler.AbstractHandlerMapping;
import org.springframework.web.servlet.handler.MappedInterceptor;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
Expand All @@ -68,6 +67,7 @@
import org.springframework.web.servlet.theme.FixedThemeResolver;
import org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.util.UrlPathHelper;
import org.springframework.web.util.pattern.PathPatternParser;

/**
Expand Down Expand Up @@ -353,7 +353,7 @@ public StandaloneMockMvcBuilder setUseTrailingSlashPatternMatch(boolean useTrail
/**
* Set if ";" (semicolon) content should be stripped from the request URI. The value,
* if provided, is in turn set on
* {@link AbstractHandlerMapping#setRemoveSemicolonContent(boolean)}.
* {@link org.springframework.web.util.UrlPathHelper#setRemoveSemicolonContent(boolean)}.
*/
public StandaloneMockMvcBuilder setRemoveSemicolonContent(boolean removeSemicolonContent) {
this.removeSemicolonContent = removeSemicolonContent;
Expand Down Expand Up @@ -477,7 +477,9 @@ public RequestMappingHandlerMapping getHandlerMapping(
handlerMapping.setPatternParser(null);
handlerMapping.setUseSuffixPatternMatch(useSuffixPatternMatch);
if (removeSemicolonContent != null) {
handlerMapping.setRemoveSemicolonContent(removeSemicolonContent);
UrlPathHelper pathHelper = new UrlPathHelper();
pathHelper.setRemoveSemicolonContent(removeSemicolonContent);
handlerMapping.setUrlPathHelper(pathHelper);
}
}
else if (patternParser != null) {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -46,6 +46,7 @@
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UrlPathHelper;

import static org.hamcrest.core.Is.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
Expand Down Expand Up @@ -119,7 +120,9 @@ private static class HandlerMappingConfigurer implements BeanPostProcessor, Prio
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof RequestMappingHandlerMapping requestMappingHandlerMapping) {
// URL decode after request mapping, not before.
requestMappingHandlerMapping.setUrlDecode(false);
UrlPathHelper pathHelper = new UrlPathHelper();
pathHelper.setUrlDecode(false);
requestMappingHandlerMapping.setUrlPathHelper(pathHelper);
}
return bean;
}
Expand Down
Expand Up @@ -179,8 +179,9 @@ public PathPatternParser getPatternParser() {
* <p><strong>Note:</strong> This property is mutually exclusive with and
* ignored when {@link #setPatternParser(PathPatternParser)} is set.
* @see org.springframework.web.util.UrlPathHelper#setAlwaysUseFullPath(boolean)
* @deprecated as of 6.0, in favor of using {@link #setUrlPathHelper(UrlPathHelper)}
*/
@SuppressWarnings("deprecation")
@Deprecated
public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
this.urlPathHelper.setAlwaysUseFullPath(alwaysUseFullPath);
if (this.corsConfigurationSource instanceof UrlBasedCorsConfigurationSource urlConfigSource) {
Expand All @@ -193,8 +194,9 @@ public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
* <p><strong>Note:</strong> This property is mutually exclusive with and
* ignored when {@link #setPatternParser(PathPatternParser)} is set.
* @see org.springframework.web.util.UrlPathHelper#setUrlDecode(boolean)
* @deprecated as of 6.0, in favor of using {@link #setUrlPathHelper(UrlPathHelper)}
*/
@SuppressWarnings("deprecation")
@Deprecated
public void setUrlDecode(boolean urlDecode) {
this.urlPathHelper.setUrlDecode(urlDecode);
if (this.corsConfigurationSource instanceof UrlBasedCorsConfigurationSource urlConfigSource) {
Expand All @@ -207,8 +209,9 @@ public void setUrlDecode(boolean urlDecode) {
* <p><strong>Note:</strong> This property is mutually exclusive with and
* ignored when {@link #setPatternParser(PathPatternParser)} is set.
* @see org.springframework.web.util.UrlPathHelper#setRemoveSemicolonContent(boolean)
* @deprecated as of 6.0, in favor of using {@link #setUrlPathHelper(UrlPathHelper)}
*/
@SuppressWarnings("deprecation")
@Deprecated
public void setRemoveSemicolonContent(boolean removeSemicolonContent) {
this.urlPathHelper.setRemoveSemicolonContent(removeSemicolonContent);
if (this.corsConfigurationSource instanceof UrlBasedCorsConfigurationSource urlConfigSource) {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,11 +37,9 @@
* a single handler.
*
* <p>Supports direct matches (given "/test" -&gt; registered "/test") and "*"
* matches (given "/test" -&gt; registered "/t*"). Note that the default is
* to map within the current servlet mapping if applicable; see the
* {@link #setAlwaysUseFullPath "alwaysUseFullPath"} property for details.
* For details on the pattern options, see the
* {@link org.springframework.util.AntPathMatcher} javadoc.
* matches (given "/test" -&gt; registered "/t*"). For details on the pattern
* options, see the {@link org.springframework.web.util.pattern.PathPattern}
* javadoc.
*
* @author Rod Johnson
* @author Juergen Hoeller
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -44,10 +44,9 @@
* with a slash, one is prepended.
*
* <p>Supports direct matches (given "/test" -&gt; registered "/test") and "*"
* pattern matches (given "/test" -&gt; registered "/t*"). Note that the default
* is to map within the current servlet mapping if applicable; see the
* {@link #setAlwaysUseFullPath "alwaysUseFullPath"} property. For details on the
* pattern options, see the {@link org.springframework.util.AntPathMatcher} javadoc.
* matches (given "/test" -&gt; registered "/t*"). For details on the pattern
* options, see the {@link org.springframework.web.util.pattern.PathPattern}
* javadoc.
* @author Rod Johnson
* @author Juergen Hoeller
Expand Down
Expand Up @@ -27,6 +27,7 @@
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.testfixture.servlet.MockServletContext;
import org.springframework.web.util.UrlPathHelper;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
Expand Down Expand Up @@ -118,9 +119,13 @@ private void doTestRequestsWithSubPaths(HandlerMapping hm) throws Exception {

@Test
public void requestsWithFullPaths() throws Exception {

UrlPathHelper pathHelper = new UrlPathHelper();
pathHelper.setAlwaysUseFullPath(true);

BeanNameUrlHandlerMapping hm = new BeanNameUrlHandlerMapping();
hm.setPatternParser(null); // the test targets AntPathPatcher-specific feature
hm.setAlwaysUseFullPath(true);
hm.setUrlPathHelper(pathHelper);
hm.setApplicationContext(wac);
Object bean = wac.getBean("godCtrl");

Expand Down
Expand Up @@ -31,6 +31,7 @@
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.testfixture.servlet.MockServletContext;
import org.springframework.web.util.UrlPathHelper;
import org.springframework.web.util.WebUtils;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -70,8 +71,10 @@ public void handlerBeanNotFound() {
@Test
public void testNewlineInRequest() throws Exception {
Object controller = new Object();
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setUrlDecode(false);
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping(Collections.singletonMap("/*/baz", controller));
mapping.setUrlDecode(false);
mapping.setUrlPathHelper(urlPathHelper);
mapping.setApplicationContext(new StaticApplicationContext());

MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo%0a%0dbar/baz");
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-202 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -57,7 +57,6 @@
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.util.ServletRequestPathUtils;
import org.springframework.web.util.UrlPathHelper;
import org.springframework.web.util.pattern.PathPatternParser;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
Expand All @@ -75,10 +74,12 @@ static Stream<?> pathPatternsArguments() {
TestController controller = new TestController();

TestRequestMappingInfoHandlerMapping mapping1 = new TestRequestMappingInfoHandlerMapping();
mapping1.setPatternParser(new PathPatternParser());

UrlPathHelper pathHelper = new UrlPathHelper();
pathHelper.setRemoveSemicolonContent(false);

TestRequestMappingInfoHandlerMapping mapping2 = new TestRequestMappingInfoHandlerMapping();
mapping2.setRemoveSemicolonContent(false);
mapping2.setUrlPathHelper(pathHelper);

return Stream.of(mapping1, mapping2).peek(mapping -> {
mapping.setApplicationContext(new StaticWebApplicationContext());
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -51,7 +51,6 @@
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.util.ServletRequestPathUtils;
import org.springframework.web.util.pattern.PathPatternParser;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
Expand All @@ -78,10 +77,10 @@ static Stream<TestRequestMappingInfoHandlerMapping> pathPatternsArguments() {
wac.refresh();

TestRequestMappingInfoHandlerMapping mapping1 = new TestRequestMappingInfoHandlerMapping();
mapping1.setPatternParser(new PathPatternParser());
wac.getAutowireCapableBeanFactory().initializeBean(mapping1, "mapping1");

TestRequestMappingInfoHandlerMapping mapping2 = new TestRequestMappingInfoHandlerMapping();
mapping2.setPatternParser(null);
wac.getAutowireCapableBeanFactory().initializeBean(mapping2, "mapping2");
wac.close();

Expand Down
Expand Up @@ -69,11 +69,12 @@ public class RequestMappingHandlerMappingTests {
static Stream<Arguments> pathPatternsArguments() {
RequestMappingHandlerMapping mapping1 = new RequestMappingHandlerMapping();
StaticWebApplicationContext wac1 = new StaticWebApplicationContext();
mapping1.setPatternParser(new PathPatternParser());
mapping1.setApplicationContext(wac1);

RequestMappingHandlerMapping mapping2 = new RequestMappingHandlerMapping();
StaticWebApplicationContext wac2 = new StaticWebApplicationContext();

RequestMappingHandlerMapping mapping2 = new RequestMappingHandlerMapping();
mapping2.setPatternParser(null);
mapping2.setApplicationContext(wac2);

return Stream.of(Arguments.of(mapping1, wac1), Arguments.of(mapping2, wac2));
Expand Down
Expand Up @@ -4,15 +4,11 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="urlMapping1" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="patternParser" >
<bean class="org.springframework.web.util.pattern.PathPatternParser"/>
</property>
<property name="mappings"><ref bean="mappings"/></property>
</bean>

<bean id="urlMapping2" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="patternParser"><null/></property>
<property name="urlDecode"><value>true</value></property>
<property name="mappings"><ref bean="mappings"/></property>
</bean>

Expand Down

0 comments on commit 14fd260

Please sign in to comment.