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;
}
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.");
}
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);
}
}
Aggregations