Skip to content

Commit 3cc0055

Browse files
committed
Merge pull request #11991 from Rupert Madden-Abbott
* gh-11991: Polish "Fix handling of static resource jars with spaces in their paths" Fix handling of static resource jars with spaces in their paths
2 parents 647c6c4 + 88423c5 commit 3cc0055

File tree

6 files changed

+62
-33
lines changed

6 files changed

+62
-33
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,7 +40,7 @@
4040
*/
4141
class IdeApplicationLauncher extends AbstractApplicationLauncher {
4242

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

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

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

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818

1919
import java.io.File;
2020
import java.io.IOException;
21+
import java.io.UnsupportedEncodingException;
2122
import java.net.JarURLConnection;
2223
import java.net.URL;
2324
import java.net.URLClassLoader;
2425
import java.net.URLConnection;
26+
import java.net.URLDecoder;
2527
import java.security.CodeSource;
2628
import java.util.ArrayList;
2729
import java.util.Arrays;
@@ -96,56 +98,71 @@ protected List<URL> getUrlsOfJarsWithMetaInfResources() {
9698
List<URL> staticResourceUrls = new ArrayList<URL>();
9799
if (classLoader instanceof URLClassLoader) {
98100
for (URL url : ((URLClassLoader) classLoader).getURLs()) {
99-
try {
100-
if ("file".equals(url.getProtocol())) {
101-
File file = new File(url.getFile());
102-
if (file.isDirectory()
103-
&& new File(file, "META-INF/resources").isDirectory()) {
104-
staticResourceUrls.add(url);
105-
}
106-
else if (isResourcesJar(file)) {
107-
staticResourceUrls.add(url);
108-
}
109-
}
110-
else {
111-
URLConnection connection = url.openConnection();
112-
if (connection instanceof JarURLConnection) {
113-
if (isResourcesJar((JarURLConnection) connection)) {
114-
staticResourceUrls.add(url);
115-
}
116-
}
117-
}
118-
}
119-
catch (IOException ex) {
120-
throw new IllegalStateException(ex);
101+
if (isStaticResourceJar(url)) {
102+
staticResourceUrls.add(url);
121103
}
122104
}
123105
}
124106
return staticResourceUrls;
125107
}
126108

109+
private boolean isStaticResourceJar(URL url) {
110+
try {
111+
if ("file".equals(url.getProtocol())) {
112+
File file = new File(getDecodedFile(url), "UTF-8");
113+
return (file.isDirectory()
114+
&& new File(file, "META-INF/resources").isDirectory())
115+
|| isResourcesJar(file);
116+
}
117+
else {
118+
URLConnection connection = url.openConnection();
119+
if (connection instanceof JarURLConnection
120+
&& isResourcesJar((JarURLConnection) connection)) {
121+
return true;
122+
}
123+
}
124+
}
125+
catch (IOException ex) {
126+
throw new IllegalStateException(ex);
127+
}
128+
return false;
129+
}
130+
131+
protected final String getDecodedFile(URL url) {
132+
try {
133+
return URLDecoder.decode(url.getFile(), "UTF-8");
134+
}
135+
catch (UnsupportedEncodingException ex) {
136+
throw new IllegalStateException(
137+
"Failed to decode '" + url.getFile() + "' using UTF-8");
138+
}
139+
}
140+
127141
private boolean isResourcesJar(JarURLConnection connection) {
128142
try {
129143
return isResourcesJar(connection.getJarFile());
130144
}
131145
catch (IOException ex) {
146+
this.logger.warn("Unable to open jar from connection '" + connection
147+
+ "' to determine if it contains static resources", ex);
132148
return false;
133149
}
134150
}
135151

136152
private boolean isResourcesJar(File file) {
137153
try {
138-
return isResourcesJar(new JarFile(file));
154+
return file.getName().endsWith(".jar") && isResourcesJar(new JarFile(file));
139155
}
140156
catch (IOException ex) {
157+
this.logger.warn("Unable to open jar '" + file
158+
+ "' to determine if it contains static resources", ex);
141159
return false;
142160
}
143161
}
144162

145163
private boolean isResourcesJar(JarFile jar) throws IOException {
146164
try {
147-
return jar.getName().endsWith(".jar")
148-
&& (jar.getJarEntry("META-INF/resources") != null);
165+
return jar.getJarEntry("META-INF/resources") != null;
149166
}
150167
finally {
151168
jar.close();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ private void configureDocumentRoot(WebAppContext handler) {
433433

434434
private Resource createResource(URL url) throws IOException {
435435
if ("file".equals(url.getProtocol())) {
436-
File file = new File(url.getFile());
436+
File file = new File(getDecodedFile(url));
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: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,9 +16,11 @@
1616

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

19+
import java.io.UnsupportedEncodingException;
1920
import java.lang.reflect.Method;
2021
import java.net.MalformedURLException;
2122
import java.net.URL;
23+
import java.net.URLDecoder;
2224
import java.util.List;
2325

2426
import javax.naming.directory.DirContext;
@@ -47,7 +49,7 @@ abstract class TomcatResources {
4749

4850
void addResourceJars(List<URL> resourceJarUrls) {
4951
for (URL url : resourceJarUrls) {
50-
String file = url.getFile();
52+
String file = getDecodedFile(url);
5153
if (file.endsWith(".jar") || file.endsWith(".jar!/")) {
5254
String jar = url.toString();
5355
if (!jar.startsWith("jar:")) {
@@ -62,6 +64,16 @@ void addResourceJars(List<URL> resourceJarUrls) {
6264
}
6365
}
6466

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+
6577
protected final Context getContext() {
6678
return this.context;
6779
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -494,7 +494,7 @@ private ResourceManager getDocumentRootResourceManager() {
494494
resourceManagers.add(rootResourceManager);
495495
for (URL url : metaInfResourceUrls) {
496496
if ("file".equals(url.getProtocol())) {
497-
File file = new File(url.getFile());
497+
File file = new File(getDecodedFile(url));
498498
if (file.isFile()) {
499499
try {
500500
resourceJarUrls.add(new URL("jar:" + url + "!/"));

spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)