Example usage for javax.naming.ldap InitialLdapContext modifyAttributes

List of usage examples for javax.naming.ldap InitialLdapContext modifyAttributes

Introduction

In this page you can find the example usage for javax.naming.ldap InitialLdapContext modifyAttributes.

Prototype

public void modifyAttributes(String name, int mod_op, Attributes attrs) throws NamingException 

Source Link

Usage

From source file:de.fiz.ddb.aas.utils.LDAPEngineUtility.java

/**
 * set modification-properties in LDAP./* ww  w  .j  a  va2s.c o  m*/
 * 
 * @param isCreate
 *            if also createProperties should be set.
 * @param performer
 *            performer.
 * @param id
 *            id to set properties for.
 * @param scope
 *            scope.
 * 
 * @return
 * @throws
 */
public void setModificationPropertiesInLdap(boolean isCreate, AasPrincipal performer, String id, Scope scope)
        throws NamingException, IllegalAccessException {
    String propertiesAttName = null;
    if (scope == Scope.PERSON) {
        propertiesAttName = Constants.ldap_ddbPerson_Properties;
    } else if (scope == Scope.ORGANIZATION) {
        propertiesAttName = Constants.ldap_ddbOrg_Properties;
    } else {
        throw new IllegalAccessException("scope does not match");
    }

    Map<String, String> attributes = getResourceAttributes(scope, id,
            new String[] { propertiesAttName, Constants.ldap_ddb_EntryDN });
    DdbProperties properties = null;
    String entryDn = null;
    if (attributes != null && StringUtils.isNotBlank(attributes.get(Constants.ldap_ddb_EntryDN))) {
        if (StringUtils.isNotBlank(attributes.get(propertiesAttName))) {
            properties = serializer.deserialize(attributes.get(propertiesAttName));
        }
        properties = PropertiesHelper.setModificationProperties(properties, isCreate, performer);
        entryDn = attributes.get(Constants.ldap_ddb_EntryDN);
    } else {
        throw new NameNotFoundException("entry with id=" + id + " not found");
    }
    InitialLdapContext ctx = null;
    try {
        ctx = LDAPConnector.getSingletonInstance().takeCtx();
        Attributes saveAttributes = new BasicAttributes(true);
        saveAttributes.put(new BasicAttribute(propertiesAttName, serializer.serialize(properties)));
        ctx.modifyAttributes(entryDn, DirContext.REPLACE_ATTRIBUTE, saveAttributes);
    } finally {
        if (ctx != null) {
            try {
                LDAPConnector.getSingletonInstance().putCtx(ctx);
            } catch (IllegalAccessException ex) {
                LOG.log(Level.SEVERE, null, ex);
            }
        }
    }

}

From source file:de.fiz.ddb.aas.utils.LDAPEngineUtility.java

/**
 * set attributes of given resource./*from  w w w .  ja  v  a  2s .  c  o  m*/
 * 
 * @param scope
 *            scope
 * @param id
 *            id of resource
 * @param attributeNames
 *            attribute-names and values to set
 * 
 * @throws NamingException
 * @throws IllegalAccessException
 */
public void setResourceAttributes(Scope scope, String entryDn, Map<String, String> attributes)
        throws NamingException, IllegalAccessException {
    InitialLdapContext ctx = null;
    try {
        ctx = LDAPConnector.getSingletonInstance().takeCtx();
        BasicAttributes saveAttributes = new BasicAttributes(true);
        for (Entry<String, String> entry : attributes.entrySet()) {
            saveAttributes.put(new BasicAttribute(entry.getKey(), entry.getValue()));
        }
        ctx.modifyAttributes(entryDn, DirContext.REPLACE_ATTRIBUTE, saveAttributes);
    } finally {
        if (ctx != null) {
            try {
                LDAPConnector.getSingletonInstance().putCtx(ctx);
            } catch (IllegalAccessException ex) {
                LOG.log(Level.SEVERE, null, ex);
            }
        }
    }
}

From source file:de.fiz.ddb.aas.auxiliaryoperations.ThreadOrganisationUpdate.java

