Search in sources :

Example 76 with Control

use of org.apache.directory.api.ldap.model.message.Control in project directory-ldap-api by apache.

the class LdifEntry method addControl.

/**
 * Add a control to the entry
 *
 * @param controls The added controls
 */
public void addControl(Control... controls) {
    if (controls == null) {
        throw new IllegalArgumentException(I18n.err(I18n.ERR_13432_NULL_ADDED_CONTROL));
    }
    for (Control control : controls) {
        if (changeType == ChangeType.None) {
            changeType = ChangeType.Add;
        }
        if (this.controls == null) {
            this.controls = new ConcurrentHashMap<>();
        }
        if (control instanceof LdifControl) {
            this.controls.put(control.getOid(), (LdifControl) control);
        } else {
            LdifControl ldifControl = new LdifControl(control.getOid());
            ldifControl.setCritical(control.isCritical());
            this.controls.put(control.getOid(), new LdifControl(control.getOid()));
        }
    }
}
Also used : Control(org.apache.directory.api.ldap.model.message.Control)

Example 77 with Control

use of org.apache.directory.api.ldap.model.message.Control in project directory-ldap-api by apache.

the class LdifEntry method equals.

/**
 * {@inheritDoc}
 */
@Override
public boolean equals(Object o) {
    // Basic equals checks
    if (this == o) {
        return true;
    }
    if (o == null) {
        return false;
    }
    if (!(o instanceof LdifEntry)) {
        return false;
    }
    LdifEntry otherEntry = (LdifEntry) o;
    // Check the Dn
    Dn thisDn = entryDn;
    Dn dnEntry = otherEntry.getDn();
    if (!thisDn.equals(dnEntry)) {
        return false;
    }
    // Check the changeType
    if (changeType != otherEntry.changeType) {
        return false;
    }
    // Check each different cases
    switch(changeType) {
        case None:
        // Fall through
        case Add:
            // Checks the attributes
            if (entry.size() != otherEntry.entry.size()) {
                return false;
            }
            if (!entry.equals(otherEntry.entry)) {
                return false;
            }
            break;
        case Delete:
            // Nothing to do, if the DNs are equals
            break;
        case Modify:
            // First, deal with special cases
            if (modificationList == null) {
                if (otherEntry.modificationList != null) {
                    return false;
                } else {
                    break;
                }
            }
            if (otherEntry.modificationList == null) {
                return false;
            }
            if (modificationList.size() != otherEntry.modificationList.size()) {
                return false;
            }
            // Now, compares the contents
            int i = 0;
            for (Modification modification : modificationList) {
                if (!modification.equals(otherEntry.modificationList.get(i))) {
                    return false;
                }
                i++;
            }
            break;
        case ModDn:
        case ModRdn:
            // Check the deleteOldRdn flag
            if (deleteOldRdn != otherEntry.deleteOldRdn) {
                return false;
            }
            // Check the newRdn value
            try {
                Rdn thisNewRdn = new Rdn(newRdn);
                Rdn entryNewRdn = new Rdn(otherEntry.newRdn);
                if (!thisNewRdn.equals(entryNewRdn)) {
                    return false;
                }
            } catch (LdapInvalidDnException ine) {
                return false;
            }
            // Check the newSuperior value
            try {
                Dn thisNewSuperior = new Dn(newSuperior);
                Dn entryNewSuperior = new Dn(otherEntry.newSuperior);
                if (!thisNewSuperior.equals(entryNewSuperior)) {
                    return false;
                }
            } catch (LdapInvalidDnException ine) {
                return false;
            }
            break;
        default:
            // do nothing
            break;
    }
    if (controls != null) {
        Map<String, LdifControl> otherControls = otherEntry.controls;
        if (otherControls == null) {
            return false;
        }
        if (controls.size() != otherControls.size()) {
            return false;
        }
        for (Map.Entry<String, LdifControl> controlEntry : controls.entrySet()) {
            String controlOid = controlEntry.getKey();
            if (!otherControls.containsKey(controlOid)) {
                return false;
            }
            Control thisControl = controlEntry.getValue();
            Control otherControl = otherControls.get(controlOid);
            if (thisControl == null) {
                if (otherControl != null) {
                    return false;
                }
            } else {
                if (!thisControl.equals(otherControl)) {
                    return false;
                }
            }
        }
        return true;
    } else {
        return otherEntry.controls == null;
    }
}
Also used : DefaultModification(org.apache.directory.api.ldap.model.entry.DefaultModification) Modification(org.apache.directory.api.ldap.model.entry.Modification) Control(org.apache.directory.api.ldap.model.message.Control) Dn(org.apache.directory.api.ldap.model.name.Dn) Rdn(org.apache.directory.api.ldap.model.name.Rdn) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)

