Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.8.x] Adding support for checking URL with protocol='bundleresource' existence #11252

Merged
merged 3 commits into from May 6, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions core/play/src/main/scala/play/api/controllers/Assets.scala
Expand Up @@ -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
}
}

Expand Down
8 changes: 4 additions & 4 deletions core/play/src/main/scala/play/utils/Resources.scala
Expand Up @@ -16,10 +16,10 @@ 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 "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}'")
}
Expand Down
44 changes: 25 additions & 19 deletions core/play/src/test/scala/play/utils/ResourcesSpec.scala
Expand Up @@ -100,25 +100,31 @@ 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 {
Expand Down