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: "application"
|
||||
apply plugin: "maven"
|
||||
|
||||
group = "com.jdbernard"
|
||||
version = "1.1"
|
||||
version = "1.2"
|
||||
|
||||
mainClassName = "com.jdbernard.net.GroovyDirectoryServer"
|
||||
|
||||
@ -17,4 +18,5 @@ dependencies {
|
||||
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:7.6.15.v20140411'
|
||||
compile 'com.jdbernard:jdb-util:4.2' }
|
||||
|
@ -1,43 +1,105 @@
|
||||
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.servlet.*
|
||||
import groovy.servlet.*
|
||||
import groovy.servlet.GroovyServlet
|
||||
import com.jdbernard.util.LightOptionParser
|
||||
|
||||
public class GroovyDirectoryServer {
|
||||
|
||||
public static final String VERSION = "1.1"
|
||||
public static final String VERSION = "1.2"
|
||||
|
||||
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 }
|
||||
catch(Exception e) {
|
||||
println "Usage: GroovyDirectoryServer.grooy <port>"
|
||||
System.exit(1) }
|
||||
def opts = LightOptionParser.parseOptions(cli, args)
|
||||
|
||||
println "Starting Jetty on port $port, press Ctrl-C to stop."
|
||||
runJetty(port)
|
||||
if (opts.v) { println "GroovyDirectoryServer v$VERSION"; System.exit(1) }
|
||||
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) {
|
||||
def server = new Server(port)
|
||||
public static void runJetty(options) {
|
||||
def server = new Server(options.port)
|
||||
|
||||
def handler = new ServletContextHandler(ServletContextHandler.SESSIONS)
|
||||
handler.contextPath = '/'
|
||||
handler.resourceBase = '.'
|
||||
|
||||
// Groovy Scripts
|
||||
handler.addServlet(GroovyServlet, '*.groovy')
|
||||
// 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]}'." }
|
||||
|
||||
// Files
|
||||
def filesHolder = handler.addServlet(DefaultServlet, '/')
|
||||
filesHolder.setInitParameter('resourceBase', '.')
|
||||
// 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]}'." }
|
||||
|
||||
server.handler = handler
|
||||
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