Search in sources :

Example 81 with DefaultAttribute

use of org.apache.directory.api.ldap.model.entry.DefaultAttribute in project directory-fortress-core by apache.

the class LdapDataProvider method loadProperties.

/**
 * Given a collection of {@link java.util.Properties}, convert to raw data name-value format and load into ldap modification set in preparation for ldap add.
 *
 * @param props    contains {@link java.util.Properties} targeted for adding to ldap.
 * @param entry    contains ldap entry to push attrs into.
 * @param attrName contains the name of the ldap attribute to be added.
 * @param separator contains the char value used to separate name and value in ldap raw format.
 * @throws LdapException If we weren't able to add the properies into the entry
 */
protected void loadProperties(Properties props, Entry entry, String attrName, char separator) throws LdapException {
    if ((props != null) && (props.size() > 0)) {
        Attribute attr = null;
        for (Enumeration<?> e = props.propertyNames(); e.hasMoreElements(); ) {
            // This LDAP attr is stored as a name-value pair separated by a ':'.
            String key = (String) e.nextElement();
            String val = props.getProperty(key);
            String prop = key + separator + val;
            if (attr == null) {
                attr = new DefaultAttribute(attrName);
            } else {
                attr.add(prop);
            }
        }
        if (attr != null) {
            entry.add(attr);
        }
    }
}
Also used : DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute)

Example 82 with DefaultAttribute

use of org.apache.directory.api.ldap.model.entry.DefaultAttribute in project structr by structr.

the class StructrLDAPWrapper method matches.

private boolean matches(final LDAPNode node, final ExprNode filter) throws FrameworkException, LdapInvalidAttributeValueException {
    if (filter instanceof SimpleNode) {
        return evaluateSimpleNode(node, (SimpleNode) filter);
    } else if (filter instanceof SubstringNode) {
        return evaluateSubstringNode(node, (SubstringNode) filter);
    } else if (filter instanceof PresenceNode) {
        final PresenceNode presence = (PresenceNode) filter;
        final Attribute attribute = new DefaultAttribute(presence.getAttributeType());
        return findAttribute(node, attribute.getId()) != null;
    } else if (filter instanceof OrNode) {
        final OrNode orNode = (OrNode) filter;
        for (final ExprNode child : orNode.getChildren()) {
            if (matches(node, child)) {
                return true;
            }
        }
        return false;
    } else if (filter instanceof AndNode) {
        final AndNode andNode = (AndNode) filter;
        boolean result = true;
        for (final ExprNode child : andNode.getChildren()) {
            result &= matches(node, child);
        }
        return result;
    } else {
        System.out.println("Unsupported filter type " + filter.getClass());
    }
    return false;
}
Also used : ExprNode(org.apache.directory.api.ldap.model.filter.ExprNode) SubstringNode(org.apache.directory.api.ldap.model.filter.SubstringNode) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) NodeAttribute(org.structr.core.graph.NodeAttribute) LDAPAttribute(org.structr.ldap.entity.LDAPAttribute) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) PresenceNode(org.apache.directory.api.ldap.model.filter.PresenceNode) AndNode(org.apache.directory.api.ldap.model.filter.AndNode) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) SimpleNode(org.apache.directory.api.ldap.model.filter.SimpleNode) OrNode(org.apache.directory.api.ldap.model.filter.OrNode)

Example 83 with DefaultAttribute

use of org.apache.directory.api.ldap.model.entry.DefaultAttribute in project structr by structr.

the class StructrLDAPWrapper method evaluateSubstringNode.

