Example usage for javax.naming.directory Attributes put

List of usage examples for javax.naming.directory Attributes put

Introduction

In this page you can find the example usage for javax.naming.directory Attributes put.

Prototype

Attribute put(Attribute attr);

Source Link

Document

Adds a new attribute to the attribute set.

Usage

From source file:Modify1Example.java

public static void main(String args[]) {
    Hashtable env = new Hashtable(11);

    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://MyHost/o=JNDIExample");
    try {/*from w ww .j  ava 2s  .c om*/
        DirContext dctx = new InitialDirContext(env);
        Attributes attrs = new BasicAttributes(true);
        attrs.put(new BasicAttribute("email", "name@site.com"));
        attrs.put(new BasicAttribute("website"));

        dctx.modifyAttributes("cn=Name, ou=People", DirContext.ADD_ATTRIBUTE, attrs);
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Attributes matchAttrs = new BasicAttributes(true);
    matchAttrs.put(new BasicAttribute("sn", "YourName"));
    matchAttrs.put(new BasicAttribute("mail"));

    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);

    NamingEnumeration e = ctx.search("ou=People", matchAttrs);

    if (e.hasMore()) {
        SearchResult entry = (SearchResult) e.next();
        // Abandon rest of results
        e.close();/* w w w .j a  va2  s .c  o m*/
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String[] attrIDs = { "sn", "number", "value", "mail" };

    Attributes matchAttrs = new BasicAttributes(true);
    matchAttrs.put(new BasicAttribute("sn", "YourName"));
    matchAttrs.put(new BasicAttribute("mail"));

    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);

    NamingEnumeration e = ctx.search("ou=People", matchAttrs, attrIDs);

    while (e.hasMore()) {
        SearchResult entry = (SearchResult) e.next();
        System.out.println(entry.getName());
    }/*w ww .j  av  a  2 s.  c  o  m*/
}

From source file:AttributeExample.java

public static void main(String[] argc) throws Exception {
    Hashtable env = new Hashtable(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

    env.put(Context.PROVIDER_URL, "ldap://MyHost/o=JNDIExample");
    DirContext dctx = new InitialDirContext(env);
    Attributes attrs = new BasicAttributes(true);
    attrs.put(new BasicAttribute("email"));
    attrs.put(new BasicAttribute("website", "www.pri.com"));

    NamingEnumeration result = dctx.search("ou=People", attrs);

    while (result.hasMore()) {
        SearchResult sr = (SearchResult) result.next();
        System.out.println("Result = " + sr.getName());
        Attributes srchAttrs = sr.getAttributes();

        NamingEnumeration attributes = srchAttrs.getAll();

        while (attributes.hasMore()) {
            Attribute attr = (Attribute) attributes.next();
            System.out.println("Attribute: " + attr.getID());
            NamingEnumeration values = attr.getAll();
            while (values.hasMore()) {
                Object value = values.next();
                System.out.println("Value = " + value);
            }//from   w w  w .  j  ava 2s  .co m
        }
    }
}

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);

    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);

    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 {//from www  . ja v a 2 s.c o m
        // 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  w  w  . java  2 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.swordess.ldap.util.AttrUtils.java

public static void putIfNotNull(Attributes attrs, Attribute toPut) {
    if (null != toPut) {
        attrs.put(toPut);
    }//from ww  w. j a  va  2s . co  m
}

From source file:security.AuthenticationManager.java

public static Map<String, String> getUserAttributes(DirContext ctx, String searchBase, String userName,
        String principalDomain, String... attributeNames) throws NamingException {
    if (StringUtils.isBlank(userName)) {
        throw new IllegalArgumentException("Username and password can not be blank.");
    }//  ww  w.  j  av  a2 s .  c  o m

    if (attributeNames.length == 0) {
        return Collections.emptyMap();
    }

    Attributes matchAttr = new BasicAttributes(true);
    BasicAttribute basicAttr = new BasicAttribute("userPrincipalName", userName + principalDomain);
    matchAttr.put(basicAttr);

    NamingEnumeration<? extends SearchResult> searchResult = ctx.search(searchBase, matchAttr, attributeNames);

    if (ctx != null) {
        ctx.close();
    }

    Map<String, String> result = new HashMap<>();

    if (searchResult.hasMore()) {
        NamingEnumeration<? extends Attribute> attributes = searchResult.next().getAttributes().getAll();

        while (attributes.hasMore()) {
            Attribute attr = attributes.next();
            String attrId = attr.getID();
            String attrValue = (String) attr.get();

            result.put(attrId, attrValue);
        }
    }
    return result;
}