Example usage for javax.naming.directory BasicAttribute BasicAttribute

List of usage examples for javax.naming.directory BasicAttribute BasicAttribute

Introduction

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

Prototype

public BasicAttribute(String id, boolean ordered) 

Source Link

Document

Constructs a new instance of a possibly ordered attribute with no value.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ModificationItem[] mods = new ModificationItem[3];

    mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("mail", "g@w.com"));

    mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("number", "5555"));

    mods[2] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, new BasicAttribute("jpeg"));
    String url = "ldap://localhost/o=JNDITutorial";
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, url);

    DirContext ctx = new InitialDirContext(env);
    ctx.modifyAttributes("cn=Name, ou=People", mods);
}

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();/*from  w  w w .  java2s  .  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());
    }//from  w  w w.j  a va2  s . c om
}

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 ww w .  j av a  2 s. c  o  m*/
        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:Modify2Example.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 {// www . j a  va2  s. c  o  m
        DirContext dctx = new InitialDirContext(env);

        ModificationItem[] mods = new ModificationItem[3];
        mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("department", "sales"));
        mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("quota", "$1"));
        mods[2] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, new BasicAttribute("assistant"));

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

From source file:Main.java

public static void main(String args[]) throws Exception {
    Hashtable<String, String> env = new Hashtable<String, String>();

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

    ModificationItem[] mods = new ModificationItem[2];

    Attribute mod0 = new BasicAttribute("number", "555-555-5555");
    Attribute mod1 = new BasicAttribute("1", "AAA");

    mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, mod0);
    mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE, mod1);

    ctx.modifyAttributes("uid=mewilcox, ou=People, o=airius.com", mods);
}

From source file:ModAttrs.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 {/*www .jav a  2 s  .c  om*/
        // Create the initial context
        DirContext ctx = new InitialDirContext(env);
        String name = "cn=Ted Geisel, ou=People";

        // Save original attributes
        Attributes orig = ctx.getAttributes(name, new String[] { "mail", "telephonenumber", "jpegphoto" });

        // Specify the changes to make
        ModificationItem[] mods = new ModificationItem[3];

        // Replace the "mail" attribute with a new value
        mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                new BasicAttribute("mail", "geisel@wizards.com"));

        // Add additional value to "telephonenumber"
        mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE,
                new BasicAttribute("telephonenumber", "+1 555 555 5555"));

        // Remove the "jpegphoto" attribute
        mods[2] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, new BasicAttribute("jpegphoto"));

        // Perform the requested modifications on the named object
        ctx.modifyAttributes(name, mods);

        // Check attributes
        System.out.println("**** new attributes *****");
        printAttrs(ctx.getAttributes(name));

        // Revert changes
        ctx.modifyAttributes(name, DirContext.REPLACE_ATTRIBUTE, orig);

        // Check that the attributes got restored
        System.out.println("**** reverted to original attributes *****");
        printAttrs(ctx.getAttributes(name));

        // Close the context when we're done
        ctx.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

public static <T> Attribute create(String id, Object value, Evaluator<T> evaluator) {
    if (null == value) {
        return null;
    }//from   ww w .  ja v  a2  s  .  c om

    if (null == evaluator) {
        return new BasicAttribute(id, value);
    } else {
        T evaled = evaluator.eval(value);
        return null != evaled ? new BasicAttribute(id, evaled) : null;
    }
}

From source file:org.esco.portlet.changeetab.dao.impl.MockUserDao.java

@Override
public void saveCurrentEtablissement(final String userId, final String etabId) {
    MockUserDao.LOG.debug("Saving current etablissement ...");

    final Attribute replaceCurrentEtabAttr = new BasicAttribute(this.currentEtabIdLdapKey, etabId);
    final Name dn = new DistinguishedName(this.userDn.replace(this.userIdTemplate, userId));
    final ModificationItem[] mods = new ModificationItem[1];

    mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, replaceCurrentEtabAttr);

    MockUserDao.LOG.info("Should call ldaptemplate.modifyAttributes() with userDn: [{}] and ldapAttr: [{}]", dn,
            replaceCurrentEtabAttr);/*from www  .j a v  a2  s . c  om*/
}

From source file:org.esco.portlet.changeetab.dao.impl.LdapUserDao.java

@Override
public void saveCurrentEtablissement(final String userId, final String etabId) {
    LdapUserDao.LOG.debug("Saving current etablissement ...");

    final Attribute replaceCurrentEtabAttr = new BasicAttribute(this.currentEtabIdLdapKey, etabId);
    final Name dn = new DistinguishedName(this.userDn.replace(this.userIdTemplate, userId));
    final ModificationItem[] mods = new ModificationItem[1];

    mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, replaceCurrentEtabAttr);
    this.ldapTemplate.modifyAttributes(dn, mods);
}