Search in sources :

Example 11 with AddResponseProtocolOp

use of com.unboundid.ldap.protocol.AddResponseProtocolOp in project ldapsdk by pingidentity.

the class CannedResponseRequestHandlerTestCase method testDefaultConstructor.

/**
 * Tests the behavior of the request handler with the default configuration.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testDefaultConstructor() throws Exception {
    final CannedResponseRequestHandler handler = new CannedResponseRequestHandler().newInstance(null);
    LDAPMessage m = handler.processAddRequest(1, new AddRequestProtocolOp("dc=example,dc=com", Arrays.asList(new Attribute("objectClass", "top", "domain"), new Attribute("dc", "example"))), Collections.<Control>emptyList());
    assertNotNull(m);
    assertEquals(m.getMessageID(), 1);
    assertTrue(m.getProtocolOp() instanceof AddResponseProtocolOp);
    assertNotNull(m.getControls());
    assertTrue(m.getControls().isEmpty());
    m = handler.processBindRequest(2, new BindRequestProtocolOp("uid=admin,dc=example,dc=com", "password"), Collections.<Control>emptyList());
    assertNotNull(m);
    assertEquals(m.getMessageID(), 2);
    assertTrue(m.getProtocolOp() instanceof BindResponseProtocolOp);
    assertNotNull(m.getControls());
    assertTrue(m.getControls().isEmpty());
    m = handler.processCompareRequest(3, new CompareRequestProtocolOp("dc=example,dc=com", "objectClass", new ASN1OctetString("top")), Collections.<Control>emptyList());
    assertNotNull(m);
    assertEquals(m.getMessageID(), 3);
    assertTrue(m.getProtocolOp() instanceof CompareResponseProtocolOp);
    assertNotNull(m.getControls());
    assertTrue(m.getControls().isEmpty());
    m = handler.processDeleteRequest(4, new DeleteRequestProtocolOp("dc=example,dc=com"), Collections.<Control>emptyList());
    assertNotNull(m);
    assertEquals(m.getMessageID(), 4);
    assertTrue(m.getProtocolOp() instanceof DeleteResponseProtocolOp);
    assertNotNull(m.getControls());
    assertTrue(m.getControls().isEmpty());
    m = handler.processExtendedRequest(5, new ExtendedRequestProtocolOp("1.2.3.4", null), Collections.<Control>emptyList());
    assertNotNull(m);
    assertEquals(m.getMessageID(), 5);
    assertTrue(m.getProtocolOp() instanceof ExtendedResponseProtocolOp);
    assertNotNull(m.getControls());
    assertTrue(m.getControls().isEmpty());
    m = handler.processModifyRequest(6, new ModifyRequestProtocolOp("dc=example,dc=com", Arrays.asList(new Modification(ModificationType.REPLACE, "description", "foo"))), Collections.<Control>emptyList());
    assertNotNull(m);
    assertEquals(m.getMessageID(), 6);
    assertTrue(m.getProtocolOp() instanceof ModifyResponseProtocolOp);
    assertNotNull(m.getControls());
    assertTrue(m.getControls().isEmpty());
    m = handler.processModifyDNRequest(6, new ModifyDNRequestProtocolOp("ou=People,dc=example,dc=com", "ou=Users", true, null), Collections.<Control>emptyList());
    assertNotNull(m);
    assertEquals(m.getMessageID(), 6);
    assertTrue(m.getProtocolOp() instanceof ModifyDNResponseProtocolOp);
    assertNotNull(m.getControls());
    assertTrue(m.getControls().isEmpty());
    m = handler.processSearchRequest(7, new SearchRequestProtocolOp("dc=example,dc=com", SearchScope.SUB, DereferencePolicy.NEVER, 0, 0, false, Filter.createEqualityFilter("uid", "test"), Arrays.<String>asList()), Collections.<Control>emptyList());
    assertNotNull(m);
    assertEquals(m.getMessageID(), 7);
    assertTrue(m.getProtocolOp() instanceof SearchResultDoneProtocolOp);
    assertNotNull(m.getControls());
    assertTrue(m.getControls().isEmpty());
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) Modification(com.unboundid.ldap.sdk.Modification) ExtendedResponseProtocolOp(com.unboundid.ldap.protocol.ExtendedResponseProtocolOp) Attribute(com.unboundid.ldap.sdk.Attribute) SearchRequestProtocolOp(com.unboundid.ldap.protocol.SearchRequestProtocolOp) AddRequestProtocolOp(com.unboundid.ldap.protocol.AddRequestProtocolOp) CompareRequestProtocolOp(com.unboundid.ldap.protocol.CompareRequestProtocolOp) DeleteResponseProtocolOp(com.unboundid.ldap.protocol.DeleteResponseProtocolOp) LDAPMessage(com.unboundid.ldap.protocol.LDAPMessage) AddResponseProtocolOp(com.unboundid.ldap.protocol.AddResponseProtocolOp) BindRequestProtocolOp(com.unboundid.ldap.protocol.BindRequestProtocolOp) ModifyDNResponseProtocolOp(com.unboundid.ldap.protocol.ModifyDNResponseProtocolOp) ExtendedRequestProtocolOp(com.unboundid.ldap.protocol.ExtendedRequestProtocolOp) DeleteRequestProtocolOp(com.unboundid.ldap.protocol.DeleteRequestProtocolOp) ModifyRequestProtocolOp(com.unboundid.ldap.protocol.ModifyRequestProtocolOp) Control(com.unboundid.ldap.sdk.Control) BindResponseProtocolOp(com.unboundid.ldap.protocol.BindResponseProtocolOp) ModifyDNRequestProtocolOp(com.unboundid.ldap.protocol.ModifyDNRequestProtocolOp) SearchResultDoneProtocolOp(com.unboundid.ldap.protocol.SearchResultDoneProtocolOp) CompareResponseProtocolOp(com.unboundid.ldap.protocol.CompareResponseProtocolOp) ModifyResponseProtocolOp(com.unboundid.ldap.protocol.ModifyResponseProtocolOp) Test(org.testng.annotations.Test)

Example 12 with AddResponseProtocolOp

use of com.unboundid.ldap.protocol.AddResponseProtocolOp in project ldapsdk by pingidentity.

the class ProxyRequestHandler method processAddRequest.

/**
 * {@inheritDoc}
 */
