ldapSearch
Purpose
This method, when given a firstName, lastName, and userId, will search for and return user matches in place of a normal LDAP search.
Applies To
LDAP
Signature
public TreeMap ldapSearch(String firstName,
String lastName,
String userId,
boolean exactMatch,
String sortBy)
Notes
The exactMatch specifies if the match is exact, the sortBy can be by “last”, “first”, “userId” or “” signifying how the returned TreeMap should be sorted. The values in the returned TreeMap must be HashMap’s with the name/value pairs used by ldap. The names can be retrived from the Configuration.properties. The names may include wildcards (*), and this method is only called if ldapOverrive() returns true.
Example
public TreeMap ldapSearch (String firstName,
String lastName,
String userId,
boolean exactMatch,
String sortBy)
{
TreeMap sortedResults = new TreeMap();
boolean extraFilter = false;
String extraFilterString = "";
ArrayList searchAttrs = new ArrayList();
ArrayList searchVals = new ArrayList();
if (TextManager.isStringVisible(firstName)) {
searchAttrs.add(Z.lu.LDAP_GIVENNAME);
searchVals.add(firstName);
}
if (TextManager.isStringVisible(lastName)) {
searchAttrs.add(Z.lu.LDAP_SURNAME);
searchVals.add(lastName);
}
if (TextManager.isStringVisible(userId)) {
searchAttrs.add(Z.lu.LDAP_PRIMARYKEY);
searchVals.add(userId);
}
if (searchAttrs.size() == 0) {
// We should never fall into this else but if we do here is an alert
Z.log.writeToLog(Z.log.ERROR, "UC: ldapSearch: " +
"Cannot seach LDAP with null information");
return sortedResults;
}
extraFilterString = Z.config.getConfigValue("LDAP_SEARCH_FILTER");
if (TextManager.isStringVisible(extraFilterString)) {
extraFilterString.trim();
extraFilter = true;
}
// Setting up the filter
StringBuffer filter = new StringBuffer();
if (searchAttrs.size() == 1 && ! extraFilter)
{
// a = b
filter.append("(");
filter.append((String)searchAttrs.get(0));
filter.append("=");
filter.append((String)searchVals.get(0));
if (! exactMatch) {
filter.append("*");
}
filter.append(")");
}
else
{
// (&(a=b)(c=d)(e=f))
filter.append("(&");
if (extraFilter) {
filter.append(extraFilterString);
}
for (int x = 0; x < searchAttrs.size(); x++)
{
filter.append("(");
filter.append((String)searchAttrs.get(x));
filter.append("=");
filter.append((String)searchVals.get(x));
if (!exactMatch) {
filter.append("*");
}
filter.append(")");
}
filter.append(")");
}
// Specify the scope of the search
// subtree: starts at the base entry and searches
// everything below it, including the base entry
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
try
{
String dn = Z.lu.getInitMgrDn();
String password = Z.lu.getInitPswd();
DirContext ctx = null;
ctx = this.getContext(dn, password);
// Perform the actual search
// We pass the searchbase, filters and constraints
// containing the scope of the search
NamingEnumeration results = ctx.search(
Z.lu.getInitSearchBase(), filter.toString(), constraints
);
// default sort is by lastname
String sortKey1 = Z.lu.LDAP_SURNAME;
String sortKey2 = Z.lu.LDAP_GIVENNAME;
String sortKey3 = Z.lu.LDAP_PRIMARYKEY;
if ("first".equalsIgnoreCase(sortBy)) {
sortKey1 = Z.lu.LDAP_GIVENNAME;
sortKey2 = Z.lu.LDAP_SURNAME;
sortKey3 = Z.lu.LDAP_PRIMARYKEY;
}
else if ("id".equalsIgnoreCase(sortBy)) {
sortKey1 = Z.lu.LDAP_PRIMARYKEY;
sortKey2 = Z.lu.LDAP_SURNAME;
sortKey3 = Z.lu.LDAP_GIVENNAME;
}
HashMap nameVals = null;
// Now Stepping through the search results
while (results.hasMoreElements())
{
nameVals = new HashMap();
SearchResult sr = (SearchResult) results.next();
Attributes attrs = sr.getAttributes();
HashMap hm = Z.lu.getLdapFields();
Iterator it = hm.keySet().iterator();
while (it.hasNext())
{
String key = (String) hm.get((String)it.next());
Attribute attr = attrs.get(key);
StringBuffer tmp = new StringBuffer();
if (attr != null)
{
NamingEnumeration nEnum = attr.getAll();
int cnt = 0;
tmp.setLength(0);
while (nEnum.hasMoreElements()) {
if (cnt++ > 0) { tmp.append(", "); }
tmp.append(((String) nEnum.next()).trim());
}
}
if (TextManager.isStringInvisible(tmp.toString()) ) {
tmp = new StringBuffer(LdapUtil.NA);
}
if (Z.lu.LDAP_PRIMARYKEY.equals(key)) {
nameVals.put(key,tmp.toString().toUpperCase());
} else {
nameVals.put(key,tmp.toString());
}
}
sortedResults.put(TextManager.catStrings((String)nameVals.get(sortKey1),
" ", (String)nameVals.get(sortKey2),
" ", (String)nameVals.get(sortKey3)), nameVals
);
}
results.close();
ctx.close();
} catch (javax.naming.PartialResultException pre) {
// Don't throw and exception here...this is for MS Active Server stuff
// throwing this exception probably because of a referral problem...
// but in any case the results are OK according to the data retrieved
Z.log.writeToLog(Z.log.WARN, "Partial Result Exception: " + pre.toString());
} catch (Exception e) {
Z.log.writeToLog(Z.log.ERROR, "UC: ldapSearch Exception: " + e);
ErrorWriter.write(e, ErrorWriter.LOGERR);
}
return sortedResults;
}