diff --git a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarURLConnection.java b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarURLConnection.java index 92ddeb0453..617fea8710 100644 --- a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarURLConnection.java +++ b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarURLConnection.java @@ -187,6 +187,15 @@ final class JarURLConnection extends java.net.JarURLConnection { @Override public int getContentLength() { + long longContentLength = getContentLengthLong(); + if (longContentLength > Integer.MAX_VALUE) { + return -1; + } + return (int) longContentLength; + } + + @Override + public long getContentLengthLong() { if (this.jarFile == null) { return -1; } diff --git a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarURLConnectionTests.java b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarURLConnectionTests.java index ef98792d9f..15c6df1a64 100644 --- a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarURLConnectionTests.java +++ b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarURLConnectionTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 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. @@ -136,6 +136,20 @@ public class JarURLConnectionTests { .hasSameContentAs(new ByteArrayInputStream(new byte[] { 3 })); } + @Test + public void getContentLengthReturnsLengthOfUnderlyingEntry() throws Exception { + URL url = new URL(new URL("jar", null, -1, + "file:" + getAbsolutePath() + "!/nested.jar!/", new Handler()), "/3.dat"); + assertThat(url.openConnection().getContentLength()).isEqualTo(1); + } + + @Test + public void getContentLengthLongReturnsLengthOfUnderlyingEntry() throws Exception { + URL url = new URL(new URL("jar", null, -1, + "file:" + getAbsolutePath() + "!/nested.jar!/", new Handler()), "/3.dat"); + assertThat(url.openConnection().getContentLengthLong()).isEqualTo(1); + } + private String getAbsolutePath() { return this.rootJarFile.getAbsolutePath().replace('\\', '/'); } diff --git a/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/JarResourceManager.java b/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/JarResourceManager.java index aa0513fa20..9515aa6734 100644 --- a/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/JarResourceManager.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/JarResourceManager.java @@ -48,7 +48,7 @@ class JarResourceManager implements ResourceManager { public Resource getResource(String path) throws IOException { URL url = new URL("jar:file:" + this.jarPath + "!" + (path.startsWith("/") ? path : "/" + path)); - URLResource resource = new URLResource(url, url.openConnection(), path); + URLResource resource = new URLResource(url, path); if (resource.getContentLength() < 0) { return null; } diff --git a/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServerFactory.java b/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServerFactory.java index 660459345e..988a22645f 100644 --- a/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServerFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServerFactory.java @@ -21,7 +21,6 @@ import java.io.IOException; import java.net.MalformedURLException; import java.net.Socket; import java.net.URL; -import java.net.URLConnection; import java.nio.charset.Charset; import java.security.KeyManagementException; import java.security.KeyStore; @@ -679,15 +678,9 @@ public class UndertowServletWebServerFactory extends AbstractServletWebServerFac @Override public Resource getResource(String path) { for (URL url : this.metaInfResourceJarUrls) { - try { - URL resourceUrl = new URL(url + "META-INF/resources" + path); - URLConnection connection = resourceUrl.openConnection(); - if (connection.getContentLength() >= 0) { - return new URLResource(resourceUrl, connection, path); - } - } - catch (IOException ex) { - // Continue + URLResource resource = getMetaInfResource(url, path); + if (resource != null) { + return resource; } } return null; @@ -704,6 +697,21 @@ public class UndertowServletWebServerFactory extends AbstractServletWebServerFac @Override public void removeResourceChangeListener(ResourceChangeListener listener) { + + } + + private URLResource getMetaInfResource(URL resourceJar, String path) { + try { + URL resourceUrl = new URL(resourceJar + "META-INF/resources" + path); + URLResource resource = new URLResource(resourceUrl, path); + if (resource.getContentLength() < 0) { + return null; + } + return resource; + } + catch (MalformedURLException ex) { + return null; + } } }