Skip to content

Instantly share code, notes, and snippets.

@seanparsons
Created March 28, 2011 21:04
Show Gist options
  • Save seanparsons/891282 to your computer and use it in GitHub Desktop.
Save seanparsons/891282 to your computer and use it in GitHub Desktop.
import org.apache.camel.Processor
import org.apache.camel.Exchange
import org.apache.camel.builder.RouteBuilder
import org.apache.camel.impl.DefaultCamelContext
import org.apache.commons.io.FileUtils
import java.net.URL
@Grab(group="org.apache.camel",module="camel-jetty",version="2.5.0")
@Grab(group="commons-io", module="commons-io",version="2.0")
class WebRouteBuilder extends RouteBuilder {
private File rootDir
private String webRoot
public WebRouteBuilder(rootDir, webRoot) {
this.rootDir = rootDir
this.webRoot = webRoot
}
void configure() {
from("jetty:" + webRoot + "?matchOnUriPrefix=true")
.process(new Processor() {
def void process(Exchange exchange) {
def url = new URL(exchange.in.getHeader("CamelHttpUrl").toString())
println(url)
def file = new File(rootDir, "." + url.path)
if (file.exists()) {
exchange.out.body = FileUtils.readFileToString(file)
} else {
exchange.out.setHeader(Exchange.HTTP_RESPONSE_CODE, 404)
}
}
})
.end()
}
}
def camelContext = new DefaultCamelContext()
def rootDir = new File(".")
camelContext.addRoutes(new WebRouteBuilder(rootDir, "http://0.0.0.0:8123/"))
println("Starting Camel")
camelContext.start()
Runtime.runtime.addShutdownHook(new Thread()
{
void run()
{
println("Stopping Camel")
camelContext.stop()
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment