Skip to content

Commit 23233c3

Browse files
committed
Shared read-only instance of UrlPathHelper
In many places UrlPathHelper is created and used without any customizations, in some cases repeatedly. This commit adds a shared read-only UrlPathHelper instance with default settings. See gh-25100
1 parent 92f7587 commit 23233c3

File tree

11 files changed

+83
-66
lines changed

11 files changed

+83
-66
lines changed

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

Lines changed: 1 addition & 4 deletions
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

Lines changed: 2 additions & 4 deletions
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/util/UrlPathHelper.java

Lines changed: 33 additions & 1 deletion
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.
@@ -626,4 +626,36 @@ private boolean shouldRemoveTrailingServletPathSlash(HttpServletRequest request)
626626
return !flagToUse;
627627
}
628628

629+
630+
/**
631+
* Shared, read-only instance of {@code UrlPathHelper}. Uses default settings:
632+
* <ul>
633+
* <li>{@code alwaysUseFullPath=false}
634+
* <li>{@code urlDecode=true}
635+
* <li>{@code removeSemicolon=true}
636+
* <li>{@code defaultEncoding=}{@link WebUtils#DEFAULT_CHARACTER_ENCODING}
637+
* </ul>
638+
*/
639+
public final static UrlPathHelper defaultInstance = new UrlPathHelper() {
640+
641+
@Override
642+
public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
643+
throw new UnsupportedOperationException();
644+
}
645+
646+
@Override
647+
public void setUrlDecode(boolean urlDecode) {
648+
throw new UnsupportedOperationException();
649+
}
650+
651+
@Override
652+
public void setRemoveSemicolonContent(boolean removeSemicolonContent) {
653+
throw new UnsupportedOperationException();
654+
}
655+
656+
@Override
657+
public void setDefaultEncoding(String defaultEncoding) {
658+
throw new UnsupportedOperationException();
659+
}
660+
};
629661
}

spring-web/src/test/java/org/springframework/web/util/UrlPathHelperTests.java

Lines changed: 38 additions & 47 deletions
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.
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.web.util;
1818

19-
import java.io.UnsupportedEncodingException;
20-
2119
import org.junit.jupiter.api.Disabled;
2220
import org.junit.jupiter.api.Test;
2321

@@ -46,7 +44,7 @@ public void getPathWithinApplication() {
4644
request.setContextPath("/petclinic");
4745
request.setRequestURI("/petclinic/welcome.html");
4846

49-
assertThat(helper.getPathWithinApplication(request)).as("Incorrect path returned").isEqualTo("/welcome.html");
47+
assertThat(helper.getPathWithinApplication(request)).isEqualTo("/welcome.html");
5048
}
5149

5250
@Test
@@ -62,7 +60,7 @@ public void getPathWithinApplicationForSlashContextPath() {
6260
request.setContextPath("/");
6361
request.setRequestURI("/welcome.html");
6462

65-
assertThat(helper.getPathWithinApplication(request)).as("Incorrect path returned").isEqualTo("/welcome.html");
63+
assertThat(helper.getPathWithinApplication(request)).isEqualTo("/welcome.html");
6664
}
6765

6866
@Test
@@ -71,7 +69,7 @@ public void getPathWithinServlet() {
7169
request.setServletPath("/main");
7270
request.setRequestURI("/petclinic/main/welcome.html");
7371

74-
assertThat(helper.getPathWithinServletMapping(request)).as("Incorrect path returned").isEqualTo("/welcome.html");
72+
assertThat(helper.getPathWithinServletMapping(request)).isEqualTo("/welcome.html");
7573
}
7674

7775
@Test
@@ -81,12 +79,10 @@ public void alwaysUseFullPath() {
8179
request.setServletPath("/main");
8280
request.setRequestURI("/petclinic/main/welcome.html");
8381

84-
assertThat(helper.getLookupPathForRequest(request)).as("Incorrect path returned").isEqualTo("/main/welcome.html");
82+
assertThat(helper.getLookupPathForRequest(request)).isEqualTo("/main/welcome.html");
8583
}
8684

