Fetch DN's ldap and Java

Hi,

I have this ldapsearch command that I want to convert to use with Java.

ldapsearch -h HOSTNAME -p PORT -b "" -s base "(objectclass=*)" namingContexts

I want to use the ctx.search(); but I have no idea how to convert this method in to a valid search so i can get the naming contexts.

Ive tried

SearchControls searchCtls = new SearchControls();

searchCtls.setSearchScope(SearchControls.OBJECT_SCOPE);

String searchFilter = "(objectclass=*)";

NamingEnumeration resultEnumeration =

ctx.search("", searchFilter, searchCtls);

If there is a better way to get hold of the DN's please suggest them too me.

Thanks

[681 byte] By [GregSimonsa] at [2007-11-27 10:30:44]
# 1

while(resultEnumeration.hasMore())

{

SearchResult resultat = (SearchResult) resultEnumeration.next();

DNchain = resultat.getNameInNamespace();

System.out.println(DNchain);

}

enm.close();

add this code under your code and it shold work fine. With the method getNameInspace(); you get the full DN for every object you searched for.

Hope this helped!

4tha at 2007-7-28 18:04:49 > top of Java-index,Core,Core APIs...
# 2

The problem seems to be that I need to get the DN for Active Directory.

This works for OpenLDAP

javax.naming.directory.Attributes attr = ctx.getAttributes("", new String[]

{

"objectclass=*",

"NamingContexts"

});

System.out.println(attr.get("namingcontexts"));

I dont want to have to do a bind to get the dn for ad I cant access the user to login etc,

Any ideas?

GregSimonsa at 2007-7-28 18:04:49 > top of Java-index,Core,Core APIs...
# 3

havent you tried my code? my code gives this in return:

CN=surename familyname,OU=class,OU=school,DC=bodensee,DC=de for example,

thats the DN you want not?

4tha at 2007-7-28 18:04:49 > top of Java-index,Core,Core APIs...
# 4

I'm guessing you want to find out the distinguished names for the naming contexts held on an Active Directory Domain Controller by reading the RootDSE.

You can find some samples in the following posts:

http://forum.java.sun.com/thread.jspa?threadID=693373&tstart=0

and

http://forum.java.sun.com/thread.jspa?threadID=581470&messageID=2959885

Note that in Active Directory the RootDSE attribute called namingContexts is multivalued. The following snippet illustrates reading all of the values from the namingContexts attribute:Attribute nc = attrs.get("namingContexts");

System.out.println(nc.getID());

for (NamingEnumeration e = nc.getAll();e.hasMore();System.out.println(e.next()));

adler_stevena at 2007-7-28 18:04:49 > top of Java-index,Core,Core APIs...