I'm trying to create a simple undertow servlet-server, but I have some problems with undertow wanting to instantiate the servlet. I don't necessarily need to use HttpServlet
, I just need access to HttpServletRequest
and HttpServletResponse
so I can run them through myServicer
. What's the best way to achieve this?
My current code:
myServicer = ...
undertow = Undertow.builder()
.addHttpListener(port, host)
.setHandler(Handlers.path(Handlers.redirect("/")).addPrefixPath("/",
Servlets.defaultContainer().addDeployment(
Servlets.deployment()
.setClassLoader(EmbeddedUndertowServer::class.java.classLoader)
.setDeploymentName("myDeployment").setContextPath("/")
.addServlets(Servlets.servlet("myServlet",
object : HttpServlet() {
override fun service(request: HttpServletRequest, response: HttpServletResponse) {
myServicer.service(request, response) // doesn't work
}
}.javaClass).addMapping("/"))
).apply { deploy() }.start()
))
.build()
undertow.start()
This doesn't work because undertow just wants a class, which it tries to instantiate.
Full code/project here: https://github.com/tipsy/javalin/pull/25/files
The solution I got is to create a "stub" servlet into which you pass the servicer.
val servletBuilder = Servlets.deployment()
.setClassLoader(EmbeddedUndertowServer::class.java.getClassLoader())
.setContextPath("/")
.setDeploymentName("javalinDeployment")
.addServletContextAttribute("javalin-servlet", javalinServlet)
.addServlets(Servlets.servlet("javalinServlet", UndertowServlet::class.java).addMapping("/"))
val manager = Servlets.defaultContainer().addDeployment(servletBuilder)
manager.deploy()
val httpHandler = manager.start()
val path = Handlers.path(Handlers.redirect("/")).addPrefixPath("/", httpHandler)
this.undertow = Undertow.builder().addHttpListener(port, host).setHandler(path).build()
undertow.start()
The servicer can then be loaded during each servlet initialisation phase:
private var javalinServlet: JavalinServlet? = null
@Throws(ServletException::class)
override fun init(config: ServletConfig) {
this.config = config
javalinServlet = config.servletContext.getAttribute("javalin-servlet") as JavalinServlet
}
You can view the changes here: https://github.com/osmundf/javalin-undertow/commit/30487196f2dd7a44d3ef524f642040a7330caf4e