Example usage for javax.naming Name add

List of usage examples for javax.naming Name add

Introduction

In this page you can find the example usage for javax.naming Name add.

Prototype

public Name add(String comp) throws InvalidNameException;

Source Link

Document

Adds a single component to the end of this name.

Usage

From source file:de.acosix.alfresco.mtsupport.repo.auth.ldap.EnhancedLDAPUserRegistry.java

/**
 * Converts a given DN into one suitable for use through JNDI. In particular, escapes special characters such as '/'
 * which have special meaning to JNDI.//from w  w  w  .j  a va 2  s.c o m
 *
 * @param dn
 *            the dn
 * @return the name
 * @throws InvalidNameException
 *             the invalid name exception
 */
protected static Name jndiName(final String dn) throws InvalidNameException {
    final Name n = new CompositeName();
    n.add(dn);
    return n;
}

From source file:com.liferay.portal.security.ldap.internal.exportimport.LDAPUserExporterImpl.java

protected Binding addGroup(long ldapServerId, LdapContext ldapContext, UserGroup userGroup, User user,
        Properties groupMappings, Properties userMappings) throws Exception {

    Name name = new CompositeName();

    name.add(_portalToLDAPConverter.getGroupDNName(ldapServerId, userGroup, groupMappings));

    Attributes attributes = _portalToLDAPConverter.getLDAPGroupAttributes(ldapServerId, userGroup, user,
            groupMappings, userMappings);

    ldapContext.bind(name, new PortalLDAPContext(attributes));

    Binding binding = _portalLDAP.getGroup(ldapServerId, userGroup.getCompanyId(), userGroup.getName());

    return binding;
}

From source file:com.liferay.portal.security.ldap.internal.exportimport.LDAPUserExporterImpl.java

protected Binding addUser(long ldapServerId, LdapContext ldapContext, User user, Properties userMappings)
        throws Exception {

    Name name = new CompositeName();

    name.add(_portalToLDAPConverter.getUserDNName(ldapServerId, user, userMappings));

    Attributes attributes = _portalToLDAPConverter.getLDAPUserAttributes(ldapServerId, user, userMappings);

    ldapContext.bind(name, new PortalLDAPContext(attributes));

    Binding binding = _portalLDAP.getUser(ldapServerId, user.getCompanyId(), user.getScreenName(),
            user.getEmailAddress());//from   ww w.j a  v a2s . c  om

    return binding;
}

From source file:com.liferay.portal.security.ldap.internal.exportimport.LDAPUserExporterImpl.java

@Override
public void exportUser(Contact contact, Map<String, Serializable> contactExpandoAttributes) throws Exception {

    long companyId = contact.getCompanyId();

    StopWatch stopWatch = new StopWatch();

    if (_log.isDebugEnabled()) {
        stopWatch.start();//from  w w w  . j a v  a  2  s . c om

        _log.debug("Exporting contact " + contact);
    }

    if (!_ldapSettings.isExportEnabled(companyId)) {
        return;
    }

    User user = _userLocalService.getUserByContactId(contact.getContactId());

    if (user.isDefaultUser() || (user.getStatus() != WorkflowConstants.STATUS_APPROVED)) {

        return;
    }

    long ldapServerId = _portalLDAP.getLdapServerId(companyId, user.getScreenName(), user.getEmailAddress());

    LdapContext ldapContext = _portalLDAP.getContext(ldapServerId, companyId);

    try {
        if (ldapContext == null) {
            return;
        }

        Properties contactMappings = _ldapSettings.getContactMappings(ldapServerId, companyId);
        Properties contactExpandoMappings = _ldapSettings.getContactExpandoMappings(ldapServerId, companyId);

        Binding binding = _portalLDAP.getUser(ldapServerId, contact.getCompanyId(), user.getScreenName(),
                user.getEmailAddress());

        if (binding == null) {
            Properties userMappings = _ldapSettings.getUserMappings(ldapServerId, companyId);

            binding = addUser(ldapServerId, ldapContext, user, userMappings);
        }

        Name name = new CompositeName();

        name.add(binding.getNameInNamespace());

        Modifications modifications = _portalToLDAPConverter.getLDAPContactModifications(contact,
                contactExpandoAttributes, contactMappings, contactExpandoMappings);

        if (modifications == null) {
            return;
        }

        ModificationItem[] modificationItems = modifications.getItems();

        ldapContext.modifyAttributes(name, modificationItems);
    } finally {
        if (ldapContext != null) {
            ldapContext.close();
        }

        if (_log.isDebugEnabled()) {
            _log.debug(StringBundler.concat("Finished exporting contact ", String.valueOf(contact), " in ",
                    String.valueOf(stopWatch.getTime()), "ms"));
        }
    }
}

