Class DefaultOperationHandle<Q extends Request,S extends Result>

java.lang.Object
org.ldaptive.transport.DefaultOperationHandle<Q,S>
Type Parameters:
Q - type of request
S - type of result
All Implemented Interfaces:
OperationHandle<Q,S>
Direct Known Subclasses:
DefaultCompareOperationHandle, DefaultExtendedOperationHandle, DefaultSearchOperationHandle, NettyConnection.BindOperationHandle

public class DefaultOperationHandle<Q extends Request,S extends Result> extends Object implements OperationHandle<Q,S>
Handle that notifies on the components of an LDAP operation request.
  • Field Details

    • DEFAULT_RESPONSE_TIMEOUT_CONDITION

      private static final Predicate<Message> DEFAULT_RESPONSE_TIMEOUT_CONDITION
      Predicate that requires any result message except unsolicited.
    • logger

      protected final Logger logger
      Logger for this class.
    • request

      private final Q extends Request request
      Protocol request to send.
    • responseTimeout

      private final Duration responseTimeout
      Time to wait for a response.
    • connection

      private TransportConnection connection
      Connection to send the request on.
    • messageID

      private Integer messageID
      Protocol message ID.
    • onResult

      private ResultHandler[] onResult
      Functions to handle response results.
    • onControl

      private ResponseControlHandler[] onControl
      Functions to handle response controls.
    • onReferral

      private ReferralHandler[] onReferral
      Functions to handle referral URLs.
    • onIntermediate

      private IntermediateResponseHandler[] onIntermediate
      Functions to handle intermediate responses.
    • onException

      private ExceptionHandler onException
      Function to handle exceptions.
    • onUnsolicitedNotification

      private UnsolicitedNotificationHandler[] onUnsolicitedNotification
      Function to handle unsolicited notifications.
    • onReferralResult

      private ReferralResultHandler<S extends Result> onReferralResult
      Function to chase referrals.
    • onComplete

      private CompleteHandler onComplete
      Function to run when the operation completes.
    • throwCondition

      private ResultPredicate throwCondition
      Function to run when a result is received to determine whether an exception should be raised.
    • responseSemaphore

      private final Semaphore responseSemaphore
      Semaphore to determine when a response has been received.
    • creationTime

      private final Instant creationTime
      Timestamp when the handle was created.
    • sentTime

      private Instant sentTime
      Timestamp when the request was sent. See TransportConnection.write(DefaultOperationHandle).
    • receivedTime

      private Instant receivedTime
      Timestamp when the result was received or an exception occurred.
    • abandonedTime

      private Instant abandonedTime
      Timestamp when the request was abandoned.
    • consumedMessage

      private volatile boolean consumedMessage
      Whether this handle has consumed any messages.
    • result

      private S extends Result result
      Protocol response result.
    • exception

      private LdapException exception
      Exception encountered attempting to process the request.
  • Constructor Details

    • DefaultOperationHandle

      public DefaultOperationHandle(Q req, TransportConnection conn, Duration timeout)
      Creates a new operation handle.
      Parameters:
      req - request to expect a response for
      conn - the request will be executed on
      timeout - duration to wait for a response
  • Method Details

    • getResponseTimeoutCondition

      protected Predicate<Message> getResponseTimeoutCondition()
      Returns a predicate to determine whether the responseTimeout semaphore should be released.
      Returns:
      response timeout condition
    • send

      public DefaultOperationHandle<Q,S> send()
      Description copied from interface: OperationHandle
      Sends this request to the server.
      Specified by:
      send in interface OperationHandle<Q extends Request,S extends Result>
      Returns:
      this handle
    • await

      public S await() throws LdapException
      Description copied from interface: OperationHandle
      Waits for a result or reports a timeout exception.
      Specified by:
      await in interface OperationHandle<Q extends Request,S extends Result>
      Returns:
      result of the operation or empty if the operation is abandoned
      Throws:
      LdapException - if an error occurs executing the request
    • evaluateThrowCondition

      protected void evaluateThrowCondition(Result r) throws LdapException
      Invokes throwCondition, if it exists, for the supplied result.
      Parameters:
      r - to evaluate
      Throws:
      LdapException - if the condition fails
    • onResult

      public DefaultOperationHandle<Q,S> onResult(ResultHandler... function)
      Description copied from interface: OperationHandle
      Sets the functions to execute when a result is received.
      Specified by:
      onResult in interface OperationHandle<Q extends Request,S extends Result>
      Parameters:
      function - to execute on a result
      Returns:
      this handle
    • onControl

      public DefaultOperationHandle<Q,S> onControl(ResponseControlHandler... function)
      Description copied from interface: OperationHandle
      Sets the functions to execute when a control is received.
      Specified by:
      onControl in interface OperationHandle<Q extends Request,S extends Result>
      Parameters:
      function - to execute on a control
      Returns:
      this handle
    • onReferral

      public DefaultOperationHandle<Q,S> onReferral(ReferralHandler... function)
      Description copied from interface: OperationHandle
      Sets the functions to execute when a referral is received.
      Specified by:
      onReferral in interface OperationHandle<Q extends Request,S extends Result>
      Parameters:
      function - to execute on a referral
      Returns:
      this handle
    • onIntermediate

      public DefaultOperationHandle<Q,S> onIntermediate(IntermediateResponseHandler... function)
      Description copied from interface: OperationHandle
      Sets the functions to execute when an intermediate response is received.
      Specified by:
      onIntermediate in interface OperationHandle<Q extends Request,S extends Result>
      Parameters:
      function - to execute on an intermediate response
      Returns:
      this handle
    • onUnsolicitedNotification

      public DefaultOperationHandle<Q,S> onUnsolicitedNotification(UnsolicitedNotificationHandler... function)
      Description copied from interface: OperationHandle
      Sets the functions to execute when an unsolicited notification is received.
      Specified by:
      onUnsolicitedNotification in interface OperationHandle<Q extends Request,S extends Result>
      Parameters:
      function - to execute on an unsolicited notification
      Returns:
      this handle
    • onReferralResult

      public DefaultOperationHandle<Q,S> onReferralResult(ReferralResultHandler<S> function)
      Description copied from interface: OperationHandle
      Sets the functions to execute when a response is complete and contains a referral result code.
      Specified by:
      onReferralResult in interface OperationHandle<Q extends Request,S extends Result>
      Parameters:
      function - to execute on a referral response
      Returns:
      this handle
    • onException

      public DefaultOperationHandle<Q,S> onException(ExceptionHandler function)
      Description copied from interface: OperationHandle
      Sets the function to execute when an exception occurs.
      Specified by:
      onException in interface OperationHandle<Q extends Request,S extends Result>
      Parameters:
      function - to execute when an exception occurs
      Returns:
      this handle
    • onComplete

      public DefaultOperationHandle<Q,S> onComplete(CompleteHandler function)
      Description copied from interface: OperationHandle
      Sets the function to execute when the operation completes.
      Specified by:
      onComplete in interface OperationHandle<Q extends Request,S extends Result>
      Parameters:
      function - to execute on completion
      Returns:
      this handle
    • throwIf

      public DefaultOperationHandle<Q,S> throwIf(ResultPredicate function)
      Description copied from interface: OperationHandle
      Sets the function to determine whether an exception should be raised by a particular result.
      Specified by:
      throwIf in interface OperationHandle<Q extends Request,S extends Result>
      Parameters:
      function - to determine whether to throw an exception
      Returns:
      this handle
    • initializeMessageFunctional

      protected final <T> T[] initializeMessageFunctional(T... functions)
      Iterates over the supplied functions and creates a new, initialized MessageFunctional if any exists.
      Type Parameters:
      T - type of functions
      Parameters:
      functions - to initialize
      Returns:
      array of functions with any MessageFunctional initialized
    • initializeMessageFunctional

      protected final <T> T initializeMessageFunctional(T function)
      Creates a new MessageFunctional if function is of that type and initializes it.
      Type Parameters:
      T - type of function
      Parameters:
      function - to initialize
      Returns:
      initialized message functional or the same function
    • abandon

      public void abandon()
      Description copied from interface: OperationHandle
      Abandons this operation.
      Specified by:
      abandon in interface OperationHandle<Q extends Request,S extends Result>
    • abandon

      public void abandon(LdapException cause)
      Abandons this operation. Any threads waiting on the result will receive an empty result. See Connection.operation(AbandonRequest).
      Parameters:
      cause - the reason this request was abandoned
    • cancel

      public ExtendedOperationHandle cancel()
      Description copied from interface: OperationHandle
      Cancels this operation. See CancelRequest.
      Specified by:
      cancel in interface OperationHandle<Q extends Request,S extends Result>
      Returns:
      extended operation handle
    • supports

      private boolean supports(Result r)
      Whether the supplied result belongs to this handle.
      Parameters:
      r - to inspect
      Returns:
      whether the supplied result belong to this handle
    • getMessageID

      public Integer getMessageID()
      Returns the message ID assigned to this handle.
      Returns:
      message ID
    • getSentTime

      public Instant getSentTime()
      Description copied from interface: OperationHandle
      Returns the time this operation sent a request.
      Specified by:
      getSentTime in interface OperationHandle<Q extends Request,S extends Result>
      Returns:
      sent time
    • getReceivedTime

      public Instant getReceivedTime()
      Description copied from interface: OperationHandle
      Returns the time this operation received a result or encountered an exception.
      Specified by:
      getReceivedTime in interface OperationHandle<Q extends Request,S extends Result>
      Returns:
      received time
    • getAbandonedTime

      public Instant getAbandonedTime()
      Description copied from interface: OperationHandle
      Returns the time this operation was abandoned.
      Specified by:
      getAbandonedTime in interface OperationHandle<Q extends Request,S extends Result>
      Returns:
      abandoned time
    • getOnResult

      public ResultHandler[] getOnResult()
      Return the result handlers.
      Returns:
      result handlers
    • getOnControl

      public ResponseControlHandler[] getOnControl()
      Return the response control handlers.
      Returns:
      response control handlers
    • getOnReferral

      public ReferralHandler[] getOnReferral()
      Return the referral handlers.
      Returns:
      referral handlers
    • getOnIntermediate

      public IntermediateResponseHandler[] getOnIntermediate()
      Return the intermediate response handlers.
      Returns:
      intermediate response handlers
    • getOnException

      public ExceptionHandler getOnException()
      Return the exception handler.
      Returns:
      exception handler
    • getOnComplete

      public CompleteHandler getOnComplete()
      Return the complete handler.
      Returns:
      complete handler
    • getThrowCondition

      public ResultPredicate getThrowCondition()
      Return the throw condition.
      Returns:
      throw condition
    • getOnUnsolicitedNotification

      public UnsolicitedNotificationHandler[] getOnUnsolicitedNotification()
      Return the unsolicited notification handlers
      Returns:
      unsolicited notification handlers
    • getOnReferralResult

      public ReferralResultHandler<S> getOnReferralResult()
      Return the referral result handler.
      Returns:
      referral result handler
    • hasConsumedMessage

      public boolean hasConsumedMessage()
      Returns whether this handle has consumed any messages.
      Returns:
      whether this handle has consumed any messages
    • getRequest

      public Q getRequest()
      Returns the request.
      Returns:
      request
    • messageID

      public void messageID(int id)
      Sets the message ID.
      Parameters:
      id - message ID
    • sent

      public void sent()
      Sets the sent time to now.
    • result

      public void result(S r)
      Invokes onResult and sets the result. Handle is considered done when this is invoked.
      Parameters:
      r - result
    • processResult

      protected void processResult(S r)
      Perform checks on the supplied result and invoke any configured onResult handlers.
      Parameters:
      r - to process
    • finalizeResult

      protected void finalizeResult(S r)
      Sets result, notifies that message has been consumed and invokes complete().
      Parameters:
      r - to finalize
    • control

      public void control(ResponseControl c)
      Invokes onControl.
      Parameters:
      c - response control
    • referral

      public void referral(String... url)
      Invokes onReferral.
      Parameters:
      url - referral url
    • intermediate

      public void intermediate(IntermediateResponse r)
      Parameters:
      r - intermediate response
    • unsolicitedNotification

      public void unsolicitedNotification(UnsolicitedNotification u)
      Parameters:
      u - unsolicited notification
    • processHandlerException

      protected void processHandlerException(Exception e)
      Invokes notifyExceptionHandlers(LdapException) by wrapping the supplied exception in LdapException. If the supplied exception is an instance of AbandonOperationException, abandon(LdapException) is invoked.
      Parameters:
      e - to process
    • notifyExceptionHandlers

      protected void notifyExceptionHandlers(LdapException e)
      Invokes any configured exception handlers with the supplied exception.
      Parameters:
      e - exception
    • exception

      public void exception(LdapException e)
      Parameters:
      e - exception
    • consumedMessage

      protected void consumedMessage()
      Indicates that a protocol message was consumed by a supplied consumer.
    • consumedMessage

      protected void consumedMessage(Message message)
      Indicates that a protocol message was consumed by a supplied consumer.
      Parameters:
      message - that was consumed
    • consumedMessage

      private void consumedMessage(boolean signalResponseSemaphore)
      Indicates that a protocol message was consumed by a supplied consumer.
      Parameters:
      signalResponseSemaphore - whether to signal the response semaphore
    • complete

      private void complete()
      Releases the latch and sets the response as received. Invokes onComplete. Handle is considered done when this is invoked.
    • processReferralResult

      protected S processReferralResult(S original, ReferralResultHandler<S> handler) throws LdapException
      Invokes the supplied referral result handler and returns the result of that handler.
      Parameters:
      original - operation result
      handler - to invoke
      Returns:
      result returned by the handler
      Throws:
      LdapException - if the handler throws an LdapException
      IllegalArgumentException - if original is not a referral
      IllegalStateException - if the handler does not return a valid result
    • toString

      public String toString()
      Overrides:
      toString in class Object