0

I am constructing a Simple Java Filter that will be eventually configured in a different WEB Application. That WEB Application will configure this filter within web.xml and provide an init-param whose value is a path of a properties file.

The custom filter is suppose to read this properties file. I am unable to read this properties file though.

Here is the code for CustomFilter. This is maven installed into a jar file.

public class CustomFilter implements Filter{
    ...
    // invoked from init()
 Properties loadProperties(FilterConfig filterConfig) {
  
  String initParamName = "someParamName";
  String pathToProperties = filterConfig
    .getInitParameter(initParamName);
  
  LOG.info("Path to Properties File is:" + pathToProperties);
  InputStream input = null;
  Properties properties = new Properties();
  try {
   // Read the Properties file a Stream
   /*input = Thread.currentThread().getContextClassLoader()
     .getResourceAsStream(pathToProperties);*/ // input is null
   
   /*input = this.getClass()
     .getResourceAsStream(pathToProperties);*/   // input is null

   ServletContext servletContext = filterConfig.getServletContext();
   
   LOG.info("servletContext.getContextPath(): " + servletContext.getContextPath());
   
            //LOG.info("servletContext.getContextPath(): " + servletContext.get;
   //input = servletContext.getResourceAsStream(pathToProperties); //input is null
   
   input = servletContext.getClass().getClassLoader().getResourceAsStream(pathToProperties);
//input is null
   
   
   LOG.info("+++++++++++++++++++++++++==" + input);
   // Load Stream to Properties, key=value in properties file gets
   // converted to [key:value] prop
   properties.load(input);

  } catch (IOException e) {
   ...
  }
  
  
  return null;
 }




}

The jar file created above is included within a REST Service Project via Maven POM. The web.xml Configuration of the filter within the REST Service is

 <filter>
  <filter-name>customFilter</filter-name>
  <filter-class>com.abc.xyz.CustomFilter</filter-class>
  <init-param>
   <param-name>someParamName</param-name>
   <param-value>hello.properties</param-value>
  </init-param>
 </filter>
 <filter-mapping>
  <filter-name>customFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

The properties file hello.properties is kept within REST Service project, in the same location wherein the web.xml resides.

Kindly note: When the same properties file is kept within the first jar, in the package where the CustomFilter is created, the code works flawlessly. However, this is not what we intend to do.

Also, I cannot use SPRING Based Solution for reading properties file. Only Java Based.

DolphinJava
  • 2,342
  • 1
  • 18
  • 34

1 Answers1

1

Your want to load a resource in folder /WEB-INF (you say it resides in the same location as web.xml).

ServletContext.getResourceAsStream is the way to go but you need to prefix the resource name with /WEB-INF/:

String pathToProperties = "/WEB-INF/" + filterConfig.getInitParameter("someParamName");
InputStream input = filterConfig.getServletContext().getResourceAsStream(pathToProperties);
wero
  • 30,527
  • 3
  • 46
  • 72