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 TransportConnection
connection
Connection to send the request on.private boolean
consumedMessage
Whether this handle has consumed any messages.private Instant
creationTime
Timestamp when the handle was created.private static Predicate<Message>
DEFAULT_RESPONSE_TIMEOUT_CONDITION
Predicate that requires any result message except unsolicited.private LdapException
exception
Exception encountered attempting to process the request.protected Logger
logger
Logger for this class.private Integer
messageID
Protocol message ID.private CompleteHandler
onComplete
Function to run when the operation completes.private ResponseControlHandler[]
onControl
Functions to handle response controls.private ExceptionHandler
onException
Function to handle exceptions.private IntermediateResponseHandler[]
onIntermediate
Functions to handle intermediate responses.private ReferralHandler[]
onReferral
Functions to handle referral URLs.private ReferralResultHandler<S>
onReferralResult
Function to chase referrals.private ResultHandler[]
onResult
Functions to handle response results.private UnsolicitedNotificationHandler[]
onUnsolicitedNotification
Function to handle unsolicited notifications.private Instant
receivedTime
Timestamp when the result was received or an exception occurred.private Q
request
Protocol request to send.private Semaphore
responseSemaphore
Semaphore to determine when a response has been received.private Duration
responseTimeout
Time to wait for a response.private S
result
Protocol response result.private Instant
sentTime
Timestamp when the request was sent.private ResultPredicate
throwCondition
Function 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 void
abandon()
Abandons this operation.void
abandon(LdapException cause)
Abandons this operation.S
await()
Waits for a result or reports a timeout exception.ExtendedOperationHandle
cancel()
Cancels this operation.private void
complete()
Releases the latch and sets the response as received.protected void
consumedMessage()
Indicates that a protocol message was consumed by a supplied consumer.private void
consumedMessage(boolean signalResponseSemaphore)
Indicates that a protocol message was consumed by a supplied consumer.protected void
consumedMessage(Message message)
Indicates that a protocol message was consumed by a supplied consumer.void
control(ResponseControl c)
InvokesonControl
.protected void
evaluateThrowCondition(Result r)
InvokesthrowCondition
, if it exists, for the supplied result.void
exception(LdapException e)
InvokesonException
followed bycomplete()
.protected void
finalizeResult(S r)
Setsresult
, notifies that message has been consumed and invokescomplete()
.Integer
getMessageID()
Returns the message ID assigned to this handle.CompleteHandler
getOnComplete()
ResponseControlHandler[]
getOnControl()
ExceptionHandler
getOnException()
IntermediateResponseHandler[]
getOnIntermediate()
ReferralHandler[]
getOnReferral()
ReferralResultHandler<S>
getOnReferralResult()
ResultHandler[]
getOnResult()
UnsolicitedNotificationHandler[]
getOnUnsolicitedNotification()
Instant
getReceivedTime()
Returns the time this operation received a result or encountered an exception.Q
getRequest()
Returns the request.protected Predicate<Message>
getResponseTimeoutCondition()
Returns a predicate to determine whether the responseTimeout semaphore should be released.Instant
getSentTime()
Returns the time this operation sent a request.ResultPredicate
getThrowCondition()
boolean
hasConsumedMessage()
Returns whether this handle has consumed any messages.protected <T> T
initializeMessageFunctional(T function)
Creates a newMessageFunctional
if function is of that type and initializes it.protected <T> T[]
initializeMessageFunctional(T... functions)
Iterates over the supplied functions and creates a new, initializedMessageFunctional
if any exists.void
intermediate(IntermediateResponse r)
InvokesonIntermediate
.void
messageID(int id)
Sets the message ID.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 S
processReferralResult(S original, ReferralResultHandler<S> handler)
Invokes the supplied referral result handler and returns the result of that handler.protected void
processResult(S r)
Perform checks on the supplied result and invoke any configuredonResult
handlers.void
referral(String... url)
InvokesonReferral
.void
result(S r)
InvokesonResult
and sets the result.DefaultOperationHandle<Q,S>
send()
Sends this request to the server.void
sent()
Sets the sent time to now.private boolean
supports(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.String
toString()
void
unsolicitedNotification(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.
-
consumedMessage
private 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:OperationHandle
Sends this request to the server.
-
await
public S await() throws LdapException
Description copied from interface:OperationHandle
Waits for a result or reports a timeout exception.- Specified by:
await
in 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:OperationHandle
Sets the functions to execute when a result is received.
-
onControl
public DefaultOperationHandle<Q,S> onControl(ResponseControlHandler... function)
Description copied from interface:OperationHandle
Sets the functions to execute when a control is received.
-
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 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:OperationHandle
Sets the functions to execute when an intermediate response is received.- Specified by:
onIntermediate
in 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:OperationHandle
Sets the functions to execute when an unsolicited notification is received.- Specified by:
onUnsolicitedNotification
in 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:OperationHandle
Sets the functions to execute when a response is complete and contains a referral result code.- Specified by:
onReferralResult
in 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:OperationHandle
Sets the function to execute when an exception occurs.- Specified by:
onException
in 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:OperationHandle
Sets the function to execute when the operation completes.- Specified by:
onComplete
in 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:OperationHandle
Sets 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, initializedMessageFunctional
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 newMessageFunctional
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.
-
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:OperationHandle
Cancels 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:OperationHandle
Returns the time this operation sent a request.- Specified by:
getSentTime
in interfaceOperationHandle<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 interfaceOperationHandle<Q extends Request,S extends Result>
- Returns:
- received 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)
InvokesonResult
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 configuredonResult
handlers.- 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
-
exception
public void exception(LdapException e)
InvokesonException
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
-
-