Skip to content

Commit

Permalink
Append trailing slash to static location
Browse files Browse the repository at this point in the history
Closes gh-33815
  • Loading branch information
rstoyanchev committed Nov 8, 2024
1 parent 37243f4 commit 59ec871
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,27 @@ else if (location instanceof ClassPathResource classPathResource) {
else {
path = location.getURL().getPath();
}
assertLocationPath(path);
Assert.isTrue(path.endsWith(FOLDER_SEPARATOR) || path.endsWith(WINDOWS_FOLDER_SEPARATOR),
"Resource location does not end with slash: " + path);
}
catch (IOException ex) {
// ignore
}
}

/**
* Assert the given location path is a directory and ends on slash.
* Check if the given static resource location path ends with a trailing
* slash, and append it if necessary.
* @param path the location path
* @return the resulting path to use
*/
public static void assertLocationPath(@Nullable String path) {
Assert.notNull(path, "Resource location path must not be null");
Assert.isTrue(path.endsWith(FOLDER_SEPARATOR) || path.endsWith(WINDOWS_FOLDER_SEPARATOR),
"Resource location does not end with slash: " + path);
public static String initLocationPath(String path) {
String separator = (path.contains(FOLDER_SEPARATOR) ? FOLDER_SEPARATOR : WINDOWS_FOLDER_SEPARATOR);
if (!path.endsWith(separator)) {
path = path.concat(separator);
logger.warn("Appended trailing slash to static resource location: " + path);
}
return path;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ private void resolveResourceLocations() {
Assert.isTrue(CollectionUtils.isEmpty(this.locationResources), "Please set " +
"either Resource-based \"locations\" or String-based \"locationValues\", but not both.");
for (String location : this.locationValues) {
ResourceHandlerUtils.assertLocationPath(location);
location = ResourceHandlerUtils.initLocationPath(location);
result.add(this.resourceLoader.getResource(location));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,27 @@ else if (location instanceof ClassPathResource classPathResource) {
else {
path = location.getURL().getPath();
}
assertLocationPath(path);
Assert.isTrue(path.endsWith(FOLDER_SEPARATOR) || path.endsWith(WINDOWS_FOLDER_SEPARATOR),
"Resource location does not end with slash: " + path);
}
catch (IOException ex) {
// ignore
}
}

/**
* Assert the given location path is a directory and ends on slash.
* Check if the given static resource location path ends with a trailing
* slash, and append it if necessary.
* @param path the location path
* @return the resulting path to use
*/
public static void assertLocationPath(@Nullable String path) {
Assert.notNull(path, "Resource location path must not be null");
Assert.isTrue(path.endsWith(FOLDER_SEPARATOR) || path.endsWith(WINDOWS_FOLDER_SEPARATOR),
"Resource location does not end with slash: " + path);
public static String initLocationPath(String path) {
String separator = (path.contains(FOLDER_SEPARATOR) ? FOLDER_SEPARATOR : WINDOWS_FOLDER_SEPARATOR);
if (!path.endsWith(separator)) {
path = path.concat(separator);
logger.warn("Appended trailing slash to static resource location: " + path);
}
return path;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ private void resolveResourceLocations() {
charset = Charset.forName(value);
location = location.substring(endIndex + 1);
}
ResourceHandlerUtils.assertLocationPath(location);
location = ResourceHandlerUtils.initLocationPath(location);
Resource resource = applicationContext.getResource(location);
if (location.equals("/") && !(resource instanceof ServletContextResource)) {
throw new IllegalStateException(
Expand Down

1 comment on commit 59ec871

@cagliostro92
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rstoyanchev Great solution.
However, I think the line you changed, compared to my solution, can lead to odd situations in case of a resource location registered, for some reason, as "path/to/folder\\".
Anyway, thanks to the "author"...this is (not) the open source world big sense...

Please sign in to comment.