use of org.apereo.cas.util.scripting.ExecutableCompiledGroovyScript in project cas by apereo.
the class LdapUtils method newLdaptiveSearchFilter.
/**
* New ldaptive search filter search filter.
*
* @param filterQuery the filter query
* @param paramName the param name
* @param values the params
* @return the search filter
*/
public static FilterTemplate newLdaptiveSearchFilter(final String filterQuery, final List<String> paramName, final List<String> values) {
val filter = new FilterTemplate();
if (ResourceUtils.doesResourceExist(filterQuery)) {
ApplicationContextProvider.getScriptResourceCacheManager().ifPresentOrElse(cacheMgr -> {
val cacheKey = ScriptResourceCacheManager.computeKey(filterQuery);
var script = (ExecutableCompiledGroovyScript) null;
if (cacheMgr.containsKey(cacheKey)) {
script = cacheMgr.get(cacheKey);
LOGGER.trace("Located cached groovy script [{}] for key [{}]", script, cacheKey);
} else {
val resource = Unchecked.supplier(() -> ResourceUtils.getRawResourceFrom(filterQuery)).get();
LOGGER.trace("Groovy script [{}] for key [{}] is not cached", resource, cacheKey);
script = new WatchableGroovyScriptResource(resource);
cacheMgr.put(cacheKey, script);
LOGGER.trace("Cached groovy script [{}] for key [{}]", script, cacheKey);
}
if (script != null) {
val parameters = new LinkedHashMap<String, String>();
IntStream.range(0, values.size()).forEachOrdered(i -> parameters.put(paramName.get(i), values.get(i)));
val args = CollectionUtils.<String, Object>wrap("filter", filter, "parameters", parameters, "applicationContext", ApplicationContextProvider.getApplicationContext(), "logger", LOGGER);
script.setBinding(args);
script.execute(args.values().toArray(), FilterTemplate.class);
}
}, () -> {
throw new RuntimeException("Script cache manager unavailable to handle LDAP filter");
});
} else {
filter.setFilter(filterQuery);
if (values != null) {
IntStream.range(0, values.size()).forEach(i -> {
val value = values.get(i);
if (filter.getFilter().contains("{" + i + '}')) {
filter.setParameter(i, value);
}
val name = paramName.get(i);
if (filter.getFilter().contains('{' + name + '}')) {
filter.setParameter(name, value);
}
});
}
}
LOGGER.debug("Constructed LDAP search filter [{}]", filter.format());
return filter;
}
Aggregations