private boolean evaluateSubstringNode(final LDAPNode node, final SubstringNode substringNode) throws FrameworkException, LdapInvalidAttributeValueException {
    final Attribute attribute = new DefaultAttribute(substringNode.getAttributeType());
    final String oid = attribute.getId();
    final String initialPart = substringNode.getInitial();
    final String finalPart = substringNode.getFinal();
    final List<String> any = new LinkedList<>();
    final List<String> fromNode = substringNode.getAny();
    // add fragments from substring node (if present)
    if (fromNode != null) {
        any.addAll(fromNode);
    }
    final Pattern pattern = SubstringNode.getRegex(initialPart, any.toArray(new String[0]), finalPart);
    for (final LDAPAttribute attr : node.getAttributes()) {
        if (oid.equals(attr.getOid())) {
            for (final LDAPValue value : attr.getValues()) {
                final String stringValue = value.getStringValue().toLowerCase();
                final Matcher matcher = pattern.matcher(stringValue);
                if (matcher.matches()) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : Pattern(java.util.regex.Pattern) LDAPAttribute(org.structr.ldap.entity.LDAPAttribute) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) NodeAttribute(org.structr.core.graph.NodeAttribute) LDAPAttribute(org.structr.ldap.entity.LDAPAttribute) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) Matcher(java.util.regex.Matcher) LDAPValue(org.structr.ldap.entity.LDAPValue) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) LinkedList(java.util.LinkedList)

Example 84 with DefaultAttribute

use of org.apache.directory.api.ldap.model.entry.DefaultAttribute in project structr by structr.

the class StructrLDAPWrapper method getAttribute.

public Attribute getAttribute(final LDAPAttribute src) throws LdapInvalidAttributeValueException {
    final AttributeType type = schemaManager.getAttributeType(src.getOid());
    final Attribute attribute = new DefaultAttribute(type);
    final String name = src.getUserProvidedId();
    if (name != null) {
        attribute.setUpId(name);
    }
    for (final LDAPValue value : src.getValues()) {
        attribute.add(value.getStringValue());
    }
    return attribute;
}
Also used : DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) NodeAttribute(org.structr.core.graph.NodeAttribute) LDAPAttribute(org.structr.ldap.entity.LDAPAttribute) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) LDAPValue(org.structr.ldap.entity.LDAPValue) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute)

Example 85 with DefaultAttribute

use of org.apache.directory.api.ldap.model.entry.DefaultAttribute in project ldapchai by ldapchai.

the class ApacheLdapProviderImpl method deleteStringAttributeValue.

public void deleteStringAttributeValue(final String entryDN, final String attributeName, final String value) throws ChaiOperationException, ChaiUnavailableException, IllegalStateException {
    activityPreCheck();
    getInputValidator().deleteStringAttributeValue(entryDN, attributeName, value);
    try {
        final ModifyRequest modifyRequest = new ModifyRequestImpl();
        modifyRequest.setName(new Dn(entryDN));
        if (value == null) {
            final Modification modification = new DefaultModification(ModificationOperation.REMOVE_ATTRIBUTE, attributeName);
            modifyRequest.addModification(modification);
        } else {
            final Modification modification = new DefaultModification();
            modification.setOperation(ModificationOperation.REMOVE_ATTRIBUTE);
            modification.setAttribute(new DefaultAttribute(attributeName, value));
        }
        final ModifyResponse response = connection.modify(modifyRequest);
        processResponse(response);
    } catch (LdapException e) {
        throw ChaiOperationException.forErrorMessage(e.getMessage());
    }
}
Also used : DefaultModification(org.apache.directory.api.ldap.model.entry.DefaultModification) Modification(org.apache.directory.api.ldap.model.entry.Modification) ModifyRequestImpl(org.apache.directory.api.ldap.model.message.ModifyRequestImpl) DefaultModification(org.apache.directory.api.ldap.model.entry.DefaultModification) Dn(org.apache.directory.api.ldap.model.name.Dn) ModifyRequest(org.apache.directory.api.ldap.model.message.ModifyRequest) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) ModifyResponse(org.apache.directory.api.ldap.model.message.ModifyResponse)

Aggregations

DefaultAttribute (org.apache.directory.api.ldap.model.entry.DefaultAttribute)159 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)131 Test (org.junit.Test)106 DefaultModification (org.apache.directory.api.ldap.model.entry.DefaultModification)41 Modification (org.apache.directory.api.ldap.model.entry.Modification)40 Entry (org.apache.directory.api.ldap.model.entry.Entry)36 DefaultEntry (org.apache.directory.api.ldap.model.entry.DefaultEntry)35 Value (org.apache.directory.api.ldap.model.entry.Value)20 ByteArrayInputStream (java.io.ByteArrayInputStream)13 ObjectInputStream (java.io.ObjectInputStream)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 ObjectOutputStream (java.io.ObjectOutputStream)12 Dn (org.apache.directory.api.ldap.model.name.Dn)12 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)11 AttributeType (org.apache.directory.api.ldap.model.schema.AttributeType)11 LdapInvalidAttributeValueException (org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException)8 ModifyRequest (org.apache.directory.api.ldap.model.message.ModifyRequest)7 ModifyRequestImpl (org.apache.directory.api.ldap.model.message.ModifyRequestImpl)7 ModifyResponse (org.apache.directory.api.ldap.model.message.ModifyResponse)7 HashSet (java.util.HashSet)6