java and ldap

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bjoern Eberth

    java and ldap

    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_CONTEX T_FACTORY,"com. sun.jndi.ldap.L dapCtxFact
    ory");
    env.put(Context .PROVIDER_URL, "ldap://server:10389/");
    env.put(Context .SECURITY_AUTHE NTICATION, "simple");
    env.put(Context .SECURITY_PRINC IPAL, "user=benut zer, ou=irgendwas,
    o=nochwas, c=de");
    env.put(Context .SECURITY_CREDE NTIALS, "passwort") ;
    try {

    // Create the initial directory context
    DirContext ctx = new InitialDirConte xt(env);
    // Ask for all attributes of the object
    Attributes attrs =
    ctx.getAttribut es("apc=einAPC, ou=irgendwas,o= nochwas,c=de");
    // Find the ip adress and print
    System.out.prin tln("ip: " + attrs.get("IP") .get());
    } catch (NamingExceptio n e) {
    System.err.prin tln("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.setSearchS cope(20);
    // Specify the search filter to match
    String filter = "(apc=*)";
    DirContext ctx = new InitialDirConte xt(env);
    // Search for objects using the filter
    NamingEnumerati on answer = ctx.search("", filter, ctls);
    System.out.prin tln("sind antworten
    vorhanden?"+ans wer.hasMore()+" ");
    while (answer.hasMore ()) {
    SearchResult sr = (SearchResult)a nswer.next();
    System.out.prin tln(">>>" + sr.getName());
    printAttrs(sr.g etAttributes()) ;
    }
    } catch (Exception err) {
    System.out.prin tln("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




Working...