Example 78 with Control

use of org.apache.directory.api.ldap.model.message.Control in project directory-ldap-api by apache.

the class LdifReader method parseEntry.

/**
 * Parse a ldif file. The following rules are processed :
 * <pre>
 * &lt;ldif-file&gt; ::= &lt;ldif-attrval-record&gt; &lt;ldif-attrval-records&gt; |
 *     &lt;ldif-change-record&gt; &lt;ldif-change-records&gt;
 * &lt;ldif-attrval-record&gt; ::= &lt;dn-spec&gt; &lt;sep&gt; &lt;attrval-spec&gt; &lt;attrval-specs&gt;
 * &lt;ldif-change-record&gt; ::= &lt;dn-spec&gt; &lt;sep&gt; &lt;controls-e&gt; &lt;changerecord&gt;
 * &lt;dn-spec&gt; ::= "dn:" &lt;fill&gt; &lt;distinguishedName&gt; | "dn::" &lt;fill&gt; &lt;base64-distinguishedName&gt;
 * &lt;changerecord&gt; ::= "changetype:" &lt;fill&gt; &lt;change-op&gt;
 * </pre>
 *
 * @return the parsed ldifEntry
 * @exception LdapException If the ldif file does not contain a valid entry
 */
protected LdifEntry parseEntry() throws LdapException {
    if ((lines == null) || lines.isEmpty()) {
        LOG.debug(I18n.msg(I18n.MSG_13408_END_OF_LDIF));
        return null;
    }
    // The entry must start with a dn: or a dn::
    String line = lines.get(0);
    lineNumber -= (lines.size() - 1);
    String name = parseDn(line);
    Dn dn = null;
    try {
        dn = new Dn(schemaManager, name);
    } catch (LdapInvalidDnException lide) {
        // Deal with the RDN whihc is not in the schema
        // First parse the DN without the schema
        dn = new Dn(name);
        Rdn rdn = dn.getRdn();
        // Process each Ava
        for (Ava ava : rdn) {
            if ((schemaManager != null) && (schemaManager.getAttributeType(ava.getType()) == null) && schemaManager.isRelaxed()) {
                // Not found : create a new one
                MutableAttributeType newAttributeType = new MutableAttributeType("1.3.6.1.4.1.18060.0.9999." + oidCounter++);
                newAttributeType.setNames(ava.getType());
                newAttributeType.setSyntax(schemaManager.getLdapSyntaxRegistry().get(SchemaConstants.DIRECTORY_STRING_SYNTAX));
                schemaManager.add(newAttributeType);
            }
        }
        dn = new Dn(schemaManager, name);
    }
    // Ok, we have found a Dn
    LdifEntry entry = createLdifEntry(schemaManager);
    entry.setLengthBeforeParsing(entryLen);
    entry.setOffset(entryOffset);
    entry.setDn(dn);
    // We remove this dn from the lines
    lines.remove(0);
    // Now, let's iterate through the other lines
    Iterator<String> iter = lines.iterator();
    // This flag is used to distinguish between an entry and a change
    int type = LDIF_ENTRY;
    // The following boolean is used to check that a control is *not*
    // found elswhere than just after the dn
    boolean controlSeen = false;
    // We use this boolean to check that we do not have AttributeValues
    // after a change operation
    boolean changeTypeSeen = false;
    ChangeType operation = ChangeType.Add;
    String lowerLine;
    Control control;
    while (iter.hasNext()) {
        lineNumber++;
        // Each line could start either with an OID, an attribute type, with
        // "control:" or with "changetype:"
        line = iter.next();
        lowerLine = Strings.toLowerCaseAscii(line);
        // 3) The first line after the Dn is anything else
        if (lowerLine.startsWith("control:")) {
            if (containsEntries) {
                LOG.error(I18n.err(I18n.ERR_13401_CHANGE_NOT_ALLOWED, lineNumber));
                throw new LdapLdifException(I18n.err(I18n.ERR_13440_NO_CHANGE));
            }
            containsChanges = true;
            if (controlSeen) {
                LOG.error(I18n.err(I18n.ERR_13418_CONTROL_ALREADY_FOUND, lineNumber));
                throw new LdapLdifException(I18n.err(I18n.ERR_13457_MISPLACED_CONTROL));
            }
            // Parse the control
            control = parseControl(line.substring("control:".length()));
            entry.addControl(control);
        } else if (lowerLine.startsWith("changetype:")) {
            if (containsEntries) {
                LOG.error(I18n.err(I18n.ERR_13401_CHANGE_NOT_ALLOWED, lineNumber));
                throw new LdapLdifException(I18n.err(I18n.ERR_13440_NO_CHANGE));
            }
            containsChanges = true;
            if (changeTypeSeen) {
                LOG.error(I18n.err(I18n.ERR_13419_CHANGETYPE_ALREADY_FOUND, lineNumber));
                throw new LdapLdifException(I18n.err(I18n.ERR_13458_MISPLACED_CHANGETYPE));
            }
            // A change request
            type = CHANGE;
            controlSeen = true;
            operation = parseChangeType(line);
            // Parse the change operation in a separate function
            parseChange(entry, iter, operation);
            changeTypeSeen = true;
        } else if (line.indexOf(':') > 0) {
            if (containsChanges) {
                LOG.error(I18n.err(I18n.ERR_13401_CHANGE_NOT_ALLOWED, lineNumber));
                throw new LdapLdifException(I18n.err(I18n.ERR_13440_NO_CHANGE));
            }
            containsEntries = true;
            if (controlSeen || changeTypeSeen) {
                LOG.error(I18n.err(I18n.ERR_13420_AT_VALUE_NOT_ALLOWED_AFTER_CONTROL, lineNumber));
                throw new LdapLdifException(I18n.err(I18n.ERR_13459_MISPLACED_ATTRIBUTETYPE));
            }
            parseAttributeValue(entry, line, lowerLine);
            type = LDIF_ENTRY;
        } else {
            // Invalid attribute Value
            LOG.error(I18n.err(I18n.ERR_13421_ATTRIBUTE_TYPE_EXPECTED, lineNumber));
            throw new LdapLdifException(I18n.err(I18n.ERR_13460_BAD_ATTRIBUTE));
        }
    }
    if (type == LDIF_ENTRY) {
        LOG.debug(I18n.msg(I18n.MSG_13406_READ_ENTRY, entry));
    } else if (type == CHANGE) {
        entry.setChangeType(operation);
        LOG.debug(I18n.msg(I18n.MSG_13404_READ_MODIF, entry));
    } else {
        LOG.error(I18n.err(I18n.ERR_13422_UNKNOWN_ENTRY_TYPE, lineNumber));
        throw new LdapLdifException(I18n.err(I18n.ERR_13461_UNKNOWN_ENTRY));
    }
    return entry;
}
Also used : Dn(org.apache.directory.api.ldap.model.name.Dn) Ava(org.apache.directory.api.ldap.model.name.Ava) Control(org.apache.directory.api.ldap.model.message.Control) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType) Rdn(org.apache.directory.api.ldap.model.name.Rdn) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)

