Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed May 23, 2022
1 parent 59c7bb1 commit 66a5742
Showing 1 changed file with 16 additions and 21 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -86,7 +86,7 @@ void cssFile(boolean usePathPatterns, String pathPrefix) throws Exception {
}

@ParameterizedTest
@MethodSource("argumentSource")
@MethodSource("argumentSource") // gh-26775
void classpathLocationWithEncodedPath(boolean usePathPatterns, String pathPrefix) throws Exception {
MockHttpServletRequest request = initRequest(pathPrefix + "/test/foo with spaces.css");
MockHttpServletResponse response = new MockHttpServletResponse();
Expand Down Expand Up @@ -130,40 +130,35 @@ static class WebConfig implements WebMvcConfigurer {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {

ClassPathResource classPathLocation = new ClassPathResource("", getClass());
String path = getPath(classPathLocation);

registerClasspathLocation("/cp/**", classPathLocation, registry);
registerFileSystemLocation("/fs/**", path, registry);
registerUrlLocation("/url/**", "file:" + path, registry);
}

protected void registerClasspathLocation(String pattern, ClassPathResource resource, ResourceHandlerRegistry registry) {
registry.addResourceHandler(pattern).addResourceLocations(resource);
}

protected void registerFileSystemLocation(String pattern, String path, ResourceHandlerRegistry registry) {
FileSystemResource fileSystemLocation = new FileSystemResource(path);
registry.addResourceHandler(pattern).addResourceLocations(fileSystemLocation);
registry.addResourceHandler("/cp/**").addResourceLocations(classPathLocation);
registry.addResourceHandler("/fs/**").addResourceLocations(new FileSystemResource(path));
registry.addResourceHandler("/url/**").addResourceLocations(urlResource(path));
}

protected void registerUrlLocation(String pattern, String path, ResourceHandlerRegistry registry) {
private String getPath(ClassPathResource resource) {
try {
UrlResource urlLocation = new UrlResource(path);
registry.addResourceHandler(pattern).addResourceLocations(urlLocation);
return resource.getFile().getCanonicalPath()
.replace('\\', '/')
.replace("classes/java", "resources") + "/";
}
catch (MalformedURLException ex) {
catch (IOException ex) {
throw new IllegalStateException(ex);
}
}

private String getPath(ClassPathResource resource) {
private UrlResource urlResource(String path) {
UrlResource urlResource;
try {
return resource.getFile().getCanonicalPath().replace('\\', '/').replace("classes/java", "resources") + "/";
urlResource = new UrlResource("file:" + path);
}
catch (IOException ex) {
catch (MalformedURLException ex) {
throw new IllegalStateException(ex);
}
return urlResource;
}
}

Expand Down

0 comments on commit 66a5742

Please sign in to comment.