From source file:com.liferay.portal.security.ldap.internal.exportimport.LDAPUserExporterImpl.java

@Override
public void exportUser(long userId, long userGroupId, UserOperation userOperation) throws Exception {

    User user = _userLocalService.getUser(userId);

    long companyId = user.getCompanyId();

    StopWatch stopWatch = new StopWatch();

    if (_log.isDebugEnabled()) {
        stopWatch.start();/*from  w  ww  .  j  a  v a  2  s. c o  m*/

        _log.debug(StringBundler.concat("Exporting user ", String.valueOf(user), " in user group ",
                String.valueOf(userGroupId)));
    }

    if (!_ldapSettings.isExportEnabled(companyId) || !_ldapSettings.isExportGroupEnabled(companyId)) {

        return;
    }

    long ldapServerId = _portalLDAP.getLdapServerId(companyId, user.getScreenName(), user.getEmailAddress());

    LdapContext ldapContext = _portalLDAP.getContext(ldapServerId, companyId);

    if (ldapContext == null) {
        return;
    }

    UserGroup userGroup = _userGroupLocalService.getUserGroup(userGroupId);

    Properties groupMappings = _ldapSettings.getGroupMappings(ldapServerId, companyId);
    Properties userMappings = _ldapSettings.getUserMappings(ldapServerId, companyId);

    Binding binding = _portalLDAP.getGroup(ldapServerId, companyId, userGroup.getName());

    if (binding == null) {
        if (userOperation == UserOperation.ADD) {
            addGroup(ldapServerId, ldapContext, userGroup, user, groupMappings, userMappings);
        } else {
            if (_log.isWarnEnabled()) {
                _log.warn("Unable to get or add LDAP bindings for user group " + userGroup.getName());
            }
        }

        return;
    }

    try {
        Name name = new CompositeName();

        name.add(binding.getNameInNamespace());

        Modifications modifications = _portalToLDAPConverter.getLDAPGroupModifications(ldapServerId, userGroup,
                user, groupMappings, userMappings, userOperation);

        ModificationItem[] modificationItems = modifications.getItems();

        ldapContext.modifyAttributes(name, modificationItems);
    } catch (SchemaViolationException sve) {
        if (_log.isInfoEnabled()) {
            _log.info("Unable to update LDAP bindings for user group " + userGroup.getName(), sve);
        }

        String fullGroupDN = binding.getNameInNamespace();

        Attributes attributes = _portalLDAP.getGroupAttributes(ldapServerId, companyId, ldapContext,
                fullGroupDN, true);

        Attribute groupMembers = attributes.get(groupMappings.getProperty(GroupConverterKeys.USER));

        if ((groupMembers != null) && (groupMembers.size() == 1)) {
            ldapContext.unbind(fullGroupDN);
        }
    } finally {
        if (ldapContext != null) {
            ldapContext.close();
        }

        if (_log.isDebugEnabled()) {
            _log.debug(StringBundler.concat("Finished exporting user ", String.valueOf(user), " in user group ",
                    String.valueOf(userGroupId), " in ", String.valueOf(stopWatch.getTime()), "ms"));
        }
    }
}

