Hi there,
i have a few problems accessing a ldap server with the java api JNDI.
I am able to get attributes with the following code:
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.j ndi.ldap.LdapCtxFact
ory");
env.put(Context.PROVIDER_URL, "ldap://server:10389/");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "user=benutzer, ou=irgendwas,
o=nochwas, c=de");
env.put(Context.SECURITY_CREDENTIALS, "passwort");
try {
// Create the initial directory context
DirContext ctx = new InitialDirContext(env);
// Ask for all attributes of the object
Attributes attrs =
ctx.getAttributes("apc=einAPC,ou=irgendwas,o=nochw as,c=de");
// Find the ip adress and print
System.out.println("ip: " + attrs.get("IP").get());
} catch (NamingException e) {
System.err.println("Problem getting attribute:" + e);
}
But what to do, if i don't know the name of the APC?
I tried with searching, but i don't get any results:
try {
SearchControls ctls = new SearchControls();
ctls.setSearchScope(20);
// Specify the search filter to match
String filter = "(apc=*)";
DirContext ctx = new InitialDirContext(env);
// Search for objects using the filter
NamingEnumeration answer = ctx.search("", filter, ctls);
System.out.println("sind antworten
vorhanden?"+answer.hasMore()+"");
while (answer.hasMore()) {
SearchResult sr = (SearchResult)answer.next();
System.out.println(">>>" + sr.getName());
printAttrs(sr.getAttributes());
}
} catch (Exception err) {
System.out.println("fehler: " + err);
}
Structure of the directory:
c=de
|- o=nochwas, c=de
|-ou=irgendwas, o=nochwas, c=de
|-ou=irgendwas,
o=nochwas, c=de, apc=someAPC
ou=irgendwas,
o=nochwas, c=de, apc=anotherAPC
All apc's have an attribute apc.
Where is the problem?
Thx in advance
Bjoern