Search in sources :

Example 1 with ModuleFilterDefinition

use of org.openmrs.module.web.filter.ModuleFilterDefinition in project openmrs-core by openmrs.

the class WebModuleUtil method loadFilters.

/**
 * This method will initialize and store this module's filters
 *
 * @param module - The Module to load and register Filters
 * @param servletContext - The servletContext within which this method is called
 */
public static void loadFilters(Module module, ServletContext servletContext) {
    // Load Filters
    Map<String, Filter> filters = new HashMap<>();
    try {
        for (ModuleFilterDefinition def : ModuleFilterDefinition.retrieveFilterDefinitions(module)) {
            if (moduleFiltersByName.containsKey(def.getFilterName())) {
                throw new ModuleException("A filter with name <" + def.getFilterName() + "> has already been registered.");
            }
            ModuleFilterConfig config = ModuleFilterConfig.getInstance(def, servletContext);
            Filter f = (Filter) ModuleFactory.getModuleClassLoader(module).loadClass(def.getFilterClass()).newInstance();
            f.init(config);
            filters.put(def.getFilterName(), f);
        }
    } catch (ModuleException e) {
        throw e;
    } catch (Exception e) {
        throw new ModuleException("An error occurred initializing Filters for module: " + module.getModuleId(), e);
    }
    moduleFilters.put(module, filters.values());
    moduleFiltersByName.putAll(filters);
    log.debug("Module: " + module.getModuleId() + " successfully loaded " + filters.size() + " filters.");
    // Load Filter Mappings
    List<ModuleFilterMapping> modMappings = ModuleFilterMapping.retrieveFilterMappings(module);
    moduleFilterMappings.addAll(modMappings);
    log.debug("Module: " + module.getModuleId() + " successfully loaded " + modMappings.size() + " filter mappings.");
}
Also used : Filter(javax.servlet.Filter) HashMap(java.util.HashMap) ModuleFilterDefinition(org.openmrs.module.web.filter.ModuleFilterDefinition) ModuleFilterConfig(org.openmrs.module.web.filter.ModuleFilterConfig) ModuleException(org.openmrs.module.ModuleException) ServletException(javax.servlet.ServletException) ModuleException(org.openmrs.module.ModuleException) SchedulerException(org.openmrs.scheduler.SchedulerException) FileNotFoundException(java.io.FileNotFoundException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ModuleFilterMapping(org.openmrs.module.web.filter.ModuleFilterMapping)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Filter (javax.servlet.Filter)1 ServletException (javax.servlet.ServletException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 ModuleException (org.openmrs.module.ModuleException)1 ModuleFilterConfig (org.openmrs.module.web.filter.ModuleFilterConfig)1 ModuleFilterDefinition (org.openmrs.module.web.filter.ModuleFilterDefinition)1 ModuleFilterMapping (org.openmrs.module.web.filter.ModuleFilterMapping)1 SchedulerException (org.openmrs.scheduler.SchedulerException)1