/* * 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 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', ';', '/'}; String escapedStr = new String(name); //Backslash is both a Java and an LDAP escape character, so escape it first escapedStr = escapedStr.replaceAll("\\\\","\\\\"); //Positional characters - see RFC 2253 escapedStr = escapedStr.replaceAll("^#","\\\\#"); escapedStr = escapedStr.replaceAll("^ | $","\\\\ "); for (char element : META_CHARS) { escapedStr = escapedStr.replaceAll("\\"+element,"\\\\" + element); } Logger.msg(6, "LDAP DN "+name+" escaped to "+escapedStr); return escapedStr; } public static String escapeSearchFilter (String filter) { //From RFC 2254 String escapedStr = new String(filter); escapedStr = escapedStr.replaceAll("\\\\","\\\\5c"); //escapedStr = escapedStr.replaceAll("\\*","\\\\2a"); // we need stars for searching escapedStr = escapedStr.replaceAll("\\(","\\\\28"); escapedStr = escapedStr.replaceAll("\\)","\\\\29"); Logger.msg(6, "LDAP Search Filter "+filter+" escaped to "+escapedStr); return escapedStr; } }