//***************** // DO NOT MAKE IMPROVEMENTS IN THIS FILE // IT IS A MAKESHIFT SOLUTION. // Contact: barr@cs.toronto.edu //***************** package wrappers; import java.util.*; import java.net.*; import netscape.ldap.LDAPSearchConstraints; import netscape.ldap.LDAPConnection; import netscape.ldap.LDAPUrl; import netscape.ldap.LDAPv2; import netscape.ldap.LDAPEntry; import netscape.ldap.LDAPAttribute; import netscape.ldap.LDAPSearchResults; import netscape.ldap.LDAPException; import weboql.tree.Tree; import weboql.tree.TreeInterface; import weboql.tree.ArcLabel; import weboql.util.Pair; public class LDAPTree2 extends Tree implements TreeInterface { private String host = null; private int port = -1; private String dn = null; private static final LDAPSearchConstraints sc = new LDAPSearchConstraints(0,LDAPv2.DEREF_NEVER,0,true,15,null,3); public LDAPTree2(String sUrl) { setUrl(sUrl); //System.out.println("LDAPTree2 constructor: "+sUrl); System.err.print("."); try { LDAPUrl url = new LDAPUrl(sUrl); host=url.getHost(); port=url.getPort(); dn=url.getDN(); } catch (MalformedURLException e) { System.err.println("LDAPTree2 constructor: Bad URL"); } createList(); } private void createList() { Enumeration e = getPairs2(); while (e.hasMoreElements()) addChild((Pair)e.nextElement()); } private Enumeration getPairs2() { if(dn==null || host==null) return new Vector(0).elements(); LDAPUrl url=new LDAPUrl(host,port,dn, (Enumeration)null,LDAPv2.SCOPE_ONE,null); 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(); } public static class PairsEnum implements Enumeration { private Enumeration results=null; private String base=null; public PairsEnum(Enumeration results, String base) { if(results==null || base==null) throw new NullPointerException("LDAPEnumeration constructor"); this.base=base; this.results=results; } public boolean hasMoreElements() { return results.hasMoreElements(); } public Object nextElement() { if(!hasMoreElements()) throw new NoSuchElementException("LDAPEnumeration.nextElement"); LDAPEntry entry=(LDAPEntry)results.nextElement(); ArcLabel arc=LDAPEntry2ArcLabel(entry); LDAPTree2 tree=new LDAPTree2(base+entry.getDN()); return new Pair(arc, tree); } public Pair next() { return (Pair)nextElement(); } private ArcLabel LDAPEntry2ArcLabel(LDAPEntry entry) { ArcLabel arc=new ArcLabel(); Enumeration attrs=entry.getAttributeSet().getAttributes(); arc.addField("dn", encodeString(entry.getDN())); while(attrs.hasMoreElements()) { LDAPAttribute attr=(LDAPAttribute)attrs.nextElement(); String name=attr.getName(); String value=encodeStrings(attr.getStringValues()); arc.addField(name, value); } return arc; } private static String encodeString(String str) { if(str==null) throw new NullPointerException("LDAPEnumeration.encodeString"); if(str.indexOf(";")==-1) return str; StringBuffer result=new StringBuffer(); for(int i=0; i