Example 79 with Control

use of org.apache.directory.api.ldap.model.message.Control in project directory-ldap-api by apache.

the class StoreControlCriticality method action.

/**
 * {@inheritDoc}
 */
public void action(ControlsContainer container) throws DecoderException {
    TLV tlv = container.getCurrentTLV();
    // Get the current control
    Control control = container.getCurrentControl();
    // Store the criticality
    // We get the value. If it's a 0, it's a FALSE. If it's
    // a FF, it's a TRUE. Any other value should be an error,
    // but we could relax this constraint. So if we have
    // something
    // which is not 0, it will be interpreted as TRUE, but we
    // will generate a warning.
    BerValue value = tlv.getValue();
    try {
        control.setCritical(BooleanDecoder.parse(value));
    } catch (BooleanDecoderException bde) {
        LOG.error(I18n.err(I18n.ERR_04100_BAD_CONTROL_CRITICALITY, Strings.dumpBytes(value.getData()), bde.getMessage()));
        // This will generate a PROTOCOL_ERROR
        throw new DecoderException(bde.getMessage(), bde);
    }
    // We can have an END transition
    container.setGrammarEndAllowed(true);
    if (IS_DEBUG) {
        LOG.debug("Control criticality : {}", control.isCritical());
    }
}
Also used : BooleanDecoderException(org.apache.directory.api.asn1.ber.tlv.BooleanDecoderException) DecoderException(org.apache.directory.api.asn1.DecoderException) Control(org.apache.directory.api.ldap.model.message.Control) BooleanDecoderException(org.apache.directory.api.asn1.ber.tlv.BooleanDecoderException) BerValue(org.apache.directory.api.asn1.ber.tlv.BerValue) TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Example 80 with Control

use of org.apache.directory.api.ldap.model.message.Control in project directory-ldap-api by apache.

the class PasswordModifyFactory method decorate.

/**
 * {@inheritDoc}
 */