@Override()
@NotNull()
public LDAPMessage processAddRequest(final int messageID, @NotNull final AddRequestProtocolOp request, @NotNull final List<Control> controls) {
    final AddRequest addRequest = new AddRequest(request.getDN(), request.getAttributes());
    if (!controls.isEmpty()) {
        addRequest.setControls(controls);
    }
    addRequest.setIntermediateResponseListener(this);
    LDAPResult addResult;
    try {
        addResult = ldapConnection.add(addRequest);
    } catch (final LDAPException le) {
        Debug.debugException(le);
        addResult = le.toLDAPResult();
    }
    final AddResponseProtocolOp addResponseProtocolOp = new AddResponseProtocolOp(addResult.getResultCode().intValue(), addResult.getMatchedDN(), addResult.getDiagnosticMessage(), Arrays.asList(addResult.getReferralURLs()));
    return new LDAPMessage(messageID, addResponseProtocolOp, Arrays.asList(addResult.getResponseControls()));
}
Also used : AddRequest(com.unboundid.ldap.sdk.AddRequest) LDAPException(com.unboundid.ldap.sdk.LDAPException) LDAPResult(com.unboundid.ldap.sdk.LDAPResult) LDAPMessage(com.unboundid.ldap.protocol.LDAPMessage) AddResponseProtocolOp(com.unboundid.ldap.protocol.AddResponseProtocolOp) NotNull(com.unboundid.util.NotNull)

Example 13 with AddResponseProtocolOp

use of com.unboundid.ldap.protocol.AddResponseProtocolOp in project ldapsdk by pingidentity.

the class TransactionExtendedOperationHandler method handleEndTransaction.

/**
 * Performs the appropriate processing for an end transaction extended
 * request.
 *
 * @param  handler    The in-memory request handler that received the request.
 * @param  messageID  The message ID for the associated request.
 * @param  request    The extended request that was received.
 *
 * @return  The result for the extended operation processing.
 */
