package coolcode import java.io.File import java.util.Properties import org.scalatra.ScalatraServlet class StaticFileServlet extends ScalatraServlet { get("/*") { val resourcePath = getResourcePath Option(servletContext.getResourceAsStream(resourcePath)) match { case Some(inputStream) => { val staticFileContent = IOUtils.toByteArray(inputStream) contentType = StaticFileServlet.resolveContentType(resourcePath) staticFileContent } case None => halt(404) } } private def getResourcePath = StringUtils.isEmpty(splatPath) match { case true => request.getServletPath case false => request.getServletPath + "/" + splatPath } private def splatPath = multiParams("splat").head } object StaticFileServlet { private val properties: Properties = new Properties() properties.load(classOf[StaticFileServlet].getResourceAsStream("mime.properties")) def resolveContentType(resourcePath: String) = { val extension = properties.get(suffix(resourcePath)) if (extension != null) extension.toString() else "text/plain" } private def suffix(path: String): String = path.reverse.takeWhile(_ != '.').reverse }