Search in sources :

Example 21 with ModifyRequest

use of org.apache.directory.api.ldap.model.message.ModifyRequest 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)

Example 22 with ModifyRequest

use of org.apache.directory.api.ldap.model.message.ModifyRequest in project ldapchai by ldapchai.

the class ApacheLdapProviderImpl method writeBinaryAttribute.

public void writeBinaryAttribute(final String entryDN, final String attributeName, final byte[][] values, final boolean overwrite, final ChaiRequestControl[] controls) throws ChaiUnavailableException, ChaiOperationException {
    try {
        final ModifyRequest modifyRequest = new ModifyRequestImpl();
        modifyRequest.setName(new Dn(entryDN));
        modifyRequest.addAllControls(figureControls(controls));
        {
            final Modification modification = new DefaultModification();
            modification.setOperation(overwrite ? ModificationOperation.REPLACE_ATTRIBUTE : ModificationOperation.ADD_ATTRIBUTE);
            modification.setAttribute(new DefaultAttribute(attributeName, values));
            modifyRequest.addModification(modification);
        }
        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)

Example 23 with ModifyRequest

use of org.apache.directory.api.ldap.model.message.ModifyRequest in project airavata by apache.

the class IULdapSSHAccountProvisioner method installSSHKey.

@Override
public String installSSHKey(String userId, String sshPublicKey) throws InvalidUsernameException {
    String username = getUsername(userId);
    String finalSSHPublicKey = sshPublicKey.trim();
    boolean success = withLdapConnection(ldapConnection -> {
        try {
            String dn = "uid=" + username + "," + ldapBaseDN;
            Entry entry = ldapConnection.lookup(dn);
            if (entry == null) {
                throw new RuntimeException("User [" + username + "] has no entry for " + dn);
            }
            boolean hasLdapPublicKey = entry.hasObjectClass(LDAP_PUBLIC_KEY_OBJECT_CLASS);
            ModifyRequest modifyRequest = new ModifyRequestImpl();
            modifyRequest.setName(new Dn(dn));
            // Add or Replace, depending on whether there is already an ldapPublicKey on the entry
            if (!hasLdapPublicKey) {
                modifyRequest.addModification(new DefaultAttribute("objectclass", LDAP_PUBLIC_KEY_OBJECT_CLASS), ModificationOperation.ADD_ATTRIBUTE);
                modifyRequest.addModification(new DefaultAttribute(SSH_PUBLIC_KEY_ATTRIBUTE_NAME, finalSSHPublicKey), ModificationOperation.ADD_ATTRIBUTE);
            } else {
                String oldSshPublicKey = entry.get(SSH_PUBLIC_KEY_ATTRIBUTE_NAME).getString();
                if (!oldSshPublicKey.equals(finalSSHPublicKey)) {
                    // Disallow overwriting the SSH key
                    throw new RuntimeException("User [" + username + "] already has an SSH public key in LDAP for [" + ldapBaseDN + "] and overwriting it isn't allowed.");
                // modifyRequest.addModification(new DefaultAttribute(SSH_PUBLIC_KEY_ATTRIBUTE_NAME,
                // sshPublicKey), ModificationOperation.REPLACE_ATTRIBUTE);
                } else {
                    // SSH key is already installed so just return
                    return true;
                }
            }
            ModifyResponse modifyResponse = ldapConnection.modify(modifyRequest);
            if (modifyResponse.getLdapResult().getResultCode() != ResultCodeEnum.SUCCESS) {
                logger.warn("installSSHKey ldap operation reported not being successful: " + modifyResponse);
            } else {
                logger.debug("installSSHKey ldap operation was successful: " + modifyResponse);
            }
            return true;
        } catch (LdapException e) {
            throw new RuntimeException(e);
        }
    });
    return username;
}
Also used : Entry(org.apache.directory.api.ldap.model.entry.Entry) ModifyRequestImpl(org.apache.directory.api.ldap.model.message.ModifyRequestImpl) 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)

Example 24 with ModifyRequest

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

the class MessageDecorator method getDecorator.

/**
 * Gets the decorator associated with a given message
 *
 * @param codec The LdapApiService to use
 * @param decoratedMessage The message to decorate
 * @return The decorator instance
 */