@NotNull()
private static EndTransactionExtendedResult handleEndTransaction(@NotNull final InMemoryRequestHandler handler, final int messageID, @NotNull final ExtendedRequest request) {
    // Get information about any transaction currently in progress on the
    // connection.  If there isn't one, then fail.
    final Map<String, Object> connectionState = handler.getConnectionState();
    final ObjectPair<?, ?> txnInfo = (ObjectPair<?, ?>) connectionState.remove(STATE_VARIABLE_TXN_INFO);
    if (txnInfo == null) {
        return new EndTransactionExtendedResult(messageID, ResultCode.CONSTRAINT_VIOLATION, ERR_TXN_EXTOP_END_NO_ACTIVE_TXN.get(), null, null, null, null, null);
    }
    // Make sure that we can decode the end transaction request.
    final ASN1OctetString existingTxnID = (ASN1OctetString) txnInfo.getFirst();
    final EndTransactionExtendedRequest endTxnRequest;
    try {
        endTxnRequest = new EndTransactionExtendedRequest(request);
    } catch (final LDAPException le) {
        Debug.debugException(le);
        try {
            handler.getClientConnection().sendUnsolicitedNotification(new AbortedTransactionExtendedResult(existingTxnID, ResultCode.PROTOCOL_ERROR, ERR_TXN_EXTOP_ABORTED_BY_MALFORMED_END_TXN.get(existingTxnID.stringValue()), null, null, null));
        } catch (final LDAPException le2) {
            Debug.debugException(le2);
        }
        return new EndTransactionExtendedResult(messageID, ResultCode.PROTOCOL_ERROR, le.getMessage(), null, null, null, null, null);
    }
    // Make sure that the transaction ID of the existing transaction matches the
    // transaction ID from the end transaction request.
    final ASN1OctetString targetTxnID = endTxnRequest.getTransactionID();
    if (!existingTxnID.stringValue().equals(targetTxnID.stringValue())) {
        // transaction has been aborted.
        try {
            handler.getClientConnection().sendUnsolicitedNotification(new AbortedTransactionExtendedResult(existingTxnID, ResultCode.CONSTRAINT_VIOLATION, ERR_TXN_EXTOP_ABORTED_BY_WRONG_END_TXN.get(existingTxnID.stringValue(), targetTxnID.stringValue()), null, null, null));
        } catch (final LDAPException le) {
            Debug.debugException(le);
            return new EndTransactionExtendedResult(messageID, le.getResultCode(), le.getMessage(), le.getMatchedDN(), le.getReferralURLs(), null, null, le.getResponseControls());
        }
        return new EndTransactionExtendedResult(messageID, ResultCode.CONSTRAINT_VIOLATION, ERR_TXN_EXTOP_END_WRONG_TXN.get(targetTxnID.stringValue(), existingTxnID.stringValue()), null, null, null, null, null);
    }
    // If the transaction should be aborted, then we can just send the response.
    if (!endTxnRequest.commit()) {
        return new EndTransactionExtendedResult(messageID, ResultCode.SUCCESS, INFO_TXN_EXTOP_END_TXN_ABORTED.get(existingTxnID.stringValue()), null, null, null, null, null);
    }
    // If we've gotten here, then we'll try to commit the transaction.  First,
    // get a snapshot of the current state so that we can roll back to it if
    // necessary.
    final InMemoryDirectoryServerSnapshot snapshot = handler.createSnapshot();
    boolean rollBack = true;
    try {
        // Create a map to hold information about response controls from
        // operations processed as part of the transaction.
        final List<?> requestMessages = (List<?>) txnInfo.getSecond();
        final Map<Integer, Control[]> opResponseControls = new LinkedHashMap<>(StaticUtils.computeMapCapacity(requestMessages.size()));
        // Iterate through the requests that have been submitted as part of the
        // transaction and attempt to process them.
        ResultCode resultCode = ResultCode.SUCCESS;
        String diagnosticMessage = null;
        String failedOpType = null;
        Integer failedOpMessageID = null;
        txnOpLoop: for (final Object o : requestMessages) {
            final LDAPMessage m = (LDAPMessage) o;
            switch(m.getProtocolOpType()) {
                case LDAPMessage.PROTOCOL_OP_TYPE_ADD_REQUEST:
                    final LDAPMessage addResponseMessage = handler.processAddRequest(m.getMessageID(), m.getAddRequestProtocolOp(), m.getControls());
                    final AddResponseProtocolOp addResponseOp = addResponseMessage.getAddResponseProtocolOp();
                    final List<Control> addControls = addResponseMessage.getControls();
                    if ((addControls != null) && (!addControls.isEmpty())) {
                        final Control[] controls = new Control[addControls.size()];
                        addControls.toArray(controls);
                        opResponseControls.put(m.getMessageID(), controls);
                    }
                    if (addResponseOp.getResultCode() != ResultCode.SUCCESS_INT_VALUE) {
                        resultCode = ResultCode.valueOf(addResponseOp.getResultCode());
                        diagnosticMessage = addResponseOp.getDiagnosticMessage();
                        failedOpType = INFO_TXN_EXTOP_OP_TYPE_ADD.get();
                        failedOpMessageID = m.getMessageID();
                        break txnOpLoop;
                    }
                    break;
                case LDAPMessage.PROTOCOL_OP_TYPE_DELETE_REQUEST:
                    final LDAPMessage deleteResponseMessage = handler.processDeleteRequest(m.getMessageID(), m.getDeleteRequestProtocolOp(), m.getControls());
                    final DeleteResponseProtocolOp deleteResponseOp = deleteResponseMessage.getDeleteResponseProtocolOp();
                    final List<Control> deleteControls = deleteResponseMessage.getControls();
                    if ((deleteControls != null) && (!deleteControls.isEmpty())) {
                        final Control[] controls = new Control[deleteControls.size()];
                        deleteControls.toArray(controls);
                        opResponseControls.put(m.getMessageID(), controls);
                    }
                    if (deleteResponseOp.getResultCode() != ResultCode.SUCCESS_INT_VALUE) {
                        resultCode = ResultCode.valueOf(deleteResponseOp.getResultCode());
                        diagnosticMessage = deleteResponseOp.getDiagnosticMessage();
                        failedOpType = INFO_TXN_EXTOP_OP_TYPE_DELETE.get();
                        failedOpMessageID = m.getMessageID();
                        break txnOpLoop;
                    }
                    break;
                case LDAPMessage.PROTOCOL_OP_TYPE_MODIFY_REQUEST:
                    final LDAPMessage modifyResponseMessage = handler.processModifyRequest(m.getMessageID(), m.getModifyRequestProtocolOp(), m.getControls());
                    final ModifyResponseProtocolOp modifyResponseOp = modifyResponseMessage.getModifyResponseProtocolOp();
                    final List<Control> modifyControls = modifyResponseMessage.getControls();
                    if ((modifyControls != null) && (!modifyControls.isEmpty())) {
                        final Control[] controls = new Control[modifyControls.size()];
                        modifyControls.toArray(controls);
                        opResponseControls.put(m.getMessageID(), controls);
                    }
                    if (modifyResponseOp.getResultCode() != ResultCode.SUCCESS_INT_VALUE) {
                        resultCode = ResultCode.valueOf(modifyResponseOp.getResultCode());
                        diagnosticMessage = modifyResponseOp.getDiagnosticMessage();
                        failedOpType = INFO_TXN_EXTOP_OP_TYPE_MODIFY.get();
                        failedOpMessageID = m.getMessageID();
                        break txnOpLoop;
                    }
                    break;
                case LDAPMessage.PROTOCOL_OP_TYPE_MODIFY_DN_REQUEST:
                    final LDAPMessage modifyDNResponseMessage = handler.processModifyDNRequest(m.getMessageID(), m.getModifyDNRequestProtocolOp(), m.getControls());
                    final ModifyDNResponseProtocolOp modifyDNResponseOp = modifyDNResponseMessage.getModifyDNResponseProtocolOp();
                    final List<Control> modifyDNControls = modifyDNResponseMessage.getControls();
                    if ((modifyDNControls != null) && (!modifyDNControls.isEmpty())) {
                        final Control[] controls = new Control[modifyDNControls.size()];
                        modifyDNControls.toArray(controls);
                        opResponseControls.put(m.getMessageID(), controls);
                    }
                    if (modifyDNResponseOp.getResultCode() != ResultCode.SUCCESS_INT_VALUE) {
                        resultCode = ResultCode.valueOf(modifyDNResponseOp.getResultCode());
                        diagnosticMessage = modifyDNResponseOp.getDiagnosticMessage();
                        failedOpType = INFO_TXN_EXTOP_OP_TYPE_MODIFY_DN.get();
                        failedOpMessageID = m.getMessageID();
                        break txnOpLoop;
                    }
                    break;
            }
        }
        if (resultCode == ResultCode.SUCCESS) {
            diagnosticMessage = INFO_TXN_EXTOP_COMMITTED.get(existingTxnID.stringValue());
            rollBack = false;
        } else {
            diagnosticMessage = ERR_TXN_EXTOP_COMMIT_FAILED.get(existingTxnID.stringValue(), failedOpType, failedOpMessageID, diagnosticMessage);
        }
        return new EndTransactionExtendedResult(messageID, resultCode, diagnosticMessage, null, null, failedOpMessageID, opResponseControls, null);
    } finally {
        if (rollBack) {
            handler.restoreSnapshot(snapshot);
        }
    }
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) DeleteResponseProtocolOp(com.unboundid.ldap.protocol.DeleteResponseProtocolOp) LDAPMessage(com.unboundid.ldap.protocol.LDAPMessage) EndTransactionExtendedRequest(com.unboundid.ldap.sdk.extensions.EndTransactionExtendedRequest) AddResponseProtocolOp(com.unboundid.ldap.protocol.AddResponseProtocolOp) ModifyDNResponseProtocolOp(com.unboundid.ldap.protocol.ModifyDNResponseProtocolOp) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) LinkedHashMap(java.util.LinkedHashMap) LDAPException(com.unboundid.ldap.sdk.LDAPException) ModifyResponseProtocolOp(com.unboundid.ldap.protocol.ModifyResponseProtocolOp) ArrayList(java.util.ArrayList) List(java.util.List) EndTransactionExtendedResult(com.unboundid.ldap.sdk.extensions.EndTransactionExtendedResult) ResultCode(com.unboundid.ldap.sdk.ResultCode) ObjectPair(com.unboundid.util.ObjectPair) AbortedTransactionExtendedResult(com.unboundid.ldap.sdk.extensions.AbortedTransactionExtendedResult) NotNull(com.unboundid.util.NotNull)

