package wrappers; // Standard java classes import java.util.*; import java.net.*; // Netscape LDAP JDK import netscape.ldap.*; // WebOQL import weboql.tree.*; import weboql.util.*; /** * Performs tree operations on LDAP sources for WebOQL engine * * @author Rimon Barr * @version 1.0 * @see LDAP */ public class LDAPTree extends Tree { /** * Host from which this LDAP tree is obtained */ private String host = null; /** * Port through this this LDAP tree is obtained */ private int port = -1; /** * Distinct name (DN) of the root of this LDAP tree */ private String dn = null; /** * Search constraints that return one level of the LDAP * structure below the DN */ private static final LDAPSearchConstraints sc = new LDAPSearchConstraints(0,LDAPv2.DEREF_NEVER,0,true,15,null,3); /** * Construct tree object from an LDAP url string * If url is invalid, the tree will be empty * @param sUrl string of LDAP url */ public LDAPTree(String sUrl) { try { // divide the url into parts and store LDAPUrl url = new LDAPUrl(sUrl); host=url.getHost(); port=url.getPort(); dn=url.getDN(); } catch (MalformedURLException e) { System.err.println("LDAPTree constructor: Bad URL"); } } /** * Returns an enumeration of the pairs emerging from this * tree's root node. Each pair represents a labelled arc * to a subtree. * @return enumeration of pairs (LDAPEnumeration) * @see LDAPEnumeration */ public Enumeration getPairs() { // check if constructor detected an invalid url if(dn==null || host==null) return new Vector(0).elements(); // Generate a url from parts LDAPUrl url=new LDAPUrl(host,port,dn, (Enumeration)null,LDAPv2.SCOPE_ONE,null); // Search LDAP source using the netscape.ldap classes LDAPSearchResults results=null; try { results = LDAPConnection.search(url); } catch (LDAPException e) {} if(results!=null) return new PairsEnum(results, "ldap://"+host+":"+port+"/"); else return new Vector(0).elements(); } /** * Enumerates the LDAP search results, as * [label, tree] pairs, which WebOQL can use. * * @author Rimon Barr * @version 1.0 * @see LDAPTree */ public static class PairsEnum implements Enumeration { /** * Stores original search results enumeration */ private Enumeration results=null; /** * Stores base LDAP url of this search, used for * constructor of subtrees */ private String base=null; /** * Initialises object with a search result set and * LDAP url wherefrom this result-set was generated * @param results LDAP * result set, usually instance of LDAPSearchResults (non-null) * @param base LDAP * url corresponding to results (non-null) * @exception NullPointerException * If either results or base is null * @see netscape.ldap.LDAPSearchResults * @see netscape.ldap.LDAPConnection */ public PairsEnum(Enumeration results, String base) { // Check for invalid parameters if(results==null || base==null) throw new NullPointerException("LDAPEnumeration constructor"); // store parameters in class variables this.base=base; this.results=results; } /** * Returns whether there are more entries in the result set * @return * Whether there are more entries in the result * set to convert to [arc-label tree] pairs */ public boolean hasMoreElements() { return results.hasMoreElements(); } /** * Converts next LDAPEntry into an [arc-label tree] pair * @return [arc-label tree] pair (java.lang.Object) */ public Object nextElement() { // Ensure we do not exceed the results enumeration if(!hasMoreElements()) throw new NoSuchElementException("LDAPEnumeration.nextElement"); // Get the next entry LDAPEntry entry=(LDAPEntry)results.nextElement(); // Generate arc label and tree ArcLabel arc=LDAPEntry2ArcLabel(entry); LDAPTree tree=new LDAPTree(base+entry.getDN()); // Return pair return new Pair(arc, tree); } /** * Same as nextElement, typed * @return [arc-label tree] pair (weboql.util.Pair) */ public Pair next() { return (Pair)nextElement(); } /** * Converts the information of an LDAP entry into an ArcLabel * @return Arc label representation of LDAP entry (weboql.tree.ArcLabel) */ private ArcLabel LDAPEntry2ArcLabel(LDAPEntry entry) { // Create new label, and an enumeration of attributes ArcLabel arc=new ArcLabel(); Enumeration attrs=entry.getAttributeSet().getAttributes(); // add DN as attribute of arc label arc.addField("dn", encodeString(entry.getDN())); // add all entry attributes to arc label while(attrs.hasMoreElements()) { LDAPAttribute attr=(LDAPAttribute)attrs.nextElement(); String name=attr.getName(); String value=encodeStrings(attr.getStringValues()); arc.addField(name, value); } // return arc label return arc; } /** * Encodes a string so that all ';' are escaped as '\;' * @param str String to be encoded * @return Encoded string * @see LDAPEnumeration#decodeString(java.lang.String) */ private static String encodeString(String str) { // check for valid input and degenerate cases if(str==null) throw new NullPointerException("LDAPEnumeration.encodeString"); if(str.indexOf(";")==-1) return str; // create and return new string with '\;' where there is ';' StringBuffer result=new StringBuffer(); for(int i=0; i