From source file:com.liferay.portal.security.ldap.internal.exportimport.LDAPUserExporterImpl.java

@Override
public void exportUser(User user, Map<String, Serializable> userExpandoAttributes) throws Exception {

    if (user.isDefaultUser() || (user.getStatus() != WorkflowConstants.STATUS_APPROVED)) {

        return;/*from w w  w. j  av a 2  s.co  m*/
    }

    long companyId = user.getCompanyId();

    if (!_ldapSettings.isExportEnabled(companyId)) {
        return;
    }

    long ldapServerId = _portalLDAP.getLdapServerId(companyId, user.getScreenName(), user.getEmailAddress());

    LdapContext ldapContext = _portalLDAP.getContext(ldapServerId, companyId);

    try {
        if (ldapContext == null) {
            return;
        }

        Properties userMappings = _ldapSettings.getUserMappings(ldapServerId, companyId);
        Properties userExpandoMappings = _ldapSettings.getUserExpandoMappings(ldapServerId, companyId);

        Binding binding = _portalLDAP.getUser(ldapServerId, user.getCompanyId(), user.getScreenName(),
                user.getEmailAddress(), true);

        if (binding == null) {
            binding = addUser(ldapServerId, ldapContext, user, userMappings);
        } else {
            Attributes attributes = _portalLDAP.getUserAttributes(ldapServerId, companyId, ldapContext,
                    binding.getNameInNamespace());

            String modifyTimestamp = LDAPUtil.getAttributeString(attributes, "modifyTimestamp");

            if (Validator.isNotNull(modifyTimestamp)) {
                Date modifiedDate = LDAPUtil.parseDate(modifyTimestamp);

                if (modifiedDate.equals(user.getModifiedDate())) {
                    if (_log.isDebugEnabled()) {
                        _log.debug("Skipping user " + user.getEmailAddress()
                                + " because he is already synchronized");
                    }

                    return;
                }
            }
        }

        Name name = new CompositeName();

        name.add(binding.getNameInNamespace());

        Modifications modifications = _portalToLDAPConverter.getLDAPUserModifications(user,
                userExpandoAttributes, userMappings, userExpandoMappings);

        if (modifications == null) {
            return;
        }

        ModificationItem[] modificationItems = modifications.getItems();

        ldapContext.modifyAttributes(name, modificationItems);

        if (!_ldapSettings.isExportGroupEnabled(companyId)) {
            return;
        }

        List<UserGroup> userGroups = _userGroupLocalService.getUserUserGroups(user.getUserId());

        for (UserGroup userGroup : userGroups) {
            exportUser(user.getUserId(), userGroup.getUserGroupId(), UserOperation.ADD);
        }

        Modifications groupModifications = _portalToLDAPConverter.getLDAPUserGroupModifications(ldapServerId,
                userGroups, user, userMappings);

        ModificationItem[] groupModificationItems = groupModifications.getItems();

        if (groupModificationItems.length > 0) {
            ldapContext.modifyAttributes(name, groupModificationItems);
        }
    } catch (NameNotFoundException nnfe) {
        LDAPAuthConfiguration ldapAuthConfiguration = _ldapAuthConfigurationProvider
                .getConfiguration(companyId);

        if (ldapAuthConfiguration.required()) {
            throw nnfe;
        }

        _log.error(nnfe, nnfe);
    } finally {
        if (ldapContext != null) {
            ldapContext.close();
        }
    }
}

From source file:dk.magenta.ldap.LDAPMultiBaseUserRegistry.java

/**
 * Converts a given DN into one suitable for use through JNDI. In particular, escapes special characters such as '/'
 * which have special meaning to JNDI.//from   w  w  w.j ava2 s  . c o  m
 *
 * @param dn
 *            the dn
 * @return the name
 * @throws javax.naming.InvalidNameException
 *             the invalid name exception
 */
private static Name jndiName(String dn) throws InvalidNameException {
    Name n = new CompositeName();
    n.add(dn);
    return n;
}