From 37dbd25ddd89ecdb75a2b57e6cb8a03d28e39139 Mon Sep 17 00:00:00 2001 From: aitzhaki Date: Sun, 16 Jan 2022 10:36:05 +0200 Subject: [PATCH 1/3] Adding support for checking URL with protocol='bundleresource' existence --- .../scala/play/api/controllers/Assets.scala | 8 ++-- .../src/main/scala/play/utils/Resources.scala | 10 ++--- .../test/scala/play/utils/ResourcesSpec.scala | 41 ++++++++++--------- 3 files changed, 31 insertions(+), 28 deletions(-) diff --git a/core/play/src/main/scala/play/api/controllers/Assets.scala b/core/play/src/main/scala/play/api/controllers/Assets.scala index 6505560df03..60b55ce0ba6 100644 --- a/core/play/src/main/scala/play/api/controllers/Assets.scala +++ b/core/play/src/main/scala/play/api/controllers/Assets.scala @@ -570,10 +570,10 @@ private class AssetInfo( } url.getProtocol match { - case "file" => Some(httpDateFormat.format(Instant.ofEpochMilli(new File(url.toURI).lastModified))) - case "jar" => getLastModified[JarURLConnection](c => c.getJarEntry.getTime) - case "bundle" => getLastModified[URLConnection](c => c.getLastModified) - case _ => None + case "file" => Some(httpDateFormat.format(Instant.ofEpochMilli(new File(url.toURI).lastModified))) + case "jar" => getLastModified[JarURLConnection](c => c.getJarEntry.getTime) + case "bundle" | "bundleresource" => getLastModified[URLConnection](c => c.getLastModified) + case _ => None } } diff --git a/core/play/src/main/scala/play/utils/Resources.scala b/core/play/src/main/scala/play/utils/Resources.scala index 3dba2f86c62..5a325554f87 100644 --- a/core/play/src/main/scala/play/utils/Resources.scala +++ b/core/play/src/main/scala/play/utils/Resources.scala @@ -16,11 +16,11 @@ import java.util.zip.ZipFile */ object Resources { def isDirectory(classLoader: ClassLoader, url: URL) = url.getProtocol match { - case "file" => new File(url.toURI).isDirectory - case "jar" => isZipResourceDirectory(url) - case "zip" => isZipResourceDirectory(url) - case "bundle" => isBundleResourceDirectory(classLoader, url) - case _ => + case "file" => new File(url.toURI).isDirectory + case "jar" => isZipResourceDirectory(url) + case "zip" => isZipResourceDirectory(url) + case "bundle" | "bundleresource" => isBundleResourceDirectory(classLoader, url) + case _ => throw new IllegalArgumentException(s"Cannot check isDirectory for a URL with protocol='${url.getProtocol}'") } diff --git a/core/play/src/test/scala/play/utils/ResourcesSpec.scala b/core/play/src/test/scala/play/utils/ResourcesSpec.scala index 21ab1adce9d..ccf3bb22bd2 100644 --- a/core/play/src/test/scala/play/utils/ResourcesSpec.scala +++ b/core/play/src/test/scala/play/utils/ResourcesSpec.scala @@ -100,25 +100,28 @@ class ResourcesSpec extends Specification { isDirectory(classloader, url) must beFalse } - "return true for a directory resource URL with the 'bundle' protocol" in { - val relativeIndex = dirBundle.getAbsolutePath.indexOf("test-bundle-") - val dir = dirBundle.getAbsolutePath.substring(relativeIndex) - val url = new URL("bundle", "325.0", 25, dir, new BundleStreamHandler) - isDirectory(osgiClassloader, url) must beTrue - } - - "return true for a directory resource URL that contains spaces with the 'bundle' protocol" in { - val relativeIndex = spacesDirBundle.getAbsolutePath.indexOf("test-bundle-") - val dir = spacesDirBundle.getAbsolutePath.substring(relativeIndex) - val url = new URL("bundle", "325.0", 25, dir, new BundleStreamHandler) - isDirectory(osgiClassloader, url) must beTrue - } - - "return false for a file resource URL with the 'bundle' protocol" in { - val relativeIndex = fileBundle.getAbsolutePath.indexOf("test-bundle-") - val file = fileBundle.getAbsolutePath.substring(relativeIndex) - val url = new URL("bundle", "325.0", 25, file, new BundleStreamHandler) - isDirectory(osgiClassloader, url) must beFalse + "return true for a directory resource URL with the 'bundle' and 'bundleresource' protocols" in { + val relativeIndex = dirBundle.getAbsolutePath.indexOf("test-bundle-") + val dir = dirBundle.getAbsolutePath.substring(relativeIndex) + val urlBundle = new URL("bundle", "325.0", 25, dir, new BundleStreamHandler) + val urlBundleresource = new URL("bundleresource", "325.0", 25, dir, new BundleStreamHandler) + (isDirectory(osgiClassloader, urlBundle) must beTrue).and(isDirectory(osgiClassloader, urlBundleresource) must beTrue) + } + + "return true for a directory resource URL that contains spaces with the 'bundle' and 'bundleresource' protocols" in { + val relativeIndex = spacesDirBundle.getAbsolutePath.indexOf("test-bundle-") + val dir = spacesDirBundle.getAbsolutePath.substring(relativeIndex) + val urlBundle = new URL("bundle", "325.0", 25, dir, new BundleStreamHandler) + val urlBundleresource = new URL("bundleresource", "325.0", 25, dir, new BundleStreamHandler) + (isDirectory(osgiClassloader, urlBundle) must beTrue).and(isDirectory(osgiClassloader, urlBundleresource) must beTrue) + } + + "return false for a file resource URL with the 'bundle' and 'bundleresource' protocols" in { + val relativeIndex = fileBundle.getAbsolutePath.indexOf("test-bundle-") + val file = fileBundle.getAbsolutePath.substring(relativeIndex) + val urlBundle = new URL("bundle", "325.0", 25, file, new BundleStreamHandler) + val urlBundleresource = new URL("bundleresource", "325.0", 25, file, new BundleStreamHandler) + (isDirectory(osgiClassloader, urlBundle) must beFalse).and(isDirectory(osgiClassloader, urlBundleresource) must beFalse) } "return true for a directory resource URL with the 'zip' protocol" in { From b54c3813c219fc1605dd152dd1b8cc2200f4e347 Mon Sep 17 00:00:00 2001 From: aitzhaki Date: Sun, 23 Jan 2022 10:15:11 +0200 Subject: [PATCH 2/3] scalafmtall --- .../src/main/scala/play/utils/Resources.scala | 2 +- .../test/scala/play/utils/ResourcesSpec.scala | 34 +++++++++++-------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/core/play/src/main/scala/play/utils/Resources.scala b/core/play/src/main/scala/play/utils/Resources.scala index 5a325554f87..e47e4ad4365 100644 --- a/core/play/src/main/scala/play/utils/Resources.scala +++ b/core/play/src/main/scala/play/utils/Resources.scala @@ -20,7 +20,7 @@ object Resources { case "jar" => isZipResourceDirectory(url) case "zip" => isZipResourceDirectory(url) case "bundle" | "bundleresource" => isBundleResourceDirectory(classLoader, url) - case _ => + case _ => throw new IllegalArgumentException(s"Cannot check isDirectory for a URL with protocol='${url.getProtocol}'") } diff --git a/core/play/src/test/scala/play/utils/ResourcesSpec.scala b/core/play/src/test/scala/play/utils/ResourcesSpec.scala index ccf3bb22bd2..80632fc11c9 100644 --- a/core/play/src/test/scala/play/utils/ResourcesSpec.scala +++ b/core/play/src/test/scala/play/utils/ResourcesSpec.scala @@ -101,27 +101,30 @@ class ResourcesSpec extends Specification { } "return true for a directory resource URL with the 'bundle' and 'bundleresource' protocols" in { - val relativeIndex = dirBundle.getAbsolutePath.indexOf("test-bundle-") - val dir = dirBundle.getAbsolutePath.substring(relativeIndex) - val urlBundle = new URL("bundle", "325.0", 25, dir, new BundleStreamHandler) - val urlBundleresource = new URL("bundleresource", "325.0", 25, dir, new BundleStreamHandler) - (isDirectory(osgiClassloader, urlBundle) must beTrue).and(isDirectory(osgiClassloader, urlBundleresource) must beTrue) + val relativeIndex = dirBundle.getAbsolutePath.indexOf("test-bundle-") + val dir = dirBundle.getAbsolutePath.substring(relativeIndex) + val urlBundle = new URL("bundle", "325.0", 25, dir, new BundleStreamHandler) + val urlBundleresource = new URL("bundleresource", "325.0", 25, dir, new BundleStreamHandler) + (isDirectory(osgiClassloader, urlBundle) must beTrue) + .and(isDirectory(osgiClassloader, urlBundleresource) must beTrue) } "return true for a directory resource URL that contains spaces with the 'bundle' and 'bundleresource' protocols" in { - val relativeIndex = spacesDirBundle.getAbsolutePath.indexOf("test-bundle-") - val dir = spacesDirBundle.getAbsolutePath.substring(relativeIndex) - val urlBundle = new URL("bundle", "325.0", 25, dir, new BundleStreamHandler) - val urlBundleresource = new URL("bundleresource", "325.0", 25, dir, new BundleStreamHandler) - (isDirectory(osgiClassloader, urlBundle) must beTrue).and(isDirectory(osgiClassloader, urlBundleresource) must beTrue) + val relativeIndex = spacesDirBundle.getAbsolutePath.indexOf("test-bundle-") + val dir = spacesDirBundle.getAbsolutePath.substring(relativeIndex) + val urlBundle = new URL("bundle", "325.0", 25, dir, new BundleStreamHandler) + val urlBundleresource = new URL("bundleresource", "325.0", 25, dir, new BundleStreamHandler) + (isDirectory(osgiClassloader, urlBundle) must beTrue) + .and(isDirectory(osgiClassloader, urlBundleresource) must beTrue) } "return false for a file resource URL with the 'bundle' and 'bundleresource' protocols" in { - val relativeIndex = fileBundle.getAbsolutePath.indexOf("test-bundle-") - val file = fileBundle.getAbsolutePath.substring(relativeIndex) - val urlBundle = new URL("bundle", "325.0", 25, file, new BundleStreamHandler) - val urlBundleresource = new URL("bundleresource", "325.0", 25, file, new BundleStreamHandler) - (isDirectory(osgiClassloader, urlBundle) must beFalse).and(isDirectory(osgiClassloader, urlBundleresource) must beFalse) + val relativeIndex = fileBundle.getAbsolutePath.indexOf("test-bundle-") + val file = fileBundle.getAbsolutePath.substring(relativeIndex) + val urlBundle = new URL("bundle", "325.0", 25, file, new BundleStreamHandler) + val urlBundleresource = new URL("bundleresource", "325.0", 25, file, new BundleStreamHandler) + (isDirectory(osgiClassloader, urlBundle) must beFalse) + .and(isDirectory(osgiClassloader, urlBundleresource) must beFalse) } "return true for a directory resource URL with the 'zip' protocol" in { @@ -160,6 +163,7 @@ class ResourcesSpec extends Specification { } object EmptyURLStreamHandler extends URLStreamHandler { + def openConnection(u: URL) = new URLConnection(u) { def connect(): Unit = {} } From 1b2c3453f45b311d43e217a6b30143253c1510b3 Mon Sep 17 00:00:00 2001 From: aitzhaki Date: Sun, 23 Jan 2022 11:38:18 +0200 Subject: [PATCH 3/3] signed the CLA