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 http://tomcat.apache.org/tomcat-7.0-doc/config/valve.html
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 java.io.IOException; import java.io.Writer; 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 --name=my.global.valve --resources=myGlobalValve.jar --dependencies=javax.servlet.api,sun.jdk,org.jboss.as.web
The following module.xml will be created for the my.global.valve module:
<?xml version="1.0" ?> <module xmlns="urn:jboss:module:1.1" name="my.global.valve"> <resources> <resource-root path="myGlobalValve.jar"/> </resources> <dependencies> <module name="javax.servlet.api"/> <module name="sun.jdk"/> <module name="org.jboss.as.web"/> </dependencies> </module>
Finally, install it on the web subsystem as follows:
/subsystem=web/valve=MyDemoValve/:add(module=my.global.valve,class-name=example.MyGlobalValve)
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:
<jboss-web> <valve> <class-name>example.MyCustomValve</class-name> </valve> </jboss-web>
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:
<jboss-web> <valve> <class-name>org.apache.catalina.valves.AccessLogValve</class-name> <param> <param-name>prefix</param-name> <param-value>myapp_access_log.</param-value> </param> <param> <param-name>suffix</param-name> <param-value>.log</param-value> </param> <param> <param-name>fileDateFormat</param-name> <param-value>yyyy-MM-dd</param-value> </param> <param> <param-name>pattern</param-name> <param-value>common</param-value> </param> <param> <param-name>directory</param-name> <param-value>${jboss.server.log.dir}</param-value> </param> <param> <param-name>resolveHosts</param-name> <param-value>false</param-value> </param> </valve> </jboss-web>