Skip to content

Commit 84fe46c

Browse files
committed
Register prefixed path patterns with FixedVersionStrategy
Prior to this change, configuring a `FixedVersionStrategy` like so would configure a single "/js/**" path pattern: ``` versionResourceResolver.addFixedVersionStrategy("v1.0.0","/js/**"); ``` This commit makes sure that for each path pattern, its prefixed version is added to the map. For example, the previous configuration also adds "/v1.0.0/js/**". Issue: SPR-13883
1 parent 8f1d06f commit 84fe46c

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
import java.net.URI;
2323
import java.net.URL;
2424
import java.util.ArrayList;
25+
import java.util.Arrays;
2526
import java.util.Collections;
2627
import java.util.Comparator;
2728
import java.util.LinkedHashMap;
2829
import java.util.List;
2930
import java.util.Map;
31+
3032
import javax.servlet.http.HttpServletRequest;
3133

3234
import org.springframework.core.io.AbstractResource;
@@ -109,14 +111,26 @@ public VersionResourceResolver addContentVersionStrategy(String... pathPatterns)
109111
* fetched from a git commit sha, a property file, or environment variable
110112
* and set with SpEL expressions in the configuration (e.g. see {@code @Value}
111113
* in Java config).
114+
* <p>If not done already, variants of the given {@code pathPatterns}, prefixed with
115+
* the {@code version} will be also configured. For example, adding a {@code "/js/**"} path pattern
116+
* will also cofigure automatically a {@code "/v1.0.0/js/**"} with {@code "v1.0.0"} the
117+
* {@code version} String given as an argument.
112118
* @param version a version string
113119
* @param pathPatterns one or more resource URL path patterns
114120
* @return the current instance for chained method invocation
115121
* @see FixedVersionStrategy
116122
*/
117123
public VersionResourceResolver addFixedVersionStrategy(String version, String... pathPatterns) {
118-
addVersionStrategy(new FixedVersionStrategy(version), pathPatterns);
119-
return this;
124+
List<String> patternsList = Arrays.asList(pathPatterns);
125+
List<String> prefixedPatterns = new ArrayList<String>(pathPatterns.length);
126+
String versionPrefix = "/" + version;
127+
for(String pattern : patternsList) {
128+
prefixedPatterns.add(pattern);
129+
if(!pattern.startsWith(versionPrefix) && !patternsList.contains(versionPrefix + pattern)) {
130+
prefixedPatterns.add(versionPrefix + pattern);
131+
}
132+
}
133+
return addVersionStrategy(new FixedVersionStrategy(version), prefixedPatterns.toArray(new String[0]));
120134
}
121135

122136
/**

spring-webmvc/src/test/java/org/springframework/web/servlet/resource/VersionResourceResolverTests.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121
import java.util.List;
2222
import java.util.Map;
2323

24+
import org.hamcrest.Matchers;
2425
import org.junit.Before;
2526
import org.junit.Test;
2627

2728
import org.springframework.core.io.ClassPathResource;
2829
import org.springframework.core.io.Resource;
2930
import org.springframework.mock.web.test.MockHttpServletRequest;
3031

31-
import static org.hamcrest.Matchers.instanceOf;
32+
import static org.hamcrest.Matchers.*;
3233
import static org.junit.Assert.*;
3334
import static org.mockito.BDDMockito.*;
3435

@@ -168,4 +169,18 @@ public void getStrategyForPath() throws Exception {
168169
assertEquals(jsStrategy, this.resolver.getStrategyForPath("bar/foo.js"));
169170
}
170171

172+
// SPR-13883
173+
@Test
174+
public void shouldConfigureFixedPrefixAutomatically() throws Exception {
175+
176+
this.resolver.addFixedVersionStrategy("fixedversion", "/js/**", "/css/**", "/fixedversion/css/**");
177+
178+
assertThat(this.resolver.getStrategyMap().size(), is(4));
179+
assertThat(this.resolver.getStrategyForPath("/js/something.js"), Matchers.instanceOf(FixedVersionStrategy.class));
180+
assertThat(this.resolver.getStrategyForPath("/fixedversion/js/something.js"), Matchers.instanceOf(FixedVersionStrategy.class));
181+
assertThat(this.resolver.getStrategyForPath("/css/something.css"), Matchers.instanceOf(FixedVersionStrategy.class));
182+
assertThat(this.resolver.getStrategyForPath("/fixedversion/css/something.css"), Matchers.instanceOf(FixedVersionStrategy.class));
183+
}
184+
185+
171186
}

0 commit comments

Comments
 (0)