edu.vt.middleware.ldap.handler.RecursiveSearchResultHandler Maven / Gradle / Ivy
/*
$Id: RecursiveSearchResultHandler.java 1330 2010-05-23 22:10:53Z dfisher $
Copyright (C) 2003-2010 Virginia Tech.
All rights reserved.
SEE LICENSE FOR MORE INFORMATION
Author: Middleware Services
Email: [email protected]
Version: $Revision: 1330 $
Updated: $Date: 2010-05-23 18:10:53 -0400 (Sun, 23 May 2010) $
*/
package edu.vt.middleware.ldap.handler;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchResult;
import edu.vt.middleware.ldap.Ldap;
/**
* RecursiveSearchResultHandler
recursively searches based on a
* supplied attribute and merges those results into the original result set. For
* the following LDIF:
*
*
dn: uugid=group1,ou=groups,dc=vt,dc=edu
uugid: group1
member: uugid=group2,ou=groups,dc=vt,dc=edu
dn: uugid=group2,ou=groups,dc=vt,dc=edu
uugid: group2
*
*
* With the following code:
*
*
RecursiveSearchResultHandler rsh = new RecurseSearchResultHandler(
ldap, "member", new String[]{"uugid"});
*
*
* Will produce this result for the query (uugid=group1):
*
*
dn: uugid=group1,ou=groups,dc=vt,dc=edu
uugid: group1
uugid: group2
member: uugid=group2,ou=groups,dc=vt,dc=edu
*
*
* @author Middleware Services
* @version $Revision: 1330 $ $Date: 2010-05-23 18:10:53 -0400 (Sun, 23 May 2010) $
*/
public class RecursiveSearchResultHandler extends CopySearchResultHandler
implements ExtendedSearchResultHandler
{
/** Ldap to use for searching. */
private Ldap ldap;
/** Attribute to recursively search on. */
private String searchAttribute;
/** Attribute(s) to merge. */
private String[] mergeAttributes;
/** Attributes to return when searching, mergeAttributes + searchAttribute. */
private String[] retAttrs;
/** Default constructor. */
public RecursiveSearchResultHandler() {}
/**
* Creates a new RecursiveAttributeHandler
with the supplied
* search attribute and merge attributes.
*
* @param searchAttr String
* @param mergeAttrs String[]
*/
public RecursiveSearchResultHandler(
final String searchAttr,
final String[] mergeAttrs)
{
this(null, searchAttr, mergeAttrs);
}
/**
* Creates a new RecursiveAttributeHandler
with the supplied
* ldap, search attribute, and merge attributes.
*
* @param l Ldap
* @param searchAttr String
* @param mergeAttrs String[]
*/
public RecursiveSearchResultHandler(
final Ldap l,
final String searchAttr,
final String[] mergeAttrs)
{
this.ldap = l;
this.searchAttribute = searchAttr;
this.mergeAttributes = mergeAttrs;
this.initalizeReturnAttributes();
}
/** {@inheritDoc} */
public Ldap getSearchResultLdap()
{
return this.ldap;
}
/** {@inheritDoc} */
public void setSearchResultLdap(final Ldap l)
{
this.ldap = l;
}
/**
* Returns the attribute name that will be recursively searched on.
*
* @return String
attribute name
*/
public String getSearchAttribute()
{
return this.searchAttribute;
}
/**
* Sets the attribute name that will be recursively searched on.
*
* @param s String
*/
public void setSearchAttribute(final String s)
{
this.searchAttribute = s;
this.initalizeReturnAttributes();
}
/**
* Returns the attribute names that will be merged by the recursive search.
*
* @return String[]
attribute names
*/
public String[] getMergeAttributes()
{
return this.mergeAttributes;
}
/**
* Sets the attribute name that will be merged by the recursive search.
*
* @param s String[]
*/
public void setMergeAttributes(final String[] s)
{
this.mergeAttributes = s;
this.initalizeReturnAttributes();
}
/**
* Initializes the return attributes array. Must be called after both
* searchAttribute and mergeAttributes have been set.
*/
protected void initalizeReturnAttributes()
{
if (this.mergeAttributes != null && this.searchAttribute != null) {
// return attributes must include the search attribute
this.retAttrs = new String[this.mergeAttributes.length + 1];
System.arraycopy(
this.mergeAttributes,
0,
this.retAttrs,
0,
this.mergeAttributes.length);
this.retAttrs[this.retAttrs.length - 1] = this.searchAttribute;
}
}
/** {@inheritDoc} */
public List process(
final SearchCriteria sc,
final NamingEnumeration extends SearchResult> en,
final Class>[] ignore)
throws NamingException
{
return this.processInternal(super.process(sc, en, ignore));
}
/** {@inheritDoc} */
public List process(
final SearchCriteria sc,
final List extends SearchResult> l)
throws NamingException
{
return this.processInternal(super.process(sc, l));
}
/**
* Recursively searches a list of attributes and merges those results with the
* existing search result set.
*
* @param results List
of search results to merge with
*
* @return List
of merged search results
*
* @throws NamingException if an error occurs reading attribute values
*/
private List processInternal(final List results)
throws NamingException
{
for (SearchResult sr : results) {
final List searchedDns = new ArrayList();
if (sr.getAttributes().get(this.searchAttribute) != null) {
searchedDns.add(sr.getName());
this.readSearchAttribute(sr.getAttributes(), searchedDns);
} else {
this.recursiveSearch(sr.getName(), sr.getAttributes(), searchedDns);
}
}
return results;
}
/**
* Reads the values of {@link #searchAttribute} from the supplied attributes
* and calls {@link #recursiveSearch} for each.
*
* @param attrs to read
* @param searchedDns list of DNs whose attributes have been read
*
* @throws NamingException if a search error occurs
*/
private void readSearchAttribute(
final Attributes attrs,
final List searchedDns)
throws NamingException
{
if (attrs != null) {
final Attribute attr = attrs.get(this.searchAttribute);
if (attr != null) {
final NamingEnumeration> en = attr.getAll();
while (en.hasMore()) {
final Object rawValue = en.next();
if (rawValue instanceof String) {
this.recursiveSearch((String) rawValue, attrs, searchedDns);
}
}
}
}
}
/**
* Recursively gets the attribute(s) {@link #mergeAttributes} for the supplied
* dn and adds the values to the supplied attributes.
*
* @param dn to get attribute(s) for
* @param attrs to merge with
* @param searchedDns list of DNs that have been searched for
*
* @throws NamingException if a search error occurs
*/
private void recursiveSearch(
final String dn,
final Attributes attrs,
final List searchedDns)
throws NamingException
{
if (!searchedDns.contains(dn)) {
Attributes newAttrs = null;
try {
newAttrs = this.ldap.getAttributes(dn, this.retAttrs);
} catch (NamingException e) {
if (this.logger.isWarnEnabled()) {
this.logger.warn(
"Error retreiving attribute(s): " + Arrays.toString(this.retAttrs),
e);
}
}
searchedDns.add(dn);
if (newAttrs != null) {
// recursively search new attributes
this.readSearchAttribute(newAttrs, searchedDns);
// merge new attribute values
for (String s : this.mergeAttributes) {
final Attribute newAttr = newAttrs.get(s);
if (newAttr != null) {
final Attribute oldAttr = attrs.get(s);
if (oldAttr == null) {
attrs.put(newAttr);
} else {
final NamingEnumeration> newValues = newAttr.getAll();
while (newValues.hasMore()) {
oldAttr.add(newValues.next());
}
}
}
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy