use of com.unboundid.ldap.protocol.LDAPMessage in project ldapsdk by pingidentity.
the class TransactionExtendedOperationHandler method handleStartTransaction.
/**
* Performs the appropriate processing for a start 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 StartTransactionExtendedResult handleStartTransaction(@NotNull final InMemoryRequestHandler handler, final int messageID, @NotNull final ExtendedRequest request) {
// If there is already an active transaction on the associated connection,
// then make sure it gets aborted.
final Map<String, Object> connectionState = handler.getConnectionState();
final ObjectPair<?, ?> existingTxnInfo = (ObjectPair<?, ?>) connectionState.remove(STATE_VARIABLE_TXN_INFO);
if (existingTxnInfo != null) {
final ASN1OctetString txnID = (ASN1OctetString) existingTxnInfo.getFirst();
try {
handler.getClientConnection().sendUnsolicitedNotification(new AbortedTransactionExtendedResult(txnID, ResultCode.CONSTRAINT_VIOLATION, ERR_TXN_EXTOP_TXN_ABORTED_BY_NEW_START_TXN.get(txnID.stringValue()), null, null, null));
} catch (final LDAPException le) {
Debug.debugException(le);
return new StartTransactionExtendedResult(new ExtendedResult(le));
}
}
// request.
try {
new StartTransactionExtendedRequest(request);
} catch (final LDAPException le) {
Debug.debugException(le);
return new StartTransactionExtendedResult(messageID, ResultCode.PROTOCOL_ERROR, le.getMessage(), null, null, null, null);
}
// Create a new object with information to use for the transaction. It will
// include the transaction ID and a list of LDAP messages that are part of
// the transaction. Store it in the connection state.
final ASN1OctetString txnID = new ASN1OctetString(String.valueOf(TXN_ID_COUNTER.getAndIncrement()));
final List<LDAPMessage> requestList = new ArrayList<>(10);
final ObjectPair<ASN1OctetString, List<LDAPMessage>> txnInfo = new ObjectPair<>(txnID, requestList);
connectionState.put(STATE_VARIABLE_TXN_INFO, txnInfo);
// Return the response to the client.
return new StartTransactionExtendedResult(messageID, ResultCode.SUCCESS, INFO_TXN_EXTOP_CREATED_TXN.get(txnID.stringValue()), null, null, txnID, null);
}
use of com.unboundid.ldap.protocol.LDAPMessage 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);
}
}
}
use of com.unboundid.ldap.protocol.LDAPMessage in project ldapsdk by pingidentity.
the class AddRequest method processSync.
/**
* Processes this add operation in synchronous mode, in which the same thread
* will send the request and read the response.
*
* @param connection The connection to use to communicate with the directory
* server.
* @param depth The current referral depth for this request. It should
* always be one for the initial request, and should only
* be incremented when following referrals.
* @param allowRetry Indicates whether the request may be re-tried on a
* re-established connection if the initial attempt fails
* in a way that indicates the connection is no longer
* valid and autoReconnect is true.
*
* @return An LDAP result object that provides information about the result
* of the add processing.
*
* @throws LDAPException If a problem occurs while sending the request or
* reading the response.
*/
@NotNull()
private LDAPResult processSync(@NotNull final LDAPConnection connection, final int depth, final boolean allowRetry) throws LDAPException {
// Create the LDAP message.
messageID = connection.nextMessageID();
final LDAPMessage message = new LDAPMessage(messageID, this, getControls());
// Send the request to the server.
final long requestTime = System.nanoTime();
Debug.debugLDAPRequest(Level.INFO, this, messageID, connection);
final LDAPConnectionLogger logger = connection.getConnectionOptions().getConnectionLogger();
if (logger != null) {
logger.logAddRequest(connection, messageID, this);
}
connection.getConnectionStatistics().incrementNumAddRequests();
try {
connection.sendMessage(message, getResponseTimeoutMillis(connection));
} catch (final LDAPException le) {
Debug.debugException(le);
if (allowRetry) {
final LDAPResult retryResult = reconnectAndRetry(connection, depth, le.getResultCode());
if (retryResult != null) {
return retryResult;
}
}
throw le;
}
while (true) {
final LDAPResponse response;
try {
response = connection.readResponse(messageID);
} catch (final LDAPException le) {
Debug.debugException(le);
if ((le.getResultCode() == ResultCode.TIMEOUT) && connection.getConnectionOptions().abandonOnTimeout()) {
connection.abandon(messageID);
}
if (allowRetry) {
final LDAPResult retryResult = reconnectAndRetry(connection, depth, le.getResultCode());
if (retryResult != null) {
return retryResult;
}
}
throw le;
}
if (response instanceof IntermediateResponse) {
final IntermediateResponseListener listener = getIntermediateResponseListener();
if (listener != null) {
listener.intermediateResponseReturned((IntermediateResponse) response);
}
} else {
return handleResponse(connection, response, requestTime, depth, allowRetry);
}
}
}
use of com.unboundid.ldap.protocol.LDAPMessage in project ldapsdk by pingidentity.
the class InMemoryOperationInterceptorRequestHandler method processBindRequest.
/**
* {@inheritDoc}
*/
@Override()
@NotNull()
public LDAPMessage processBindRequest(final int messageID, @NotNull final BindRequestProtocolOp request, @NotNull final List<Control> controls) {
if (request.getCredentialsType() == BindRequestProtocolOp.CRED_TYPE_SIMPLE) {
final InterceptedSimpleBindOperation op = new InterceptedSimpleBindOperation(connection, messageID, request, toArray(controls));
activeOperations.put(messageID, op);
try {
for (final InMemoryOperationInterceptor i : interceptors) {
try {
i.processSimpleBindRequest(op);
} catch (final LDAPException le) {
Debug.debugException(le);
return new LDAPMessage(messageID, new BindResponseProtocolOp(le.toLDAPResult()), le.getResponseControls());
} catch (final Exception e) {
Debug.debugException(e);
return new LDAPMessage(messageID, new BindResponseProtocolOp(ResultCode.OTHER_INT_VALUE, null, ERR_DS_INTERCEPTOR_REQUEST_ERROR.get(String.valueOf(op), i.getClass().getName(), StaticUtils.getExceptionMessage(e)), null, null));
}
}
final LDAPMessage resultMessage = wrappedHandler.processBindRequest(messageID, new BindRequestProtocolOp(op.getRequest()), op.getRequest().getControlList());
op.setResult(resultMessage.getBindResponseProtocolOp().toBindResult(toArray(resultMessage.getControls())));
for (final InMemoryOperationInterceptor i : interceptors) {
try {
i.processSimpleBindResult(op);
} catch (final Exception e) {
Debug.debugException(e);
return new LDAPMessage(messageID, new BindResponseProtocolOp(ResultCode.OTHER_INT_VALUE, null, ERR_DS_INTERCEPTOR_RESULT_ERROR.get(String.valueOf(op), i.getClass().getName(), StaticUtils.getExceptionMessage(e)), null, null));
}
}
return new LDAPMessage(messageID, new BindResponseProtocolOp(op.getResult()), op.getResult().getResponseControls());
} finally {
activeOperations.remove(messageID);
}
} else {
final InterceptedSASLBindOperation op = new InterceptedSASLBindOperation(connection, messageID, request, toArray(controls));
activeOperations.put(messageID, op);
try {
for (final InMemoryOperationInterceptor i : interceptors) {
try {
i.processSASLBindRequest(op);
} catch (final LDAPException le) {
Debug.debugException(le);
return new LDAPMessage(messageID, new BindResponseProtocolOp(le.toLDAPResult()), le.getResponseControls());
} catch (final Exception e) {
Debug.debugException(e);
return new LDAPMessage(messageID, new BindResponseProtocolOp(ResultCode.OTHER_INT_VALUE, null, ERR_DS_INTERCEPTOR_REQUEST_ERROR.get(String.valueOf(op), i.getClass().getName(), StaticUtils.getExceptionMessage(e)), null, null));
}
}
final LDAPMessage resultMessage = wrappedHandler.processBindRequest(messageID, new BindRequestProtocolOp(op.getRequest()), op.getRequest().getControlList());
op.setResult(resultMessage.getBindResponseProtocolOp().toBindResult(toArray(resultMessage.getControls())));
for (final InMemoryOperationInterceptor i : interceptors) {
try {
i.processSASLBindResult(op);
} catch (final Exception e) {
Debug.debugException(e);
return new LDAPMessage(messageID, new BindResponseProtocolOp(ResultCode.OTHER_INT_VALUE, null, ERR_DS_INTERCEPTOR_RESULT_ERROR.get(String.valueOf(op), i.getClass().getName(), StaticUtils.getExceptionMessage(e)), null, null));
}
}
return new LDAPMessage(messageID, new BindResponseProtocolOp(op.getResult()), op.getResult().getResponseControls());
} finally {
activeOperations.remove(messageID);
}
}
}
use of com.unboundid.ldap.protocol.LDAPMessage in project ldapsdk by pingidentity.
the class InMemoryOperationInterceptorRequestHandler method processModifyRequest.
/**
* {@inheritDoc}
*/
@Override()
@NotNull()
public LDAPMessage processModifyRequest(final int messageID, @NotNull final ModifyRequestProtocolOp request, @NotNull final List<Control> controls) {
final InterceptedModifyOperation op = new InterceptedModifyOperation(connection, messageID, request, toArray(controls));
activeOperations.put(messageID, op);
try {
for (final InMemoryOperationInterceptor i : interceptors) {
try {
i.processModifyRequest(op);
} catch (final LDAPException le) {
Debug.debugException(le);
return new LDAPMessage(messageID, new ModifyResponseProtocolOp(le.toLDAPResult()), le.getResponseControls());
} catch (final Exception e) {
Debug.debugException(e);
return new LDAPMessage(messageID, new ModifyResponseProtocolOp(ResultCode.OTHER_INT_VALUE, null, ERR_DS_INTERCEPTOR_REQUEST_ERROR.get(String.valueOf(op), i.getClass().getName(), StaticUtils.getExceptionMessage(e)), null));
}
}
final LDAPMessage resultMessage = wrappedHandler.processModifyRequest(messageID, new ModifyRequestProtocolOp((ModifyRequest) op.getRequest()), op.getRequest().getControlList());
op.setResult(resultMessage.getModifyResponseProtocolOp().toLDAPResult(toArray(resultMessage.getControls())));
for (final InMemoryOperationInterceptor i : interceptors) {
try {
i.processModifyResult(op);
} catch (final Exception e) {
Debug.debugException(e);
return new LDAPMessage(messageID, new ModifyResponseProtocolOp(ResultCode.OTHER_INT_VALUE, null, ERR_DS_INTERCEPTOR_RESULT_ERROR.get(String.valueOf(op), i.getClass().getName(), StaticUtils.getExceptionMessage(e)), null));
}
}
return new LDAPMessage(messageID, new ModifyResponseProtocolOp(op.getResult()), op.getResult().getResponseControls());
} finally {
activeOperations.remove(messageID);
}
}
Aggregations