Skip to content

Instantly share code, notes, and snippets.

@laurilehmijoki
Last active December 12, 2018 04:59
Show Gist options
  • Save laurilehmijoki/4483113 to your computer and use it in GitHub Desktop.
Save laurilehmijoki/4483113 to your computer and use it in GitHub Desktop.
Static file servlet with Scalatra
ai = application/postscript
aif = audio/x-aiff
aifc = audio/x-aiff
aiff = audio/x-aiff
apk = application/vnd.android.package-archive
asc = text/plain
asf = video/x.ms.asf
asx = video/x.ms.asx
au = audio/basic
avi = video/x-msvideo
bcpio = application/x-bcpio
bin = application/octet-stream
cab = application/x-cabinet
cdf = application/x-netcdf
class = application/java-vm
cpio = application/x-cpio
cpt = application/mac-compactpro
crt = application/x-x509-ca-cert
csh = application/x-csh
css = text/css
csv = text/comma-separated-values
dcr = application/x-director
dir = application/x-director
dll = application/x-msdownload
dms = application/octet-stream
doc = application/msword
dtd = application/xml-dtd
dvi = application/x-dvi
dxr = application/x-director
eps = application/postscript
etx = text/x-setext
exe = application/octet-stream
ez = application/andrew-inset
gif = image/gif
gtar = application/x-gtar
gz = application/gzip
gzip = application/gzip
hdf = application/x-hdf
hqx = application/mac-binhex40
htc = text/x-component
htm = text/html
html = text/html
ice = x-conference/x-cooltalk
ico = image/x-icon
ief = image/ief
iges = model/iges
igs = model/iges
jad = text/vnd.sun.j2me.app-descriptor
jar = application/java-archive
java = text/plain
jnlp = application/x-java-jnlp-file
jpe = image/jpeg
jpeg = image/jpeg
jpg = image/jpeg
js = application/x-javascript
jsp = text/html
kar = audio/midi
latex = application/x-latex
lha = application/octet-stream
lzh = application/octet-stream
man = application/x-troff-man
mathml = application/mathml+xml
me = application/x-troff-me
mesh = model/mesh
mid = audio/midi
midi = audio/midi
mif = application/vnd.mif
mol = chemical/x-mdl-molfile
mov = video/quicktime
movie = video/x-sgi-movie
mp2 = audio/mpeg
mp3 = audio/mpeg
mpe = video/mpeg
mpeg = video/mpeg
mpg = video/mpeg
mpga = audio/mpeg
ms = application/x-troff-ms
msh = model/mesh
msi = application/octet-stream
nc = application/x-netcdf
oda = application/oda
odb = application/vnd.oasis.opendocument.database
odc = application/vnd.oasis.opendocument.chart
odf = application/vnd.oasis.opendocument.formula
odg = application/vnd.oasis.opendocument.graphics
odi = application/vnd.oasis.opendocument.image
odm = application/vnd.oasis.opendocument.text-master
odp = application/vnd.oasis.opendocument.presentation
ods = application/vnd.oasis.opendocument.spreadsheet
odt = application/vnd.oasis.opendocument.text
ogg = application/ogg
otc = application/vnd.oasis.opendocument.chart-template
otf = application/vnd.oasis.opendocument.formula-template
otg = application/vnd.oasis.opendocument.graphics-template
oth = application/vnd.oasis.opendocument.text-web
oti = application/vnd.oasis.opendocument.image-template
otp = application/vnd.oasis.opendocument.presentation-template
ots = application/vnd.oasis.opendocument.spreadsheet-template
ott = application/vnd.oasis.opendocument.text-template
pbm = image/x-portable-bitmap
pdb = chemical/x-pdb
pdf = application/pdf
pgm = image/x-portable-graymap
pgn = application/x-chess-pgn
png = image/png
pnm = image/x-portable-anymap
ppm = image/x-portable-pixmap
pps = application/vnd.ms-powerpoint
ppt = application/vnd.ms-powerpoint
ps = application/postscript
qt = video/quicktime
ra = audio/x-pn-realaudio
ram = audio/x-pn-realaudio
ras = image/x-cmu-raster
rdf = application/rdf+xml
rgb = image/x-rgb
rm = audio/x-pn-realaudio
roff = application/x-troff
rpm = application/x-rpm
rtf = application/rtf
rtx = text/richtext
rv = video/vnd.rn-realvideo
ser = application/java-serialized-object
sgm = text/sgml
sgml = text/sgml
sh = application/x-sh
shar = application/x-shar
silo = model/mesh
sit = application/x-stuffit
skd = application/x-koan
skm = application/x-koan
skp = application/x-koan
skt = application/x-koan
smi = application/smil
smil = application/smil
snd = audio/basic
spl = application/x-futuresplash
src = application/x-wais-source
sv4cpio = application/x-sv4cpio
sv4crc = application/x-sv4crc
svg = image/svg+xml
swf = application/x-shockwave-flash
t = application/x-troff
tar = application/x-tar
tar.gz = application/x-gtar
tcl = application/x-tcl
tex = application/x-tex
texi = application/x-texinfo
texinfo = application/x-texinfo
tgz = application/x-gtar
tif = image/tiff
tiff = image/tiff
tr = application/x-troff
tsv = text/tab-separated-values
txt = text/plain
ustar = application/x-ustar
vcd = application/x-cdlink
vrml = model/vrml
vxml = application/voicexml+xml
wav = audio/x-wav
wbmp = image/vnd.wap.wbmp
wml = text/vnd.wap.wml
wmlc = application/vnd.wap.wmlc
wmls = text/vnd.wap.wmlscript
wmlsc = application/vnd.wap.wmlscriptc
wrl = model/vrml
wtls-ca-certificate = application/vnd.wap.wtls-ca-certificate
xbm = image/x-xbitmap
xht = application/xhtml+xml
xhtml = application/xhtml+xml
xls = application/vnd.ms-excel
xml = application/xml
xpm = image/x-xpixmap
xsl = application/xml
xslt = application/xslt+xml
xul = application/vnd.mozilla.xul+xml
xwd = image/x-xwindowdump
xyz = chemical/x-xyz
z = application/compress
zip = application/zip
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment