How to install Tomcat Web Valves on JBoss

Important notice: this tutorial has been written for JBoss EAP 6 and JBoss AS 7. Apache Valves are not available anymore in WildFly application server. You can replace them with Undertow Handlers. Check this tutorial for more information: Converting Tomcat Valves to Undertow Handlers

This tutorial shows how to create a Tomcat Valves and install them on the top of JBoss EAP 6 / Jboss AS 7.

A Valve element represents a component that will be inserted into the request processing pipeline for the associated Catalina container (Engine, Host, or Context). There are several valves available and each one has distinct processing capabilities.

The official source of information about Tomcat Valves is Tomcat documentation

We will start by creating a Valve that will work as a global valve in JBoss 7.2 (EAP 6.1) in the web subsystem.

Create a Valve Class

A Valve Class extends org.apache.catalina.valves.ValveBase and has access to both the HTTP Request and the HTTP Response:

package example;

import; import; import org.apache.catalina.connector.Request; import org.apache.catalina.connector.Response; import org.apache.catalina.util.RequestUtil; import org.apache.catalina.valves.ValveBase; import javax.servlet.http.HttpServletRequest; import javax.servlet.ServletRequest; import javax.servlet.ServletException; public class ExampleGlobalValve extends ValveBase { public void invoke(Request request, Response response) throws IOException, ServletException { System.out.println("Example Global Valve invoked!"); getNext().invoke(request, response); } }

Then, compile and package the Class in a JAR file. From the CLI install it as a module:

module add --resources=myGlobalValve.jar --dependencies=javax.servlet.api,sun.jdk,

The following module.xml will be created for the module:

<?xml version="1.0" ?>
<module xmlns="urn:jboss:module:1.1" name="">
        <resource-root path="myGlobalValve.jar"/>
        <module name="javax.servlet.api"/>
        <module name="sun.jdk"/>
        <module name=""/>

Finally, install it on the web subsystem as follows:


Installing the Valve at application level

If you need to install your Valve on application basis, then you need to provide this information in your jboss-web.xml file. Here is an example:




Using built-in Tomcat Valves in your application

Finally, if you want to use Tomcat built-in valves in your application, check that they are available in your JBoss installation. For example, supposing you want to install the AccessLogValve

[modules]$ grep -r "AccessLogValve"
Binary file system/layers/base/org/jboss/as/web/main/jbossweb-7.5.7.Final-redhat-1.jar matches

As you can see, the AccessLogValve is bundled in the jbossweb JAR file. You can use it by configure jboss-web.xml as follows: