/* * Lookup helper class. */ package com.c2kernel.lookup; //import netscape.ldap.*; //import netscape.ldap.util.*; import com.c2kernel.common.ObjectAlreadyExistsException; import com.c2kernel.common.ObjectCannotBeUpdated; import com.c2kernel.common.ObjectNotFoundException; import com.c2kernel.utils.Logger; import com.novell.ldap.LDAPAttribute; import com.novell.ldap.LDAPAttributeSet; import com.novell.ldap.LDAPConnection; import com.novell.ldap.LDAPDN; import com.novell.ldap.LDAPEntry; import com.novell.ldap.LDAPException; import com.novell.ldap.LDAPModification; import com.novell.ldap.LDAPSearchConstraints; import com.novell.ldap.LDAPSearchResults; /** * @version $Revision: 1.74 $ $Date: 2006/03/03 13:52:21 $ * @author $Author: abranson $ */ final public class LDAPLookupUtils { static final char[] META_CHARS = {'+', '=', '"', ',', '<', '>', ';', '/'}; static final String[] META_ESCAPED = {"2B", "3D", "22", "2C", "3C", "3E", "3B", "2F"}; static public LDAPEntry getEntry(LDAPConnection ld, String dn,int dereference) throws ObjectNotFoundException { try { LDAPSearchConstraints searchCons = new LDAPSearchConstraints(); searchCons.setBatchSize(0); searchCons.setDereference(dereference); LDAPEntry thisEntry = ld.read(dn,searchCons); if (thisEntry != null) return thisEntry; } catch (LDAPException ex) { throw new ObjectNotFoundException("LDAP Exception: "+ex.getMessage(), ""); } throw new ObjectNotFoundException(dn+" does not exist", ""); } //Given a DN, return an LDAP Entry static public LDAPEntry getEntry(LDAPConnection ld, String dn) throws ObjectNotFoundException { return getEntry(ld, dn, LDAPSearchConstraints.DEREF_NEVER); } static public String getFirstAttributeValue(LDAPEntry anEntry, String attribute) throws ObjectNotFoundException { LDAPAttribute attr = anEntry.getAttribute(attribute); if (attr==null) throw new ObjectNotFoundException("No attributes named '"+attribute+"'", ""); return (String)attr.getStringValues().nextElement(); } static public String[] getAllAttributeValues(LDAPEntry anEntry, String attribute) throws ObjectNotFoundException { LDAPAttribute attr = anEntry.getAttribute(attribute); if (attr!=null) return attr.getStringValueArray(); throw new ObjectNotFoundException("No attributes named '"+attribute+"'", ""); } static public boolean existsAttributeValue(LDAPEntry anEntry, String attribute, String value) { LDAPAttribute attr = anEntry.getAttribute(attribute); if (attr!=null) { String[] attrValues = new String[attr.size()]; attrValues = attr.getStringValueArray(); for (int i=0;i