@Override
public PasswordModifyResponseDecorator decorate(ExtendedResponse decoratedResponse) {
    if (decoratedResponse instanceof PasswordModifyResponseDecorator) {
        return (PasswordModifyResponseDecorator) decoratedResponse;
    }
    if (decoratedResponse instanceof PasswordModifyResponse) {
        return new PasswordModifyResponseDecorator(codec, (PasswordModifyResponse) decoratedResponse);
    }
    // It's an opaque extended operation
    @SuppressWarnings("unchecked") ExtendedResponseDecorator<ExtendedResponse> response = (ExtendedResponseDecorator<ExtendedResponse>) decoratedResponse;
    // Decode the response, as it's an opaque operation
    Asn1Decoder decoder = new Asn1Decoder();
    byte[] value = response.getResponseValue();
    PasswordModifyResponseContainer container = new PasswordModifyResponseContainer();
    PasswordModifyResponse pwdModifyResponse;
    if (value != null) {
        ByteBuffer buffer = ByteBuffer.wrap(value);
        try {
            decoder.decode(buffer, container);
            pwdModifyResponse = container.getPwdModifyResponse();
            // Now, update the created response with what we got from the extendedResponse
            pwdModifyResponse.getLdapResult().setResultCode(response.getLdapResult().getResultCode());
            pwdModifyResponse.getLdapResult().setDiagnosticMessage(response.getLdapResult().getDiagnosticMessage());
            pwdModifyResponse.getLdapResult().setMatchedDn(response.getLdapResult().getMatchedDn());
            pwdModifyResponse.getLdapResult().setReferral(response.getLdapResult().getReferral());
        } catch (DecoderException de) {
            StringWriter sw = new StringWriter();
            de.printStackTrace(new PrintWriter(sw));
            String stackTrace = sw.toString();
            // Error while decoding the value.
            pwdModifyResponse = new PasswordModifyResponseImpl(decoratedResponse.getMessageId(), ResultCodeEnum.OPERATIONS_ERROR, stackTrace);
        }
    } else {
        pwdModifyResponse = new PasswordModifyResponseImpl();
        // Now, update the created response with what we got from the extendedResponse
        pwdModifyResponse.getLdapResult().setResultCode(response.getLdapResult().getResultCode());
        pwdModifyResponse.getLdapResult().setDiagnosticMessage(response.getLdapResult().getDiagnosticMessage());
        pwdModifyResponse.getLdapResult().setMatchedDn(response.getLdapResult().getMatchedDn());
        pwdModifyResponse.getLdapResult().setReferral(response.getLdapResult().getReferral());
    }
    PasswordModifyResponseDecorator decorated = new PasswordModifyResponseDecorator(codec, pwdModifyResponse);
    Control ppolicyControl = response.getControl(PasswordPolicy.OID);
    if (ppolicyControl != null) {
        decorated.addControl(ppolicyControl);
    }
    return decorated;
}
Also used : ByteBuffer(java.nio.ByteBuffer) DecoderException(org.apache.directory.api.asn1.DecoderException) Control(org.apache.directory.api.ldap.model.message.Control) PasswordModifyResponse(org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyResponse) StringWriter(java.io.StringWriter) ExtendedResponse(org.apache.directory.api.ldap.model.message.ExtendedResponse) ExtendedResponseDecorator(org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) PasswordModifyResponseImpl(org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyResponseImpl) PrintWriter(java.io.PrintWriter)

Aggregations

Control (org.apache.directory.api.ldap.model.message.Control)145 Test (org.junit.Test)124 DsmlControl (org.apache.directory.api.dsmlv2.DsmlControl)85 DecoderException (org.apache.directory.api.asn1.DecoderException)45 AbstractTest (org.apache.directory.api.dsmlv2.AbstractTest)45 Dsmlv2Parser (org.apache.directory.api.dsmlv2.Dsmlv2Parser)45 AbstractResponseTest (org.apache.directory.api.dsmlv2.AbstractResponseTest)40 Dsmlv2ResponseParser (org.apache.directory.api.dsmlv2.Dsmlv2ResponseParser)40 ByteBuffer (java.nio.ByteBuffer)39 CodecControl (org.apache.directory.api.ldap.codec.api.CodecControl)38 EncoderException (org.apache.directory.api.asn1.EncoderException)37 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)37 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)36 LdapURLEncodingException (org.apache.directory.api.ldap.model.exception.LdapURLEncodingException)36 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)35 SearchResponse (org.apache.directory.api.dsmlv2.response.SearchResponse)12 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)12 BindRequest (org.apache.directory.api.ldap.model.message.BindRequest)11 AbandonRequest (org.apache.directory.api.ldap.model.message.AbandonRequest)9 SearchRequest (org.apache.directory.api.ldap.model.message.SearchRequest)9