Example usage for javax.naming.directory DirContext ADD_ATTRIBUTE

List of usage examples for javax.naming.directory DirContext ADD_ATTRIBUTE

Introduction

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

Prototype

int ADD_ATTRIBUTE

To view the source code for javax.naming.directory DirContext ADD_ATTRIBUTE.

Click Source Link

Document

This constant specifies to add an attribute with the specified values.

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: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 {/*from w w  w  . j  a va  2  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: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 {/* ww w.j  a  v a 2s .com*/
        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 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 {/*from www.j a v  a 2 s . co m*/
        // 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:com.liferay.portal.security.ldap.BasePortalToLDAPConverter.java

public Modifications getLDAPGroupModifications(long ldapServerId, UserGroup userGroup, User user,
        Properties groupMappings, Properties userMappings) throws Exception {

    Modifications modifications = Modifications.getInstance();

    String groupDN = getGroupDNName(ldapServerId, userGroup, groupMappings);
    String userDN = getUserDNName(ldapServerId, user, userMappings);

    if (!PortalLDAPUtil.isGroupMember(ldapServerId, user.getCompanyId(), groupDN, userDN)) {

        modifications.addItem(DirContext.ADD_ATTRIBUTE, groupMappings.getProperty(GroupConverterKeys.USER),
                userDN);/*w w  w .ja  v a 2 s .c  om*/
    }

    return modifications;
}

From source file:ca.tnt.ldaputils.impl.LdapEntry.java

/**
 * Please note, the preferred method is to call setXXXX() where XXXX is the
 * attribute name, followed by save()./*from   ww  w  . j a  v a 2s  .c o m*/
 * <p/>
 * This sets a batch attribute.  This means that it will be added to a queue
 * for changing LDAP.  You can modify the same attribute multiple times,
 * assuming LDAP supports multivalued attributes for that attribute. You are
 * then required to call modifyBatchAttributes(), which will actually do the
 * operations requested.
 * <p/>
 * You should call this one or more times per attribute, followed by
 * modifyBatchAttributes().
 * <p/>
 * Each time you call this method, for the same attribute, you should
 * specify the same operation, otherwise you will get an
 * IllegalArgumentException, with an appropriate error message.
 *
 * @param operation one of ADD_ATTRIBUTE, REPLACE_ATTRIBUTE,
 *                  REMOVE_ATTRIBUTE
 * @param attribute the name of the attribute
 * @param value     the value of the attribute
 *
 * @see #ADD_ATTRIBUTE ADD_ATTRIBUTE
 * @see #REPLACE_ATTRIBUTE REPLACE_ATTRIBUTE
 * @see #REMOVE_ATTRIBUTE REMOVE_ATTRIBUTE
 */
public void modifyBatchAttribute(final int operation, final String attribute, final Object value) {
    final Attribute newAttribute;
    ModificationItem modItem;
    final int mod_op;

    switch (operation) {
    case ADD_ATTRIBUTE:
        mod_op = DirContext.ADD_ATTRIBUTE;
        break;
    case REPLACE_ATTRIBUTE:
        mod_op = DirContext.REPLACE_ATTRIBUTE;
        break;
    case REMOVE_ATTRIBUTE:
        mod_op = DirContext.REMOVE_ATTRIBUTE;
        break;
    default:
        mod_op = DirContext.ADD_ATTRIBUTE;
    }

    modItem = (ModificationItem) modificationItems.get(attribute);
    if (modItem == null) { // first time we are doing something with this attribute
        newAttribute = new BasicAttribute(attribute, value);
        modItem = new ModificationItem(mod_op, newAttribute);
    } else { // we will add it to the attribute values for this attribute
        if (modItem.getModificationOp() != mod_op) { // make sure they aren't changing their mind on which op
            throw new IllegalArgumentException(
                    "error, operation does not match previous batch items for this attribute");
        }

        modItem.getAttribute().add(value);
    }
    modified = true;
    modificationItems.put(attribute, modItem);
}

From source file:edu.kit.scc.ldap.LdapPosixGroupDao.java

/**
 * Adds a POSIX user to the specified POSIX group.
 * /*from   w ww  .  j  a  v a2s  .  c om*/
 * @param group the POSIX group
 * @param memberUid the POSIX user's uid
 * @return true on success
 */
public boolean addMember(PosixGroup group, String memberUid) {
    ModificationItem[] modificationItems = new ModificationItem[] {
            new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("memberUid", memberUid)) };
    LdapName groupDn = LdapUtils.emptyLdapName();
    try {
        groupDn = new LdapName(groupBase);
        groupDn.add("cn=" + group.getCommonName());
        log.debug("Add member {} to {}", memberUid, groupDn.toString());
        ldapTemplate.modifyAttributes(groupDn, modificationItems);
        return true;
    } catch (AttributeInUseException ex) {
        log.error("ERROR {}", ex.toString());
    } catch (InvalidNameException ex) {
        log.error("ERROR {}", ex.toString());
    }
    return false;
}

From source file:com.liferay.portal.security.ldap.BasePortalToLDAPConverter.java

public Modifications getLDAPUserGroupModifications(long ldapServerId, List<UserGroup> userGroups, User user,
        Properties userMappings) throws Exception {

    Modifications modifications = Modifications.getInstance();

    Properties groupMappings = LDAPSettingsUtil.getGroupMappings(ldapServerId, user.getCompanyId());

    String userDN = getUserDNName(ldapServerId, user, userMappings);

    for (UserGroup userGroup : userGroups) {
        String groupDN = getGroupDNName(ldapServerId, userGroup, groupMappings);

        if (PortalLDAPUtil.isUserGroupMember(ldapServerId, user.getCompanyId(), groupDN, userDN)) {

            continue;
        }/*from   w ww.j a v  a2 s  .  c  o m*/

        modifications.addItem(DirContext.ADD_ATTRIBUTE, userMappings.getProperty(UserConverterKeys.GROUP),
                groupDN);
    }

    return modifications;
}

From source file:gov.medicaid.dao.impl.LDAPIdentityProviderDAOBean.java

/**
 * Synchronizes the roles between the application and the identity provider.
 *
 * @param username the user to synchronize the role for
 * @param role the role that should be set on the identity provider
 * @throws PortalServiceException for any errors encountered
 *///  w  w  w . ja  v a 2s .  c  om
private void synchRoles(String username, Role role) throws PortalServiceException {
    List<String> roles = findRoles(username);

    DirContext ctx = null;
    try {
        ctx = new InitialDirContext(env);

        // remove all roles, we expect only one
        for (String existingRole : roles) {
            if (!existingRole.equals(role.getDescription())) {
                removeRoleAssignment(ctx, username, existingRole);
            }
        }

        // add the new role if needed
        if (!roles.contains(role.getDescription())) {
            ModificationItem[] mods = new ModificationItem[1];
            BasicAttribute m = new BasicAttribute(groupMemberAttr,
                    MessageFormat.format(userDNPattern, username));
            mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, m);
            ctx.modifyAttributes(MessageFormat.format(groupDNPattern, role.getDescription()), mods);
        }
    } catch (NamingException e) {
        throw new PortalServiceConfigurationException("Unable to reset password.", e);
    } finally {
        closeContext(ctx);
    }

}