Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * ====================
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   * 
   * Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved.     
   * 
   * The contents of this file are subject to the terms of the Common Development 
   * and Distribution License("CDDL") (the "License").  You may not use this file 
   * except in compliance with the License.
  * 
  * You can obtain a copy of the License at 
  * http://IdentityConnectors.dev.java.net/legal/license.txt
  * See the License for the specific language governing permissions and limitations 
  * under the License. 
  * 
  * When distributing the Covered Code, include this CDDL Header Notice in each file
  * and include the License file at identityconnectors/legal/license.txt.
  * If applicable, add the following below this CDDL Header, with the fields 
  * enclosed by brackets [] replaced by your own identifying information: 
  * "Portions Copyrighted [year] [name of copyright owner]"
  * ====================
  */
 package org.connid.ad.crud;
 
 import static org.connid.ad.ADConnector.*;
 
 import static org.identityconnectors.common.CollectionUtil.isEmpty;
 import static org.identityconnectors.common.CollectionUtil.newSet;
 import static org.identityconnectors.common.CollectionUtil.nullAsEmpty;
 import static org.identityconnectors.ldap.LdapUtil.checkedListByFilter;
 
 import java.util.List;
 import java.util.Set;
 
 
 
 public class ADUpdate extends LdapModifyOperation {
 
     private static final Log LOG = Log.getLog(ADUpdate.class);
 
     private final ObjectClass oclass;
 
     private Uid uid;
 
     @SuppressWarnings("FieldNameHidesFieldInSuperclass")
     private ADConnection conn;
 
     public ADUpdate(
             final ADConnection connfinal ObjectClass oclassfinal Uid uid) {
 
         super(conn);
         this. = oclass;
         this. = uid;
         this. = conn;
     }
 
     public Uid update(final Set<Attributeattrs) {
         final ConnectorObject obj = getEntryToBeUpdated();
         String entryDN = obj.getName().getNameValue();
 
         // ---------------------------------
         // Check if entry should be renamed.
         // ---------------------------------
         final Name name = (Name) AttributeUtil.find(.attrs);
 
         Set<AttributeattrToBeUpdated = attrs;
        Name newName = null;
        if (name != null) {
            attrToBeUpdated = newSet(attrs);
            attrToBeUpdated.remove(name);
            final ADUtilities utils = new ADUtilities((ADConnection);
            if (utils.isDN(name.getNameValue())) {
                newName = new Name(.getSchemaMapping().getEntryDN(name));
            } else {
                try {
                    final List<Rdnrdns = new ArrayList<Rdn>(new LdapName(entryDN).getRdns());
                    if (!rdns.get(rdns.size() - 1).getValue().toString().equalsIgnoreCase(name.getNameValue())) {
                        Rdn naming = new Rdn(rdns.get(rdns.size() - 1).getType(), name.getNameValue());
                        rdns.remove(rdns.size() - 1);
                        rdns.add(naming);
                        newName = new Name(new LdapName(rdns).toString());
                    }
                } catch (InvalidNameException e) {
                    .error("Error retrieving new DN. Ignore rename request."e);
                }
            }
        }
        // ---------------------------------
        // ---------------------------------
        // Perform modify/rename
        // ---------------------------------
        final Pair<AttributesADGuardedPasswordAttributeattrToModify = getAttributesToModify(objattrToBeUpdated);
        final Attributes ldapAttrs = attrToModify.first;
        // Update the attributes.
        modifyAttributes(entryDNattrToModify.);
        // Rename the entry if needed.
        if (newName != null) {
            entryDN = .getSchemaMapping().rename(entryDNnewName);
        }
        // ---------------------------------
        // ---------------------------------
        // Perform group memberships
        // ---------------------------------
        final List<StringldapGroups = getStringListValue(attrToBeUpdated.);
        if (ldapGroups != null) {
            final Set<StringoldMemberships = new TreeSet<String>(.);
            oldMemberships.addAll(.getLdapGroups(entryDN));
            final Set<StringnewMemberships = new TreeSet<String>(.);
            newMemberships.addAll(ldapGroups);
            // Update the LDAP groups.
            final Modification<GroupMembershipldapGroupMod = new Modification<GroupMembership>();
            if (!newMemberships.equals(oldMemberships)) {
                oldMemberships.addAll(newMemberships);
                for (String membership : oldMemberships) {
                    ldapGroupMod.remove(new GroupMembership(entryDNmembership));
                }
                for (String membership : newMemberships) {
                    ldapGroupMod.add(new GroupMembership(entryDNmembership));
                }
            }
            .modifyLdapGroupMemberships(ldapGroupMod);
        }
        // ---------------------------------
        return .getSchemaMapping().createUid(entryDN);
    }
    public Uid addAttributeValues(Set<Attributeattrs) {
        final ConnectorObject obj = getEntryToBeUpdated();
        final String entryDN = obj.getName().getNameValue();
        final Pair<AttributesADGuardedPasswordAttributeattrsToModify = getAttributesToModify(objattrs);
        modifyAttributes(entryDNattrsToModify.);
        List<StringldapGroups = getStringListValue(attrs.);
        if (!isEmpty(ldapGroups)) {
            .addLdapGroupMemberships(entryDNldapGroups);
        }
        return ;
    }
    public Uid removeAttributeValues(Set<Attributeattrs) {
        final ConnectorObject obj = getEntryToBeUpdated();
        final String entryDN = obj.getName().getNameValue();
        final Pair<AttributesADGuardedPasswordAttributeattrsToModify = getAttributesToModify(objattrs);
        Attributes ldapAttrs = attrsToModify.first;
        modifyAttributes(entryDNattrsToModify.);
        List<StringldapGroups = getStringListValue(attrs.);
        if (!isEmpty(ldapGroups)) {
            .removeLdapGroupMemberships(entryDNldapGroups);
        }
        return ;
    }
            final ConnectorObject objfinal Set<Attributeattrs) {
        final BasicAttributes ldapAttrs = new BasicAttributes();
        ADGuardedPasswordAttribute pwdAttr = null;
        for (Attribute attr : attrs) {
            javax.naming.directory.Attribute ldapAttr = null;
            if (attr.is(.)) {
                throw new IllegalArgumentException("Unable to modify an object's uid");
            } else if (attr.is(.)) {
                // Such a change would have been handled in update() above.
                throw new IllegalArgumentException("Unable to modify an object's name");
            } else if (LdapConstants.isLdapGroups(attr.getName())) {
                // Handled elsewhere.
            } else if (attr.is(.)) {
                pwdAttr = ADGuardedPasswordAttribute.create(.getConfiguration().getPasswordAttribute(), attr);
            } else if (attr.is(.)) {
                final Attribute uac = obj.getAttributeByName();
                int uacValue = uac != null && uac.getValue() != null && !uac.getValue().isEmpty()
                        ? Integer.parseInt(uac.getValue().get(0).toString()) : 0;
                boolean enabled = attr.getValue() == null
                        || attr.getValue().isEmpty() || Boolean.parseBoolean(attr.getValue().get(0).toString());
                if (enabled) {
                    // if not enabled yet --> enable removing 0x00002
                    if (uacValue % 16 == ) {
                        uacValue -= ;
                    }
                } else {
                    // if not disabled yet --> disable adding 0x00002
                    if (uacValue % 16 != ) {
                        uacValue += ;
                    }
                }
                ldapAttr = .getSchemaMapping().encodeAttribute(
                        , AttributeBuilder.build(, Integer.toString(uacValue)));
            } else {
                ldapAttr = .getSchemaMapping().encodeAttribute(attr);
            }
            if (ldapAttr != null) {
                final javax.naming.directory.Attribute existingAttr = ldapAttrs.get(ldapAttr.getID());
                if (existingAttr != null) {
                    try {
                        NamingEnumeration<?> all = ldapAttr.getAll();
                        while (all.hasMoreElements()) {
                            existingAttr.add(all.nextElement());
                        }
                    } catch (NamingException e) {
                        throw new ConnectorException(e);
                    }
                } else {
                    ldapAttrs.put(ldapAttr);
                }
            }
        }
        return new Pair<AttributesADGuardedPasswordAttribute>(
                ldapAttrspwdAttr);
    }
    private void modifyAttributes(
            final String entryDN,
            final Pair<AttributesADGuardedPasswordAttributeattrs,
            final int modifyOp) {
        final List<ModificationItemmodItems = new ArrayList<ModificationItem>(attrs.first.size());
        NamingEnumeration<? extends javax.naming.directory.AttributeattrEnum = attrs.first.getAll();
        while (attrEnum.hasMoreElements()) {
            modItems.add(new ModificationItem(modifyOpattrEnum.nextElement()));
        }
        if (attrs.second != null) {
            attrs.second.access(new Accessor() {
                @Override
                public void access(BasicAttribute attr) {
                    try {
                        if (attr.get() != null) {
                            modItems.add(new ModificationItem(modifyOpattr));
                            modifyAttributes(entryDNmodItems);
                        }
                    } catch (NamingException e) {
                        .error(e"Error retrieving password value");
                    }
                }
            });
        }
        modifyAttributes(entryDNmodItems);
    }
    private void modifyAttributes(
            final String entryDNfinal List<ModificationItemmodItems) {
        try {
            .getInitialContext().modifyAttributes(entryDNmodItems.toArray(new ModificationItem[modItems.size()]));
        } catch (NamingException e) {
            throw new ConnectorException(e);
        }
    }
    private List<StringgetStringListValue(
            final Set<Attributeattrsfinal String attrName) {
        final Attribute attr = AttributeUtil.find(attrNameattrs);
        if (attr != null) {
            return checkedListByFilter(nullAsEmpty(attr.getValue()), String.class);
        }
        return null;
    }
        final String filter = .getConfiguration().getUidAttribute() + "=" + .getUidValue();
        final ConnectorObject obj = LdapSearches.findObject(
                , LdapFilter.forNativeFilter(filter), );
        if (obj == null) {
            throw new ConnectorException("Entry not found");
        }
        return obj;
    }
New to GrepCode? Check out our FAQ X