Skip to content

Commit f88759c

Browse files
committed
Shared read-only instances of UrlPathHelper
UrlPathHelper is often created and used without customizations or with the same customizations. This commit introduces re-usable, instances. Effectively a backport of commit 23233c. See gh-25690
1 parent 4f0f298 commit f88759c

File tree

10 files changed

+76
-50
lines changed

10 files changed

+76
-50
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@
8383
public class MockHttpServletRequestBuilder
8484
implements ConfigurableSmartRequestBuilder<MockHttpServletRequestBuilder>, Mergeable {
8585

86-
private static final UrlPathHelper urlPathHelper = new UrlPathHelper();
87-
88-
8986
private final String method;
9087

9188
private final URI url;
@@ -781,7 +778,7 @@ private void updatePathRequestProperties(MockHttpServletRequest request, String
781778
}
782779
String extraPath = requestUri.substring(this.contextPath.length() + this.servletPath.length());
783780
this.pathInfo = (StringUtils.hasText(extraPath) ?
784-
urlPathHelper.decodeRequestString(request, extraPath) : null);
781+
UrlPathHelper.defaultInstance.decodeRequestString(request, extraPath) : null);
785782
}
786783
request.setPathInfo(this.pathInfo);
787784
}

spring-test/src/main/java/org/springframework/test/web/servlet/setup/PatternMappingFilterProxy.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,8 +45,6 @@ final class PatternMappingFilterProxy implements Filter {
4545

4646
private static final String PATH_MAPPING_PATTERN = "/*";
4747

48-
private static final UrlPathHelper urlPathHelper = new UrlPathHelper();
49-
5048
private final Filter delegate;
5149

5250
/** Patterns that require an exact match, e.g. "/test" */
@@ -96,7 +94,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
9694
throws IOException, ServletException {
9795

9896
HttpServletRequest httpRequest = (HttpServletRequest) request;
99-
String requestPath = urlPathHelper.getPathWithinApplication(httpRequest);
97+
String requestPath = UrlPathHelper.defaultInstance.getPathWithinApplication(httpRequest);
10098

10199
if (matches(requestPath)) {
102100
this.delegate.doFilter(request, response, filterChain);

spring-web/src/main/java/org/springframework/web/filter/ForwardedHeaderFilter.java

+6-20
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,11 @@ public class ForwardedHeaderFilter extends OncePerRequestFilter {
7979
}
8080

8181

82-
private final UrlPathHelper pathHelper;
83-
8482
private boolean removeOnly;
8583

8684
private boolean relativeRedirects;
8785

8886

89-
public ForwardedHeaderFilter() {
90-
this.pathHelper = new UrlPathHelper();
91-
this.pathHelper.setUrlDecode(false);
92-
this.pathHelper.setRemoveSemicolonContent(false);
93-
}
94-
95-
9687
/**
9788
* Enables mode in which any "Forwarded" or "X-Forwarded-*" headers are
9889
* removed only and the information in them ignored.
@@ -149,7 +140,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
149140
}
150141
else {
151142
HttpServletRequest wrappedRequest =
152-
new ForwardedHeaderExtractingRequest(request, this.pathHelper);
143+
new ForwardedHeaderExtractingRequest(request);
153144

154145
HttpServletResponse wrappedResponse = this.relativeRedirects ?
155146
RelativeRedirectResponseWrapper.wrapIfNecessary(response, HttpStatus.SEE_OTHER) :
@@ -230,7 +221,7 @@ private static class ForwardedHeaderExtractingRequest extends ForwardedHeaderRem
230221
private final ForwardedPrefixExtractor forwardedPrefixExtractor;
231222

232223

233-
ForwardedHeaderExtractingRequest(HttpServletRequest request, UrlPathHelper pathHelper) {
224+
ForwardedHeaderExtractingRequest(HttpServletRequest request) {
234225
super(request);
235226

236227
HttpRequest httpRequest = new ServletServerHttpRequest(request);
@@ -244,7 +235,7 @@ private static class ForwardedHeaderExtractingRequest extends ForwardedHeaderRem
244235

245236
String baseUrl = this.scheme + "://" + this.host + (port == -1 ? "" : ":" + port);
246237
Supplier<HttpServletRequest> delegateRequest = () -> (HttpServletRequest) getRequest();
247-
this.forwardedPrefixExtractor = new ForwardedPrefixExtractor(delegateRequest, pathHelper, baseUrl);
238+
this.forwardedPrefixExtractor = new ForwardedPrefixExtractor(delegateRequest, baseUrl);
248239
}
249240

250241

@@ -296,8 +287,6 @@ private static class ForwardedPrefixExtractor {
296287

297288
private final Supplier<HttpServletRequest> delegate;
298289

299-
private final UrlPathHelper pathHelper;
300-
301290
private final String baseUrl;
302291

303292
private String actualRequestUri;
@@ -316,14 +305,10 @@ private static class ForwardedPrefixExtractor {
316305
* @param delegateRequest supplier for the current
317306
* {@link HttpServletRequestWrapper#getRequest() delegate request} which
318307
* may change during a forward (e.g. Tomcat.
319-
* @param pathHelper the path helper instance
320308
* @param baseUrl the host, scheme, and port based on forwarded headers
321309
*/
322-
public ForwardedPrefixExtractor(
323-
Supplier<HttpServletRequest> delegateRequest, UrlPathHelper pathHelper, String baseUrl) {
324-
310+
public ForwardedPrefixExtractor(Supplier<HttpServletRequest> delegateRequest, String baseUrl) {
325311
this.delegate = delegateRequest;
326-
this.pathHelper = pathHelper;
327312
this.baseUrl = baseUrl;
328313
this.actualRequestUri = delegateRequest.get().getRequestURI();
329314

@@ -353,7 +338,8 @@ private static String initForwardedPrefix(HttpServletRequest request) {
353338
@Nullable
354339
private String initRequestUri() {
355340
if (this.forwardedPrefix != null) {
356-
return this.forwardedPrefix + this.pathHelper.getPathWithinApplication(this.delegate.get());
341+
return this.forwardedPrefix +
342+
UrlPathHelper.rawPathInstance.getPathWithinApplication(this.delegate.get());
357343
}
358344
return null;
359345
}

spring-web/src/main/java/org/springframework/web/util/UrlPathHelper.java

+55
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.commons.logging.LogFactory;
2929

3030
import org.springframework.lang.Nullable;
31+
import org.springframework.util.Assert;
3132
import org.springframework.util.LinkedMultiValueMap;
3233
import org.springframework.util.MultiValueMap;
3334
import org.springframework.util.StringUtils;
@@ -70,6 +71,8 @@ public class UrlPathHelper {
7071

7172
private String defaultEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING;
7273

74+
private boolean readOnly = false;
75+
7376

7477
/**
7578
* Whether URL lookups should always use the full path within the current
@@ -81,6 +84,7 @@ public class UrlPathHelper {
8184
* <p>By default this is set to "false".
8285
*/
8386
public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
87+
checkReadOnly();
8488
this.alwaysUseFullPath = alwaysUseFullPath;
8589
}
8690

@@ -103,6 +107,7 @@ public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
103107
* @see java.net.URLDecoder#decode(String, String)
104108
*/
105109
public void setUrlDecode(boolean urlDecode) {
110+
checkReadOnly();
106111
this.urlDecode = urlDecode;
107112
}
108113

@@ -119,13 +124,15 @@ public boolean isUrlDecode() {
119124
* <p>Default is "true".
120125
*/
121126
public void setRemoveSemicolonContent(boolean removeSemicolonContent) {
127+
checkReadOnly();
122128
this.removeSemicolonContent = removeSemicolonContent;
123129
}
124130

125131
/**
126132
* Whether configured to remove ";" (semicolon) content from the request URI.
127133
*/
128134
public boolean shouldRemoveSemicolonContent() {
135+
checkReadOnly();
129136
return this.removeSemicolonContent;
130137
}
131138

@@ -143,6 +150,7 @@ public boolean shouldRemoveSemicolonContent() {
143150
* @see WebUtils#DEFAULT_CHARACTER_ENCODING
144151
*/
145152
public void setDefaultEncoding(String defaultEncoding) {
153+
checkReadOnly();
146154
this.defaultEncoding = defaultEncoding;
147155
}
148156

@@ -153,6 +161,17 @@ protected String getDefaultEncoding() {
153161
return this.defaultEncoding;
154162
}
155163

164+
/**
165+
* Switch to read-only mode where further configuration changes are not allowed.
166+
*/
167+
private void setReadOnly() {
168+
this.readOnly = true;
169+
}
170+
171+
private void checkReadOnly() {
172+
Assert.isTrue(!this.readOnly, "This instance cannot be modified");
173+
}
174+
156175

157176
/**
158177
* Return the mapping lookup path for the given request, within the current
@@ -640,4 +659,40 @@ private boolean shouldRemoveTrailingServletPathSlash(HttpServletRequest request)
640659
return !flagToUse;
641660
}
642661

662+
663+
664+
/**
665+
* Shared, read-only instance with defaults. The following apply:
666+
* <ul>
667+
* <li>{@code alwaysUseFullPath=false}
668+
* <li>{@code urlDecode=true}
669+
* <li>{@code removeSemicolon=true}
670+
* <li>{@code defaultEncoding=}{@link WebUtils#DEFAULT_CHARACTER_ENCODING}
671+
* </ul>
672+
*/
673+
public static final UrlPathHelper defaultInstance = new UrlPathHelper();
674+
675+
static {
676+
defaultInstance.setReadOnly();
677+
}
678+
679+
680+
/**
681+
* Shared, read-only instance for the full, encoded path. The following apply:
682+
* <ul>
683+
* <li>{@code alwaysUseFullPath=true}
684+
* <li>{@code urlDecode=false}
685+
* <li>{@code removeSemicolon=false}
686+
* <li>{@code defaultEncoding=}{@link WebUtils#DEFAULT_CHARACTER_ENCODING}
687+
* </ul>
688+
*/
689+
public static final UrlPathHelper rawPathInstance = new UrlPathHelper();
690+
691+
static {
692+
rawPathInstance.setAlwaysUseFullPath(true);
693+
rawPathInstance.setUrlDecode(false);
694+
rawPathInstance.setRemoveSemicolonContent(false);
695+
rawPathInstance.setReadOnly();
696+
}
697+
643698
}

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/PatternsRequestCondition.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public PatternsRequestCondition(String[] patterns, @Nullable UrlPathHelper urlPa
121121
boolean useTrailingSlashMatch, @Nullable List<String> fileExtensions) {
122122

123123
this.patterns = initPatterns(patterns);
124-
this.pathHelper = urlPathHelper != null ? urlPathHelper : new UrlPathHelper();
124+
this.pathHelper = urlPathHelper != null ? urlPathHelper : UrlPathHelper.defaultInstance;
125125
this.pathMatcher = pathMatcher != null ? pathMatcher : new AntPathMatcher();
126126
this.useSuffixPatternMatch = useSuffixPatternMatch;
127127
this.useTrailingSlashMatch = useTrailingSlashMatch;

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodProcessor.java

+3-13
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,6 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe
9191
new ParameterizedTypeReference<List<ResourceRegion>>() { }.getType();
9292

9393

94-
private static final UrlPathHelper decodingUrlPathHelper = new UrlPathHelper();
95-
96-
private static final UrlPathHelper rawUrlPathHelper = new UrlPathHelper();
97-
98-
static {
99-
rawUrlPathHelper.setRemoveSemicolonContent(false);
100-
rawUrlPathHelper.setUrlDecode(false);
101-
}
102-
103-
10494
private final ContentNegotiationManager contentNegotiationManager;
10595

10696
private final Set<String> safeExtensions = new HashSet<>();
@@ -430,7 +420,7 @@ private void addContentDispositionHeader(ServletServerHttpRequest request, Servl
430420
}
431421

432422
HttpServletRequest servletRequest = request.getServletRequest();
433-
String requestUri = rawUrlPathHelper.getOriginatingRequestUri(servletRequest);
423+
String requestUri = UrlPathHelper.rawPathInstance.getOriginatingRequestUri(servletRequest);
434424

435425
int index = requestUri.lastIndexOf('/') + 1;
436426
String filename = requestUri.substring(index);
@@ -442,10 +432,10 @@ private void addContentDispositionHeader(ServletServerHttpRequest request, Servl
442432
filename = filename.substring(0, index);
443433
}
444434

445-
filename = decodingUrlPathHelper.decodeRequestString(servletRequest, filename);
435+
filename = UrlPathHelper.defaultInstance.decodeRequestString(servletRequest, filename);
446436
String ext = StringUtils.getFilenameExtension(filename);
447437

448-
pathParams = decodingUrlPathHelper.decodeRequestString(servletRequest, pathParams);
438+
pathParams = UrlPathHelper.defaultInstance.decodeRequestString(servletRequest, pathParams);
449439
String extInPathParams = StringUtils.getFilenameExtension(pathParams);
450440

451441
if (!safeExtension(servletRequest, ext) || !safeExtension(servletRequest, extInPathParams)) {

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletCookieValueMethodArgumentResolver.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,7 +37,7 @@
3737
*/
3838
public class ServletCookieValueMethodArgumentResolver extends AbstractCookieValueMethodArgumentResolver {
3939

40-
private UrlPathHelper urlPathHelper = new UrlPathHelper();
40+
private UrlPathHelper urlPathHelper = UrlPathHelper.defaultInstance;
4141

4242

4343
public ServletCookieValueMethodArgumentResolver(@Nullable ConfigurableBeanFactory beanFactory) {

spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlProvider.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,7 +54,7 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
5454

5555
protected final Log logger = LogFactory.getLog(getClass());
5656

57-
private UrlPathHelper urlPathHelper = new UrlPathHelper();
57+
private UrlPathHelper urlPathHelper = UrlPathHelper.defaultInstance;
5858

5959
private PathMatcher pathMatcher = new AntPathMatcher();
6060

spring-webmvc/src/main/java/org/springframework/web/servlet/support/AbstractFlashMapManager.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,7 +54,7 @@ public abstract class AbstractFlashMapManager implements FlashMapManager {
5454

5555
private int flashMapTimeout = 180;
5656

57-
private UrlPathHelper urlPathHelper = new UrlPathHelper();
57+
private UrlPathHelper urlPathHelper = UrlPathHelper.defaultInstance;
5858

5959

6060
/**

spring-webmvc/src/main/java/org/springframework/web/servlet/support/ServletUriComponentsBuilder.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -92,7 +92,7 @@ public static ServletUriComponentsBuilder fromContextPath(HttpServletRequest req
9292
*/
9393
public static ServletUriComponentsBuilder fromServletMapping(HttpServletRequest request) {
9494
ServletUriComponentsBuilder builder = fromContextPath(request);
95-
if (StringUtils.hasText(new UrlPathHelper().getPathWithinServletMapping(request))) {
95+
if (StringUtils.hasText(UrlPathHelper.defaultInstance.getPathWithinServletMapping(request))) {
9696
builder.path(request.getServletPath());
9797
}
9898
return builder;

0 commit comments

Comments
 (0)