Package org.ldaptive.transport
Class DefaultOperationHandle<Q extends Request,S extends Result>
- java.lang.Object
-
- org.ldaptive.transport.DefaultOperationHandle<Q,S>
-
- Type Parameters:
Q- type of requestS- 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 Summary
Fields Modifier and Type Field Description private InstantabandonedTimeTimestamp when the request was abandoned.private TransportConnectionconnectionConnection to send the request on.private booleanconsumedMessageWhether this handle has consumed any messages.private InstantcreationTimeTimestamp when the handle was created.private static Predicate<Message>DEFAULT_RESPONSE_TIMEOUT_CONDITIONPredicate that requires any result message except unsolicited.private LdapExceptionexceptionException encountered attempting to process the request.protected LoggerloggerLogger for this class.private IntegermessageIDProtocol message ID.private CompleteHandleronCompleteFunction to run when the operation completes.private ResponseControlHandler[]onControlFunctions to handle response controls.private ExceptionHandleronExceptionFunction to handle exceptions.private IntermediateResponseHandler[]onIntermediateFunctions to handle intermediate responses.private ReferralHandler[]onReferralFunctions to handle referral URLs.private ReferralResultHandler<S>onReferralResultFunction to chase referrals.private ResultHandler[]onResultFunctions to handle response results.private UnsolicitedNotificationHandler[]onUnsolicitedNotificationFunction to handle unsolicited notifications.private InstantreceivedTimeTimestamp when the result was received or an exception occurred.private QrequestProtocol request to send.private SemaphoreresponseSemaphoreSemaphore to determine when a response has been received.private DurationresponseTimeoutTime to wait for a response.private SresultProtocol response result.private InstantsentTimeTimestamp when the request was sent.private ResultPredicatethrowConditionFunction to run when a result is received to determine whether an exception should be raised.
-
Constructor Summary
Constructors Constructor Description DefaultOperationHandle(Q req, TransportConnection conn, Duration timeout)Creates a new operation handle.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidabandon()Abandons this operation.voidabandon(LdapException cause)Abandons this operation.Sawait()Waits for a result or reports a timeout exception.ExtendedOperationHandlecancel()Cancels this operation.private voidcomplete()Releases the latch and sets the response as received.protected voidconsumedMessage()Indicates that a protocol message was consumed by a supplied consumer.private voidconsumedMessage(boolean signalResponseSemaphore)Indicates that a protocol message was consumed by a supplied consumer.protected voidconsumedMessage(Message message)Indicates that a protocol message was consumed by a supplied consumer.voidcontrol(ResponseControl c)InvokesonControl.protected voidevaluateThrowCondition(Result r)InvokesthrowCondition, if it exists, for the supplied result.voidexception(LdapException e)InvokesnotifyExceptionHandlers(LdapException)followed bycomplete().protected voidfinalizeResult(S r)Setsresult, notifies that message has been consumed and invokescomplete().InstantgetAbandonedTime()Returns the time this operation was abandoned.IntegergetMessageID()Returns the message ID assigned to this handle.CompleteHandlergetOnComplete()ResponseControlHandler[]getOnControl()ExceptionHandlergetOnException()IntermediateResponseHandler[]getOnIntermediate()ReferralHandler[]getOnReferral()ReferralResultHandler<S>getOnReferralResult()ResultHandler[]getOnResult()UnsolicitedNotificationHandler[]getOnUnsolicitedNotification()InstantgetReceivedTime()Returns the time this operation received a result or encountered an exception.QgetRequest()Returns the request.protected Predicate<Message>getResponseTimeoutCondition()Returns a predicate to determine whether the responseTimeout semaphore should be released.InstantgetSentTime()Returns the time this operation sent a request.ResultPredicategetThrowCondition()booleanhasConsumedMessage()Returns whether this handle has consumed any messages.protected <T> TinitializeMessageFunctional(T function)Creates a newMessageFunctionalif function is of that type and initializes it.protected <T> T[]initializeMessageFunctional(T... functions)Iterates over the supplied functions and creates a new, initializedMessageFunctionalif any exists.voidintermediate(IntermediateResponse r)InvokesonIntermediate.voidmessageID(int id)Sets the message ID.protected voidnotifyExceptionHandlers(LdapException e)Invokes any configured exception handlers with the supplied exception.DefaultOperationHandle<Q,S>onComplete(CompleteHandler function)Sets the function to execute when the operation completes.DefaultOperationHandle<Q,S>onControl(ResponseControlHandler... function)Sets the functions to execute when a control is received.DefaultOperationHandle<Q,S>onException(ExceptionHandler function)Sets the function to execute when an exception occurs.DefaultOperationHandle<Q,S>onIntermediate(IntermediateResponseHandler... function)Sets the functions to execute when an intermediate response is received.DefaultOperationHandle<Q,S>onReferral(ReferralHandler... function)Sets the functions to execute when a referral is received.DefaultOperationHandle<Q,S>onReferralResult(ReferralResultHandler<S> function)Sets the functions to execute when a response is complete and contains a referral result code.DefaultOperationHandle<Q,S>onResult(ResultHandler... function)Sets the functions to execute when a result is received.DefaultOperationHandle<Q,S>onUnsolicitedNotification(UnsolicitedNotificationHandler... function)Sets the functions to execute when an unsolicited notification is received.protected voidprocessHandlerException(Exception e)InvokesnotifyExceptionHandlers(LdapException)by wrapping the supplied exception inLdapException.protected SprocessReferralResult(S original, ReferralResultHandler<S> handler)Invokes the supplied referral result handler and returns the result of that handler.protected voidprocessResult(S r)Perform checks on the supplied result and invoke any configuredonResulthandlers.voidreferral(String... url)InvokesonReferral.voidresult(S r)InvokesonResultand sets the result.DefaultOperationHandle<Q,S>send()Sends this request to the server.voidsent()Sets the sent time to now.private booleansupports(Result r)Whether the supplied result belongs to this handle.DefaultOperationHandle<Q,S>throwIf(ResultPredicate function)Sets the function to determine whether an exception should be raised by a particular result.StringtoString()voidunsolicitedNotification(UnsolicitedNotification u)InvokesonUnsolicitedNotification.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.ldaptive.OperationHandle
execute
-
-
-
-
Field Detail
-
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.
-
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. SeeTransportConnection.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.
-
exception
private LdapException exception
Exception encountered attempting to process the request.
-
-
Constructor Detail
-
DefaultOperationHandle
public DefaultOperationHandle(Q req, TransportConnection conn, Duration timeout)
Creates a new operation handle.- Parameters:
req- request to expect a response forconn- the request will be executed ontimeout- duration to wait for a response
-
-
Method Detail
-
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:OperationHandleSends this request to the server.
-
await
public S await() throws LdapException
Description copied from interface:OperationHandleWaits for a result or reports a timeout exception.- Specified by:
awaitin interfaceOperationHandle<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
InvokesthrowCondition, 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:OperationHandleSets the functions to execute when a result is received.
-
onControl
public DefaultOperationHandle<Q,S> onControl(ResponseControlHandler... function)
Description copied from interface:OperationHandleSets the functions to execute when a control is received.
-
onReferral
public DefaultOperationHandle<Q,S> onReferral(ReferralHandler... function)
Description copied from interface:OperationHandleSets the functions to execute when a referral is received.- Specified by:
onReferralin interfaceOperationHandle<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:OperationHandleSets the functions to execute when an intermediate response is received.- Specified by:
onIntermediatein interfaceOperationHandle<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:OperationHandleSets the functions to execute when an unsolicited notification is received.- Specified by:
onUnsolicitedNotificationin interfaceOperationHandle<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:OperationHandleSets the functions to execute when a response is complete and contains a referral result code.- Specified by:
onReferralResultin interfaceOperationHandle<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:OperationHandleSets the function to execute when an exception occurs.- Specified by:
onExceptionin interfaceOperationHandle<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:OperationHandleSets the function to execute when the operation completes.- Specified by:
onCompletein interfaceOperationHandle<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:OperationHandleSets the function to determine whether an exception should be raised by a particular result.
-
initializeMessageFunctional
protected final <T> T[] initializeMessageFunctional(T... functions)
Iterates over the supplied functions and creates a new, initializedMessageFunctionalif any exists.- Type Parameters:
T- type of functions- Parameters:
functions- to initialize- Returns:
- array of functions with any
MessageFunctionalinitialized
-
initializeMessageFunctional
protected final <T> T initializeMessageFunctional(T function)
Creates a newMessageFunctionalif 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:OperationHandleAbandons this operation.
-
abandon
public void abandon(LdapException cause)
Abandons this operation. Any threads waiting on the result will receive an empty result. SeeConnection.operation(AbandonRequest).- Parameters:
cause- the reason this request was abandoned
-
cancel
public ExtendedOperationHandle cancel()
Description copied from interface:OperationHandleCancels this operation. SeeCancelRequest.
-
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:OperationHandleReturns the time this operation sent a request.- Specified by:
getSentTimein interfaceOperationHandle<Q extends Request,S extends Result>- Returns:
- sent time
-
getReceivedTime
public Instant getReceivedTime()
Description copied from interface:OperationHandleReturns the time this operation received a result or encountered an exception.- Specified by:
getReceivedTimein interfaceOperationHandle<Q extends Request,S extends Result>- Returns:
- received time
-
getAbandonedTime
public Instant getAbandonedTime()
Description copied from interface:OperationHandleReturns the time this operation was abandoned.- Specified by:
getAbandonedTimein interfaceOperationHandle<Q extends Request,S extends Result>- Returns:
- abandoned time
-
getOnResult
public ResultHandler[] getOnResult()
-
getOnControl
public ResponseControlHandler[] getOnControl()
-
getOnReferral
public ReferralHandler[] getOnReferral()
-
getOnIntermediate
public IntermediateResponseHandler[] getOnIntermediate()
-
getOnException
public ExceptionHandler getOnException()
-
getOnComplete
public CompleteHandler getOnComplete()
-
getThrowCondition
public ResultPredicate getThrowCondition()
-
getOnUnsolicitedNotification
public UnsolicitedNotificationHandler[] getOnUnsolicitedNotification()
-
getOnReferralResult
public ReferralResultHandler<S> getOnReferralResult()
-
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)
InvokesonResultand 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 configuredonResulthandlers.- Parameters:
r- to process
-
finalizeResult
protected void finalizeResult(S r)
Setsresult, notifies that message has been consumed and invokescomplete().- Parameters:
r- to finalize
-
control
public void control(ResponseControl c)
InvokesonControl.- Parameters:
c- response control
-
referral
public void referral(String... url)
InvokesonReferral.- Parameters:
url- referral url
-
intermediate
public void intermediate(IntermediateResponse r)
InvokesonIntermediate.- Parameters:
r- intermediate response
-
unsolicitedNotification
public void unsolicitedNotification(UnsolicitedNotification u)
InvokesonUnsolicitedNotification.- Parameters:
u- unsolicited notification
-
processHandlerException
protected void processHandlerException(Exception e)
InvokesnotifyExceptionHandlers(LdapException)by wrapping the supplied exception inLdapException. If the supplied exception is an instance ofAbandonOperationException,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)
InvokesnotifyExceptionHandlers(LdapException)followed bycomplete().- 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. InvokesonComplete. 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 resulthandler- to invoke- Returns:
- result returned by the handler
- Throws:
LdapException- if the handler throws an LdapExceptionIllegalArgumentException- if original is not a referralIllegalStateException- if the handler does not return a valid result
-
-