Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-users] embedded welcomefiles+http2-push+gzip-static-content+rest-api configuration

i have a dillema where my jetty configs led me to a certain configuration but the PushCacheFilter does not work on launch, it throws an NPE. 

i use a restapi that previously inheritted defaultservlet and served the static content itself, and this worked well for PushCacheFilter though i did not want to duplicate the welcomefiles functionality and so refactored to this now.

TIA

```kotlin

package foo.pkg

import org.eclipse.jetty.alpn.server.*
import org.eclipse.jetty.http2.*
import org.eclipse.jetty.http2.server.*
import org.eclipse.jetty.security.*
import org.eclipse.jetty.server.*
import org.eclipse.jetty.server.handler.*
import org.eclipse.jetty.server.handler.gzip.*
import org.eclipse.jetty.servlet.*
import org.eclipse.jetty.util.resource.*
import org.eclipse.jetty.util.security.*
import org.eclipse.jetty.util.security.Constraint.*
import org.eclipse.jetty.util.ssl.*


const val SSL_PORT = 8443

/**
*
*/
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(
ContextHandler().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()
}


}
```

Back to the top