Example usage for javax.naming.directory Attribute contains

List of usage examples for javax.naming.directory Attribute contains

Introduction

In this page you can find the example usage for javax.naming.directory Attribute contains.

Prototype

boolean contains(Object attrVal);

Source Link

Document

Determines whether a value is in the attribute.

Usage

From source file:edu.internet2.middleware.psp.ldap.LdapSpmlTarget.java

/**
 * Determine the schema entity appropriate for the given <code>LdapEntry</code>.
 * /*from   w  w w .  ja v  a2  s  .co m*/
 * @param entry the <code>LdapEntry</code>
 * @return the <code>PSODefintion</code>
 * @throws PspException if the schema entity cannot be determined.
 */
protected Pso getPSODefinition(LdapEntry entry) throws PspException {

    Attributes attributes = entry.getLdapAttributes().toAttributes();

    Pso definition = null;

    for (Pso psoDefinition : getPSP().getPsos(getId())) {
        PsoIdentifyingAttribute ia = psoDefinition.getPsoIdentifyingAttribute();
        if (ia == null) {
            continue;
        }
        String idAttrName = ia.getName();
        String idAttrValue = ia.getValue();
        Attribute attribute = attributes.get(idAttrName);
        if (attribute != null && attribute.contains(idAttrValue)) {
            if (definition != null) {
                LOG.error("More than one schema entity found for " + entry.getDn());
                throw new PspException("More than one schema entity found for " + entry.getDn());
            }
            definition = psoDefinition;
        }
    }
    if (definition == null) {
        LOG.error("Unable to determine schema entity for " + entry.getDn());
        throw new PspException("Unable to determine schema entity for " + entry.getDn());
    }

    return definition;
}

From source file:org.apache.directory.server.operations.bind.SaslBindIT.java

/**
 * Tests to make sure the server properly returns the supportedSASLMechanisms.
 *///from w ww  .j a va  2 s  . c o  m
@Test
public void testSupportedSASLMechanisms() throws Exception {
    // We have to tell the server that it should accept anonymous
    // auth, because we are reading the rootDSE
    getLdapServer().getDirectoryService().setAllowAnonymousAccess(true);

    // Point on rootDSE
    DirContext context = new InitialDirContext();

    Attributes attrs = context.getAttributes(Network.ldapLoopbackUrl(getLdapServer().getPort()),
            new String[] { "supportedSASLMechanisms" });

    //             Thread.sleep( 10 * 60 * 1000 );
    NamingEnumeration<? extends Attribute> answer = attrs.getAll();
    Attribute result = answer.next();
    assertEquals(6, result.size());
    assertTrue(result.contains(SupportedSaslMechanisms.GSSAPI));
    assertTrue(result.contains(SupportedSaslMechanisms.DIGEST_MD5));
    assertTrue(result.contains(SupportedSaslMechanisms.CRAM_MD5));
    assertTrue(result.contains(SupportedSaslMechanisms.NTLM));
    assertTrue(result.contains(SupportedSaslMechanisms.PLAIN));
    assertTrue(result.contains(SupportedSaslMechanisms.GSS_SPNEGO));
}

From source file:org.libreplan.web.users.services.LDAPCustomAuthenticationProvider.java

@SuppressWarnings("unchecked")
private List<String> getRolesUsingNodeStrategy(Set<ConfigurationRolesLDAP> rolesLdap, String queryRoles,
        final LDAPConfiguration configuration) {

    String roleProperty = configuration.getLdapRoleProperty();

    List<String> rolesReturn = new ArrayList<>();
    for (ConfigurationRolesLDAP roleLDAP : rolesLdap) {
        if (roleLDAP.getRoleLdap().equals(WILDCHAR_ALL)) {
            rolesReturn.add(roleLDAP.getRoleLibreplan());
            continue;
        }/*from   www.java2s.c  o m*/

        // We must make a search for each role-matching in nodes
        List<Attribute> resultsSearch = new ArrayList<>();
        resultsSearch.addAll(ldapTemplate.search(DistinguishedName.EMPTY_PATH,
                new EqualsFilter(roleProperty, roleLDAP.getRoleLdap()).toString(), new AttributesMapper() {
                    @Override
                    public Object mapFromAttributes(Attributes attributes) throws NamingException {
                        return attributes.get(configuration.getLdapUserId());
                    }
                }));

        for (Attribute atrib : resultsSearch) {
            if (atrib.contains(queryRoles)) {
                rolesReturn.add(roleLDAP.getRoleLibreplan());
            }
        }
    }

    return rolesReturn;
}

From source file:org.nuxeo.ecm.directory.ldap.LDAPReference.java

/**
 * Store new links using the LDAP staticAttributeId strategy.
 *
 * @see org.nuxeo.ecm.directory.Reference#addLinks(String, List)
 *//*ww  w .j  ava  2 s .c  om*/
