-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfolder-web-server.camel
51 lines (42 loc) · 1.6 KB
/
folder-web-server.camel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
@Grab('com.github.yihtserns:camelscript:0.0.1')
@Grab('org.apache.camel:camel-jetty:2.4.0')
import org.codehaus.groovy.runtime.StackTraceUtils
import groovy.xml.MarkupBuilder
def currentFolder = new File('.')
disableJMX()
routes {
onException(Throwable).process {
def ex = it.getProperty('CamelExceptionCaught')
throw StackTraceUtils.deepSanitize(ex)
}
from ("jetty:http://0.0.0.0:8889?matchOnUriPrefix=true") {
process {
String relativeFilePath = it.in.getHeader('CamelHttpPath')
if ("/".equals(relativeFilePath)) {
relativeFilePath = "/index.html"
if (!new File(currentFolder, relativeFilePath).exists()) {
def sw = new StringWriter()
new MarkupBuilder(sw).html {
ul {
currentFolder.list().each { fileName ->
li {
a(href: fileName, fileName)
}
}
}
}
it.out.body = sw.toString()
return
}
}
File file = new File(currentFolder, relativeFilePath)
if (!file.exists() || file.isDirectory()) {
log.warn("Cannot resolve file: {}", file.canonicalPath)
throw new FileNotFoundException(relativeFilePath)
} else {
log.info("Resolved file: {}", file.canonicalPath)
it.out.body = file
}
}
}
}