How to create an Undertow Handler in JBoss EAP 7
Environment
Red Hat JBoss Enterprise Application Platform (EAP) 7
Issue
- How to create an Undertow Handler in JBoss EAP 7
- Tomcat/JBossWeb Valve is not supported in EAP 7 and Undertow Handler needs to be used instead of valve in EAP 7. How can we create an Undertow Handler?
Resolution
1. Create a custom handler that implements io.undertow.server.HttpHandler
package com.redhat.jboss.support.example;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import java.util.logging.Logger;
public class UndertowExampleHandler implements HttpHandler {
private static final Logger LOGGER = Logger.getLogger(UndertowExampleHandler.class.getName());
private static final String DEFAULT_MESSAGE = "Hello, World!";
private String message;
private HttpHandler next;
public UndertowExampleHandler(HttpHandler next) {
message = DEFAULT_MESSAGE;
this.next = next;
}
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
LOGGER.info("handleRequest() is called");
LOGGER.info("message = " + this.getMessage());
LOGGER.info("requestPath = " + exchange.getRequestPath());
next.handleRequest(exchange);
}
public String getMessage() {
return message;
}
public void setMessage(String str) {
LOGGER.info("setMessage() is called: str = " + str);
if (str != null && !str.isEmpty()) {
this.message = str;
}
}
}
2. Create a JBoss Module that contains the jar with the Handler class
Execute the following JBoss CLI commands below, which do the followings:
- Create a custom module named
examples.undertow.handlercontaining the jarundertow-example-handler.jarwhich has the ExampleHandler. - Define the
custom-filterin the undertow filters section. - Add the
filter-refto the undertow host which you want it to be applied.
# Install the module
# Depends on Java/Java EE API and Undertow Core API
module add --name=com.redhat.jboss.support.example.undertow.handler --slot=main --resources=jboss-undertow-example-handler.jar --dependencies=io.undertow.core,javaee.api,javax.api
# add the handler to undertow subsystem
/subsystem=undertow/configuration=filter/custom-filter=example-handler:add(class-name=com.redhat.jboss.support.example.UndertowExampleHandler, module=com.redhat.jboss.support.example.undertow.handler)
# (Optional) add a parameter to the handler
# /subsystem=undertow/configuration=filter/custom-filter=example-handler:write-attribute(name=parameters.message,value=test)
# add the filter ref to the host to enable it
/subsystem=undertow/server=default-server/host=default-host/filter-ref=example-handler:add()
The module add command above results in this $JBOSS_HOME/modules/com/redhat/jboss/support/example/undertow/handler/main/module.xml:
<?xml version="1.0" ?>
<module xmlns="urn:jboss:module:1.1" name="com.redhat.jboss.support.example.undertow.handler" slot="main">
<resources>
<resource-root path="jboss-undertow-example-handler.jar"/>
</resources>
<dependencies>
<module name="io.undertow.core"/>
<module name="javaee.api"/>
<module name="javax.api"/>
</dependencies>
</module>
The CLI commands add the fliter and filter-ref to the undertow subsystem such as:
<subsystem xmlns="urn:jboss:domain:undertow:3.1">
...
<server name="default-server">
...
<host name="default-host" alias="localhost">
...
<filter-ref name="example-handler"/>
</host>
</server>
...
<filters>
...
<filter name="example-handler" module="com.redhat.jboss.support.example.undertow.handler" class-name="com.redhat.jboss.support.example.UndertowExampleHandler">
<param name="message" value="test"/>
</filter>
</filters>
</subsystem>
Components
Category
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.