Example usage for javax.naming CompositeName CompositeName

List of usage examples for javax.naming CompositeName CompositeName

Introduction

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

Prototype

public CompositeName() 

Source Link

Document

Constructs a new empty composite name.

Usage

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 ww  w  . j  ava  2 s  .c  o m*/

        _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  va  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;/* w w  w  . j  av a2s.  com*/
    }

    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: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 w ww  .j  a v a  2  s . c  o m

    return binding;
}

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   ww  w. j av  a 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: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 .ja  v  a2 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;
}

From source file:org.apache.ambari.server.serveraction.kerberos.ADKerberosOperationHandler.java

/**
 * Creates a new principal in a previously configured KDC
 * <p/>/*from www. j  a v a  2  s . c  o m*/
 * The implementation is specific to a particular type of KDC.
 *
 * @param principal a String containing the principal to add
 * @param password  a String containing the password to use when creating the principal
 * @param service   a boolean value indicating whether the principal is to be created as a service principal or not
 * @return an Integer declaring the generated key number
 * @throws KerberosOperationException
 */
@Override
public Integer createPrincipal(String principal, String password, boolean service)
        throws KerberosOperationException {
    if (!isOpen()) {
        throw new KerberosOperationException("This operation handler has not been opened");
    }

    if (principal == null) {
        throw new KerberosOperationException("principal is null");
    }
    if (password == null) {
        throw new KerberosOperationException("principal password is null");
    }

    DeconstructedPrincipal deconstructedPrincipal = createDeconstructPrincipal(principal);

    String realm = deconstructedPrincipal.getRealm();
    if (realm == null) {
        realm = "";
    }

    Map<String, Object> context = new HashMap<String, Object>();
    context.put("normalized_principal", deconstructedPrincipal.getNormalizedPrincipal());
    context.put("principal_name", deconstructedPrincipal.getPrincipalName());
    context.put("principal_primary", deconstructedPrincipal.getPrimary());
    context.put("principal_instance", deconstructedPrincipal.getInstance());
    context.put("realm", realm);
    context.put("realm_lowercase", realm.toLowerCase());
    context.put("password", password);
    context.put("is_service", service);
    context.put("container_dn", this.principalContainerDn);
    context.put("principal_digest", DigestUtils.sha1Hex(deconstructedPrincipal.getNormalizedPrincipal()));

    Map<String, Object> data = processCreateTemplate(context);

    Attributes attributes = new BasicAttributes();
    String cn = null;

    if (data != null) {
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();

            if ("unicodePwd".equals(key)) {
                if (value instanceof String) {
                    try {
                        attributes.put(new BasicAttribute("unicodePwd",
                                String.format("\"%s\"", password).getBytes("UTF-16LE")));
                    } catch (UnsupportedEncodingException ue) {
                        throw new KerberosOperationException("Can not encode password with UTF-16LE", ue);
                    }
                }
            } else {
                Attribute attribute = new BasicAttribute(key);
                if (value instanceof Collection) {
                    for (Object object : (Collection) value) {
                        attribute.add(object);
                    }
                } else {
                    attribute.add(value);

                    if ("cn".equals(key) && (value != null)) {
                        cn = value.toString();
                    }
                }
                attributes.put(attribute);
            }
        }
    }

    if (cn == null) {
        cn = deconstructedPrincipal.getNormalizedPrincipal();
    }
    try {
        Name name = new CompositeName().add(String.format("cn=%s,%s", cn, principalContainerDn));
        ldapContext.createSubcontext(name, attributes);
    } catch (NamingException ne) {
        throw new KerberosOperationException("Can not create principal : " + principal, ne);
    }
    return 0;
}

From source file:org.apache.directory.studio.connection.core.io.jndi.JNDIConnectionWrapper.java

/**
 * Gets a Name object that is save for JNDI operations.
 * <p>/*  w  ww.  j  a  v  a  2 s  .c o m*/
 * In JNDI we have could use the following classes for names:
 * <ul>
 * <li>Dn as String</li>
 * <li>javax.naming.CompositeName</li>
 * <li>javax.naming.ldap.LdapName (since Java5)</li>
 * <li>org.apache.directory.api.ldap.name.LdapDN</li>
 * </ul>
 * <p>
 * There are some drawbacks when using this classes:
 * <ul>
 * <li>When passing Dn as String, JNDI doesn't handle slashes '/' correctly.
 * So we must use a Name object here.</li>
 * <li>With CompositeName we have the same problem with slashes '/'.</li>
 * <li>When using LdapDN from shared-ldap, JNDI uses the toString() method
 * and LdapDN.toString() returns the normalized ATAV, but we need the
 * user provided ATAV.</li>
 * <li>When using LdapName for the empty Dn (Root DSE) JNDI _sometimes_ throws
 * an Exception (java.lang.IndexOutOfBoundsException: Posn: -1, Size: 0
 * at javax.naming.ldap.LdapName.getPrefix(LdapName.java:240)).</li>
 * <li>Using LdapDN for the RootDSE doesn't work with Apache Harmony because
 * its JNDI provider only accepts intstances of CompositeName or LdapName.</li>
 * </ul>
 * <p>
 * So we use LdapName as default and the CompositeName for the empty Dn.
 * 
 * @param name the Dn
 * 
 * @return the save JNDI name
 * 
 * @throws InvalidNameException the invalid name exception
 */
static Name getSaveJndiName(String name) throws InvalidNameException {
    if (name == null || StringUtils.isEmpty(name)) //$NON-NLS-1$
    {
        return new CompositeName();
    } else {
        return new LdapName(name);
    }
}

From source file:org.ballerinalang.auth.ldap.util.LdapUtils.java

/**
 * This method performs the additional level escaping for ldap search. In ldap search / and " characters
 * have to be escaped again./*from   ww w. jav  a2 s.c  om*/
 *
 * @param dn DN
 * @return composite name
 * @throws InvalidNameException failed to build composite name
 */
public static Name escapeDNForSearch(String dn) throws InvalidNameException {
    return new CompositeName().add(dn);
}