Skip to content

Commit efb5ba5

Browse files
committed
Refactor VersionStrategy in WebFlux
Collapse the base interface VersionPathStrategy into its extension VersionStrategy and then turn the prefix nad fliename based implementations into abstract base classes (vs delegate strategies). It is simpler to have one VersionStrategy hierarchy vs that plus a separate VersionPathStrategy as a delegate. In practice each VersionStrategy is suited to be prefix or filename based. Also none of our code cares about the distinction between those two interfaces.
1 parent 704d4c3 commit efb5ba5

File tree

7 files changed

+173
-217
lines changed

7 files changed

+173
-217
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.reactive.resource;
18+
19+
import java.util.regex.Matcher;
20+
import java.util.regex.Pattern;
21+
22+
import org.apache.commons.logging.Log;
23+
import org.apache.commons.logging.LogFactory;
24+
25+
import org.springframework.util.StringUtils;
26+
27+
/**
28+
* Abstract base class for filename suffix based {@link VersionStrategy}
29+
* implementations, e.g. "static/myresource-version.js"
30+
*
31+
* @author Rossen Stoyanchev
32+
* @author Brian Clozel
33+
* @since 5.0
34+
*/
35+
public abstract class AbstractFileNameVersionStrategy implements VersionStrategy {
36+
37+
protected final Log logger = LogFactory.getLog(getClass());
38+
39+
private static final Pattern pattern = Pattern.compile("-(\\S*)\\.");
40+
41+
42+
@Override
43+
public String extractVersion(String requestPath) {
44+
Matcher matcher = pattern.matcher(requestPath);
45+
if (matcher.find()) {
46+
String match = matcher.group(1);
47+
return (match.contains("-") ? match.substring(match.lastIndexOf('-') + 1) : match);
48+
}
49+
else {
50+
return null;
51+
}
52+
}
53+
54+
@Override
55+
public String removeVersion(String requestPath, String version) {
56+
return StringUtils.delete(requestPath, "-" + version);
57+
}
58+
59+
@Override
60+
public String addVersion(String requestPath, String version) {
61+
String baseFilename = StringUtils.stripFilenameExtension(requestPath);
62+
String extension = StringUtils.getFilenameExtension(requestPath);
63+
return (baseFilename + '-' + version + '.' + extension);
64+
}
65+
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.reactive.resource;
18+
19+
import org.apache.commons.logging.Log;
20+
import org.apache.commons.logging.LogFactory;
21+
22+
import org.springframework.util.Assert;
23+
24+
/**
25+
* Abstract base class for {@link VersionStrategy} implementations that insert
26+
* a prefix into the URL path, e.g. "version/static/myresource.js".
27+
*
28+
* @author Rossen Stoyanchev
29+
* @author Brian Clozel
30+
* @since 5.0
31+
*/
32+
public abstract class AbstractPrefixVersionStrategy implements VersionStrategy {
33+
34+
protected final Log logger = LogFactory.getLog(getClass());
35+
36+
37+
private final String prefix;
38+
39+
40+
protected AbstractPrefixVersionStrategy(String version) {
41+
Assert.hasText(version, "'version' must not be empty");
42+
this.prefix = version;
43+
}
44+
45+
46+
public String getPrefix() {
47+
return this.prefix;
48+
}
49+
50+
51+
@Override
52+
public String extractVersion(String requestPath) {
53+
return requestPath.startsWith(this.prefix) ? this.prefix : null;
54+
}
55+
56+
@Override
57+
public String removeVersion(String requestPath, String version) {
58+
return requestPath.substring(this.prefix.length());
59+
}
60+
61+
@Override
62+
public String addVersion(String path, String version) {
63+
if (path.startsWith(".")) {
64+
return path;
65+
}
66+
else if (this.prefix.endsWith("/") || path.startsWith("/")) {
67+
return this.prefix + path;
68+
}
69+
else {
70+
return this.prefix + '/' + path;
71+
}
72+
}
73+
74+
}

spring-webflux/src/main/java/org/springframework/web/reactive/resource/AbstractVersionStrategy.java

Lines changed: 0 additions & 147 deletions
This file was deleted.

spring-webflux/src/main/java/org/springframework/web/reactive/resource/ContentVersionStrategy.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,7 @@
3131
* @since 5.0
3232
* @see VersionResourceResolver
3333
*/
34-
public class ContentVersionStrategy extends AbstractVersionStrategy {
35-
36-
37-
public ContentVersionStrategy() {
38-
super(new FileNameVersionPathStrategy());
39-
}
34+
public class ContentVersionStrategy extends AbstractFileNameVersionStrategy {
4035

4136

4237
@Override

spring-webflux/src/main/java/org/springframework/web/reactive/resource/FixedVersionStrategy.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,21 @@
3131
* @since 5.0
3232
* @see VersionResourceResolver
3333
*/
34-
public class FixedVersionStrategy extends AbstractVersionStrategy {
35-
36-
private final String version;
34+
public class FixedVersionStrategy extends AbstractPrefixVersionStrategy {
3735

3836

3937
/**
4038
* Create a new FixedVersionStrategy with the given version string.
4139
* @param version the fixed version string to use
4240
*/
4341
public FixedVersionStrategy(String version) {
44-
super(new PrefixVersionPathStrategy(version));
45-
this.version = version;
42+
super(version);
4643
}
4744

4845

4946
@Override
5047
public String getResourceVersion(Resource resource) {
51-
return this.version;
48+
return getPrefix();
5249
}
5350

5451
}

spring-webflux/src/main/java/org/springframework/web/reactive/resource/VersionPathStrategy.java

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)