public static MessageDecorator<? extends Message> getDecorator(LdapApiService codec, Message decoratedMessage) {
    if (decoratedMessage instanceof MessageDecorator) {
        return (MessageDecorator<?>) decoratedMessage;
    }
    MessageDecorator<?> decorator;
    switch(decoratedMessage.getType()) {
        case ABANDON_REQUEST:
            decorator = new AbandonRequestDecorator(codec, (AbandonRequest) decoratedMessage);
            break;
        case ADD_REQUEST:
            decorator = new AddRequestDecorator(codec, (AddRequest) decoratedMessage);
            break;
        case ADD_RESPONSE:
            decorator = new AddResponseDecorator(codec, (AddResponse) decoratedMessage);
            break;
        case BIND_REQUEST:
            decorator = new BindRequestDecorator(codec, (BindRequest) decoratedMessage);
            break;
        case BIND_RESPONSE:
            decorator = new BindResponseDecorator(codec, (BindResponse) decoratedMessage);
            break;
        case COMPARE_REQUEST:
            decorator = new CompareRequestDecorator(codec, (CompareRequest) decoratedMessage);
            break;
        case COMPARE_RESPONSE:
            decorator = new CompareResponseDecorator(codec, (CompareResponse) decoratedMessage);
            break;
        case DEL_REQUEST:
            decorator = new DeleteRequestDecorator(codec, (DeleteRequest) decoratedMessage);
            break;
        case DEL_RESPONSE:
            decorator = new DeleteResponseDecorator(codec, (DeleteResponse) decoratedMessage);
            break;
        case EXTENDED_REQUEST:
            decorator = codec.decorate((ExtendedRequest) decoratedMessage);
            break;
        case EXTENDED_RESPONSE:
            decorator = codec.decorate((ExtendedResponse) decoratedMessage);
            break;
        case INTERMEDIATE_RESPONSE:
            decorator = new IntermediateResponseDecorator(codec, (IntermediateResponse) decoratedMessage);
            break;
        case MODIFY_REQUEST:
            decorator = new ModifyRequestDecorator(codec, (ModifyRequest) decoratedMessage);
            break;
        case MODIFY_RESPONSE:
            decorator = new ModifyResponseDecorator(codec, (ModifyResponse) decoratedMessage);
            break;
        case MODIFYDN_REQUEST:
            decorator = new ModifyDnRequestDecorator(codec, (ModifyDnRequest) decoratedMessage);
            break;
        case MODIFYDN_RESPONSE:
            decorator = new ModifyDnResponseDecorator(codec, (ModifyDnResponse) decoratedMessage);
            break;
        case SEARCH_REQUEST:
            decorator = new SearchRequestDecorator(codec, (SearchRequest) decoratedMessage);
            break;
        case SEARCH_RESULT_DONE:
            decorator = new SearchResultDoneDecorator(codec, (SearchResultDone) decoratedMessage);
            break;
        case SEARCH_RESULT_ENTRY:
            decorator = new SearchResultEntryDecorator(codec, (SearchResultEntry) decoratedMessage);
            break;
        case SEARCH_RESULT_REFERENCE:
            decorator = new SearchResultReferenceDecorator(codec, (SearchResultReference) decoratedMessage);
            break;
        case UNBIND_REQUEST:
            decorator = new UnbindRequestDecorator(codec, (UnbindRequest) decoratedMessage);
            break;
        default:
            return null;
    }
    Map<String, Control> controls = decoratedMessage.getControls();
    if (controls != null) {
        for (Control control : controls.values()) {
            decorator.addControl(control);
        }
    }
    return decorator;
}
Also used : ModifyRequestDecorator(org.apache.directory.api.ldap.codec.decorators.ModifyRequestDecorator) SearchRequest(org.apache.directory.api.ldap.model.message.SearchRequest) DeleteRequestDecorator(org.apache.directory.api.ldap.codec.decorators.DeleteRequestDecorator) SearchResultEntryDecorator(org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator) AbandonRequest(org.apache.directory.api.ldap.model.message.AbandonRequest) BindRequest(org.apache.directory.api.ldap.model.message.BindRequest) DeleteResponseDecorator(org.apache.directory.api.ldap.codec.decorators.DeleteResponseDecorator) SearchResultReference(org.apache.directory.api.ldap.model.message.SearchResultReference) BindResponseDecorator(org.apache.directory.api.ldap.codec.decorators.BindResponseDecorator) BindResponse(org.apache.directory.api.ldap.model.message.BindResponse) ModifyRequest(org.apache.directory.api.ldap.model.message.ModifyRequest) AbandonRequestDecorator(org.apache.directory.api.ldap.codec.decorators.AbandonRequestDecorator) AddResponseDecorator(org.apache.directory.api.ldap.codec.decorators.AddResponseDecorator) AddRequest(org.apache.directory.api.ldap.model.message.AddRequest) IntermediateResponseDecorator(org.apache.directory.api.ldap.codec.decorators.IntermediateResponseDecorator) IntermediateResponse(org.apache.directory.api.ldap.model.message.IntermediateResponse) Control(org.apache.directory.api.ldap.model.message.Control) BindRequestDecorator(org.apache.directory.api.ldap.codec.decorators.BindRequestDecorator) UnbindRequestDecorator(org.apache.directory.api.ldap.codec.decorators.UnbindRequestDecorator) CompareRequestDecorator(org.apache.directory.api.ldap.codec.decorators.CompareRequestDecorator) ModifyDnRequestDecorator(org.apache.directory.api.ldap.codec.decorators.ModifyDnRequestDecorator) ModifyDnResponse(org.apache.directory.api.ldap.model.message.ModifyDnResponse) SearchResultReferenceDecorator(org.apache.directory.api.ldap.codec.decorators.SearchResultReferenceDecorator) ModifyResponse(org.apache.directory.api.ldap.model.message.ModifyResponse) UnbindRequest(org.apache.directory.api.ldap.model.message.UnbindRequest) CompareResponse(org.apache.directory.api.ldap.model.message.CompareResponse) SearchResultDone(org.apache.directory.api.ldap.model.message.SearchResultDone) ModifyDnRequest(org.apache.directory.api.ldap.model.message.ModifyDnRequest) AddResponse(org.apache.directory.api.ldap.model.message.AddResponse) CompareResponseDecorator(org.apache.directory.api.ldap.codec.decorators.CompareResponseDecorator) CompareRequest(org.apache.directory.api.ldap.model.message.CompareRequest) DeleteResponse(org.apache.directory.api.ldap.model.message.DeleteResponse) ModifyResponseDecorator(org.apache.directory.api.ldap.codec.decorators.ModifyResponseDecorator) ModifyDnResponseDecorator(org.apache.directory.api.ldap.codec.decorators.ModifyDnResponseDecorator) ExtendedResponse(org.apache.directory.api.ldap.model.message.ExtendedResponse) ExtendedRequest(org.apache.directory.api.ldap.model.message.ExtendedRequest) SearchResultDoneDecorator(org.apache.directory.api.ldap.codec.decorators.SearchResultDoneDecorator) AddRequestDecorator(org.apache.directory.api.ldap.codec.decorators.AddRequestDecorator) DeleteRequest(org.apache.directory.api.ldap.model.message.DeleteRequest) SearchRequestDecorator(org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry)

