+htp smaller improvements in FileAndResourceDirectives

- in getFromResourceDirectory allow specification of classLoader
 - quick constructor for ContentTypeResolver
 - allow overriding of `defaultClassLoader` choice
This commit is contained in:
Johannes Rudolph 2014-11-28 10:30:45 +01:00
parent 78d34538e8
commit c3d741c07d

View file

@ -84,7 +84,7 @@ trait FileAndResourceDirectives {
* some other thread !).
* If the resource cannot be found or read the Route rejects the request.
*/
def getFromResource(resourceName: String, contentType: ContentType, theClassLoader: ClassLoader = classOf[ActorSystem].getClassLoader): Route =
def getFromResource(resourceName: String, contentType: ContentType, theClassLoader: ClassLoader = defaultClassLoader): Route =
if (!resourceName.endsWith("/"))
get {
theClassLoader.getResource(resourceName) match {
@ -175,18 +175,20 @@ trait FileAndResourceDirectives {
* Same as "getFromDirectory" except that the file is not fetched from the file system but rather from a
* "resource directory".
*/
def getFromResourceDirectory(directoryName: String)(implicit resolver: ContentTypeResolver): Route = {
def getFromResourceDirectory(directoryName: String, classLoader: ClassLoader = defaultClassLoader)(implicit resolver: ContentTypeResolver): Route = {
val base = if (directoryName.isEmpty) "" else withTrailingSlash(directoryName)
extractUnmatchedPath { path
extractLog { log
fileSystemPath(base, path, log, separator = '/') match {
case "" reject
case resourceName getFromResource(resourceName)
case resourceName getFromResource(resourceName, resolver(resourceName), classLoader)
}
}
}
}
protected[http] def defaultClassLoader: ClassLoader = classOf[ActorSystem].getClassLoader
}
object FileAndResourceDirectives extends FileAndResourceDirectives {
@ -251,6 +253,11 @@ object ContentTypeResolver {
}
}
}
def apply(f: String ContentType): ContentTypeResolver =
new ContentTypeResolver {
def apply(fileName: String): ContentType = f(fileName)
}
}
case class DirectoryListing(path: String, isRoot: Boolean, files: Seq[File])