From fd125b4a4a1f6690dce1cfbf00ccb8bcd07eb7f7 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 21 Jun 2018 12:40:48 +0100 Subject: [PATCH] Remove assumption that a file URI can be turned into a File Closes gh-13493 --- .../web/servlet/server/StaticResourceJars.java | 15 ++++++++++++--- .../servlet/server/StaticResourceJarsTests.java | 8 ++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/StaticResourceJars.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/StaticResourceJars.java index 0b1f4929965..a6248141fec 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/StaticResourceJars.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/StaticResourceJars.java @@ -78,15 +78,24 @@ class StaticResourceJars { throw new IllegalStateException( "Failed to create File from URL '" + url + "'"); } + catch (IllegalArgumentException ex) { + return null; + } } private void addUrl(List urls, URL url) { try { - if ("file".equals(url.getProtocol())) { - addUrlFile(urls, url, toFile(url)); + if (!"file".equals(url.getProtocol())) { + addUrlConnection(urls, url, url.openConnection()); } else { - addUrlConnection(urls, url, url.openConnection()); + File file = toFile(url); + if (file != null) { + addUrlFile(urls, url, file); + } + else { + addUrlConnection(urls, url, url.openConnection()); + } } } catch (IOException ex) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/StaticResourceJarsTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/StaticResourceJarsTests.java index fbbff0255f4..015798254b8 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/StaticResourceJarsTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/StaticResourceJarsTests.java @@ -74,6 +74,14 @@ public class StaticResourceJarsTests { assertThat(staticResourceJarUrls).hasSize(0); } + @Test + public void uncPathsAreTolerated() throws Exception { + File jarFile = createResourcesJar("test-resources.jar"); + List staticResourceJarUrls = new StaticResourceJars().getUrlsFrom( + jarFile.toURI().toURL(), new URL("file://unc.example.com/test.jar")); + assertThat(staticResourceJarUrls).hasSize(1); + } + private File createResourcesJar(String name) throws IOException { return createJar(name, (output) -> { JarEntry jarEntry = new JarEntry("META-INF/resources");