Example usage for javax.naming.directory DirContext REPLACE_ATTRIBUTE

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

Introduction

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

Prototype

int REPLACE_ATTRIBUTE

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

Click Source Link

Document

This constant specifies to replace an attribute with 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 {// ww w.  java  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: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   ww w.j  a  v a 2 s  . c o  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);/*  w w  w  .  ja v a 2  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);
}

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

/**
 * ?  //w  w w.  j  a v  a2  s .c o 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:LDAPTest.java

/**
     * Saves the changes that the user made.
     *//*from  w  w  w  .j  a v a 2  s .  c  o  m*/
    public void saveEntry() {
        try {
            if (dataPanel == null)
                return;
            if (context == null)
                context = getContext();
            if (uidField.getText().equals(uid)) // update existing entry
            {
                String dn = "uid=" + uidField.getText() + ",ou=people,dc=mycompany,dc=com";
                Attributes editedAttrs = dataPanel.getEditedAttributes();
                NamingEnumeration<? extends Attribute> attrEnum = attrs.getAll();
                while (attrEnum.hasMore()) {
                    Attribute attr = attrEnum.next();
                    String id = attr.getID();
                    Attribute editedAttr = editedAttrs.get(id);
                    if (editedAttr != null && !attr.get().equals(editedAttr.get()))
                        context.modifyAttributes(dn, DirContext.REPLACE_ATTRIBUTE,
                                new BasicAttributes(id, editedAttr.get()));
                }
            } else
            // create new entry
            {
                String dn = "uid=" + uidField.getText() + ",ou=people,dc=mycompany,dc=com";
                attrs = dataPanel.getEditedAttributes();
                Attribute objclass = new BasicAttribute("objectClass");
                objclass.add("uidObject");
                objclass.add("person");
                attrs.put(objclass);
                attrs.put("uid", uidField.getText());
                context.createSubcontext(dn, attrs);
            }

            findEntry();
        } catch (NamingException e) {
            JOptionPane.showMessageDialog(LDAPFrame.this, e);
            e.printStackTrace();
        } catch (IOException e) {
            JOptionPane.showMessageDialog(LDAPFrame.this, e);
            e.printStackTrace();
        }
    }

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   www  . j av  a2s. 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:ldap.ActiveLoginImpl.java

/**
 * This updates the UserAccount./*from  w w w.  ja v  a  2 s. co m*/
 * It requires at a minimum a name;
 * and optionally any other attributes.
 *
 * Note that this will REPLACE any attributes passed, deleting any existing values
 * for the specified attribute (e.g. if the attribute is userPassword, the old userPassword will
 * be discarded, rather than there being two userPasswords in the entry).
 *
 * Modifying the naming attribute will probably result in an error (depending on the directory).
 *
 * @param account
 * @throws Exception
 */
public void updateAccount(UserAccount account, DirContext context, String userDN) throws Exception {

    //if (account.get(Config.USER_NAMING_ATT) == null)
    if (account.get(LdapConstants.ldapDnAttrType) == null)
        throw new NamingException("UpdateAccount(), UserAccount has no naming Attribute");

    // should not be used
    //logger.info("Updating: \n" + account.getUserDN() + "\n" + account.toString());
    logger.info("Updating: \n" + userDN + "\n" + account.toString());

    // remove the naming attribute from the account before adding

    Attributes atts = copyAttributes(account); // create a local copy

    //atts.remove(Config.USER_NAMING_ATT);  // we can't modify the naming attribute this way, so don't try...
    //atts.remove(LdapConstants.ldapAttrUid);  // we can't modify the naming attribute this way, so don't try...
    atts.remove(LdapConstants.ldapDnAttrType); // we can't modify the naming attribute this way, so don't try...
    atts = hashPasswordAttribute(atts);
    // context.modifyAttributes(account.getUserDN(), DirContext.REPLACE_ATTRIBUTE, atts);
    context.modifyAttributes(userDN, DirContext.REPLACE_ATTRIBUTE, atts);
}