87-
// SPR-11101
88-
89-
@Test
85+
@Test // SPR-11101
9086
public void getPathWithinServletWithoutUrlDecoding() {
9187
request.setContextPath("/SPR-11101");
9288
request.setServletPath("/test_url_decoding/a/b");
@@ -101,32 +97,29 @@ public void getPathWithinServletWithoutUrlDecoding() {
10197
@Test
10298
public void getRequestUri() {
10399
request.setRequestURI("/welcome.html");
104-
assertThat(helper.getRequestUri(request)).as("Incorrect path returned").isEqualTo("/welcome.html");
100+
assertThat(helper.getRequestUri(request)).isEqualTo("/welcome.html");
105101

106102
request.setRequestURI("/foo%20bar");
107-
assertThat(helper.getRequestUri(request)).as("Incorrect path returned").isEqualTo("/foo bar");
103+
assertThat(helper.getRequestUri(request)).isEqualTo("/foo bar");
108104

109105
request.setRequestURI("/foo+bar");
110-
assertThat(helper.getRequestUri(request)).as("Incorrect path returned").isEqualTo("/foo+bar");
106+
assertThat(helper.getRequestUri(request)).isEqualTo("/foo+bar");
111107
}
112108

113109
@Test
114-
public void getRequestRemoveSemicolonContent() throws UnsupportedEncodingException {
110+
public void getRequestRemoveSemicolonContent() {
115111
helper.setRemoveSemicolonContent(true);
116-
117112
request.setRequestURI("/foo;f=F;o=O;o=O/bar;b=B;a=A;r=R");
118113
assertThat(helper.getRequestUri(request)).isEqualTo("/foo/bar");
119114

120115
// SPR-13455
121-
122-
request.setServletPath("/foo/1");
123116
request.setRequestURI("/foo/;test/1");
124-
117+
request.setServletPath("/foo/1");
125118
assertThat(helper.getRequestUri(request)).isEqualTo("/foo/1");
126119
}
127120

128121
@Test
129-
public void getRequestKeepSemicolonContent() throws UnsupportedEncodingException {
122+
public void getRequestKeepSemicolonContent() {
130123
helper.setRemoveSemicolonContent(false);
131124

132125
request.setRequestURI("/foo;a=b;c=d");
@@ -139,7 +132,6 @@ public void getRequestKeepSemicolonContent() throws UnsupportedEncodingException
139132
assertThat(helper.getRequestUri(request)).as("jsessionid should always be removed").isEqualTo("/foo;a=b;c=d");
140133

141134
// SPR-10398
142-
143135
request.setRequestURI("/foo;a=b;JSESSIONID=c0o7fszeb1;c=d");
144136
assertThat(helper.getRequestUri(request)).as("JSESSIONID should always be removed").isEqualTo("/foo;a=b;c=d");
145137
}
@@ -178,37 +170,36 @@ public void getLookupPathWithSemicolonContentAndNullPathInfo() {
178170
//
179171

180172
@Test
181-
public void tomcatDefaultServletRoot() throws Exception {
173+
public void tomcatDefaultServletRoot() {
182174
request.setContextPath("/test");
183-
request.setPathInfo(null);
184175
request.setServletPath("/");
176+
request.setPathInfo(null);
185177
request.setRequestURI("/test/");
186178
assertThat(helper.getLookupPathForRequest(request)).isEqualTo("/");
187179
}
188180

189181
@Test
190-
public void tomcatDefaultServletFile() throws Exception {
182+
public void tomcatDefaultServletFile() {
191183
request.setContextPath("/test");
192-
request.setPathInfo(null);
193184
request.setServletPath("/foo");
185+
request.setPathInfo(null);
194186
request.setRequestURI("/test/foo");
195187

196188
assertThat(helper.getLookupPathForRequest(request)).isEqualTo("/foo");
197189
}
198190

199191
@Test
200-
public void tomcatDefaultServletFolder() throws Exception {
192+
public void tomcatDefaultServletFolder() {
201193
request.setContextPath("/test");
202-
request.setPathInfo(null);
203194
request.setServletPath("/foo/");
195+
request.setPathInfo(null);
204196
request.setRequestURI("/test/foo/");
205197

206198
assertThat(helper.getLookupPathForRequest(request)).isEqualTo("/foo/");
207199
}
208200

209-
//SPR-12372 & SPR-13455
210-
@Test
211-
public void removeDuplicateSlashesInPath() throws Exception {
201+
@Test //SPR-12372, SPR-13455
202+
public void removeDuplicateSlashesInPath() {
212203
request.setContextPath("/SPR-12372");
213204
request.setPathInfo(null);
214205
request.setServletPath("/foo/bar/");
@@ -229,7 +220,7 @@ public void removeDuplicateSlashesInPath() throws Exception {
229220
}
230221

231222
@Test
232-
public void wasDefaultServletRoot() throws Exception {
223+
public void wasDefaultServletRoot() {
233224
request.setContextPath("/test");
234225
request.setPathInfo("/");
235226
request.setServletPath("");
@@ -240,13 +231,13 @@ public void wasDefaultServletRoot() throws Exception {
240231
}
241232

242233
@Test
243-
public void wasDefaultServletRootWithCompliantSetting() throws Exception {
234+
public void wasDefaultServletRootWithCompliantSetting() {
244235
request.setAttribute(WEBSPHERE_URI_ATTRIBUTE, "/test/");
245236
tomcatDefaultServletRoot();
246237
}
247238

248239
@Test
249-
public void wasDefaultServletFile() throws Exception {
240+
public void wasDefaultServletFile() {
250241
request.setContextPath("/test");
251242
request.setPathInfo("/foo");
252243
request.setServletPath("");
@@ -257,13 +248,13 @@ public void wasDefaultServletFile() throws Exception {
257248
}
258249

259250
@Test
260-
public void wasDefaultServletFileWithCompliantSetting() throws Exception {
251+
public void wasDefaultServletFileWithCompliantSetting() {
261252
request.setAttribute(WEBSPHERE_URI_ATTRIBUTE, "/test/foo");
262253
tomcatDefaultServletFile();
263254
}
264255

265256
@Test
266-
public void wasDefaultServletFolder() throws Exception {
257+
public void wasDefaultServletFolder() {
267258
request.setContextPath("/test");
268259
request.setPathInfo("/foo/");
269260
request.setServletPath("");
@@ -274,7 +265,7 @@ public void wasDefaultServletFolder() throws Exception {
274265
}
275266

276267
@Test
277-
public void wasDefaultServletFolderWithCompliantSetting() throws Exception {
268+
public void wasDefaultServletFolderWithCompliantSetting() {
278269
UrlPathHelper.websphereComplianceFlag = true;
279270
try {
280271
request.setAttribute(WEBSPHERE_URI_ATTRIBUTE, "/test/foo/");
@@ -291,7 +282,7 @@ public void wasDefaultServletFolderWithCompliantSetting() throws Exception {
291282
//
292283

293284
@Test
294-
public void tomcatCasualServletRoot() throws Exception {
285+
public void tomcatCasualServletRoot() {
295286
request.setContextPath("/test");
296287
request.setPathInfo("/");
297288
request.setServletPath("/foo");
@@ -303,7 +294,7 @@ public void tomcatCasualServletRoot() throws Exception {
303294
@Disabled
304295
// test the root mapping for /foo/* w/o a trailing slash - <host>/<context>/foo
305296
@Test
306-
public void tomcatCasualServletRootWithMissingSlash() throws Exception {
297+
public void tomcatCasualServletRootWithMissingSlash() {
307298
request.setContextPath("/test");
308299
request.setPathInfo(null);
309300
request.setServletPath("/foo");
@@ -313,7 +304,7 @@ public void tomcatCasualServletRootWithMissingSlash() throws Exception {
313304
}
314305

315306
@Test
316-
public void tomcatCasualServletFile() throws Exception {
307+
public void tomcatCasualServletFile() {
317308
request.setContextPath("/test");
318309
request.setPathInfo("/foo");
319310
request.setServletPath("/foo");
@@ -323,7 +314,7 @@ public void tomcatCasualServletFile() throws Exception {
323314
}
324315

325316
@Test
326-
public void tomcatCasualServletFolder() throws Exception {
317+
public void tomcatCasualServletFolder() {
327318
request.setContextPath("/test");
328319
request.setPathInfo("/foo/");
329320
request.setServletPath("/foo");
@@ -333,7 +324,7 @@ public void tomcatCasualServletFolder() throws Exception {
333324
}
334325

335326
@Test
336-
public void wasCasualServletRoot() throws Exception {
327+
public void wasCasualServletRoot() {
337328
request.setContextPath("/test");
338329
request.setPathInfo(null);
339330
request.setServletPath("/foo/");
@@ -344,15 +335,15 @@ public void wasCasualServletRoot() throws Exception {
344335
}
345336

346337
@Test
347-
public void wasCasualServletRootWithCompliantSetting() throws Exception {
338+
public void wasCasualServletRootWithCompliantSetting() {
348339
request.setAttribute(WEBSPHERE_URI_ATTRIBUTE, "/test/foo/");
349340
tomcatCasualServletRoot();
350341
}
351342

352343
@Disabled
353344
// test the root mapping for /foo/* w/o a trailing slash - <host>/<context>/foo
354345
@Test
355-
public void wasCasualServletRootWithMissingSlash() throws Exception {
346+
public void wasCasualServletRootWithMissingSlash() {
356347
request.setContextPath("/test");
357348
request.setPathInfo(null);
358349
request.setServletPath("/foo");
@@ -364,13 +355,13 @@ public void wasCasualServletRootWithMissingSlash() throws Exception {
364355

365356
@Disabled
366357
@Test
367-
public void wasCasualServletRootWithMissingSlashWithCompliantSetting() throws Exception {
358+
public void wasCasualServletRootWithMissingSlashWithCompliantSetting() {
368359
request.setAttribute(WEBSPHERE_URI_ATTRIBUTE, "/test/foo");
369360
tomcatCasualServletRootWithMissingSlash();
370361
}
371362

372363
@Test
373-
public void wasCasualServletFile() throws Exception {
364+
public void wasCasualServletFile() {
374365
request.setContextPath("/test");
375366
request.setPathInfo("/foo");
376367
request.setServletPath("/foo");
@@ -381,13 +372,13 @@ public void wasCasualServletFile() throws Exception {
381372
}
382373

383374
@Test
384-
public void wasCasualServletFileWithCompliantSetting() throws Exception {
375+
public void wasCasualServletFileWithCompliantSetting() {
385376
request.setAttribute(WEBSPHERE_URI_ATTRIBUTE, "/test/foo/foo");
386377
tomcatCasualServletFile();
387378
}
388379

389380
@Test
390-
public void wasCasualServletFolder() throws Exception {
381+
public void wasCasualServletFolder() {
391382
request.setContextPath("/test");
392383
request.setPathInfo("/foo/");
393384
request.setServletPath("/foo");
@@ -398,7 +389,7 @@ public void wasCasualServletFolder() throws Exception {
398389
}
399390

400391
@Test
401-
public void wasCasualServletFolderWithCompliantSetting() throws Exception {
392+
public void wasCasualServletFolderWithCompliantSetting() {
402393
request.setAttribute(WEBSPHERE_URI_ATTRIBUTE, "/test/foo/foo/");
403394
tomcatCasualServletFolder();
404395
}

0 commit comments

Comments
 (0)