use of org.apache.directory.ldap.client.api.future.DeleteFuture in project directory-ldap-api by apache.
the class LdapNetworkConnection method deleteAsync.
/**
* {@inheritDoc}
*/
@Override
public DeleteFuture deleteAsync(DeleteRequest deleteRequest) throws LdapException {
if (deleteRequest == null) {
String msg = "Cannot process a null deleteRequest";
LOG.debug(msg);
throw new IllegalArgumentException(msg);
}
if (deleteRequest.getName() == null) {
String msg = "Cannot process a deleteRequest which DN is null";
LOG.debug(msg);
throw new IllegalArgumentException(msg);
}
// try to connect, if we aren't already connected.
connect();
checkSession();
int newId = messageId.incrementAndGet();
deleteRequest.setMessageId(newId);
DeleteFuture deleteFuture = new DeleteFuture(this, newId);
addToFutureMap(newId, deleteFuture);
// Send the request to the server
writeRequest(deleteRequest);
// Ok, done return the future
return deleteFuture;
}
use of org.apache.directory.ldap.client.api.future.DeleteFuture in project directory-ldap-api by apache.
the class LdapNetworkConnection method delete.
/**
* {@inheritDoc}
*/
@Override
public DeleteResponse delete(DeleteRequest deleteRequest) throws LdapException {
if (deleteRequest == null) {
String msg = "Cannot process a null deleteRequest";
LOG.debug(msg);
throw new IllegalArgumentException(msg);
}
DeleteFuture deleteFuture = deleteAsync(deleteRequest);
// Get the result from the future
try {
// Read the response, waiting for it if not available immediately
// Get the response, blocking
DeleteResponse delResponse = deleteFuture.get(timeout, TimeUnit.MILLISECONDS);
if (delResponse == null) {
// We didn't received anything : this is an error
if (LOG.isErrorEnabled()) {
LOG.error(I18n.err(I18n.ERR_03203_OP_FAILED_TIMEOUT, "Delete"));
}
throw new LdapException(TIME_OUT_ERROR);
}
if (delResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS) {
// Everything is fine, return the response
if (LOG.isDebugEnabled()) {
LOG.debug(I18n.msg(I18n.MSG_03217_DELETE_SUCCESSFUL, delResponse));
}
} else {
// We have had an error
if (LOG.isDebugEnabled()) {
LOG.debug(I18n.msg(I18n.MSG_03216_DELETE_FAILED, delResponse));
}
}
return delResponse;
} catch (Exception ie) {
// Catch all other exceptions
LOG.error(NO_RESPONSE_ERROR, ie);
// Send an abandon request
if (!deleteFuture.isCancelled()) {
abandon(deleteRequest.getMessageId());
}
throw new LdapException(NO_RESPONSE_ERROR, ie);
}
}
use of org.apache.directory.ldap.client.api.future.DeleteFuture in project directory-ldap-api by apache.
the class LdapNetworkConnection method messageReceived.
/**
* Handle the incoming LDAP messages. This is where we feed the cursor for search
* requests, or call the listener.
*
* @param session The session that received a message
* @param message The received message
* @throws Exception If there is some error while processing the message
*/
@Override
public void messageReceived(IoSession session, Object message) throws Exception {
// Feed the response and store it into the session
if (message instanceof SslFilter.SslFilterMessage) {
// This is a SSL message telling if the session has been secured or not
HandshakeFuture handshakeFuture = (HandshakeFuture) ldapSession.getAttribute("HANDSHAKE_FUTURE");
if (message == SslFilter.SESSION_SECURED) {
// SECURED
handshakeFuture.secured();
} else {
// UNSECURED
handshakeFuture.cancel();
}
ldapSession.removeAttribute("HANDSHAKE_FUTURE");
return;
}
Message response = (Message) message;
if (LOG.isDebugEnabled()) {
LOG.debug(I18n.msg(I18n.MSG_03243_MESSAGE_RECEIVED, response));
}
int messageId = response.getMessageId();
// this check is necessary to prevent adding an abandoned operation's
// result(s) to corresponding queue
ResponseFuture<? extends Response> responseFuture = peekFromFutureMap(messageId);
boolean isNoD = isNoticeOfDisconnect(response);
if ((responseFuture == null) && !isNoD) {
LOG.info("There is no future associated with the messageId {}, ignoring the message", messageId);
return;
}
if (isNoD) {
// close the session
session.closeNow();
return;
}
switch(response.getType()) {
case ADD_RESPONSE:
// Transform the response
AddResponse addResponse = (AddResponse) response;
AddFuture addFuture = (AddFuture) responseFuture;
// remove the listener from the listener map
if (LOG.isDebugEnabled()) {
if (addResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS) {
// Everything is fine, return the response
LOG.debug(I18n.msg(I18n.MSG_03209_ADD_SUCCESSFUL, addResponse));
} else {
// We have had an error
LOG.debug(I18n.msg(I18n.MSG_03208_ADD_FAILED, addResponse));
}
}
// Store the response into the future
addFuture.set(addResponse);
// Remove the future from the map
removeFromFutureMaps(messageId);
break;
case BIND_RESPONSE:
// Transform the response
BindResponse bindResponse = (BindResponse) response;
BindFuture bindFuture = (BindFuture) responseFuture;
// remove the listener from the listener map
if (bindResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS) {
authenticated.set(true);
// Everything is fine, return the response
if (LOG.isDebugEnabled()) {
LOG.debug(I18n.msg(I18n.MSG_03202_BIND_SUCCESSFUL, bindResponse));
}
} else {
// We have had an error
if (LOG.isDebugEnabled()) {
LOG.debug(I18n.msg(I18n.MSG_03201_BIND_FAIL, bindResponse));
}
}
// Store the response into the future
bindFuture.set(bindResponse);
// Remove the future from the map
removeFromFutureMaps(messageId);
break;
case COMPARE_RESPONSE:
// Transform the response
CompareResponse compareResponse = (CompareResponse) response;
CompareFuture compareFuture = (CompareFuture) responseFuture;
// remove the listener from the listener map
if (LOG.isDebugEnabled()) {
if (compareResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS) {
// Everything is fine, return the response
LOG.debug(I18n.msg(I18n.MSG_03215_COMPARE_SUCCESSFUL, compareResponse));
} else {
// We have had an error
LOG.debug(I18n.msg(I18n.MSG_03214_COMPARE_FAILED, compareResponse));
}
}
// Store the response into the future
compareFuture.set(compareResponse);
// Remove the future from the map
removeFromFutureMaps(messageId);
break;
case DEL_RESPONSE:
// Transform the response
DeleteResponse deleteResponse = (DeleteResponse) response;
DeleteFuture deleteFuture = (DeleteFuture) responseFuture;
if (LOG.isDebugEnabled()) {
if (deleteResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS) {
// Everything is fine, return the response
LOG.debug(I18n.msg(I18n.MSG_03217_DELETE_SUCCESSFUL, deleteResponse));
} else {
// We have had an error
LOG.debug(I18n.msg(I18n.MSG_03216_DELETE_FAILED, deleteResponse));
}
}
// Store the response into the future
deleteFuture.set(deleteResponse);
// Remove the future from the map
removeFromFutureMaps(messageId);
break;
case EXTENDED_RESPONSE:
// Transform the response
ExtendedResponse extendedResponse = (ExtendedResponse) response;
ExtendedFuture extendedFuture = (ExtendedFuture) responseFuture;
// remove the listener from the listener map
if (LOG.isDebugEnabled()) {
if (extendedResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS) {
// Everything is fine, return the response
LOG.debug(I18n.msg(I18n.MSG_03219_EXTENDED_SUCCESSFUL, extendedResponse));
} else {
// We have had an error
LOG.debug(I18n.msg(I18n.MSG_03218_EXTENDED_FAILED, extendedResponse));
}
}
// Store the response into the future
extendedFuture.set(extendedResponse);
// Remove the future from the map
removeFromFutureMaps(messageId);
break;
case INTERMEDIATE_RESPONSE:
IntermediateResponse intermediateResponse;
if (responseFuture instanceof SearchFuture) {
intermediateResponse = new IntermediateResponseImpl(messageId);
addControls(intermediateResponse, response);
((SearchFuture) responseFuture).set(intermediateResponse);
} else if (responseFuture instanceof ExtendedFuture) {
intermediateResponse = new IntermediateResponseImpl(messageId);
addControls(intermediateResponse, response);
((ExtendedFuture) responseFuture).set(intermediateResponse);
} else {
// currently we only support IR for search and extended operations
throw new UnsupportedOperationException("Unknown ResponseFuture type " + responseFuture.getClass().getName());
}
intermediateResponse.setResponseName(((IntermediateResponse) response).getResponseName());
intermediateResponse.setResponseValue(((IntermediateResponse) response).getResponseValue());
break;
case MODIFY_RESPONSE:
// Transform the response
ModifyResponse modifyResponse = (ModifyResponse) response;
ModifyFuture modifyFuture = (ModifyFuture) responseFuture;
if (LOG.isDebugEnabled()) {
if (modifyResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS) {
// Everything is fine, return the response
if (LOG.isDebugEnabled()) {
LOG.debug(I18n.msg(I18n.MSG_03224_MODIFY_SUCCESSFUL, modifyResponse));
}
} else {
// We have had an error
if (LOG.isDebugEnabled()) {
LOG.debug(I18n.msg(I18n.MSG_03223_MODIFY_FAILED, modifyResponse));
}
}
}
// Store the response into the future
modifyFuture.set(modifyResponse);
// Remove the future from the map
removeFromFutureMaps(messageId);
break;
case MODIFYDN_RESPONSE:
// Transform the response
ModifyDnResponse modifyDnResponse = (ModifyDnResponse) response;
ModifyDnFuture modifyDnFuture = (ModifyDnFuture) responseFuture;
if (LOG.isDebugEnabled()) {
if (modifyDnResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS) {
// Everything is fine, return the response
LOG.debug(I18n.msg(I18n.MSG_03226_MODIFYDN_SUCCESSFUL, modifyDnResponse));
} else {
// We have had an error
LOG.debug(I18n.msg(I18n.MSG_03225_MODIFYDN_FAILED, modifyDnResponse));
}
}
// Store the response into the future
modifyDnFuture.set(modifyDnResponse);
// Remove the future from the map
removeFromFutureMaps(messageId);
break;
case SEARCH_RESULT_DONE:
// Store the response into the responseQueue
SearchResultDone searchResultDone = (SearchResultDone) response;
SearchFuture searchFuture = (SearchFuture) responseFuture;
if (LOG.isDebugEnabled()) {
if (searchResultDone.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS) {
// Everything is fine, return the response
LOG.debug(I18n.msg(I18n.MSG_03232_SEARCH_SUCCESSFUL, searchResultDone));
} else {
// We have had an error
LOG.debug(I18n.msg(I18n.MSG_03230_SEARCH_FAILED, searchResultDone));
}
}
// Store the response into the future
searchFuture.set(searchResultDone);
// Remove the future from the map
removeFromFutureMaps(messageId);
break;
case SEARCH_RESULT_ENTRY:
// Store the response into the responseQueue
SearchResultEntry searchResultEntry = (SearchResultEntry) response;
if (schemaManager != null) {
searchResultEntry.setEntry(new DefaultEntry(schemaManager, searchResultEntry.getEntry()));
}
searchFuture = (SearchFuture) responseFuture;
if (LOG.isDebugEnabled()) {
LOG.debug(I18n.msg(I18n.MSG_03229_SEARCH_ENTRY_FOUND, searchResultEntry));
}
// Store the response into the future
searchFuture.set(searchResultEntry);
break;
case SEARCH_RESULT_REFERENCE:
// Store the response into the responseQueue
SearchResultReference searchResultReference = (SearchResultReference) response;
searchFuture = (SearchFuture) responseFuture;
if (LOG.isDebugEnabled()) {
LOG.debug(I18n.msg(I18n.MSG_03231_SEARCH_REFERENCE_FOUND, searchResultReference));
}
// Store the response into the future
searchFuture.set(searchResultReference);
break;
default:
throw new IllegalStateException("Unexpected response type " + response.getType());
}
}
use of org.apache.directory.ldap.client.api.future.DeleteFuture in project directory-ldap-api by apache.
the class LdapNetworkConnection method connect.
// -------------------------- The methods ---------------------------//
/**
* {@inheritDoc}
*/
@Override
public boolean connect() throws LdapException {
if ((ldapSession != null) && connected.get()) {
// No need to connect if we already have a connected session
return true;
}
// Create the connector if needed
if (connector == null) {
createConnector();
}
// Build the connection address
SocketAddress address = new InetSocketAddress(config.getLdapHost(), config.getLdapPort());
// And create the connection future
timeout = config.getTimeout();
long maxRetry = System.currentTimeMillis() + timeout;
ConnectFuture connectionFuture = null;
boolean interrupted = false;
while (maxRetry > System.currentTimeMillis() && !interrupted) {
connectionFuture = connector.connect(address);
boolean result = false;
// Wait until it's established
try {
result = connectionFuture.await(timeout);
} catch (InterruptedException e) {
connector.dispose();
connector = null;
LOG.debug(I18n.msg(I18n.MSG_03221_INTERRUPTED_WAITING_FOR_CONNECTION, config.getLdapHost(), config.getLdapPort()), e);
interrupted = true;
throw new LdapOtherException(e.getMessage(), e);
} finally {
if (result) {
boolean isConnected = connectionFuture.isConnected();
if (!isConnected) {
Throwable connectionException = connectionFuture.getException();
if ((connectionException instanceof ConnectException) || (connectionException instanceof UnresolvedAddressException)) {
// We know that there was a permanent error such as "connection refused".
if (LOG.isDebugEnabled()) {
LOG.debug(I18n.msg(I18n.MSG_03245_CONNECTION_ERROR, connectionFuture.getException().getMessage()));
}
}
if (LOG.isDebugEnabled()) {
LOG.debug(I18n.msg(I18n.MSG_03244_CONNECTION_RETRYING));
}
// Wait 500 ms and retry
try {
Thread.sleep(500);
} catch (InterruptedException e) {
connector = null;
LOG.debug(I18n.msg(I18n.MSG_03221_INTERRUPTED_WAITING_FOR_CONNECTION, config.getLdapHost(), config.getLdapPort()), e);
interrupted = true;
throw new LdapOtherException(e.getMessage(), e);
}
} else {
break;
}
}
}
}
if (connectionFuture == null) {
connector.dispose();
throw new InvalidConnectionException("Cannot connect");
}
boolean isConnected = connectionFuture.isConnected();
if (!isConnected) {
// disposing connector if not connected
try {
close();
} catch (IOException ioe) {
// Nothing to do
}
Throwable e = connectionFuture.getException();
if (e != null) {
StringBuilder message = new StringBuilder("Cannot connect to the server: ");
// (most of the time no message is associated with this exception)
if ((e instanceof UnresolvedAddressException) && (e.getMessage() == null)) {
message.append("Hostname '");
message.append(config.getLdapHost());
message.append("' could not be resolved.");
throw new InvalidConnectionException(message.toString(), e);
}
// Default case
message.append(e.getMessage());
throw new InvalidConnectionException(message.toString(), e);
}
return false;
}
// Get the close future for this session
CloseFuture closeFuture = connectionFuture.getSession().getCloseFuture();
// Add a listener to close the session in the session.
closeFuture.addListener(new IoFutureListener<IoFuture>() {
@Override
public void operationComplete(IoFuture future) {
// Process all the waiting operations and cancel them
LOG.debug(I18n.msg(I18n.MSG_03238_NOD_RECEIVED));
for (ResponseFuture<?> responseFuture : futureMap.values()) {
LOG.debug(I18n.msg(I18n.MSG_03235_CLOSING, responseFuture));
responseFuture.cancel();
try {
if (responseFuture instanceof AddFuture) {
((AddFuture) responseFuture).set(AddNoDResponse.PROTOCOLERROR);
} else if (responseFuture instanceof BindFuture) {
((BindFuture) responseFuture).set(BindNoDResponse.PROTOCOLERROR);
} else if (responseFuture instanceof CompareFuture) {
((CompareFuture) responseFuture).set(CompareNoDResponse.PROTOCOLERROR);
} else if (responseFuture instanceof DeleteFuture) {
((DeleteFuture) responseFuture).set(DeleteNoDResponse.PROTOCOLERROR);
} else if (responseFuture instanceof ExtendedFuture) {
((ExtendedFuture) responseFuture).set(ExtendedNoDResponse.PROTOCOLERROR);
} else if (responseFuture instanceof ModifyFuture) {
((ModifyFuture) responseFuture).set(ModifyNoDResponse.PROTOCOLERROR);
} else if (responseFuture instanceof ModifyDnFuture) {
((ModifyDnFuture) responseFuture).set(ModifyDnNoDResponse.PROTOCOLERROR);
} else if (responseFuture instanceof SearchFuture) {
((SearchFuture) responseFuture).set(SearchNoDResponse.PROTOCOLERROR);
}
} catch (InterruptedException e) {
LOG.error(I18n.err(I18n.ERR_03202_ERROR_PROCESSING_NOD, responseFuture), e);
}
futureMap.remove(messageId.get());
}
futureMap.clear();
}
});
// Get back the session
ldapSession = connectionFuture.getSession();
connected.set(true);
// Store the container into the session if we don't have one
@SuppressWarnings("unchecked") LdapMessageContainer<MessageDecorator<? extends Message>> container = (LdapMessageContainer<MessageDecorator<? extends Message>>) ldapSession.getAttribute(LdapDecoder.MESSAGE_CONTAINER_ATTR);
if (container != null) {
if ((schemaManager != null) && !(container.getBinaryAttributeDetector() instanceof SchemaBinaryAttributeDetector)) {
container.setBinaryAttributeDetector(new SchemaBinaryAttributeDetector(schemaManager));
}
} else {
BinaryAttributeDetector atDetector = new DefaultConfigurableBinaryAttributeDetector();
if (schemaManager != null) {
atDetector = new SchemaBinaryAttributeDetector(schemaManager);
}
ldapSession.setAttribute(LdapDecoder.MESSAGE_CONTAINER_ATTR, new LdapMessageContainer<MessageDecorator<? extends Message>>(codec, atDetector));
}
// Initialize the MessageId
messageId.set(0);
// And return
return true;
}
Aggregations