Example usage for javax.naming.directory Attribute add

List of usage examples for javax.naming.directory Attribute add

Introduction

In this page you can find the example usage for javax.naming.directory Attribute add.

Prototype

boolean add(Object attrVal);

Source Link

Document

Adds a new value to the attribute.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Attributes attrs = new BasicAttributes(true);
    Attribute objclass = new BasicAttribute("objectclass");
    objclass.add("top");
    objclass.add("extensible");
    attrs.put(objclass);//from w ww .ja v a2  s. c  o m

    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "yourURL");

    DirContext ctx = new InitialDirContext(env);
    Context entry = ctx.createSubcontext("cn=Sample", attrs);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Attributes attrs = new BasicAttributes(true);
    Attribute objclass = new BasicAttribute("objectclass");
    objclass.add("top");
    objclass.add("extensible");
    attrs.put(objclass);/*from  w ww  .j a  va2 s . c  om*/

    Object obj = "yourObject";
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "yourURL");

    DirContext ctx = new InitialDirContext(env);

    ctx.bind("cn=Sample", obj, attrs);
}

From source file:Create.java

public static void main(String[] args) {

    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {/*ww  w .j av a  2s  .c  om*/
        // Create the initial context
        DirContext ctx = new InitialDirContext(env);

        // Create attributes to be associated with the new context
        Attributes attrs = new BasicAttributes(true); // case-ignore
        Attribute objclass = new BasicAttribute("objectclass");
        objclass.add("top");
        objclass.add("organizationalUnit");
        attrs.put(objclass);

        // Create the context
        Context result = ctx.createSubcontext("ou=NewOu", attrs);

        // Check that it was created by listing its parent
        NamingEnumeration list = ctx.list("");

        // Go through each item in list
        while (list.hasMore()) {
            NameClassPair nc = (NameClassPair) list.next();
            System.out.println(nc);
        }

        // Close the contexts when we're done
        result.close();
        ctx.close();
    } catch (NamingException e) {
        System.out.println("Create failed: " + e);
    }
}

From source file:org.alfresco.repo.security.authentication.ldap.LDAPInitialDirContextFactoryImpl.java

public static void main(String[] args) {
    // ....build a pyramid selling scheme .....

    // A group has three user members and 2 group members .... and off we go ....
    // We make the people and groups to represent this and stick them into LDAP ...used to populate a test data base for user and groups

    int userMembers = Integer.parseInt(args[3]);

    ApplicationContext applicationContext = ApplicationContextHelper.getApplicationContext();
    LDAPInitialDirContextFactory factory = (LDAPInitialDirContextFactory) applicationContext
            .getBean("ldapInitialDirContextFactory");

    InitialDirContext ctx = null;
    try {/*from w  ww.ja va2  s. c  om*/
        ctx = factory.getInitialDirContext("cn=" + args[0] + "," + args[2], args[1]);

        /* Values we'll use in creating the entry */
        Attribute objClasses = new BasicAttribute("objectclass");
        objClasses.add("top");
        objClasses.add("person");
        objClasses.add("organizationalPerson");
        objClasses.add("inetOrgPerson");

        for (int i = 0; i < userMembers; i++) {

            Attribute cn = new BasicAttribute("cn", "User" + i + " TestUser");
            Attribute sn = new BasicAttribute("sn", "TestUser");
            Attribute givenNames = new BasicAttribute("givenName", "User" + i);
            Attribute telephoneNumber = new BasicAttribute("telephoneNumber", "123");
            Attribute uid = new BasicAttribute("uid", "User" + i);
            Attribute mail = new BasicAttribute("mail", "woof@woof");
            Attribute o = new BasicAttribute("o", "Alfresco");
            Attribute userPassword = new BasicAttribute("userPassword", "bobbins");
            /* Specify the DN we're adding */
            String dn = "cn=User" + i + " TestUser," + args[2];

            Attributes orig = new BasicAttributes();
            orig.put(objClasses);
            orig.put(cn);
            orig.put(sn);
            orig.put(givenNames);
            orig.put(telephoneNumber);
            orig.put(uid);
            orig.put(mail);
            orig.put(o);
            orig.put(userPassword);

            try {
                ctx.destroySubcontext(dn);
            } catch (NamingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            ctx.createSubcontext(dn, orig);
        }

    } catch (NamingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (NamingException e) {

                e.printStackTrace();
            }
        }
    }

}

From source file:org.easy.ldap.NamingFactory.java

public static Attribute getOrgObjectClasses() {
    Attribute objClasses = new BasicAttribute(LdapClasseNames.OBJECT_CLASS.toString());
    objClasses.add(LdapClasseNames.TOP.toString());
    objClasses.add(LdapClasseNames.ORGANIZATION.toString());

    return objClasses;
}

From source file:org.easy.ldap.NamingFactory.java

public static Attribute getRoleObjectClasses() {
    Attribute objClasses = new BasicAttribute(LdapClasseNames.OBJECT_CLASS.toString());
    objClasses.add(LdapClasseNames.TOP.toString());
    objClasses.add(LdapClasseNames.GROUP_OF_UNIQUE_NAMES.toString());

    return objClasses;
}

From source file:org.easy.ldap.NamingFactory.java

public static Attribute getRolesObjectClasses() {
    Attribute objClasses = new BasicAttribute(LdapClasseNames.OBJECT_CLASS.toString());
    objClasses.add(LdapClasseNames.TOP.toString());
    objClasses.add(LdapClasseNames.ORG_UNIT.toString());

    return objClasses;
}

From source file:org.easy.ldap.NamingFactory.java

public static Attribute getUsersObjectClasses() {
    Attribute objClasses = new BasicAttribute(LdapClasseNames.OBJECT_CLASS.toString());
    objClasses.add(LdapClasseNames.TOP.toString());
    objClasses.add(LdapClasseNames.ORG_UNIT.toString());

    return objClasses;

}

From source file:org.easy.ldap.NamingFactory.java

public static Attribute getUserObjectClasses() {
    Attribute objClasses = new BasicAttribute(LdapClasseNames.OBJECT_CLASS.toString());
    objClasses.add(LdapClasseNames.TOP.toString());
    objClasses.add(LdapClasseNames.PERSON.toString());
    objClasses.add(LdapClasseNames.ORG_PERSON.toString());
    objClasses.add(LdapClasseNames.INT_ORG_PERSON.toString());
    objClasses.add(LdapClasseNames.EXTENSIBLE_OBJECT.toString());

    return objClasses;
}

From source file:org.swordess.ldap.util.ModUtils.java

public static <T> ModificationItem create(int operationMod, String id, Object[] values,
        Evaluator<T> evaluator) {/*from w w w  . j a va 2s . c  o m*/
    if (ArrayUtils.isEmpty(values)) {
        return null;
    }

    boolean hasOneNotNullAtLeast = false;
    Attribute attr = new BasicAttribute(id);

    if (null == evaluator) {
        for (Object value : values) {
            if (null != value) {
                hasOneNotNullAtLeast = true;
                attr.add(value);
            }
        }

    } else {
        for (Object value : values) {
            if (null == value) {
                continue;
            }
            T evaled = evaluator.eval(value);
            if (null != evaled) {
                hasOneNotNullAtLeast = true;
                attr.add(evaled);
            }
        }
    }
    return hasOneNotNullAtLeast ? new ModificationItem(operationMod, attr) : null;
}