From 254ee6f47eebfc00462c10756a92066e82cc1a96 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Tue, 21 Jun 2011 15:46:02 +0200 Subject: Initial commit --- source/com/c2kernel/lookup/LDAPLookupUtils.java | 317 ++++++++++++++++++++++++ 1 file changed, 317 insertions(+) create mode 100755 source/com/c2kernel/lookup/LDAPLookupUtils.java (limited to 'source/com/c2kernel/lookup/LDAPLookupUtils.java') diff --git a/source/com/c2kernel/lookup/LDAPLookupUtils.java b/source/com/c2kernel/lookup/LDAPLookupUtils.java new file mode 100755 index 0000000..6516374 --- /dev/null +++ b/source/com/c2kernel/lookup/LDAPLookupUtils.java @@ -0,0 +1,317 @@ +/* + * 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 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 (int i=0;i < META_CHARS.length;i++) { + escapedStr = escapedStr.replaceAll("\\"+META_CHARS[i],"\\\\" + META_CHARS[i]); + } + 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; + } +} -- cgit v1.2.3