Skip to content

Commit 26141b0

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 3900ac8 commit 26141b0

File tree

10 files changed

+73
-43
lines changed

10 files changed

+73
-43
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
@@ -82,9 +82,6 @@
8282
public class MockHttpServletRequestBuilder
8383
implements ConfigurableSmartRequestBuilder<MockHttpServletRequestBuilder>, Mergeable {
8484

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

9087
private final URI url;
@@ -696,7 +693,7 @@ private void updatePathRequestProperties(MockHttpServletRequest request, String
696693
}
697694
String extraPath = requestUri.substring(this.contextPath.length() + this.servletPath.length());
698695
this.pathInfo = (StringUtils.hasText(extraPath) ?
699-
urlPathHelper.decodeRequestString(request, extraPath) : null);
696+
UrlPathHelper.defaultInstance.decodeRequestString(request, extraPath) : null);
700697
}
701698
request.setPathInfo(this.pathInfo);
702699
}

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.
@@ -44,8 +44,6 @@ final class PatternMappingFilterProxy implements Filter {
4444

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

47-
private static final UrlPathHelper urlPathHelper = new UrlPathHelper();
48-
4947
private final Filter delegate;
5048

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

9795
HttpServletRequest httpRequest = (HttpServletRequest) request;
98-
String requestPath = urlPathHelper.getPathWithinApplication(httpRequest);
96+
String requestPath = UrlPathHelper.defaultInstance.getPathWithinApplication(httpRequest);
9997

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

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

+3-12
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,11 @@ public class ForwardedHeaderFilter extends OncePerRequestFilter {
7676
}
7777

7878

79-
private final UrlPathHelper pathHelper;
80-
8179
private boolean removeOnly;
8280

8381
private boolean relativeRedirects;
8482

8583

86-
public ForwardedHeaderFilter() {
87-
this.pathHelper = new UrlPathHelper();
88-
this.pathHelper.setUrlDecode(false);
89-
this.pathHelper.setRemoveSemicolonContent(false);
90-
}
91-
92-
9384
/**
9485
* Enables mode in which any "Forwarded" or "X-Forwarded-*" headers are
9586
* removed only and the information in them ignored.
@@ -145,7 +136,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
145136
filterChain.doFilter(theRequest, response);
146137
}
147138
else {
148-
HttpServletRequest theRequest = new ForwardedHeaderExtractingRequest(request, this.pathHelper);
139+
HttpServletRequest theRequest = new ForwardedHeaderExtractingRequest(request);
149140
HttpServletResponse theResponse = (this.relativeRedirects ?
150141
RelativeRedirectResponseWrapper.wrapIfNecessary(response, HttpStatus.SEE_OTHER) :
151142
new ForwardedHeaderExtractingResponse(response, theRequest));
@@ -221,7 +212,7 @@ private static class ForwardedHeaderExtractingRequest extends ForwardedHeaderRem
221212

222213
private final String requestUrl;
223214

224-
public ForwardedHeaderExtractingRequest(HttpServletRequest request, UrlPathHelper pathHelper) {
215+
public ForwardedHeaderExtractingRequest(HttpServletRequest request) {
225216
super(request);
226217

227218
HttpRequest httpRequest = new ServletServerHttpRequest(request);
@@ -235,7 +226,7 @@ public ForwardedHeaderExtractingRequest(HttpServletRequest request, UrlPathHelpe
235226

236227
String prefix = getForwardedPrefix(request);
237228
this.contextPath = (prefix != null ? prefix : request.getContextPath());
238-
this.requestUri = this.contextPath + pathHelper.getPathWithinApplication(request);
229+
this.requestUri = this.contextPath + UrlPathHelper.rawPathInstance.getPathWithinApplication(request);
239230
this.requestUrl = this.scheme + "://" + this.host + (port == -1 ? "" : ":" + port) + this.requestUri;
240231
}
241232

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

+54
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.commons.logging.LogFactory;
2828

2929
import org.springframework.lang.Nullable;
30+
import org.springframework.util.Assert;
3031
import org.springframework.util.LinkedMultiValueMap;
3132
import org.springframework.util.MultiValueMap;
3233
import org.springframework.util.StringUtils;
@@ -69,6 +70,8 @@ public class UrlPathHelper {
6970

7071
private String defaultEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING;
7172

73+
private boolean readOnly = false;
74+
7275

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

@@ -102,6 +106,7 @@ public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
102106
* @see java.net.URLDecoder#decode(String, String)
103107
*/
104108
public void setUrlDecode(boolean urlDecode) {
109+
checkReadOnly();
105110
this.urlDecode = urlDecode;
106111
}
107112

@@ -118,13 +123,15 @@ public boolean isUrlDecode() {
118123
* <p>Default is "true".
119124
*/
120125
public void setRemoveSemicolonContent(boolean removeSemicolonContent) {
126+
checkReadOnly();
121127
this.removeSemicolonContent = removeSemicolonContent;
122128
}
123129

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

@@ -142,6 +149,7 @@ public boolean shouldRemoveSemicolonContent() {
142149
* @see WebUtils#DEFAULT_CHARACTER_ENCODING
143150
*/
144151
public void setDefaultEncoding(String defaultEncoding) {
152+
checkReadOnly();
145153
this.defaultEncoding = defaultEncoding;
146154
}
147155

@@ -152,6 +160,17 @@ protected String getDefaultEncoding() {
152160
return this.defaultEncoding;
153161
}
154162

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

156175
/**
157176
* Return the mapping lookup path for the given request, within the current
@@ -605,4 +624,39 @@ private boolean shouldRemoveTrailingServletPathSlash(HttpServletRequest request)
605624
return !flagToUse;
606625
}
607626

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

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.
@@ -105,7 +105,7 @@ private PatternsRequestCondition(Collection<String> patterns, @Nullable UrlPathH
105105
boolean useTrailingSlashMatch, @Nullable List<String> fileExtensions) {
106106

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

8686

87-
private static final UrlPathHelper decodingUrlPathHelper = new UrlPathHelper();
88-
89-
private static final UrlPathHelper rawUrlPathHelper = new UrlPathHelper();
90-
91-
static {
92-
rawUrlPathHelper.setRemoveSemicolonContent(false);
93-
rawUrlPathHelper.setUrlDecode(false);
94-
}
95-
96-
9787
private final ContentNegotiationManager contentNegotiationManager;
9888

9989
private final PathExtensionContentNegotiationStrategy pathStrategy;
@@ -406,7 +396,7 @@ private void addContentDispositionHeader(ServletServerHttpRequest request, Servl
406396
}
407397

408398
HttpServletRequest servletRequest = request.getServletRequest();
409-
String requestUri = rawUrlPathHelper.getOriginatingRequestUri(servletRequest);
399+
String requestUri = UrlPathHelper.rawPathInstance.getOriginatingRequestUri(servletRequest);
410400

411401
int index = requestUri.lastIndexOf('/') + 1;
412402
String filename = requestUri.substring(index);
@@ -418,10 +408,10 @@ private void addContentDispositionHeader(ServletServerHttpRequest request, Servl
418408
filename = filename.substring(0, index);
419409
}
420410

421-
filename = decodingUrlPathHelper.decodeRequestString(servletRequest, filename);
411+
filename = UrlPathHelper.defaultInstance.decodeRequestString(servletRequest, filename);
422412
String ext = StringUtils.getFilenameExtension(filename);
423413

424-
pathParams = decodingUrlPathHelper.decodeRequestString(servletRequest, pathParams);
414+
pathParams = UrlPathHelper.defaultInstance.decodeRequestString(servletRequest, pathParams);
425415
String extInPathParams = StringUtils.getFilenameExtension(pathParams);
426416

427417
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.
@@ -52,7 +52,7 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
5252

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

55-
private UrlPathHelper urlPathHelper = new UrlPathHelper();
55+
private UrlPathHelper urlPathHelper = UrlPathHelper.defaultInstance;
5656

5757
private PathMatcher pathMatcher = new AntPathMatcher();
5858

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-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.
@@ -108,7 +108,7 @@ public static ServletUriComponentsBuilder fromContextPath(HttpServletRequest req
108108
*/
109109
public static ServletUriComponentsBuilder fromServletMapping(HttpServletRequest request) {
110110
ServletUriComponentsBuilder builder = fromContextPath(request);
111-
if (StringUtils.hasText(new UrlPathHelper().getPathWithinServletMapping(request))) {
111+
if (StringUtils.hasText(UrlPathHelper.defaultInstance.getPathWithinServletMapping(request))) {
112112
builder.path(request.getServletPath());
113113
}
114114
return builder;

0 commit comments

Comments
 (0)