Example usage for javax.naming.directory ModificationItem ModificationItem

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

Introduction

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

Prototype

public ModificationItem(int mod_op, Attribute attr) 

Source Link

Document

Creates a new instance of ModificationItem.

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 {/* w  w  w  . j  av a 2s. 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 {/*from   w  w  w .  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: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  w  w w.  jav a 2  s  .  co  m*/
}

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

From source file:egovframework.com.ext.ldapumt.service.impl.OrgManageLdapDAO.java

/**
 * ?  //from   w  w w.  j a v a  2s  .  co m
 * @param vo
 * vo? dn? ? ??  ? ??.
 */
protected void updateOrg(LdapObject vo) {
    String dn = vo.getDn();

    final ArrayList<ModificationItem> itemList = new ArrayList<ModificationItem>();

    introspect(vo, new Executable() {
        @Override
        public void execute(String key, Object value) {
            Attribute attr = new BasicAttribute(key, value);
            ModificationItem item = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr);
            itemList.add(item);
        }
    });

    ModificationItem[] items = new ModificationItem[itemList.size()];
    itemList.toArray(items);
    ldapTemplate.modifyAttributes(dn, items);
}

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

/**
 * Updates the profile of the user on the external provider.
 *
 * @param user the profile to be updated (it is assumed username is never changed)
 * @throws PortalServiceException for any errors encountered
 *//*from  w  w  w.j  a  v  a 2s. co  m*/
public void updateUser(CMSUser user) throws PortalServiceException {
    DirContext ctx = null;
    try {
        ctx = new InitialDirContext(env);

        List<ModificationItem> mods = new ArrayList<ModificationItem>();
        List<Attribute> profile = mapAttributes(user);
        for (Attribute attribute : profile) {
            mods.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attribute));
        }
        ctx.modifyAttributes(MessageFormat.format(userDNPattern, user.getUsername()),
                mods.toArray(new ModificationItem[0]));

        synchRoles(user.getUsername(), user.getRole());
    } catch (NamingException e) {
        throw new PortalServiceConfigurationException("Unable to save user.", e);
    } finally {
        closeContext(ctx);
    }
}

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

/**
 * Adds a POSIX user to the specified POSIX group.
 * /*from  w  w w  . j  a  va 2s  . co m*/
 * @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:gov.medicaid.dao.impl.LDAPIdentityProviderDAOBean.java

/**
 * Resets the password for the given user.
 *
 * @param username the username// w w w  . j  a v a  2 s.  co  m
 * @param password the new password
 * @throws PortalServiceException for any errors encountered
 */
public void resetPassword(String username, String password) throws PortalServiceException {
    DirContext ctx = null;
    try {
        ctx = new InitialDirContext(env);
        BasicAttribute pw = new BasicAttribute("userPassword", hash(password));

        ModificationItem[] mods = new ModificationItem[1];
        mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, pw);
        ctx.modifyAttributes(MessageFormat.format(userDNPattern, username), mods);
    } catch (NamingException e) {
        throw new PortalServiceConfigurationException("Unable to reset password.", e);
    } finally {
        closeContext(ctx);
    }
}