Skip to content

Commit 2281e42

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. Closes gh-25690
1 parent 55d0756 commit 2281e42

File tree

10 files changed

+76
-51
lines changed

10 files changed

+76
-51
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;
@@ -697,7 +694,7 @@ private void updatePathRequestProperties(MockHttpServletRequest request, String
697694
}
698695
String extraPath = requestUri.substring(this.contextPath.length() + this.servletPath.length());
699696
this.pathInfo = (StringUtils.hasText(extraPath) ?
700-
urlPathHelper.decodeRequestString(request, extraPath) : null);
697+
UrlPathHelper.defaultInstance.decodeRequestString(request, extraPath) : null);
701698
}
702699
request.setPathInfo(this.pathInfo);
703700
}

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

+54
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
@@ -606,4 +625,39 @@ private boolean shouldRemoveTrailingServletPathSlash(HttpServletRequest request)
606625
return !flagToUse;
607626
}
608627

628+
629+
/**
630+
* Shared, read-only instance with defaults. The following apply:
631+
* <ul>
632+
* <li>{@code alwaysUseFullPath=false}
633+
* <li>{@code urlDecode=true}
634+
* <li>{@code removeSemicolon=true}
635+
* <li>{@code defaultEncoding=}{@link WebUtils#DEFAULT_CHARACTER_ENCODING}
636+
* </ul>
637+
*/
638+
public static final UrlPathHelper defaultInstance = new UrlPathHelper();
639+
640+
static {
641+
defaultInstance.setReadOnly();
642+
}
643+
644+
645+
/**
646+
* Shared, read-only instance for the full, encoded path. The following apply:
647+
* <ul>
648+
* <li>{@code alwaysUseFullPath=true}
649+
* <li>{@code urlDecode=false}
650+
* <li>{@code removeSemicolon=false}
651+
* <li>{@code defaultEncoding=}{@link WebUtils#DEFAULT_CHARACTER_ENCODING}
652+
* </ul>
653+
*/
654+
public static final UrlPathHelper rawPathInstance = new UrlPathHelper();
655+
656+
static {
657+
rawPathInstance.setAlwaysUseFullPath(true);
658+
rawPathInstance.setUrlDecode(false);
659+
rawPathInstance.setRemoveSemicolonContent(false);
660+
rawPathInstance.setReadOnly();
661+
}
662+
609663
}

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/PatternsRequestCondition.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.
@@ -106,7 +106,7 @@ private PatternsRequestCondition(Collection<String> patterns, @Nullable UrlPathH
106106
boolean useTrailingSlashMatch, @Nullable List<String> fileExtensions) {
107107

108108
this.patterns = Collections.unmodifiableSet(prependLeadingSlash(patterns));
109-
this.pathHelper = (urlPathHelper != null ? urlPathHelper : new UrlPathHelper());
109+
this.pathHelper = (urlPathHelper != null ? urlPathHelper : UrlPathHelper.defaultInstance);
110110
this.pathMatcher = (pathMatcher != null ? pathMatcher : new AntPathMatcher());
111111
this.useSuffixPatternMatch = useSuffixPatternMatch;
112112
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
@@ -90,16 +90,6 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe
9090
new ParameterizedTypeReference<List<ResourceRegion>>() { }.getType();
9191

9292

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

10595
private final PathExtensionContentNegotiationStrategy pathStrategy;
@@ -426,7 +416,7 @@ private void addContentDispositionHeader(ServletServerHttpRequest request, Servl
426416
}
427417

428418
HttpServletRequest servletRequest = request.getServletRequest();
429-
String requestUri = rawUrlPathHelper.getOriginatingRequestUri(servletRequest);
419+
String requestUri = UrlPathHelper.rawPathInstance.getOriginatingRequestUri(servletRequest);
430420

431421
int index = requestUri.lastIndexOf('/') + 1;
432422
String filename = requestUri.substring(index);
@@ -438,10 +428,10 @@ private void addContentDispositionHeader(ServletServerHttpRequest request, Servl
438428
filename = filename.substring(0, index);
439429
}
440430

441-
filename = decodingUrlPathHelper.decodeRequestString(servletRequest, filename);
431+
filename = UrlPathHelper.defaultInstance.decodeRequestString(servletRequest, filename);
442432
String ext = StringUtils.getFilenameExtension(filename);
443433

444-
pathParams = decodingUrlPathHelper.decodeRequestString(servletRequest, pathParams);
434+
pathParams = UrlPathHelper.defaultInstance.decodeRequestString(servletRequest, pathParams);
445435
String extInPathParams = StringUtils.getFilenameExtension(pathParams);
446436

447437
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-2018 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.
@@ -53,7 +53,7 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
5353

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

56-
private UrlPathHelper urlPathHelper = new UrlPathHelper();
56+
private UrlPathHelper urlPathHelper = UrlPathHelper.defaultInstance;
5757

5858
private PathMatcher pathMatcher = new AntPathMatcher();
5959

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.
@@ -53,7 +53,7 @@ public abstract class AbstractFlashMapManager implements FlashMapManager {
5353

5454
private int flashMapTimeout = 180;
5555

56-
private UrlPathHelper urlPathHelper = new UrlPathHelper();
56+
private UrlPathHelper urlPathHelper = UrlPathHelper.defaultInstance;
5757

5858

5959
/**

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)