use of com.unboundid.ldap.protocol.ExtendedResponseProtocolOp in project ldapsdk by pingidentity.
the class InMemoryRequestHandler method processExtendedRequest.
/**
* Attempts to process the provided extended request, if an extended operation
* handler is defined for the given request OID.
*
* @param messageID The message ID of the LDAP message containing the
* extended request.
* @param request The extended request that was included in the LDAP
* message that was received.
* @param controls The set of controls included in the LDAP message. It
* may be empty if there were no controls, but will not be
* {@code null}.
*
* @return The {@link LDAPMessage} containing the response to send to the
* client. The protocol op in the {@code LDAPMessage} must be an
* {@code ExtendedResponseProtocolOp}.
*/
@Override()
@NotNull()
public LDAPMessage processExtendedRequest(final int messageID, @NotNull final ExtendedRequestProtocolOp request, @NotNull final List<Control> controls) {
synchronized (entryMap) {
// Sleep before processing, if appropriate.
sleepBeforeProcessing();
boolean isInternalOp = false;
for (final Control c : controls) {
if (c.getOID().equals(OID_INTERNAL_OPERATION_REQUEST_CONTROL)) {
isInternalOp = true;
break;
}
}
// If this operation type is not allowed, then reject it.
if ((!isInternalOp) && (!config.getAllowedOperationTypes().contains(OperationType.EXTENDED))) {
return new LDAPMessage(messageID, new ExtendedResponseProtocolOp(ResultCode.UNWILLING_TO_PERFORM_INT_VALUE, null, ERR_MEM_HANDLER_EXTENDED_NOT_ALLOWED.get(), null, null, null));
}
// client is authenticated.
if ((authenticatedDN.isNullDN() && config.getAuthenticationRequiredOperationTypes().contains(OperationType.EXTENDED))) {
return new LDAPMessage(messageID, new ExtendedResponseProtocolOp(ResultCode.INSUFFICIENT_ACCESS_RIGHTS_INT_VALUE, null, ERR_MEM_HANDLER_EXTENDED_REQUIRES_AUTH.get(), null, null, null));
}
final String oid = request.getOID();
final InMemoryExtendedOperationHandler handler = extendedRequestHandlers.get(oid);
if (handler == null) {
return new LDAPMessage(messageID, new ExtendedResponseProtocolOp(ResultCode.UNWILLING_TO_PERFORM_INT_VALUE, null, ERR_MEM_HANDLER_EXTENDED_OP_NOT_SUPPORTED.get(oid), null, null, null));
}
try {
final Control[] controlArray = new Control[controls.size()];
controls.toArray(controlArray);
final ExtendedRequest extendedRequest = new ExtendedRequest(oid, request.getValue(), controlArray);
final ExtendedResult extendedResult = handler.processExtendedOperation(this, messageID, extendedRequest);
return new LDAPMessage(messageID, new ExtendedResponseProtocolOp(extendedResult.getResultCode().intValue(), extendedResult.getMatchedDN(), extendedResult.getDiagnosticMessage(), Arrays.asList(extendedResult.getReferralURLs()), extendedResult.getOID(), extendedResult.getValue()), extendedResult.getResponseControls());
} catch (final Exception e) {
Debug.debugException(e);
return new LDAPMessage(messageID, new ExtendedResponseProtocolOp(ResultCode.OTHER_INT_VALUE, null, ERR_MEM_HANDLER_EXTENDED_OP_FAILURE.get(StaticUtils.getExceptionMessage(e)), null, null, null));
}
}
}
use of com.unboundid.ldap.protocol.ExtendedResponseProtocolOp in project ldapsdk by pingidentity.
the class JSONAccessLogRequestHandler method processExtendedRequest.
/**
* {@inheritDoc}
*/
@Override()
@NotNull()
public LDAPMessage processExtendedRequest(final int messageID, @NotNull final ExtendedRequestProtocolOp request, @NotNull final List<Control> controls) {
final long opID = nextOperationID.getAndIncrement();
final JSONBuffer buffer = getRequestHeader("extended", opID, messageID);
buffer.appendString("request-oid", request.getOID());
buffer.endObject();
logHandler.publish(new LogRecord(Level.INFO, buffer.toString()));
logHandler.flush();
final long startTimeNanos = System.nanoTime();
final LDAPMessage responseMessage = requestHandler.processExtendedRequest(messageID, request, controls);
final long eTimeNanos = System.nanoTime() - startTimeNanos;
final ExtendedResponseProtocolOp protocolOp = responseMessage.getExtendedResponseProtocolOp();
generateResponse(buffer, "extended", opID, messageID, protocolOp.getResultCode(), protocolOp.getDiagnosticMessage(), protocolOp.getMatchedDN(), protocolOp.getReferralURLs(), eTimeNanos);
final String responseOID = protocolOp.getResponseOID();
if (responseOID != null) {
buffer.appendString("response-oid", responseOID);
}
buffer.endObject();
logHandler.publish(new LogRecord(Level.INFO, buffer.toString()));
logHandler.flush();
return responseMessage;
}
use of com.unboundid.ldap.protocol.ExtendedResponseProtocolOp in project ldapsdk by pingidentity.
the class LDAPListenerClientConnection method run.
/**
* Operates in a loop, waiting for a request to arrive from the client and
* handing it off to the request handler for processing. This method is for
* internal use only and must not be invoked by external callers.
*/
@InternalUseOnly()
@Override()
public void run() {
try {
while (true) {
final LDAPMessage requestMessage;
try {
requestMessage = LDAPMessage.readFrom(asn1Reader, false);
if (requestMessage == null) {
// so we won't notify the exception handler.
try {
close();
} catch (final IOException ioe) {
Debug.debugException(ioe);
}
return;
}
} catch (final LDAPException le) {
// This indicates that the client sent a malformed request.
Debug.debugException(le);
close(le);
return;
}
try {
final int messageID = requestMessage.getMessageID();
final List<Control> controls = requestMessage.getControls();
LDAPMessage responseMessage;
switch(requestMessage.getProtocolOpType()) {
case LDAPMessage.PROTOCOL_OP_TYPE_ABANDON_REQUEST:
requestHandler.processAbandonRequest(messageID, requestMessage.getAbandonRequestProtocolOp(), controls);
responseMessage = null;
break;
case LDAPMessage.PROTOCOL_OP_TYPE_ADD_REQUEST:
try {
responseMessage = requestHandler.processAddRequest(messageID, requestMessage.getAddRequestProtocolOp(), controls);
} catch (final Exception e) {
Debug.debugException(e);
responseMessage = new LDAPMessage(messageID, new AddResponseProtocolOp(ResultCode.OTHER_INT_VALUE, null, ERR_CONN_REQUEST_HANDLER_FAILURE.get(StaticUtils.getExceptionMessage(e)), null));
}
break;
case LDAPMessage.PROTOCOL_OP_TYPE_BIND_REQUEST:
try {
responseMessage = requestHandler.processBindRequest(messageID, requestMessage.getBindRequestProtocolOp(), controls);
} catch (final Exception e) {
Debug.debugException(e);
responseMessage = new LDAPMessage(messageID, new BindResponseProtocolOp(ResultCode.OTHER_INT_VALUE, null, ERR_CONN_REQUEST_HANDLER_FAILURE.get(StaticUtils.getExceptionMessage(e)), null, null));
}
break;
case LDAPMessage.PROTOCOL_OP_TYPE_COMPARE_REQUEST:
try {
responseMessage = requestHandler.processCompareRequest(messageID, requestMessage.getCompareRequestProtocolOp(), controls);
} catch (final Exception e) {
Debug.debugException(e);
responseMessage = new LDAPMessage(messageID, new CompareResponseProtocolOp(ResultCode.OTHER_INT_VALUE, null, ERR_CONN_REQUEST_HANDLER_FAILURE.get(StaticUtils.getExceptionMessage(e)), null));
}
break;
case LDAPMessage.PROTOCOL_OP_TYPE_DELETE_REQUEST:
try {
responseMessage = requestHandler.processDeleteRequest(messageID, requestMessage.getDeleteRequestProtocolOp(), controls);
} catch (final Exception e) {
Debug.debugException(e);
responseMessage = new LDAPMessage(messageID, new DeleteResponseProtocolOp(ResultCode.OTHER_INT_VALUE, null, ERR_CONN_REQUEST_HANDLER_FAILURE.get(StaticUtils.getExceptionMessage(e)), null));
}
break;
case LDAPMessage.PROTOCOL_OP_TYPE_EXTENDED_REQUEST:
try {
responseMessage = requestHandler.processExtendedRequest(messageID, requestMessage.getExtendedRequestProtocolOp(), controls);
} catch (final Exception e) {
Debug.debugException(e);
responseMessage = new LDAPMessage(messageID, new ExtendedResponseProtocolOp(ResultCode.OTHER_INT_VALUE, null, ERR_CONN_REQUEST_HANDLER_FAILURE.get(StaticUtils.getExceptionMessage(e)), null, null, null));
}
break;
case LDAPMessage.PROTOCOL_OP_TYPE_MODIFY_REQUEST:
try {
responseMessage = requestHandler.processModifyRequest(messageID, requestMessage.getModifyRequestProtocolOp(), controls);
} catch (final Exception e) {
Debug.debugException(e);
responseMessage = new LDAPMessage(messageID, new ModifyResponseProtocolOp(ResultCode.OTHER_INT_VALUE, null, ERR_CONN_REQUEST_HANDLER_FAILURE.get(StaticUtils.getExceptionMessage(e)), null));
}
break;
case LDAPMessage.PROTOCOL_OP_TYPE_MODIFY_DN_REQUEST:
try {
responseMessage = requestHandler.processModifyDNRequest(messageID, requestMessage.getModifyDNRequestProtocolOp(), controls);
} catch (final Exception e) {
Debug.debugException(e);
responseMessage = new LDAPMessage(messageID, new ModifyDNResponseProtocolOp(ResultCode.OTHER_INT_VALUE, null, ERR_CONN_REQUEST_HANDLER_FAILURE.get(StaticUtils.getExceptionMessage(e)), null));
}
break;
case LDAPMessage.PROTOCOL_OP_TYPE_SEARCH_REQUEST:
try {
responseMessage = requestHandler.processSearchRequest(messageID, requestMessage.getSearchRequestProtocolOp(), controls);
} catch (final Exception e) {
Debug.debugException(e);
responseMessage = new LDAPMessage(messageID, new SearchResultDoneProtocolOp(ResultCode.OTHER_INT_VALUE, null, ERR_CONN_REQUEST_HANDLER_FAILURE.get(StaticUtils.getExceptionMessage(e)), null));
}
break;
case LDAPMessage.PROTOCOL_OP_TYPE_UNBIND_REQUEST:
requestHandler.processUnbindRequest(messageID, requestMessage.getUnbindRequestProtocolOp(), controls);
close();
return;
default:
close(new LDAPException(ResultCode.PROTOCOL_ERROR, ERR_CONN_INVALID_PROTOCOL_OP_TYPE.get(StaticUtils.toHex(requestMessage.getProtocolOpType()))));
return;
}
if (responseMessage != null) {
try {
sendMessage(responseMessage);
} catch (final LDAPException le) {
Debug.debugException(le);
close(le);
return;
}
}
} catch (final Throwable t) {
close(new LDAPException(ResultCode.LOCAL_ERROR, ERR_CONN_EXCEPTION_IN_REQUEST_HANDLER.get(String.valueOf(requestMessage), StaticUtils.getExceptionMessage(t))));
StaticUtils.throwErrorOrRuntimeException(t);
}
}
} finally {
if (listener != null) {
listener.connectionClosed(this);
}
}
}
use of com.unboundid.ldap.protocol.ExtendedResponseProtocolOp in project ldapsdk by pingidentity.
the class ProxyRequestHandler method processExtendedRequest.
/**
* {@inheritDoc}
*/
@Override()
@NotNull()
public LDAPMessage processExtendedRequest(final int messageID, @NotNull final ExtendedRequestProtocolOp request, @NotNull final List<Control> controls) {
final ExtendedRequest extendedRequest;
if (controls.isEmpty()) {
extendedRequest = new ExtendedRequest(request.getOID(), request.getValue());
} else {
final Control[] controlArray = new Control[controls.size()];
controls.toArray(controlArray);
extendedRequest = new ExtendedRequest(request.getOID(), request.getValue(), controlArray);
}
extendedRequest.setIntermediateResponseListener(this);
try {
final ExtendedResult extendedResult = ldapConnection.processExtendedOperation(extendedRequest);
final ExtendedResponseProtocolOp extendedResponseProtocolOp = new ExtendedResponseProtocolOp(extendedResult.getResultCode().intValue(), extendedResult.getMatchedDN(), extendedResult.getDiagnosticMessage(), Arrays.asList(extendedResult.getReferralURLs()), extendedResult.getOID(), extendedResult.getValue());
return new LDAPMessage(messageID, extendedResponseProtocolOp, Arrays.asList(extendedResult.getResponseControls()));
} catch (final LDAPException le) {
Debug.debugException(le);
final ExtendedResponseProtocolOp extendedResponseProtocolOp = new ExtendedResponseProtocolOp(le.getResultCode().intValue(), le.getMatchedDN(), le.getMessage(), Arrays.asList(le.getReferralURLs()), null, null);
return new LDAPMessage(messageID, extendedResponseProtocolOp, Arrays.asList(le.getResponseControls()));
}
}
use of com.unboundid.ldap.protocol.ExtendedResponseProtocolOp in project ldapsdk by pingidentity.
the class InMemoryDirectoryServer method processExtendedOperation.
/**
* Processes the provided extended request. Note that because some types of
* extended operations return unusual result codes under "normal" conditions,
* the server may not always throw an exception for a failed extended
* operation like it does for other types of operations. It will throw an
* exception under conditions where there appears to be a problem with the
* connection or the server to which the connection is established, but there
* may be many circumstances in which an extended operation is not processed
* correctly but this method does not throw an exception. In the event that
* no exception is thrown, it is the responsibility of the caller to interpret
* the result to determine whether the operation was processed as expected.
* <BR><BR>
* This method may be used regardless of whether the server is listening for
* client connections, and regardless of whether extended operations are
* allowed in the server.
*
* @param extendedRequest The extended request to be processed. It must not
* be {@code null}.
*
* @return The extended result object that provides information about the
* result of the request processing. It may or may not indicate that
* the operation was successful.
*
* @throws LDAPException If a problem occurs while sending the request or
* reading the response.
*/
@NotNull()
public ExtendedResult processExtendedOperation(@NotNull final ExtendedRequest extendedRequest) throws LDAPException {
Validator.ensureNotNull(extendedRequest);
final ArrayList<Control> requestControlList = new ArrayList<>(extendedRequest.getControlList());
requestControlList.add(new Control(InMemoryRequestHandler.OID_INTERNAL_OPERATION_REQUEST_CONTROL, false));
final LDAPMessage responseMessage = inMemoryHandler.processExtendedRequest(1, new ExtendedRequestProtocolOp(extendedRequest.getOID(), extendedRequest.getValue()), requestControlList);
final ExtendedResponseProtocolOp extendedResponse = responseMessage.getExtendedResponseProtocolOp();
final ResultCode rc = ResultCode.valueOf(extendedResponse.getResultCode());
final String[] referralURLs;
final List<String> referralURLList = extendedResponse.getReferralURLs();
if ((referralURLList == null) || referralURLList.isEmpty()) {
referralURLs = StaticUtils.NO_STRINGS;
} else {
referralURLs = new String[referralURLList.size()];
referralURLList.toArray(referralURLs);
}
final Control[] responseControls;
final List<Control> controlList = responseMessage.getControls();
if ((controlList == null) || controlList.isEmpty()) {
responseControls = StaticUtils.NO_CONTROLS;
} else {
responseControls = new Control[controlList.size()];
controlList.toArray(responseControls);
}
final ExtendedResult extendedResult = new ExtendedResult(responseMessage.getMessageID(), rc, extendedResponse.getDiagnosticMessage(), extendedResponse.getMatchedDN(), referralURLs, extendedResponse.getResponseOID(), extendedResponse.getResponseValue(), responseControls);
if ((extendedResult.getOID() == null) && (extendedResult.getValue() == null)) {
switch(rc.intValue()) {
case ResultCode.OPERATIONS_ERROR_INT_VALUE:
case ResultCode.PROTOCOL_ERROR_INT_VALUE:
case ResultCode.BUSY_INT_VALUE:
case ResultCode.UNAVAILABLE_INT_VALUE:
case ResultCode.OTHER_INT_VALUE:
case ResultCode.SERVER_DOWN_INT_VALUE:
case ResultCode.LOCAL_ERROR_INT_VALUE:
case ResultCode.ENCODING_ERROR_INT_VALUE:
case ResultCode.DECODING_ERROR_INT_VALUE:
case ResultCode.TIMEOUT_INT_VALUE:
case ResultCode.NO_MEMORY_INT_VALUE:
case ResultCode.CONNECT_ERROR_INT_VALUE:
throw new LDAPException(extendedResult);
}
}
return extendedResult;
}
Aggregations