Este contenido no está disponible en el idioma seleccionado.

4.6. Create a Custom Valve


A Valve is a Java class that gets inserted into the request processing pipeline for an application before the application's servlet filters. This can be used to modify the request or perform any other behavior. This task demonstrates the basic steps required for implementing a valve.

Procedure 4.3. Create a Custom Valve

  1. Configure the Maven dependencies.

    Add the following dependency configuration to the project pom.xml file.
    <dependency>
      <groupId>org.jboss.web</groupId>
      <artifactId>jbossweb</artifactId>
      <version>7.5.7.Final-redhat-1</version>
      <scope>provided</scope>
    </dependency>
    Copy to Clipboard Toggle word wrap

    Note

    The jbossweb-VERSION.jar file should not be included in the application. It is available to the JBoss EAP server runtime classpath as a JBoss module at this location: EAP_HOME/modules/system/layers/base/org/jboss/as/web/main/jbossweb-7.5.7.Final-redhat-1.jar.
  2. Create the Valve class

    Create a subclass of org.apache.catalina.valves.ValveBase.
    package org.jboss.samplevalves;
    
    import org.apache.catalina.valves.ValveBase;
    import org.apache.catalina.connector.Request;
    import org.apache.catalina.connector.Response;
    
    public class RestrictedUserAgentsValve extends ValveBase {
    
    }
    Copy to Clipboard Toggle word wrap
  3. Implement the invoke method.

    The invoke() method is called when this valve is executed in the pipeline. The request and response objects are passed as parameters. Perform any processing and modification of the request and response here.
    public void invoke(Request request, Response response)
    {
    
    }
    Copy to Clipboard Toggle word wrap
  4. Invoke the next pipeline step.

    The last thing the invoke method must do is invoke the next step of the pipeline and pass the modified request and response objects along. This is done using the getNext().invoke() method
    getNext().invoke(request, response);
    Copy to Clipboard Toggle word wrap
  5. Optional: Specify parameters.

    If the valve must be configurable, enable this by adding a parameter. Do this by adding an instance variable and a setter method for each parameter.
    private String restrictedUserAgents = null;
    
    public void setRestricteduserAgents(String mystring) 
    {
       this.restrictedUserAgents = mystring;
    }
    Copy to Clipboard Toggle word wrap
  6. Review the completed code example.

    The class should now look like the following example.

    Example 4.4. Sample Custom Valve

    package org.jboss.samplevalves;
    
    import java.io.IOException;
    import java.util.regex.Pattern;
    
    import javax.servlet.ServletException;
    import org.apache.catalina.valves.ValveBase;
    import org.apache.catalina.connector.Request;
    import org.apache.catalina.connector.Response;
    
    public class RestrictedUserAgentsValve extends ValveBase 
    {
        private String restrictedUserAgents = null;
    
        public void setRestrictedUserAgents(String mystring) 
        {
            this.restrictedUserAgents = mystring;
        }
    
        public void invoke(Request request, Response response) throws IOException, ServletException 
        {
          String agent = request.getHeader("User-Agent");
          System.out.println("user-agent: " + agent + " : " + restrictedUserAgents);
          if (Pattern.matches(restrictedUserAgents, agent)) 
          {
             System.out.println("user-agent: " + agent + " matches: " + restrictedUserAgents);
             response.addHeader("Connection", "close");
          }
          getNext().invoke(request, response);
        }
    }
    Copy to Clipboard Toggle word wrap
Volver arriba
Red Hat logoGithubredditYoutubeTwitter

Aprender

Pruebe, compre y venda

Comunidades

Acerca de la documentación de Red Hat

Ayudamos a los usuarios de Red Hat a innovar y alcanzar sus objetivos con nuestros productos y servicios con contenido en el que pueden confiar. Explore nuestras recientes actualizaciones.

Hacer que el código abierto sea más inclusivo

Red Hat se compromete a reemplazar el lenguaje problemático en nuestro código, documentación y propiedades web. Para más detalles, consulte el Blog de Red Hat.

Acerca de Red Hat

Ofrecemos soluciones reforzadas que facilitan a las empresas trabajar en plataformas y entornos, desde el centro de datos central hasta el perímetro de la red.

Theme

© 2025 Red Hat