-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfolder-web-server.camel
More file actions
52 lines (43 loc) · 1.64 KB
/
folder-web-server.camel
File metadata and controls
52 lines (43 loc) · 1.64 KB
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
52
@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
import org.apache.camel.Exchange
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)
it.out.setHeader(Exchange.HTTP_RESPONSE_CODE, 404)
} else {
log.info("Resolved file: {}", file.canonicalPath)
it.out.body = file
}
}
}
}