|
6 | 6 | import org.apache.commons.logging.Log; |
7 | 7 | import org.apache.commons.logging.LogFactory; |
8 | 8 |
|
| 9 | +import org.springframework.util.Assert; |
9 | 10 | import org.springframework.util.StringUtils; |
10 | 11 |
|
11 | 12 | /** |
|
20 | 21 | * version string. |
21 | 22 | * |
22 | 23 | * @author Brian Clozel |
| 24 | + * @author Rossen Stoyanchev |
23 | 25 | * @since 4.1 |
24 | 26 | */ |
25 | 27 | public abstract class AbstractVersionStrategy implements VersionStrategy { |
26 | 28 |
|
27 | | - private static final Pattern pattern = Pattern.compile("-(\\S*)\\."); |
28 | | - |
29 | 29 | protected final Log logger = LogFactory.getLog(getClass()); |
30 | 30 |
|
31 | | - /** |
32 | | - * Extracts a version string from the request path, as a suffix in the resource |
33 | | - * file name. |
34 | | - * @param requestPath the request path to extract the version string from |
35 | | - * @return a version string or an empty string if none was found |
36 | | - */ |
37 | | - protected String extractVersionFromFilename(String requestPath) { |
38 | | - Matcher matcher = pattern.matcher(requestPath); |
39 | | - if (matcher.find()) { |
40 | | - String match = matcher.group(1); |
41 | | - return match.contains("-") ? match.substring(match.lastIndexOf("-") + 1) : match; |
42 | | - } |
43 | | - else { |
44 | | - return ""; |
45 | | - } |
| 31 | + private final VersionPathStrategy pathStrategy; |
| 32 | + |
| 33 | + |
| 34 | + protected AbstractVersionStrategy(VersionPathStrategy pathStrategy) { |
| 35 | + Assert.notNull(pathStrategy, "'pathStrategy' is required"); |
| 36 | + this.pathStrategy = pathStrategy; |
46 | 37 | } |
47 | 38 |
|
48 | | - /** |
49 | | - * Deletes the given candidate version string from the request path. |
50 | | - * The version string should be a suffix in the resource file name. |
51 | | - */ |
52 | | - protected String deleteVersionFromFilename(String requestPath, String candidateVersion) { |
53 | | - return StringUtils.delete(requestPath, "-" + candidateVersion); |
| 39 | + |
| 40 | + public VersionPathStrategy getVersionPathStrategy() { |
| 41 | + return this.pathStrategy; |
54 | 42 | } |
55 | 43 |
|
56 | | - /** |
57 | | - * Adds the given version string to the baseUrl, as a file name suffix. |
58 | | - */ |
59 | | - protected String addVersionToFilename(String baseUrl, String version) { |
60 | | - String baseFilename = StringUtils.stripFilenameExtension(baseUrl); |
61 | | - String extension = StringUtils.getFilenameExtension(baseUrl); |
62 | | - return baseFilename + "-" + version + "." + extension; |
| 44 | + |
| 45 | + @Override |
| 46 | + public String extractVersion(String requestPath) { |
| 47 | + return this.pathStrategy.extractVersion(requestPath); |
63 | 48 | } |
64 | 49 |
|
65 | | - /** |
66 | | - * Extracts a version string from the request path, as a prefix in the request path. |
67 | | - * @param requestPath the request path to extract the version string from |
68 | | - * @return a version string or an empty string if none was found |
69 | | - */ |
70 | | - protected String extractVersionAsPrefix(String requestPath, String prefix) { |
71 | | - if (requestPath.startsWith(prefix)) { |
72 | | - return prefix; |
73 | | - } |
74 | | - return ""; |
| 50 | + @Override |
| 51 | + public String removeVersion(String requestPath, String version) { |
| 52 | + return this.pathStrategy.removeVersion(requestPath, version); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public String addVersion(String requestPath, String version) { |
| 57 | + return this.pathStrategy.addVersion(requestPath, version); |
75 | 58 | } |
76 | 59 |
|
| 60 | + |
77 | 61 | /** |
78 | | - * Deletes the given candidate version string from the request path. |
79 | | - * The version string should be a prefix in the request path. |
| 62 | + * A prefix-based {@code VersionPathStrategy}, |
| 63 | + * e.g. {@code "{version}/path/foo.js"}. |
80 | 64 | */ |
81 | | - protected String deleteVersionAsPrefix(String requestPath, String version) { |
82 | | - return requestPath.substring(version.length()); |
| 65 | + protected static class PrefixVersionPathStrategy implements VersionPathStrategy { |
| 66 | + |
| 67 | + private final String prefix; |
| 68 | + |
| 69 | + |
| 70 | + public PrefixVersionPathStrategy(String version) { |
| 71 | + Assert.hasText(version, "'version' is required and must not be empty"); |
| 72 | + this.prefix = version; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public String extractVersion(String requestPath) { |
| 77 | + return (requestPath.startsWith(this.prefix) ? this.prefix : null); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public String removeVersion(String requestPath, String version) { |
| 82 | + return requestPath.substring(this.prefix.length()); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public String addVersion(String path, String version) { |
| 87 | + return (this.prefix.endsWith("/") || path.startsWith("/") ? this.prefix + path : this.prefix + "/" + path); |
| 88 | + } |
| 89 | + |
83 | 90 | } |
84 | 91 |
|
85 | 92 | /** |
86 | | - * Adds the given version string to the baseUrl, as a prefix in the request path. |
| 93 | + * File name-based {@code VersionPathStrategy}, |
| 94 | + * e.g. {@code "path/foo-{version}.css"}. |
87 | 95 | */ |
88 | | - protected String addVersionAsPrefix(String baseUrl, String version) { |
89 | | - return version + baseUrl; |
| 96 | + protected static class FileNameVersionPathStrategy implements VersionPathStrategy { |
| 97 | + |
| 98 | + private static final Pattern pattern = Pattern.compile("-(\\S*)\\."); |
| 99 | + |
| 100 | + |
| 101 | + @Override |
| 102 | + public String extractVersion(String requestPath) { |
| 103 | + Matcher matcher = pattern.matcher(requestPath); |
| 104 | + if (matcher.find()) { |
| 105 | + String match = matcher.group(1); |
| 106 | + return match.contains("-") ? match.substring(match.lastIndexOf("-") + 1) : match; |
| 107 | + } |
| 108 | + else { |
| 109 | + return null; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public String removeVersion(String requestPath, String version) { |
| 115 | + return StringUtils.delete(requestPath, "-" + version); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public String addVersion(String requestPath, String version) { |
| 120 | + String baseFilename = StringUtils.stripFilenameExtension(requestPath); |
| 121 | + String extension = StringUtils.getFilenameExtension(requestPath); |
| 122 | + return baseFilename + "-" + version + "." + extension; |
| 123 | + } |
90 | 124 | } |
91 | 125 |
|
92 | 126 | } |
0 commit comments