Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Ignore resource directories when trying to loadMessages in MessagesPl…

…ugin, otherwise app don't start if there's a matching directory
  • Loading branch information...
commit 686a9b4535815d24381cea9acd901461fd28a4fa 1 parent 3fe7d53
@orrsella orrsella authored
View
3  framework/src/play/src/main/scala/play/api/i18n/Messages.scala
@@ -7,6 +7,7 @@ import scala.language.postfixOps
import play.api._
import play.core._
+import play.utils.Resources
import java.io._
@@ -326,7 +327,7 @@ class DefaultMessagesPlugin(app: Application) extends MessagesPlugin {
}
protected def loadMessages(file: String): Map[String, String] = {
- app.classloader.getResources(joinPaths(messagesPrefix, file)).asScala.toList.reverse.map { messageFile =>
+ app.classloader.getResources(joinPaths(messagesPrefix, file)).asScala.toList.filterNot(Resources.isDirectory).reverse.map { messageFile =>
Messages.messages(messageFile.asInput, messageFile.toString).fold(e => throw e, identity)
}.foldLeft(Map.empty[String, String]) { _ ++ _ }
}
View
41 framework/src/play/src/main/scala/play/utils/Resources.scala
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
+ */
+package play.utils
+
+import java.net.URL
+import java.io.File
+import java.util.zip.ZipFile
+
+/**
+ * Provide resources helpers
+ */
+object Resources {
+
+ def isDirectory(url: URL) = url.getProtocol match {
+ case "file" => new File(url.getFile).isDirectory
+ case "jar" => isJarResourceDirectory(url)
+ case _ => throw new IllegalArgumentException(s"Cannot check isDirectory for a URL with protocol='${url.getProtocol}'")
+ }
+
+ private def isJarResourceDirectory(url: URL): Boolean = {
+ val startIndex = if (url.getFile.startsWith("file:")) 5 else 0
+ val bangIndex = url.getFile.indexOf("!")
+ val jarFilePath = url.getFile.substring(startIndex, bangIndex)
+ val resourcePath = url.getFile.substring(bangIndex + 2)
+ val zip = new ZipFile(jarFilePath)
+
+ try {
+ val entry = zip.getEntry(resourcePath)
+ if (entry.isDirectory) true
+ else {
+ val stream = zip.getInputStream(entry)
+ val isDir = stream == null
+ if (stream != null) stream.close()
+ isDir
+ }
+ } finally {
+ zip.close()
+ }
+ }
+}
View
90 framework/src/play/src/test/scala/play/utils/ResourcesSpec.scala
@@ -0,0 +1,90 @@
+package play.utils
+
+import java.io.{FileInputStream, BufferedInputStream, File, FileOutputStream}
+import java.net.URL
+import java.util.zip.{ZipEntry, ZipOutputStream}
+import org.specs2.mutable.Specification
+
+/**
+ * Tests for Resources object
+ */
+object ResourcesSpec extends Specification {
+ import Resources._
+
+ lazy val tmpDir = createTempDir("resources-", ".tmp")
+ lazy val jar = File.createTempFile("jar-", ".tmp", tmpDir)
+ lazy val fileRes = File.createTempFile("file-", ".tmp", tmpDir)
+ lazy val dirRes = createTempDir("dir-", ".tmp", tmpDir)
+
+ sequential
+ "resources isDirectory" should {
+
+ step {
+ createZip(jar, Seq(fileRes, dirRes))
+ }
+
+ "return true for a directory resource URL with the 'file' protocol" in {
+ val url = new URL("file", "", dirRes.getAbsolutePath)
+ isDirectory(url) must beTrue
+ }
+
+ "return false for a file resource URL with the 'file' protocol" in {
+ val url = new URL("file", "", fileRes.getAbsolutePath)
+ isDirectory(url) must beFalse
+ }
+
+ "return true for a directory resource URL with the 'jar' protocol" in {
+ val url = new URL("jar", "", s"file:${jar.getAbsolutePath}!/${dirRes.getName}")
+ isDirectory(url) must beTrue
+ }
+
+ "return false for a file resource URL with the 'jar' protocol" in {
+ val url = new URL("jar", "", s"file:${jar.getAbsolutePath}!/${fileRes.getName}")
+ isDirectory(url) must beFalse
+ }
+
+ "throw an exception for a URL with a protocol other than 'file'/'jar'" in {
+ val url = new URL("ftp", "", "/some/path")
+ isDirectory(url) must throwAn[IllegalArgumentException]
+ }
+
+ step {
+ tmpDir.listFiles().foreach(f => f.delete())
+ tmpDir.delete()
+ }
+ }
+
+ private def createTempDir(prefix: String, suffix: String, parent: File = null) = {
+ val f = File.createTempFile(prefix, suffix, parent)
+ f.delete()
+ f.mkdir()
+ f
+ }
+
+ private def createZip(zip: File, files: Seq[File]) = {
+ val zipOutputStream = new ZipOutputStream(new FileOutputStream(zip))
+ addFileToZip(zipOutputStream, fileRes)
+ addFileToZip(zipOutputStream, dirRes)
+ zipOutputStream.close()
+ }
+
+ private def addFileToZip(zip: ZipOutputStream, file: File) = {
+ val entryName =
+ if (file.isDirectory) file.getName + "/"
+ else file.getName
+
+ zip.putNextEntry(new ZipEntry(entryName))
+
+ if (!file.isDirectory) {
+ val in = new BufferedInputStream(new FileInputStream(file))
+ var b = in.read()
+ while (b > -1) {
+ zip.write(b)
+ b = in.read()
+ }
+ in.close()
+ }
+
+ zip.closeEntry()
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.