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

View File

@ -4,13 +4,14 @@ import org.eclipse.jetty.rewrite.handler.RewriteHandler
import org.eclipse.jetty.rewrite.handler.RewriteRegexRule import org.eclipse.jetty.rewrite.handler.RewriteRegexRule
import org.eclipse.jetty.servlet.DefaultServlet import org.eclipse.jetty.servlet.DefaultServlet
import org.eclipse.jetty.servlet.ServletContextHandler import org.eclipse.jetty.servlet.ServletContextHandler
import org.eclipse.jetty.servlet.ServletHolder
import org.eclipse.jetty.server.Server import org.eclipse.jetty.server.Server
import groovy.servlet.GroovyServlet import groovy.servlet.GroovyServlet
import com.jdbernard.util.LightOptionParser import com.jdbernard.util.LightOptionParser
public class GroovyDirectoryServer { public class GroovyDirectoryServer {
public static final String VERSION = "1.2" public static final String VERSION = "2.0"
public static void main(String[] args) { public static void main(String[] args) {
def port = 9000 def port = 9000
@ -40,36 +41,50 @@ public class GroovyDirectoryServer {
public static Server runJetty(options) { public static Server runJetty(options) {
def server = new Server(options.port) def server = new Server(options.port)
def handler = new ServletContextHandler(ServletContextHandler.SESSIONS) def rewriteHandler = null
handler.contextPath = '/' def context = new ServletContextHandler(ServletContextHandler.SESSIONS)
handler.resourceBase = '.' context.contextPath = '/'
// 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]}'." }
// Groovy Scripts // Groovy Scripts
options.groovyPatterns.each { pattern -> options.groovyPatterns.eachWithIndex { pattern, idx ->
handler.addServlet(GroovyServlet, pattern) def servletHolder = new ServletHolder("groovy-$idx", GroovyServlet)
context.addServlet(servletHolder, pattern)
println "Using GroovyServlet for urls matching '$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 // Rewrite rules
if (options.rewriteRules) { if (options.rewriteRules) {
def rewriteHandler = new RewriteHandler() rewriteHandler = new RewriteHandler()
rewriteHandler.setHandler(handler) rewriteHandler.setHandler(context) }
handler = rewriteHandler }
options.rewriteRules.each { pair -> options.rewriteRules.each { pair ->
def rule = new RewriteRegexRule() def rule = new RewriteRegexRule()
rule.regex = pair[0] rule.regex = pair[0]
rule.replacement = pair[1] rule.replacement = pair[1]
handler.addRule(rule) rewriteHandler.addRule(rule)
println "Rewriting '${pair[0]}' to '${pair[1]}'." } println "Rewriting '${pair[0]}' to '${pair[1]}'." }
server.handler = handler server.handler = rewriteHandler ?: context
server.start() server.start()
println "Jetty started on port ${options.port}." println "Jetty started on port ${options.port}."