@Override
public void addLinks(String sourceId, List<String> targetIds) throws DirectoryException {

    if (targetIds.isEmpty()) {
        // optim: nothing to do, return silently without further creating
        // session instances
        return;
    }

    LDAPDirectory ldapTargetDirectory = (LDAPDirectory) getTargetDirectory();
    LDAPDirectory ldapSourceDirectory = (LDAPDirectory) getSourceDirectory();
    String attributeId = getStaticAttributeId();
    if (attributeId == null) {
        if (log.isTraceEnabled()) {
            log.trace(String.format("trying to edit a non-static reference from %s in directory %s: ignoring",
                    sourceId, ldapSourceDirectory.getName()));
        }
        return;
    }
    try (LDAPSession targetSession = (LDAPSession) ldapTargetDirectory.getSession();
            LDAPSession sourceSession = (LDAPSession) ldapSourceDirectory.getSession()) {
        // fetch the entry to be able to run the security policy
        // implemented in an entry adaptor
        DocumentModel sourceEntry = sourceSession.getEntry(sourceId, false);
        if (sourceEntry == null) {
            throw new DirectoryException(String.format("could not add links from unexisting %s in directory %s",
                    sourceId, ldapSourceDirectory.getName()));
        }
        if (!BaseSession.isReadOnlyEntry(sourceEntry)) {
            SearchResult ldapEntry = sourceSession.getLdapEntry(sourceId);

            String sourceDn = ldapEntry.getNameInNamespace();
            Attribute storedAttr = ldapEntry.getAttributes().get(attributeId);
            String emptyRefMarker = ldapSourceDirectory.getDescriptor().getEmptyRefMarker();
            Attribute attrToAdd = new BasicAttribute(attributeId);
            for (String targetId : targetIds) {
                if (staticAttributeIdIsDn) {
                    // TODO optim: avoid LDAP search request when targetDn
                    // can be forged client side (rdnAttribute = idAttribute and scope is onelevel)
                    ldapEntry = targetSession.getLdapEntry(targetId);
                    if (ldapEntry == null) {
                        log.warn(String.format(
                                "entry '%s' in directory '%s' not found: could not add link from '%s' in directory '%s' for '%s'",
                                targetId, ldapTargetDirectory.getName(), sourceId,
                                ldapSourceDirectory.getName(), this));
                        continue;
                    }
                    String dn = ldapEntry.getNameInNamespace();
                    if (storedAttr == null || !storedAttr.contains(dn)) {
                        attrToAdd.add(dn);
                    }
                } else {
                    if (storedAttr == null || !storedAttr.contains(targetId)) {
                        attrToAdd.add(targetId);
                    }
                }
            }
            if (attrToAdd.size() > 0) {
                try {
                    // do the LDAP request to store missing dns
                    Attributes attrsToAdd = new BasicAttributes();
                    attrsToAdd.put(attrToAdd);

                    if (log.isDebugEnabled()) {
                        log.debug(String.format(
                                "LDAPReference.addLinks(%s, [%s]): LDAP modifyAttributes dn='%s' "
                                        + "mod_op='ADD_ATTRIBUTE' attrs='%s' [%s]",
                                sourceId, StringUtils.join(targetIds, ", "), sourceDn, attrsToAdd, this));
                    }
                    sourceSession.dirContext.modifyAttributes(sourceDn, DirContext.ADD_ATTRIBUTE, attrsToAdd);

                    // robustly clean any existing empty marker now that we are sure that the list in not empty
                    if (storedAttr.contains(emptyRefMarker)) {
                        Attributes cleanAttrs = new BasicAttributes(attributeId, emptyRefMarker);

                        if (log.isDebugEnabled()) {
                            log.debug(String.format(
                                    "LDAPReference.addLinks(%s, [%s]): LDAP modifyAttributes dn='%s'"
                                            + " mod_op='REMOVE_ATTRIBUTE' attrs='%s' [%s]",
                                    sourceId, StringUtils.join(targetIds, ", "), sourceDn, cleanAttrs, this));
                        }
                        sourceSession.dirContext.modifyAttributes(sourceDn, DirContext.REMOVE_ATTRIBUTE,
                                cleanAttrs);
                    }
                } catch (SchemaViolationException e) {
                    if (isDynamic()) {
                        // we are editing an entry that has no static part
                        log.warn(String.format("cannot update dynamic reference in field %s for source %s",
                                getFieldName(), sourceId));
                    } else {
                        // this is a real schema configuration problem,
                        // wrap up the exception
                        throw new DirectoryException(e);
                    }
                }
            }
        }
    } catch (NamingException e) {
        throw new DirectoryException("addLinks failed: " + e.getMessage(), e);
    }
}

From source file:org.nuxeo.ecm.directory.ldap.LDAPReference.java

/**
 * Store new links using the LDAP staticAttributeId strategy.
 *
 * @see org.nuxeo.ecm.directory.Reference#addLinks(List, String)
 *///from  w  ww.  jav a  2s .com
