Upgrade to Jetty 8. Fixed multiple path issue.

This commit is contained in:
Jonathan Bernard 2016-01-18 23:11:38 -06:00
parent 6ebe75402a
commit a90f10f7a0
2 changed files with 39 additions and 26 deletions

View File

@ -3,7 +3,7 @@ apply plugin: "application"
apply plugin: "maven"
group = "com.jdbernard"
version = "1.2"
version = "2.0"
mainClassName = "com.jdbernard.net.GroovyDirectoryServer"
@ -12,11 +12,9 @@ repositories {
mavenCentral() }
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.6'
compile localGroovy()
compile 'org.slf4j:slf4j-api:1.7.10'
compile 'ch.qos.logback:logback-core:1.1.2'
compile 'ch.qos.logback:logback-classic:1.1.2'
compile 'javax.servlet:servlet-api:2.4'
compile 'javax.servlet.jsp:jsp-api:2.1'
compile 'org.eclipse.jetty.aggregate:jetty-all:7.6.15.v20140411'
compile 'org.eclipse.jetty.aggregate:jetty-all:8.1.18.v20150929'
compile 'com.jdbernard:jdb-util:4.2' }

View File

@ -4,17 +4,18 @@ import org.eclipse.jetty.rewrite.handler.RewriteHandler
import org.eclipse.jetty.rewrite.handler.RewriteRegexRule
import org.eclipse.jetty.servlet.DefaultServlet
import org.eclipse.jetty.servlet.ServletContextHandler
import org.eclipse.jetty.servlet.ServletHolder
import org.eclipse.jetty.server.Server
import groovy.servlet.GroovyServlet
import com.jdbernard.util.LightOptionParser
public class GroovyDirectoryServer {
public static final String VERSION = "1.2"
public static final String VERSION = "2.0"
public static void main(String[] args) {
def port = 9000
def cli = [
v: [longName: 'version'],
h: [longName: 'help'],
@ -33,43 +34,57 @@ public class GroovyDirectoryServer {
mappedPaths: opts.P ?: [['/', '.']],
rewriteRules: opts.r ?: [],
groovyPatterns: opts.g ?: ["*.groovy"] ]
runJetty(options).join()
}
public static Server runJetty(options) {
def server = new Server(options.port)
def handler = new ServletContextHandler(ServletContextHandler.SESSIONS)
handler.contextPath = '/'
handler.resourceBase = '.'
// Maped paths.
println options.mappedPaths
options.mappedPaths.each { pair ->
def pathHandler = handler.addServlet(DefaultServlet, pair[0])
pathHandler.setInitParameter('resourceBase', pair[1])
println "Serving '${pair[1]}' from base url '${pair[0]}'." }
def rewriteHandler = null
def context = new ServletContextHandler(ServletContextHandler.SESSIONS)
context.contextPath = '/'
// Groovy Scripts
options.groovyPatterns.each { pattern ->
handler.addServlet(GroovyServlet, pattern)
options.groovyPatterns.eachWithIndex { pattern, idx ->
def servletHolder = new ServletHolder("groovy-$idx", GroovyServlet)
context.addServlet(servletHolder, pattern)
println "Using GroovyServlet for urls matching '$pattern'." }
// Mapped paths.
def rootPair = options.mappedPaths.find { pair -> pair[0] == '/' }
if (rootPair) {
options.mappedPaths -= [rootPair]
context.resourceBase = rootPair[1] }
else {
println "No root path specified: defaulting to the current directory."
context.resourceBase = '.' }
options.mappedPaths.eachWithIndex { pair, idx ->
def servletHolder = new ServletHolder("static-$idx", DefaultServlet)
servletHolder.setInitParameter('resourceBase', pair[1])
servletHolder.setInitParameter('dirAllowed', 'true')
servletHolder.setInitParameter('pathInfoOnly', 'true')
context.addServlet(servletHolder, pair[0])
println "Serving '${pair[1]}' from base url '${pair[0]}'." }
def rootServletHolder = new ServletHolder('default', DefaultServlet)
rootServletHolder.setInitParameter('dirAllowed', 'true')
context.addServlet(rootServletHolder, '/')
// Rewrite rules
if (options.rewriteRules) {
def rewriteHandler = new RewriteHandler()
rewriteHandler.setHandler(handler)
handler = rewriteHandler }
rewriteHandler = new RewriteHandler()
rewriteHandler.setHandler(context) }
options.rewriteRules.each { pair ->
def rule = new RewriteRegexRule()
rule.regex = pair[0]
rule.replacement = pair[1]
handler.addRule(rule)
rewriteHandler.addRule(rule)
println "Rewriting '${pair[0]}' to '${pair[1]}'." }
server.handler = handler
server.handler = rewriteHandler ?: context
server.start()
println "Jetty started on port ${options.port}."