Skip to content

Commit a29a70d

Browse files
committed
Fix handling of static resource jar paths containing a +
Closes gh-12942
1 parent 08bc306 commit a29a70d

File tree

5 files changed

+38
-34
lines changed

5 files changed

+38
-34
lines changed

spring-boot-integration-tests/spring-boot-integration-tests-embedded-servlet-container/src/test/java/org/springframework/boot/context/embedded/IdeApplicationLauncher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
*/
4141
class IdeApplicationLauncher extends AbstractApplicationLauncher {
4242

43-
private final File exploded = new File("target/ide application");
43+
private final File exploded = new File("target/the+ide application");
4444

4545
IdeApplicationLauncher(ApplicationBuilder applicationBuilder) {
4646
super(applicationBuilder);

spring-boot/src/main/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactory.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ protected List<URL> getUrlsOfJarsWithMetaInfResources() {
109109
private boolean isStaticResourceJar(URL url) {
110110
try {
111111
if ("file".equals(url.getProtocol())) {
112-
File file = new File(getDecodedFile(url));
112+
File file = new File(url.toURI());
113113
return (file.isDirectory()
114114
&& new File(file, "META-INF/resources").isDirectory())
115115
|| isResourcesJar(file);
@@ -122,12 +122,20 @@ && isResourcesJar((JarURLConnection) connection)) {
122122
}
123123
}
124124
}
125-
catch (IOException ex) {
125+
catch (Exception ex) {
126126
throw new IllegalStateException(ex);
127127
}
128128
return false;
129129
}
130130

131+
/**
132+
* Converts the given {@code url} into a decoded file path.
133+
*
134+
* @param url the url to convert
135+
* @return the file path
136+
* @deprecated Since 1.5.13 in favor of {@link File#File(java.net.URI)}
137+
*/
138+
@Deprecated
131139
protected final String getDecodedFile(URL url) {
132140
try {
133141
return URLDecoder.decode(url.getFile(), "UTF-8");

spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,9 @@ private void configureDocumentRoot(WebAppContext handler) {
431431
}
432432
}
433433

434-
private Resource createResource(URL url) throws IOException {
434+
private Resource createResource(URL url) throws Exception {
435435
if ("file".equals(url.getProtocol())) {
436-
File file = new File(getDecodedFile(url));
436+
File file = new File(url.toURI());
437437
if (file.isFile()) {
438438
return Resource.newResource("jar:" + url + "!/META-INF/resources");
439439
}

spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatResources.java

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
package org.springframework.boot.context.embedded.tomcat;
1818

19-
import java.io.UnsupportedEncodingException;
19+
import java.io.File;
2020
import java.lang.reflect.Method;
2121
import java.net.MalformedURLException;
22+
import java.net.URISyntaxException;
2223
import java.net.URL;
23-
import java.net.URLDecoder;
2424
import java.util.List;
2525

2626
import javax.naming.directory.DirContext;
@@ -49,31 +49,27 @@ abstract class TomcatResources {
4949

5050
void addResourceJars(List<URL> resourceJarUrls) {
5151
for (URL url : resourceJarUrls) {
52-
String file = getDecodedFile(url);
53-
if (file.endsWith(".jar") || file.endsWith(".jar!/")) {
54-
String jar = url.toString();
55-
if (!jar.startsWith("jar:")) {
56-
// A jar file in the file system. Convert to Jar URL.
57-
jar = "jar:" + jar + "!/";
52+
try {
53+
String path = url.getPath();
54+
if (path.endsWith(".jar") || path.endsWith(".jar!/")) {
55+
String jar = url.toString();
56+
if (!jar.startsWith("jar:")) {
57+
// A jar file in the file system. Convert to Jar URL.
58+
jar = "jar:" + jar + "!/";
59+
}
60+
addJar(jar);
61+
}
62+
else {
63+
addDir(new File(url.toURI()).getAbsolutePath(), url);
5864
}
59-
addJar(jar);
6065
}
61-
else {
62-
addDir(file, url);
66+
catch (URISyntaxException ex) {
67+
throw new IllegalStateException(
68+
"Failed to create File from URL '" + url + "'");
6369
}
6470
}
6571
}
6672

67-
private String getDecodedFile(URL url) {
68-
try {
69-
return URLDecoder.decode(url.getFile(), "UTF-8");
70-
}
71-
catch (UnsupportedEncodingException ex) {
72-
throw new IllegalStateException(
73-
"Failed to decode '" + url.getFile() + "' using UTF-8");
74-
}
75-
}
76-
7773
protected final Context getContext() {
7874
return this.context;
7975
}

spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactory.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -494,18 +494,18 @@ private ResourceManager getDocumentRootResourceManager() {
494494
resourceManagers.add(rootResourceManager);
495495
for (URL url : metaInfResourceUrls) {
496496
if ("file".equals(url.getProtocol())) {
497-
File file = new File(getDecodedFile(url));
498-
if (file.isFile()) {
499-
try {
497+
try {
498+
File file = new File(url.toURI());
499+
if (file.isFile()) {
500500
resourceJarUrls.add(new URL("jar:" + url + "!/"));
501501
}
502-
catch (MalformedURLException ex) {
503-
throw new RuntimeException(ex);
502+
else {
503+
resourceManagers.add(new FileResourceManager(
504+
new File(file, "META-INF/resources"), 0));
504505
}
505506
}
506-
else {
507-
resourceManagers.add(new FileResourceManager(
508-
new File(file, "META-INF/resources"), 0));
507+
catch (Exception ex) {
508+
throw new RuntimeException(ex);
509509
}
510510
}
511511
else {

0 commit comments

Comments
 (0)