Skip to content

Commit

Permalink
Merge pull request #11252 from mkurz/backport_11108
Browse files Browse the repository at this point in the history
[2.8.x] Adding support for checking URL with protocol='bundleresource' existence
  • Loading branch information
mergify[bot] committed May 6, 2022
2 parents 57fe175 + a1ffb47 commit adfa342
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
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

0 comments on commit adfa342

Please sign in to comment.