tags:

views:

51

answers:

2

Hi, I am very new to LDAP and JNDI. Basically i would like to add a new OU into LDAP from JNDI. My LDAP server is setup from OpenDS.

Here's my code:

public static void main(String args[])
{
    String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory";
    String MY_HOST = "ldap://localhost:1389";
    String MGR_DN = "cn=Directory Manager";
    String MGR_PW = "password";
    String MY_SEARCHBASE = "dc=QuizPortal";

    try
    {
        Hashtable env = new Hashtable();

        env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);

        env.put(Context.PROVIDER_URL, MY_HOST);

        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, MGR_DN);
        env.put(Context.SECURITY_CREDENTIALS, MGR_PW);

        DirContext ctx = new InitialDirContext(env);

        Attributes attrs = new BasicAttributes(true); // case-ignore
        Attribute objclass = new BasicAttribute("objectclass");
        objclass.add("top");
        objclass.add("organizationalUnit");
        attrs.put(objclass);

        ctx.createSubcontext("ou=NewOu", attrs);
    }

    catch(Exception e)
    {
        e.printStackTrace();
        System.exit(1);
    }
}

Here's the error message:

javax.naming.NameNotFoundException: [LDAP: error code 32 - The provided entry ou=NewOu cannot be added because it does not have a parent and is not defined as one of the suffixes within the Directory Server]; remaining name 'ou=NewOu'
        at com.sun.jndi.ldap.LdapCtx.mapErrorCode(LdapCtx.java:3066)
        at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2987)
        at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2794)
        at com.sun.jndi.ldap.LdapCtx.c_createSubcontext(LdapCtx.java:788)
        at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_createSubcontext(ComponentDirContext.java:319)
        at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.createSubcontext(PartialCompositeDirContext.java:248)
        at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.createSubcontext(PartialCompositeDirContext.java:236)
        at javax.naming.directory.InitialDirContext.createSubcontext(InitialDirContext.java:178)
        at JUNDIAdd2.main(JUNDIAdd2.java:43)

Addition info, i have o=IT, dc=QuizPortal where i want to add in the new OU.

Could anyone guide me around this error?? Any help will be greatly appreciated. Thanks!

Kevin

+1  A: 

Have you tried:

ctx.createSubcontext("ou=NewOu,dc=QuizPortal,o=IT", attrs);

or this:

ctx.createSubcontext("ou=NewOu,dc=QuizPortal", attrs);

Hope this will help you

SourceRebels
Hi there, I have tried these 2 methods but still having the error. I manage to add the newOu by changing MY_HOST = "ldap://localhost:1389"; to MY_HOST = "ldap://localhost:1389/dc=QuizPortal"; But it add the newOu under QuizPortal which is not what i wanted. I wanted to add newOu into IT
Nivek
+2  A: 

Try:

ctx.bind("ou=NewOu,o=IT", null, attrs);

You may need to set the ou attribute before:

attrs.put("ou", "NewOu");
Maurice Perry
Hey THANKS.. that works like a charm. For the benefit of others, with perry's answer and i have edited MY_HOST = "ldap://localhost:1389/dc=QuizPortal", i am able to add the newOu under o=IT...
Nivek