Example 14 with AddResponseProtocolOp

use of com.unboundid.ldap.protocol.AddResponseProtocolOp in project ldapsdk by pingidentity.

the class MultiUpdateExtendedResult method encodeValue.

/**
 * Encodes the information from the provided set of results into a form
 * suitable for use as the value of a multi-update extended result.
 *
 * @param  changesApplied  The value which indicates whether any or all of the
 *                         changes from the request were successfully applied.
 * @param  results         The set of operation results to be included in the
 *                         extended result value.  It may be {@code null} or
 *                         empty if no operation results should be included.
 *
 * @return  An ASN.1 element suitable for use as the value of a multi-update
 *          extended result.
 *
 * @throws  LDAPException  If any of the results are for an inappropriate
 *                         operation type.
 */
@Nullable()
private static ASN1OctetString encodeValue(@Nullable final MultiUpdateChangesApplied changesApplied, @Nullable final List<ObjectPair<OperationType, LDAPResult>> results) throws LDAPException {
    if ((results == null) || results.isEmpty()) {
        return null;
    }
    final ArrayList<ASN1Element> opElements = new ArrayList<>(results.size());
    for (final ObjectPair<OperationType, LDAPResult> p : results) {
        final OperationType t = p.getFirst();
        final LDAPResult r = p.getSecond();
        final ASN1Element protocolOpElement;
        switch(t) {
            case ADD:
                protocolOpElement = new AddResponseProtocolOp(r).encodeProtocolOp();
                break;
            case DELETE:
                protocolOpElement = new DeleteResponseProtocolOp(r).encodeProtocolOp();
                break;
            case EXTENDED:
                protocolOpElement = new ExtendedResponseProtocolOp(r).encodeProtocolOp();
                break;
            case MODIFY:
                protocolOpElement = new ModifyResponseProtocolOp(r).encodeProtocolOp();
                break;
            case MODIFY_DN:
                protocolOpElement = new ModifyDNResponseProtocolOp(r).encodeProtocolOp();
                break;
            default:
                throw new LDAPException(ResultCode.PARAM_ERROR, ERR_MULTI_UPDATE_RESULT_INVALID_OP_TYPE.get(t.name()));
        }
        final Control[] controls = r.getResponseControls();
        if ((controls == null) || (controls.length == 0)) {
            opElements.add(new ASN1Sequence(protocolOpElement));
        } else {
            opElements.add(new ASN1Sequence(protocolOpElement, Control.encodeControls(controls)));
        }
    }
    final ASN1Sequence valueSequence = new ASN1Sequence(new ASN1Enumerated(changesApplied.intValue()), new ASN1Sequence(opElements));
    return new ASN1OctetString(valueSequence.encode());
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ExtendedResponseProtocolOp(com.unboundid.ldap.protocol.ExtendedResponseProtocolOp) DeleteResponseProtocolOp(com.unboundid.ldap.protocol.DeleteResponseProtocolOp) ArrayList(java.util.ArrayList) LDAPResult(com.unboundid.ldap.sdk.LDAPResult) AddResponseProtocolOp(com.unboundid.ldap.protocol.AddResponseProtocolOp) ModifyDNResponseProtocolOp(com.unboundid.ldap.protocol.ModifyDNResponseProtocolOp) Control(com.unboundid.ldap.sdk.Control) ASN1Sequence(com.unboundid.asn1.ASN1Sequence) LDAPException(com.unboundid.ldap.sdk.LDAPException) ASN1Enumerated(com.unboundid.asn1.ASN1Enumerated) ASN1Element(com.unboundid.asn1.ASN1Element) ModifyResponseProtocolOp(com.unboundid.ldap.protocol.ModifyResponseProtocolOp) OperationType(com.unboundid.ldap.sdk.OperationType) Nullable(com.unboundid.util.Nullable)

Aggregations

AddResponseProtocolOp (com.unboundid.ldap.protocol.AddResponseProtocolOp)14 LDAPMessage (com.unboundid.ldap.protocol.LDAPMessage)11 LDAPException (com.unboundid.ldap.sdk.LDAPException)8 NotNull (com.unboundid.util.NotNull)8 Control (com.unboundid.ldap.sdk.Control)7 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)5 AddRequestProtocolOp (com.unboundid.ldap.protocol.AddRequestProtocolOp)4 DeleteResponseProtocolOp (com.unboundid.ldap.protocol.DeleteResponseProtocolOp)4 AuthorizationIdentityRequestControl (com.unboundid.ldap.sdk.controls.AuthorizationIdentityRequestControl)4 ManageDsaITRequestControl (com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl)4 ArrayList (java.util.ArrayList)4 ModifyDNResponseProtocolOp (com.unboundid.ldap.protocol.ModifyDNResponseProtocolOp)3 ModifyResponseProtocolOp (com.unboundid.ldap.protocol.ModifyResponseProtocolOp)3 AssertionRequestControl (com.unboundid.ldap.sdk.controls.AssertionRequestControl)3 AuthorizationIdentityResponseControl (com.unboundid.ldap.sdk.controls.AuthorizationIdentityResponseControl)3 DontUseCopyRequestControl (com.unboundid.ldap.sdk.controls.DontUseCopyRequestControl)3 DraftLDUPSubentriesRequestControl (com.unboundid.ldap.sdk.controls.DraftLDUPSubentriesRequestControl)3 PermissiveModifyRequestControl (com.unboundid.ldap.sdk.controls.PermissiveModifyRequestControl)3 PostReadRequestControl (com.unboundid.ldap.sdk.controls.PostReadRequestControl)3 PostReadResponseControl (com.unboundid.ldap.sdk.controls.PostReadResponseControl)3