Example 25 with ModifyRequest

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

the class InitModifyRequest method action.

/**
 * {@inheritDoc}
 */
public void action(LdapMessageContainer<ModifyRequestDecorator> container) {
    // Now, we can allocate the ModifyRequest Object
    ModifyRequest internalModifyRequest = new ModifyRequestImpl();
    internalModifyRequest.setMessageId(container.getMessageId());
    ModifyRequestDecorator modifyRequestDecorator = new ModifyRequestDecorator(container.getLdapCodecService(), internalModifyRequest);
    container.setMessage(modifyRequestDecorator);
}
Also used : ModifyRequestDecorator(org.apache.directory.api.ldap.codec.decorators.ModifyRequestDecorator) ModifyRequestImpl(org.apache.directory.api.ldap.model.message.ModifyRequestImpl) ModifyRequest(org.apache.directory.api.ldap.model.message.ModifyRequest)

Aggregations

ModifyRequest (org.apache.directory.api.ldap.model.message.ModifyRequest)40 Modification (org.apache.directory.api.ldap.model.entry.Modification)25 Test (org.junit.Test)24 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)22 AbstractTest (org.apache.directory.api.dsmlv2.AbstractTest)17 Dsmlv2Parser (org.apache.directory.api.dsmlv2.Dsmlv2Parser)17 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)14 ModifyRequestDecorator (org.apache.directory.api.ldap.codec.decorators.ModifyRequestDecorator)11 ModifyRequestImpl (org.apache.directory.api.ldap.model.message.ModifyRequestImpl)11 ModifyResponse (org.apache.directory.api.ldap.model.message.ModifyResponse)10 Dn (org.apache.directory.api.ldap.model.name.Dn)10 ByteBuffer (java.nio.ByteBuffer)7 DecoderException (org.apache.directory.api.asn1.DecoderException)7 EncoderException (org.apache.directory.api.asn1.EncoderException)7 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)7 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)7 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)7 DefaultAttribute (org.apache.directory.api.ldap.model.entry.DefaultAttribute)7 DefaultModification (org.apache.directory.api.ldap.model.entry.DefaultModification)7 Control (org.apache.directory.api.ldap.model.message.Control)7