Polishing

This commit is contained in:
rstoyanchev 2022-05-23 10:07:03 +01:00
parent 59c7bb1f86
commit 66a5742df3
1 changed files with 20 additions and 25 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -86,7 +86,7 @@ public class ResourceHttpRequestHandlerIntegrationTests {
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("argumentSource") @MethodSource("argumentSource") // gh-26775
void classpathLocationWithEncodedPath(boolean usePathPatterns, String pathPrefix) throws Exception { void classpathLocationWithEncodedPath(boolean usePathPatterns, String pathPrefix) throws Exception {
MockHttpServletRequest request = initRequest(pathPrefix + "/test/foo with spaces.css"); MockHttpServletRequest request = initRequest(pathPrefix + "/test/foo with spaces.css");
MockHttpServletResponse response = new MockHttpServletResponse(); MockHttpServletResponse response = new MockHttpServletResponse();
@ -130,41 +130,36 @@ public class ResourceHttpRequestHandlerIntegrationTests {
@Override @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) { public void addResourceHandlers(ResourceHandlerRegistry registry) {
ClassPathResource classPathLocation = new ClassPathResource("", getClass()); ClassPathResource classPathLocation = new ClassPathResource("", getClass());
String path = getPath(classPathLocation); String path = getPath(classPathLocation);
registerClasspathLocation("/cp/**", classPathLocation, registry); registry.addResourceHandler("/cp/**").addResourceLocations(classPathLocation);
registerFileSystemLocation("/fs/**", path, registry); registry.addResourceHandler("/fs/**").addResourceLocations(new FileSystemResource(path));
registerUrlLocation("/url/**", "file:" + path, registry); registry.addResourceHandler("/url/**").addResourceLocations(urlResource(path));
}
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);
}
protected void registerUrlLocation(String pattern, String path, ResourceHandlerRegistry registry) {
try {
UrlResource urlLocation = new UrlResource(path);
registry.addResourceHandler(pattern).addResourceLocations(urlLocation);
}
catch (MalformedURLException ex) {
throw new IllegalStateException(ex);
}
} }
private String getPath(ClassPathResource resource) { private String getPath(ClassPathResource resource) {
try { try {
return resource.getFile().getCanonicalPath().replace('\\', '/').replace("classes/java", "resources") + "/"; return resource.getFile().getCanonicalPath()
.replace('\\', '/')
.replace("classes/java", "resources") + "/";
} }
catch (IOException ex) { catch (IOException ex) {
throw new IllegalStateException(ex); throw new IllegalStateException(ex);
} }
} }
private UrlResource urlResource(String path) {
UrlResource urlResource;
try {
urlResource = new UrlResource("file:" + path);
}
catch (MalformedURLException ex) {
throw new IllegalStateException(ex);
}
return urlResource;
}
} }