javatomcatforms-authenticationtomcat-valve

Get the webapp name in the tomcat valve class


I have my custom tomcat Valve extending FormAuthenticator, and same is defined in context.xml of each webapp. I need to read some property files in 'initInternal' of Valve, based on webapp name. i.e. for each webapp different property file. Since Valve is common, I need a way to find out the context in which my Valve is loaded. Is there any way to get the context in Valve class.

I tried to set system property in 'ServletContextListener' but Valve class is loaded before the ServletContextListener is initialised.

Here is how my context.xml look.

<Context className="org.apache.catalina.core.StandardContext" debug="0" allowLinking="true" docBase="auth" path="/auth" privileged="true" reloadable="false">
  <Realm className="org.apache.catalina.realm.LockOutRealm" failureCount="5" lockOutTime="300" >
  <Realm className="com.foo.realm.FooRealm" /></Realm>
  <Valve className="com.foo.valves.FooRESTValve" />
</Context>

Only 'docbase' and 'path' is different for each webapp.


Solution

  • You can pass parameters to your valve in the context.xml:

     <Valve className="com.foo.valves.FooRESTValve" myParam="bar" />
    

    Tomcat will use the parameter name to try and set it's value on the valve implementation by calling it's JavaBean setter:

    public class FooRESTValve extends ValveBase {
    
        ...
    
        private String myParam;
    
        ...
    
        public void setMyParam(String myParam) {
            this.myParam = myParam;
        }
    
        ...
    
    
    }
    

    You could either configure all your properties like this, or just provide the name or path for your properties file.