Added rewrite functionality, multiple paths, groovy servlet patterns.
This commit is contained in:
parent
237997837b
commit
81b88344b3
@ -1,8 +1,9 @@
|
|||||||
apply plugin: "groovy"
|
apply plugin: "groovy"
|
||||||
apply plugin: "application"
|
apply plugin: "application"
|
||||||
|
apply plugin: "maven"
|
||||||
|
|
||||||
group = "com.jdbernard"
|
group = "com.jdbernard"
|
||||||
version = "1.1"
|
version = "1.2"
|
||||||
|
|
||||||
mainClassName = "com.jdbernard.net.GroovyDirectoryServer"
|
mainClassName = "com.jdbernard.net.GroovyDirectoryServer"
|
||||||
|
|
||||||
@ -17,4 +18,5 @@ dependencies {
|
|||||||
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 'javax.servlet:servlet-api:2.4'
|
||||||
compile 'javax.servlet.jsp:jsp-api:2.1'
|
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:7.6.15.v20140411'
|
||||||
|
compile 'com.jdbernard:jdb-util:4.2' }
|
||||||
|
@ -1,43 +1,105 @@
|
|||||||
package com.jdbernard.net
|
package com.jdbernard.net
|
||||||
|
|
||||||
|
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.server.Server
|
import org.eclipse.jetty.server.Server
|
||||||
import org.eclipse.jetty.servlet.*
|
import groovy.servlet.GroovyServlet
|
||||||
import groovy.servlet.*
|
import com.jdbernard.util.LightOptionParser
|
||||||
|
|
||||||
public class GroovyDirectoryServer {
|
public class GroovyDirectoryServer {
|
||||||
|
|
||||||
public static final String VERSION = "1.1"
|
public static final String VERSION = "1.2"
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
def port = 9002
|
def port = 9000
|
||||||
|
|
||||||
if (args.length < 1) { println "Defaulting to port 9002" }
|
def cli = [
|
||||||
|
v: [longName: 'version'],
|
||||||
|
h: [longName: 'help'],
|
||||||
|
p: [longName: 'port', arguments: 1],
|
||||||
|
P: [longName: 'map-path', arguments: 2],
|
||||||
|
r: [longName: 'rewrite', arguments: 2],
|
||||||
|
g: [longName: 'groovy-servlet', arguments: 1] ]
|
||||||
|
|
||||||
else try { port = args[0] as int }
|
def opts = LightOptionParser.parseOptions(cli, args)
|
||||||
catch(Exception e) {
|
|
||||||
println "Usage: GroovyDirectoryServer.grooy <port>"
|
|
||||||
System.exit(1) }
|
|
||||||
|
|
||||||
println "Starting Jetty on port $port, press Ctrl-C to stop."
|
if (opts.v) { println "GroovyDirectoryServer v$VERSION"; System.exit(1) }
|
||||||
runJetty(port)
|
if (opts.h) { println getUsage(); System.exit(1) }
|
||||||
|
|
||||||
|
def options = [
|
||||||
|
port: opts.p ? opts.p[0] as int : 9000,
|
||||||
|
mappedPaths: opts.P ?: [['/', '.']],
|
||||||
|
rewriteRules: opts.r ?: [],
|
||||||
|
groovyPatterns: opts.g ?: ["*.groovy"] ]
|
||||||
|
|
||||||
|
runJetty(options)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void runJetty(int port) {
|
public static void runJetty(options) {
|
||||||
def server = new Server(port)
|
def server = new Server(options.port)
|
||||||
|
|
||||||
def handler = new ServletContextHandler(ServletContextHandler.SESSIONS)
|
def handler = new ServletContextHandler(ServletContextHandler.SESSIONS)
|
||||||
handler.contextPath = '/'
|
handler.contextPath = '/'
|
||||||
handler.resourceBase = '.'
|
handler.resourceBase = '.'
|
||||||
|
|
||||||
// Groovy Scripts
|
// Maped paths.
|
||||||
handler.addServlet(GroovyServlet, '*.groovy')
|
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
|
||||||
|
options.groovyPatterns.each { pattern ->
|
||||||
|
handler.addServlet(GroovyServlet, pattern)
|
||||||
|
println "Using GroovyServlet for urls matching '$pattern'." }
|
||||||
|
|
||||||
|
// Rewrite rules
|
||||||
|
if (options.rewriteRules) {
|
||||||
|
def rewriteHandler = new RewriteHandler()
|
||||||
|
rewriteHandler.setHandler(handler)
|
||||||
|
handler = rewriteHandler }
|
||||||
|
|
||||||
|
options.rewriteRules.each { pair ->
|
||||||
|
def rule = new RewriteRegexRule()
|
||||||
|
rule.regex = pair[0]
|
||||||
|
rule.replacement = pair[1]
|
||||||
|
handler.addRule(rule)
|
||||||
|
println "Rewriting '${pair[0]}' to '${pair[1]}'." }
|
||||||
|
|
||||||
// Files
|
|
||||||
def filesHolder = handler.addServlet(DefaultServlet, '/')
|
|
||||||
filesHolder.setInitParameter('resourceBase', '.')
|
|
||||||
|
|
||||||
server.handler = handler
|
server.handler = handler
|
||||||
server.start()
|
server.start()
|
||||||
|
|
||||||
|
println "Jetty started on port ${options.port}."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getUsage() {
|
||||||
|
return """\
|
||||||
|
GroovyDirectoryServer v$VERSION
|
||||||
|
usage: GroovyDirectoryServer [options]
|
||||||
|
|
||||||
|
Options:
|
||||||
|
|
||||||
|
-h,--help Print this usage information.
|
||||||
|
|
||||||
|
-v, --version Print versioning information.
|
||||||
|
|
||||||
|
-p, --port <port> Listen on the given port. Defaults to 9000.
|
||||||
|
|
||||||
|
-P <url-path> <dir-path>, --map-path <url-path> <dir-path>
|
||||||
|
|
||||||
|
Map the contents of the filesystem at <dir-path> to the url prefix at
|
||||||
|
<url-path>.
|
||||||
|
|
||||||
|
-r <url-pattern> <replacement>, --rewrite <url-pattern> <replacement>
|
||||||
|
|
||||||
|
Rewrite URLs matching <url-pattern> to <replacement>.
|
||||||
|
|
||||||
|
-g <url=pattern>, --groovy-servlet
|
||||||
|
|
||||||
|
Invoke the GroovyServet for any url matching <url-pattern>
|
||||||
|
""";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user