use of org.apache.directory.fortress.core.util.cache.DsdCacheEntry in project directory-fortress-core by apache.
the class SDUtil method putDsdCache.
/**
* Get the matching DSD's from directory and add to the cache (if found). If matching DSD not found,
* add dummy entry to cache to prevent repeated searches.
*
* @param roleName of Role is used to search directory for matching DSD's.
* @param contextId maps to sub-tree in DIT, e.g. ou=contextId, dc=example, dc=com.
* @return Set of DSD's who have matching Role member.
* @throws SecurityException in the event of system or rule violation.
*/
private Set<SDSet> putDsdCache(String roleName, String contextId) throws SecurityException {
contextId = getContextId(contextId);
Role role = new Role(roleName);
role.setContextId(contextId);
List<SDSet> dsdList = sp.search(role, SDSet.SDType.DYNAMIC);
Set<SDSet> finalSet = new HashSet<>(dsdList);
if (CollectionUtils.isNotEmpty(dsdList)) {
for (SDSet dsd : dsdList) {
dsd.setContextId(contextId);
Set<String> members = dsd.getMembers();
if (members != null) {
// Seed the cache with DSD objects mapped to role name:
for (String member : members) {
String key = buildKey(dsd.getName(), member);
DsdCacheEntry entry = new DsdCacheEntry(member, dsd, false);
entry.setName(dsd.getName());
m_dsdCache.put(getKey(key, contextId), entry);
}
}
}
} else {
// Seed the cache with dummy entry for Role that does not have DSD:
String key = buildKey(EMPTY_ELEMENT, roleName);
SDSet sdSet = new SDSet();
sdSet.setType(SDSet.SDType.DYNAMIC);
sdSet.setName(key);
sdSet.setMember(roleName);
sdSet.setContextId(contextId);
DsdCacheEntry entry = new DsdCacheEntry(roleName, sdSet, true);
entry.setName(key);
m_dsdCache.put(getKey(sdSet.getName(), contextId), entry);
}
return finalSet;
}
use of org.apache.directory.fortress.core.util.cache.DsdCacheEntry in project directory-fortress-core by apache.
the class SDUtil method getDsdCache.
/**
* Given a Set of authorized Roles, return the set of DSD's that have matching members.
*
* @param authorizedRoleSet contains an un-order Set of authorized Roles.
* @param contextId maps to sub-tree in DIT, e.g. ou=contextId, dc=example, dc=com.
* @return un-ordered set of matching DSD's.
* @throws SecurityException in the event of system or rule violation.
*/
Set<SDSet> getDsdCache(Set<String> authorizedRoleSet, String contextId) throws SecurityException {
contextId = getContextId(contextId);
Set<SDSet> dsdRetSets = new HashSet<>();
// Need to proceed?
if (!CollectionUtils.isNotEmpty(authorizedRoleSet)) {
return dsdRetSets;
}
// Was the DSD Cache switched off?
boolean isCacheDisabled = Config.getInstance().getBoolean(IS_DSD_CACHE_DISABLED_PARM, false);
// If so, get DSD's from LDAP:
if (isCacheDisabled) {
SDSet sdSet = new SDSet();
sdSet.setType(SDSet.SDType.DYNAMIC);
sdSet.setContextId(contextId);
dsdRetSets = sp.search(authorizedRoleSet, sdSet);
} else // Search the DSD cache for matching Role members:
{
// Search on roleName attribute which maps to 'member' attr on the cache record:
Attribute<String> member = m_dsdCache.getSearchAttribute(SchemaConstants.MEMBER_AT);
Attribute<String> context = m_dsdCache.getSearchAttribute(CONTEXT_ID);
Query query = m_dsdCache.createQuery();
query.includeKeys();
query.includeValues();
// Add the passed in authorized Role names to this cache query:
Set<String> roles = new HashSet<>(authorizedRoleSet);
query.addCriteria(member.in(roles).and(context.eq(contextId)));
// Return all DSD cache entries that match roleName to the 'member' attribute in cache entry:
Results results = query.execute();
for (Result result : results.all()) {
DsdCacheEntry entry = (DsdCacheEntry) result.getValue();
// Do not add dummy DSD sets to the final list:
if (!entry.isEmpty()) {
dsdRetSets.add(entry.getSdSet());
}
// Remove role member from authorizedRoleSet to preclude from upcoming DSD search:
// authorizedRoleSet.remove(entry.getMember());
}
// Authorized roles remaining in this set correspond to missed cache hits from above:
if (authorizedRoleSet.size() > 0) {
dsdRetSets = putDsdCache(authorizedRoleSet, contextId);
}
}
return dsdRetSets;
}
use of org.apache.directory.fortress.core.util.cache.DsdCacheEntry in project directory-fortress-core by apache.
the class SDUtil method putDsdCache.
/**
* Get the matching DSD's from directory and add to the cache (if found). If matching DSD not found,
* add dummy entry to cache to prevent repeated searches.
*
* @param authorizedRoleSet contains set of Roles used to search directory for matching DSD's.
* @param contextId maps to sub-tree in DIT, e.g. ou=contextId, dc=example, dc=com.
* @return List of DSD's who have matching Role members.
* @throws SecurityException in the event of system or rule violation.
*/
private Set<SDSet> putDsdCache(Set<String> authorizedRoleSet, String contextId) throws SecurityException {
contextId = getContextId(contextId);
Set<SDSet> dsdSets = new HashSet<>();
// Search the DSD's iteratively to seed the DSD cache by Role name:
for (String roleName : authorizedRoleSet) {
Role role = new Role(roleName);
role.setContextId(contextId);
List<SDSet> dsdList = sp.search(role, SDSet.SDType.DYNAMIC);
if (CollectionUtils.isNotEmpty(dsdList)) {
for (SDSet dsd : dsdList) {
dsd.setContextId(contextId);
Set<String> members = dsd.getMembers();
if (members != null) {
// Seed the cache with DSD objects mapped to role name:
for (String member : members) {
String key = buildKey(dsd.getName(), member);
DsdCacheEntry entry = new DsdCacheEntry(member, dsd, false);
entry.setName(dsd.getName());
m_dsdCache.put(getKey(key, contextId), entry);
}
}
}
// Maintain the set of DSD's to be returned to the caller:
dsdSets.addAll(dsdList);
} else {
// Seed the cache with dummy entry for a Role that is not referenced by DSD:
String key = buildKey(EMPTY_ELEMENT, roleName);
SDSet sdSet = new SDSet();
sdSet.setType(SDSet.SDType.DYNAMIC);
sdSet.setName(key);
sdSet.setMember(roleName);
sdSet.setContextId(contextId);
DsdCacheEntry entry = new DsdCacheEntry(roleName, sdSet, true);
entry.setName(key);
m_dsdCache.put(getKey(sdSet.getName(), contextId), entry);
}
}
return dsdSets;
}
use of org.apache.directory.fortress.core.util.cache.DsdCacheEntry in project directory-fortress-core by apache.
the class SDUtil method getDsdCache.
/**
* Given a role name, return the set of DSD's that have a matching member.
*
* @param name contains name of authorized Role used to search the cache.
* @param contextId maps to sub-tree in DIT, e.g. ou=contextId, dc=example, dc=com.
* @return un-ordered set of matching DSD's.
* @throws SecurityException in the event of system or rule violation.
*/
private Set<SDSet> getDsdCache(String name, String contextId) throws SecurityException {
contextId = getContextId(contextId);
Set<SDSet> finalSet = new HashSet<>();
Attribute<String> context = m_dsdCache.getSearchAttribute(CONTEXT_ID);
Attribute<String> member = m_dsdCache.getSearchAttribute(SchemaConstants.MEMBER_AT);
Query query = m_dsdCache.createQuery();
query.includeKeys();
query.includeValues();
query.addCriteria(member.eq(name).and(context.eq(contextId)));
Results results = query.execute();
boolean empty = false;
for (Result result : results.all()) {
DsdCacheEntry entry = (DsdCacheEntry) result.getValue();
if (!entry.isEmpty()) {
finalSet.add(entry.getSdSet());
finalSet = putDsdCache(name, contextId);
} else {
empty = true;
}
finalSet.add(entry.getSdSet());
}
// If nothing was found in the cache, determine if it needs to be seeded:
if (finalSet.size() == 0 && !empty) {
finalSet = putDsdCache(name, contextId);
}
return finalSet;
}
Aggregations