@Override
public void addLinks(List<String> sourceIds, String targetId) throws DirectoryException {
    String attributeId = getStaticAttributeId();
    if (attributeId == null && !sourceIds.isEmpty()) {
        log.warn("trying to edit a non-static reference: ignoring");
        return;
    }
    LDAPDirectory ldapTargetDirectory = (LDAPDirectory) getTargetDirectory();
    LDAPDirectory ldapSourceDirectory = (LDAPDirectory) getSourceDirectory();

    String emptyRefMarker = ldapSourceDirectory.getDescriptor().getEmptyRefMarker();
    try (LDAPSession targetSession = (LDAPSession) ldapTargetDirectory.getSession();
            LDAPSession sourceSession = (LDAPSession) ldapSourceDirectory.getSession()) {
        if (!sourceSession.isReadOnly()) {
            // compute the target dn to add to all the matching source
            // entries
            SearchResult ldapEntry = targetSession.getLdapEntry(targetId);
            if (ldapEntry == null) {
                throw new DirectoryException(
                        String.format("could not add links to unexisting %s in directory %s", targetId,
                                ldapTargetDirectory.getName()));
            }
            String targetAttributeValue;
            if (staticAttributeIdIsDn) {
                targetAttributeValue = ldapEntry.getNameInNamespace();
            } else {
                targetAttributeValue = targetId;
            }

            for (String sourceId : sourceIds) {
                // fetch the entry to be able to run the security policy
                // implemented in an entry adaptor
                DocumentModel sourceEntry = sourceSession.getEntry(sourceId, false);
                if (sourceEntry == null) {
                    log.warn(String.format(
                            "entry %s in directory %s not found: could not add link to %s in directory %s",
                            sourceId, ldapSourceDirectory.getName(), targetId, ldapTargetDirectory.getName()));
                    continue;
                }
                if (BaseSession.isReadOnlyEntry(sourceEntry)) {
                    // skip this entry since it cannot be edited to add the
                    // reference to targetId
                    log.warn(String.format(
                            "entry %s in directory %s is readonly: could not add link to %s in directory %s",
                            sourceId, ldapSourceDirectory.getName(), targetId, ldapTargetDirectory.getName()));
                    continue;
                }
                ldapEntry = sourceSession.getLdapEntry(sourceId);
                String sourceDn = ldapEntry.getNameInNamespace();
                Attribute storedAttr = ldapEntry.getAttributes().get(attributeId);
                if (storedAttr.contains(targetAttributeValue)) {
                    // no need to readd
                    continue;
                }
                try {
                    // add the new dn
                    Attributes attrs = new BasicAttributes(attributeId, targetAttributeValue);

                    if (log.isDebugEnabled()) {
                        log.debug(String.format(
                                "LDAPReference.addLinks([%s], %s): LDAP modifyAttributes dn='%s'"
                                        + " mod_op='ADD_ATTRIBUTE' attrs='%s' [%s]",
                                StringUtils.join(sourceIds, ", "), targetId, sourceDn, attrs, this));
                    }
                    sourceSession.dirContext.modifyAttributes(sourceDn, DirContext.ADD_ATTRIBUTE, attrs);

                    // robustly clean any existing empty marker now that we
                    // are sure that the list in not empty
                    if (storedAttr.contains(emptyRefMarker)) {
                        Attributes cleanAttrs = new BasicAttributes(attributeId, emptyRefMarker);
                        if (log.isDebugEnabled()) {
                            log.debug(String.format(
                                    "LDAPReference.addLinks(%s, %s): LDAP modifyAttributes dn='%s'"
                                            + " mod_op='REMOVE_ATTRIBUTE' attrs='%s' [%s]",
                                    StringUtils.join(sourceIds, ", "), targetId, sourceDn,
                                    cleanAttrs.toString(), this));
                        }
                        sourceSession.dirContext.modifyAttributes(sourceDn, DirContext.REMOVE_ATTRIBUTE,
                                cleanAttrs);
                    }
                } catch (SchemaViolationException e) {
                    if (isDynamic()) {
                        // we are editing an entry that has no static part
                        log.warn(String.format("cannot add dynamic reference in field %s for target %s",
                                getFieldName(), targetId));
                    } else {
                        // this is a real schema configuration problem,
                        // wrap the exception
                        throw new DirectoryException(e);
                    }
                }
            }
        }
    } catch (NamingException e) {
        throw new DirectoryException("addLinks failed: " + e.getMessage(), e);
    }
}

From source file:org.springframework.ldap.core.DirContextAdapter.java

public void addAttributeValue(String name, Object value, boolean addIfDuplicateExists) {
    if (!updateMode && value != null) {
        Attribute attr = originalAttrs.get(name);
        if (attr == null) {
            originalAttrs.put(name, value);
        } else {/* w  ww.  j  a  v  a 2 s  .c o  m*/
            attr.add(value);
        }
    } else if (updateMode) {
        Attribute attr = updatedAttrs.get(name);
        if (attr == null) {
            if (originalAttrs.get(name) == null) {
                // No match in the original attributes -
                // add a new Attribute to updatedAttrs
                updatedAttrs.put(name, value);
            } else {
                // The attribute exists in the original attributes - clone
                // that and add the new entry to it
                attr = (Attribute) originalAttrs.get(name).clone();
                if (addIfDuplicateExists || !attr.contains(value)) {
                    attr.add(value);
                }
                updatedAttrs.put(attr);
            }
        } else {
            attr.add(value);
        }
    }
}