Search in sources :

Example 1 with ModuleFilterMapping

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

the class WebModuleUtil method getFiltersForRequest.

/**
 * Return List of Filters that have been loaded through Modules that have mappings that pass for
 * the passed request
 *
 * @param request - The request to check for matching {@link Filter}s
 * @return List of all {@link Filter}s that have filter mappings that match the passed request
 */
public static List<Filter> getFiltersForRequest(ServletRequest request) {
    List<Filter> filters = new ArrayList<>();
    if (request != null) {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String requestPath = httpRequest.getRequestURI();
        if (requestPath != null) {
            if (requestPath.startsWith(httpRequest.getContextPath())) {
                requestPath = requestPath.substring(httpRequest.getContextPath().length());
            }
            for (ModuleFilterMapping filterMapping : WebModuleUtil.getFilterMappings()) {
                if (ModuleFilterMapping.filterMappingPasses(filterMapping, requestPath)) {
                    Filter passedFilter = moduleFiltersByName.get(filterMapping.getFilterName());
                    if (passedFilter != null) {
                        filters.add(passedFilter);
                    } else {
                        log.warn("Unable to retrieve filter that has a name of " + filterMapping.getFilterName() + " in filter mapping.");
                    }
                }
            }
        }
    }
    return filters;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Filter(javax.servlet.Filter) ArrayList(java.util.ArrayList) ModuleFilterMapping(org.openmrs.module.web.filter.ModuleFilterMapping)

Example 2 with ModuleFilterMapping

use of org.openmrs.module.web.filter.ModuleFilterMapping 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)

Example 3 with ModuleFilterMapping

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

the class WebModuleUtil method unloadFilters.

/**
 * This method will destroy and remove all filters that were registered by the passed
 * {@link Module}
 *
 * @param module - The Module for which you want to remove and destroy filters.
 */
public static void unloadFilters(Module module) {
    // Unload Filter Mappings
    for (Iterator<ModuleFilterMapping> mapIter = moduleFilterMappings.iterator(); mapIter.hasNext(); ) {
        ModuleFilterMapping mapping = mapIter.next();
        if (module.equals(mapping.getModule())) {
            mapIter.remove();
            log.debug("Removed ModuleFilterMapping: " + mapping);
        }
    }
    // unload Filters
    Collection<Filter> filters = moduleFilters.get(module);
    if (filters != null) {
        try {
            for (Filter f : filters) {
                f.destroy();
            }
        } catch (Exception e) {
            log.warn("An error occurred while trying to destroy and remove module Filter.", e);
        }
        log.debug("Module: " + module.getModuleId() + " successfully unloaded " + filters.size() + " filters.");
        moduleFilters.remove(module);
        moduleFiltersByName.values().removeIf(filters::contains);
    }
}
Also used : Filter(javax.servlet.Filter) ModuleFilterMapping(org.openmrs.module.web.filter.ModuleFilterMapping) 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)

Aggregations

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