package wrappers; // Standard java classes import java.util.*; import java.net.*; // Netscape LDAP JDK import netscape.ldap.*; // WebOQL import weboql.tree.*; import weboql.util.*; public class LDAP { public static void main(String[] args) { testTree(); } public static Tree getTree(String url) { return new LDAPTree(url); } /////////////// // Various debug routines. // public static void testTree() { Date d1=null, d2=null; d1=new Date(); LDAPTree tree = // new LDAPTree("ldap://qew.cs:3002/ou=Accounting, o=Ace Industry,c=US"); new LDAPTree("ldap://qew.cs:3002/o=Ace Industry,c=US"); PrintTree.print(tree); //printArcs(tree); d2=new Date(); System.out.println("Time: "+(d2.getTime()-d1.getTime())); } public static void printArcs(LDAPTree tree) { LDAPTree.PairsEnum resultEnum = (LDAPTree.PairsEnum)tree.getPairs(); int num=0; while(resultEnum.hasMoreElements()) { Pair pair = resultEnum.next(); ArcLabel arc = (ArcLabel)pair.first; Enumeration e = arc.getFields(); System.out.print(num+": "); while(e.hasMoreElements()) { String name=(String)e.nextElement(); System.out.print(name+"="+arc.getField(name).toString()+";"); } System.out.println(); num++; } System.out.println("Number of arcs = "+num); } public static void testConnect() { System.out.println("*** LDAPConnection test: BEGIN ***"); LDAPConnection ld=new LDAPConnection(); final String HOST = "qew.cs"; final int PORT = 3002; final String FILTER = "objectclass=*"; final String SEARCHBASE = "o=Ace Industry,c=US"; System.out.println("Connecting to "+HOST+" on port "+PORT); try { ld.connect(HOST, PORT); } catch (LDAPException e) { printException(e); System.exit(1); } // connection status printStatus(ld); // search constraints printSearchConstraints(ld.getSearchConstraints()); // response System.out.println("Response = "); printControls(ld.getResponseControls(), "\n "); try { ld.disconnect(); } catch (LDAPException e) { printException(e); } System.out.println("*** LDAPConnection test: END ***"); } public static void printEntry(LDAPEntry le) { System.out.println(le.getDN()); Enumeration nodeAttrs = le.getAttributeSet().getAttributes(); while (nodeAttrs.hasMoreElements()) { LDAPAttribute attr = (LDAPAttribute)nodeAttrs.nextElement(); System.out.print(" "+attr.getName()+":"); Enumeration attrVals = attr.getStringValues(); while(attrVals.hasMoreElements()) System.out.print(" "+(String)attrVals.nextElement()); System.out.println(); } } public static void printStatus(LDAPConnection ld) { //version System.out.println("LDAP version = "+LDAPConnection.LDAP_VERSION); try { // protocol version System.out.print(" "+LDAPConnection.LDAP_PROPERTY_PROTOCOL+" = "+ (Float)ld.getProperty(LDAPConnection.LDAP_PROPERTY_PROTOCOL)+" "); // sdk version System.out.print(LDAPConnection.LDAP_PROPERTY_SDK+" = "+ (Float)ld.getProperty(LDAPConnection.LDAP_PROPERTY_SDK)+" "); // security types System.out.println(LDAPConnection.LDAP_PROPERTY_SECURITY+" = "+ (String)ld.getProperty(LDAPConnection.LDAP_PROPERTY_SECURITY)); } catch (LDAPException e) { System.out.println(e); } // connected? System.out.print("Connection? = "+ld.isConnected()); // authenticated? System.out.println(" Authenticated? = "+ld.isAuthenticated()); // host, port System.out.println(" Host = "+ld.getHost()+" Port = "+ld.getPort()); // name, pw System.out.println(" Name = "+ld.getAuthenticationDN()+ " Password = "+ld.getAuthenticationPassword()); } public static void printControls(LDAPControl[] lc, String prefix) { if(lc!=null) for(int i=0; i"); } System.out.println(); // hop limit System.out.println(" Hop limit = "+sc.getHopLimit()); // max results System.out.println(" Max results = "+sc.getMaxResults()); // rebind proc System.out.println(" Rebind proc = "+sc.getRebindProc()); // referrals? System.out.println(" Referals? = "+sc.getReferrals()); // server controls System.out.print(" Server controls = "); printControls(sc.getServerControls(), "\n "); // time limit System.out.println(" Time limit = "+sc.getTimeLimit()); } public static void printException(LDAPException e) { System.out.println(e); System.out.println(" Msg - "+e.getLDAPErrorMessage()); String code=""; switch(e.getLDAPResultCode()) { case LDAPException.ALIAS_DEREFERENCING_PROBLEM: code="An error occurred when dereferencing an alias."; break; case LDAPException.ALIAS_PROBLEM: code="An problem occurred with an alias."; break; case LDAPException.ATTRIBUTE_OR_VALUE_EXISTS: code="The value that you are adding to an attribute already exists in the attribute."; break; case LDAPException.AUTH_METHOD_NOT_SUPPORTED: code="The specified authentication method is not supported by the LDAP server that you are connecting to."; break; case LDAPException.BUSY: code="The DSA is busy."; break; case LDAPException.COMPARE_FALSE: code="Value returned by an LDAP compare operation if the specified attribute and value is not found in the entry (no matching value found)."; break; case LDAPException.COMPARE_TRUE: code="Value returned by an LDAP compare operation if the specified attribute and value is found in the entry (matching value found)."; break; case LDAPException.CONSTRAINT_VIOLATION: code="An internal error occurred in the LDAP server."; break; case LDAPException.ENTRY_ALREADY_EXISTS: code="The specified entry already exists."; break; case LDAPException.INAPPROPRIATE_AUTHENTICATION: code="The authentication presented to the server is inappropriate."; break; case LDAPException.INAPPROPRIATE_MATCHING: code="An inappropriate type of matching was used."; break; case LDAPException.INSUFFICIENT_ACCESS_RIGHTS: code="The client is authenticated as a user who does not have the access privileges to perform this operation."; break; case LDAPException.INVALID_ATTRIBUTE_SYNTAX: code="The request contains invalid syntax."; break; case LDAPException.INVALID_CREDENTIALS: code="The credentials presented to the server for authentication are not valid."; break; case LDAPException.INVALID_DN_SYNTAX: code="The specified distinguished name (DN) uses invalid syntax."; break; case LDAPException.IS_LEAF: code="The specified entry is a \"leaf\" entry (it has no entries beneath it in the directory tree)."; break; case LDAPException.LDAP_PARTIAL_RESULTS: code="The LDAP server is referring your client to another LDAP server."; break; case LDAPException.LOOP_DETECT: code="A loop has been detected."; break; case LDAPException.NAMING_VIOLATION: code="A naming violation has occurred."; break; case LDAPException.NO_SUCH_ATTRIBUTE: code="The specified attribute could not be found."; break; case LDAPException.NO_SUCH_OBJECT: code="The entry specified in the request does not exist."; break; case LDAPException.NOT_ALLOWED_ON_NONLEAF: code="The requested operation can only be performed on an entry that has no entries beneath it in the directory tree (in other words, a \"leaf\" entry)."; break; case LDAPException.NOT_ALLOWED_ON_RDN: code="The specified operation cannot be performed on a relative distinguished name (RDN)."; break; case LDAPException.OBJECT_CLASS_MODS_PROHIBITED: code="You cannot modify the specified object class."; break; case LDAPException.OBJECT_CLASS_VIOLATION: code="The requested operation will add or change data so that the data no longer complies with the schema."; break; case LDAPException.OPERATION_ERROR: code="An internal error occurred in the LDAP server."; break; case LDAPException.OTHER: code="Other types of errors."; break; case LDAPException.PROTOCOL_ERROR: code="A LDAP server could not correctly interpret the request sent by your client because the request does not strictly comply with the LDAP protocol."; break; case LDAPException.SIZE_LIMIT_EXCEEDED: code="The search found more than the maximum number of results."; break; case LDAPException.STRONG_AUTH_REQUIRED: code="A stronger authentication method (more than LDAP_AUTH_SIMPLE) is required by the LDAP server that you are connecting to."; break; case LDAPException.SUCCESS: code="The operation completed successfully."; break; case LDAPException.TIME_LIMIT_EXCEEDED: code="The search operation could not be completed within the maximum time limit."; break; case LDAPException.UNAVAILABLE: code="The DSA is unavailable."; break; case LDAPException.UNDEFINED_ATTRIBUTE_TYPE: code="The specified attribute is not defined."; break; case LDAPException.UNWILLING_TO_PERFORM: code="The DSA is unable to perform the specified operation."; break; } System.err.println(" Code - "+e.getLDAPResultCode()+": "+code); } } // ldap