class JettyStarter(
/**
*
*/
private vararg val args: String) : AutoCloseable {
private val server: Server =
Server().apply {
val httpFactory = HttpConnectionFactory(HttpConfiguration().apply {
addCustomizer(SecureRequestCustomizer())
})
val http2Factory = HTTP2ServerConnectionFactory(HttpConfiguration().apply {
secureScheme = "https"
securePort = SSL_PORT
sendXPoweredBy = false
sendServerVersion = false
addCustomizer(SecureRequestCustomizer())
})
val alpn1 = ALPNServerConnectionFactory().apply {
defaultProtocol = httpFactory.protocol
}
requestLog = AsyncNCSARequestLog()
// HTTP Configuration
connectors = arrayOf(
//Create a connector on port 80 to listen for HTTP requests (that will get redirected)... fail
ServerConnector(this, httpFactory).apply {
setPort(8080)
},
ServerConnector(this, SslConnectionFactory(SslContextFactory().apply {
keyStorePath = JettyStarter::class.java.getResource("/keystore").toExternalForm()
setKeyStorePassword("changeit")
cipherComparator = HTTP2Cipher.COMPARATOR
isUseCipherSuitesOrder = true
}, alpn1.protocol),
alpn1,
http2Factory,
httpFactory
).apply { port = SSL_PORT })
//setup the constraint that causes all http requests to return a !403 error
handler = ConstraintSecurityHandler().apply {
//makes the constraint apply to all uri paths
addConstraintMapping(ConstraintMapping().apply {
pathSpec = "/*"
constraint = Constraint().apply { dataConstraint = DC_CONFIDENTIAL }
})
handler = GzipHandler().apply {
handler = ContextHandlerCollection(
org.eclipse.jetty.servlet.ServletContextHandler().apply {
contextPath = "/"
handler = ResourceHandler().apply {
welcomeFiles = arrayOf("index.html")
baseResource = Resource.newResource(
args.firstOrNull() ?: AdapterServlet.resourceBase.toString())
}
servletContext.addFilter("push", PushCacheFilter::class.java).apply {
initParameters = mapOf("maxAssociations" to "32",
"ports" to Objects.toString(SSL_PORT )
)
}
//TODO: repair http2 PUSH
// where do I slip this in?
},
ServletContextHandler().apply {
addServlet(AdapterServlet::class.java, "/*")
}
)
}
}
}
/**
*
*/
@Throws(Exception::class)
fun start() {
server.start()
}
/**
*
*/
@Throws(Exception::class)
override fun close() {
server.stop()
}
}