private void updateOrg() throws NameNotFoundException, AASUnauthorizedException, AttributeModificationException,
        ExecutionException {/*  w  w w  .j a  v  a  2  s  .  c om*/
    boolean vChange = false;
    InitialLdapContext vCtx = null;
    try {

        if (this._oldOrganisation == null) {
            LOG.log(Level.WARNING, "No such organization ''{0}'' with oid: ''{1}''.",
                    new Object[] { this._organisation.getDisplayName(), this._organisation.getOIDs() });
            throw new NameNotFoundException("No such organization '" + this._organisation.getDisplayName()
                    + "' with oid: '" + this._organisation.getOIDs() + "'.");
        }

        GeoAdresse vGeoAdresse;
        String vLocalDispalyName = null;
        if (_submit != null) { // hier ist "GeoLocationDisplayName" breits ausgefhrt
            try {
                vGeoAdresse = _submit.get(10, TimeUnit.SECONDS);
                if (vGeoAdresse.getRequestStatus() == GeoRequestStatus.OK) {
                    this._organisation.getAddress().setLatitude(vGeoAdresse.getLatitude());
                    this._organisation.getAddress().setLongitude(vGeoAdresse.getLongitude());
                    this._organisation.getAddress()
                            .setLocationDisplayName(vGeoAdresse.getLocationDisplayName());
                } else {
                    LOG.log(Level.WARNING, "GeoRequestStatus: {0}, (organization id: {1})",
                            new Object[] { vGeoAdresse.getRequestStatus(), this._organisation.getOIDs() });
                }
            } catch (InterruptedException ex) {
                LOG.log(Level.WARNING,
                        "Geocoding request exeption for organization id: " + this._organisation.getOIDs(), ex);
            } catch (TimeoutException ex) {
                LOG.log(Level.WARNING,
                        "Geocoding request exeption for organization id: " + this._organisation.getOIDs(), ex);
            }
        } else if (_submitGeoLocDisplayName != null) {
            try {
                vLocalDispalyName = _submitGeoLocDisplayName.get(5, TimeUnit.SECONDS);
                this._organisation.getAddress().setLocationDisplayName(vLocalDispalyName);
                //LOG.info("LocalDisplayName='" + vLocalDispalyName + "'" + vLocalDispalyName + "'");
            } catch (InterruptedException ex) {
                LOG.log(Level.WARNING,
                        this._organisation.getOIDs() + " without location display name: " + ex.getMessage());
            } catch (ExecutionException ex) {
                LOG.log(Level.WARNING,
                        this._organisation.getOIDs() + " without location display name: " + ex.getMessage());
            } catch (TimeoutException ex) {
                LOG.log(Level.WARNING,
                        this._organisation.getOIDs() + " without location display name: " + ex.getMessage());
            }

        }

        LOG.info("newOIDs: '" + this._organisation.getOIDs() + "'");
        LOG.info("oldOIDs: '" + this._oldOrganisation.getOIDs() + "'");

        if (this._organisation.getOrgRDN() == null) {
            // -- Ansonsten eine nicht gesetzte RDN kann zum Knall fhren...
            this._organisation.setOrgRDN(this._oldOrganisation.getOrgRDN());
        } else if (!this._organisation.getOrgRDN().equals(this._oldOrganisation.getOrgRDN())) {
            // -- Hier ist etwas faul...
            LOG.log(Level.WARNING,
                    "The organization ''{0}'' has RDN: ''{1}'', but there exist an organization ''{0}'' with RDN: ''{2}''!",
                    new Object[] { this._organisation.getId(), this._organisation.getOrgRDN(),
                            this._oldOrganisation.getOrgRDN() });
            throw new NameNotFoundException("No such organization '" + this._organisation.getDisplayName()
                    + "' with oid: '" + this._organisation.getOIDs() + "'.");
        }

        if (this.isPrivilegesUpdate()) {
            Set<PrivilegeEnum> removePrivileges = this.privilegeDiff(this._organisation.getPrivilegesSet(),
                    this._oldOrganisation.getPrivilegesSet());
            Set<PrivilegeEnum> addPrivileges = this.privilegeDiff(this._oldOrganisation.getPrivilegesSet(),
                    this._organisation.getPrivilegesSet());
            if (!removePrivileges.isEmpty() || !addPrivileges.isEmpty()) {
                vChange = true;
                for (PrivilegeEnum p : removePrivileges) {
                    ThreadSinglePrivilegeDelete threadSinglePrivilegeDelete = new ThreadSinglePrivilegeDelete(p,
                            this._organisation, this._performer);
                    threadSinglePrivilegeDelete.call();
                }
                for (PrivilegeEnum p : addPrivileges) {
                    ThreadSinglePrivilegeCreate threadSinglePrivilegeCreate = new ThreadSinglePrivilegeCreate(p,
                            this._organisation, this._performer);
                    threadSinglePrivilegeCreate.call();
                }
            }
        }

        Attributes orgAttributes = new BasicAttributes(true);
        Attributes orgRemoveAttributes = new BasicAttributes(true);

        if (vChange = this.convertOrganizationToLdapOrgAttrsForUpdate(this._organisation, this._oldOrganisation,
                orgAttributes, orgRemoveAttributes, getPerformer())) {

            // -- If any changes, the status is set to 'revised'
            //    but not if status will be explicitly changed or by a update operation on Licenses directory
            if (!isChangeOfStatus() && !isUpdatingOfLicensedOrgs()) {
                if ((ConstEnumOrgStatus.approved.equals(this._organisation.getStatus()))) {
                    // -- ...then go retrospectively into "revised" status:
                    this._organisation.setStatus(ConstEnumOrgStatus.revised);

                    orgAttributes.put(Constants.ldap_ddbOrg_Status,
                            String.valueOf(this._organisation.getStatus().name()));
                }
            }
        }
        // ---------------------------------------------------------------------
        if (vChange) {

            // -- Save changes to the corresponding directory:
            StringBuilder vOrgEntryDN = (isUpdatingOfLicensedOrgs()
                    ? this.getLicensedOrgsDN(this._organisation.getOIDs())
                    : this.getOrgDN(this._organisation.getOIDs()));
            LOG.log(Level.INFO, "DEBUG-Info: destination OrgEntryDN = '" + vOrgEntryDN + "'");

            vCtx = LDAPConnector.getSingletonInstance().takeCtx();
            if (orgRemoveAttributes.size() > 0) {
                vCtx.modifyAttributes(vOrgEntryDN.toString(), DirContext.REMOVE_ATTRIBUTE, orgRemoveAttributes);
            }
            vCtx.modifyAttributes(vOrgEntryDN.toString(), DirContext.REPLACE_ATTRIBUTE, orgAttributes);
        } else {
            throw new AttributeModificationException(
                    "Not modified: oid = '" + this._organisation.getOIDs() + "'");
        }

    } catch (RejectedExecutionException ex) {
        LOG.log(Level.SEVERE, "RejectedExecutionException\n{0}", ex);
        throw new ExecutionException(ex.getMessage(), ex.getCause());
    } catch (IllegalAccessException ex) {
        LOG.log(Level.SEVERE, "Connection-Error\n{0}", ex);
        throw new ExecutionException(ex.getMessage(), ex.getCause());
    } catch (NameNotFoundException ex) {
        LOG.log(Level.WARNING, null, ex);
        throw ex;
    } catch (AttributeModificationException ex) {
        LOG.log(Level.WARNING, "AttributeModificationException\n{0}", ex.getMessage());
        // !!!!AttributeModificationException extends NamingExeption:
        //throw ex;
        throw new AttributeModificationException(ex.getMessage());
    } catch (NamingException ne) {
        LOG.log(Level.SEVERE, "NamingException\n{0}", ne);
        throw new ExecutionException(ne.getMessage(), ne.getCause());
    } finally {
        if (vCtx != null) {
            try {
                LDAPConnector.getSingletonInstance().putCtx(vCtx);
            } catch (Exception ex) {
                LOG.log(Level.SEVERE, "Exception", ex);
            }
        }
    }

}

From source file:org.atricore.idbus.idojos.ldapidentitystore.LDAPIdentityStore.java

protected void replaceAttributes(String bane, Attributes atts) throws NamingException {
    InitialLdapContext ctx = this.createLdapInitialContext();
    ctx.modifyAttributes(bane, InitialLdapContext.REPLACE_ATTRIBUTE, atts);
}

From source file:org.josso.gateway.identity.service.store.ldap.LDAPIdentityStore.java

protected void replaceAttributes(String bane, Attributes atts) throws NamingException, IOException {
    InitialLdapContext ctx = null;
    try {//from  w ww  . ja v a 2  s. c om
        ctx = createLdapInitialContext(getUseBindCredentials());
    } catch (NamingException e) {
        if (getUseBindCredentials()) {
            // in case we are using virtual identity store
            return;
        } else {
            throw e;
        }
    }
    ctx.modifyAttributes(bane, InitialLdapContext.REPLACE_ATTRIBUTE, atts);
}