/* * Lookup helper class. */ package com.c2kernel.lookup.ldap; //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 for dn:"+dn+": \n"+ex.getMessage()); } throw new ObjectNotFoundException(dn+" does not exist"); } /** * Utility method to connect to an LDAP server * @param lp LDAP properties to connect with * @return a novell LDAPConnection object * @throws LDAPException when the connection was unsuccessful */ public static LDAPConnection createConnection(LDAPProperties lp) throws LDAPException { LDAPConnection ld = new LDAPConnection(); Logger.msg(3, "LDAPLookup - connecting to " + lp.mHost); ld.connect(lp.mHost, Integer.valueOf(lp.mPort).intValue()); Logger.msg(3, "LDAPLookup - authenticating user:" + lp.mUser); ld.bind( LDAPConnection.LDAP_V3, lp.mUser, String.valueOf(lp.mPassword).getBytes()); Logger.msg(3, "LDAPLookup - authentication successful"); LDAPSearchConstraints searchCons = new LDAPSearchConstraints(); searchCons.setMaxResults(0); ld.setConstraints(searchCons); return ld; } //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