Search in sources :

Example 6 with BindFuture

use of org.apache.directory.ldap.client.api.future.BindFuture in project directory-ldap-api by apache.

the class LdapNetworkConnection method bind.

/**
 * {@inheritDoc}
 */
@Override
public BindResponse bind(BindRequest bindRequest) throws LdapException {
    if (bindRequest == null) {
        String msg = "Cannot process a null bindRequest";
        LOG.debug(msg);
        throw new IllegalArgumentException(msg);
    }
    BindFuture bindFuture = bindAsync(bindRequest);
    // Get the result from the future
    try {
        // Read the response, waiting for it if not available immediately
        // Get the response, blocking
        BindResponse bindResponse = bindFuture.get(timeout, TimeUnit.MILLISECONDS);
        if (bindResponse == null) {
            // We didn't received anything : this is an error
            if (LOG.isErrorEnabled()) {
                LOG.error(I18n.err(I18n.ERR_03203_OP_FAILED_TIMEOUT, "Bind"));
            }
            throw new LdapException(TIME_OUT_ERROR);
        }
        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));
            }
        }
        return bindResponse;
    } catch (Exception ie) {
        // Catch all other exceptions
        LOG.error(NO_RESPONSE_ERROR, ie);
        throw new LdapException(NO_RESPONSE_ERROR, ie);
    }
}
Also used : BindFuture(org.apache.directory.ldap.client.api.future.BindFuture) BindResponse(org.apache.directory.api.ldap.model.message.BindResponse) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException) InvalidConnectionException(org.apache.directory.ldap.client.api.exception.InvalidConnectionException) LdapOperationException(org.apache.directory.api.ldap.model.exception.LdapOperationException) LdapAuthenticationException(org.apache.directory.api.ldap.model.exception.LdapAuthenticationException) MessageEncoderException(org.apache.directory.api.ldap.codec.api.MessageEncoderException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) DecoderException(org.apache.directory.api.asn1.DecoderException) LdapNoPermissionException(org.apache.directory.api.ldap.model.exception.LdapNoPermissionException) LdapOtherException(org.apache.directory.api.ldap.model.exception.LdapOtherException) ProtocolEncoderException(org.apache.mina.filter.codec.ProtocolEncoderException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 7 with BindFuture

use of org.apache.directory.ldap.client.api.future.BindFuture in project directory-ldap-api by apache.

the class LdapNetworkConnection method bind.

/**
 * Bind to the server using a DigestMd5Request object.
 *
 * @param request The DigestMd5Request POJO containing all the needed parameters
 * @return A LdapResponse containing the result
 * @throws LdapException if some error occurred
 */
public BindResponse bind(SaslDigestMd5Request request) throws LdapException {
    if (request == null) {
        String msg = I18n.msg(I18n.MSG_03204_NULL_REQUEST);
        LOG.debug(msg);
        throw new IllegalArgumentException(msg);
    }
    BindFuture bindFuture = bindAsync(request);
    // Get the result from the future
    try {
        // Read the response, waiting for it if not available immediately
        // Get the response, blocking
        BindResponse bindResponse = bindFuture.get(timeout, TimeUnit.MILLISECONDS);
        if (bindResponse == null) {
            // We didn't received anything : this is an error
            if (LOG.isErrorEnabled()) {
                LOG.error(I18n.err(I18n.ERR_03203_OP_FAILED_TIMEOUT, "Bind"));
            }
            throw new LdapException(TIME_OUT_ERROR);
        }
        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));
            }
        }
        return bindResponse;
    } catch (Exception ie) {
        // Catch all other exceptions
        LOG.error(NO_RESPONSE_ERROR, ie);
        throw new LdapException(NO_RESPONSE_ERROR, ie);
    }
}
Also used : BindFuture(org.apache.directory.ldap.client.api.future.BindFuture) BindResponse(org.apache.directory.api.ldap.model.message.BindResponse) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException) InvalidConnectionException(org.apache.directory.ldap.client.api.exception.InvalidConnectionException) LdapOperationException(org.apache.directory.api.ldap.model.exception.LdapOperationException) LdapAuthenticationException(org.apache.directory.api.ldap.model.exception.LdapAuthenticationException) MessageEncoderException(org.apache.directory.api.ldap.codec.api.MessageEncoderException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) DecoderException(org.apache.directory.api.asn1.DecoderException) LdapNoPermissionException(org.apache.directory.api.ldap.model.exception.LdapNoPermissionException) LdapOtherException(org.apache.directory.api.ldap.model.exception.LdapOtherException) ProtocolEncoderException(org.apache.mina.filter.codec.ProtocolEncoderException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 8 with BindFuture

use of org.apache.directory.ldap.client.api.future.BindFuture 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;
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) ModifyFuture(org.apache.directory.ldap.client.api.future.ModifyFuture) Message(org.apache.directory.api.ldap.model.message.Message) InetSocketAddress(java.net.InetSocketAddress) IoFuture(org.apache.mina.core.future.IoFuture) ConnectFuture(org.apache.mina.core.future.ConnectFuture) BindFuture(org.apache.directory.ldap.client.api.future.BindFuture) ModifyDnFuture(org.apache.directory.ldap.client.api.future.ModifyDnFuture) InvalidConnectionException(org.apache.directory.ldap.client.api.exception.InvalidConnectionException) DefaultConfigurableBinaryAttributeDetector(org.apache.directory.api.ldap.codec.api.DefaultConfigurableBinaryAttributeDetector) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) CompareFuture(org.apache.directory.ldap.client.api.future.CompareFuture) SchemaBinaryAttributeDetector(org.apache.directory.api.ldap.codec.api.SchemaBinaryAttributeDetector) LdapOtherException(org.apache.directory.api.ldap.model.exception.LdapOtherException) ConnectException(java.net.ConnectException) CloseFuture(org.apache.mina.core.future.CloseFuture) ResponseFuture(org.apache.directory.ldap.client.api.future.ResponseFuture) ExtendedFuture(org.apache.directory.ldap.client.api.future.ExtendedFuture) IOException(java.io.IOException) SchemaBinaryAttributeDetector(org.apache.directory.api.ldap.codec.api.SchemaBinaryAttributeDetector) DefaultConfigurableBinaryAttributeDetector(org.apache.directory.api.ldap.codec.api.DefaultConfigurableBinaryAttributeDetector) BinaryAttributeDetector(org.apache.directory.api.ldap.codec.api.BinaryAttributeDetector) DeleteFuture(org.apache.directory.ldap.client.api.future.DeleteFuture) MessageDecorator(org.apache.directory.api.ldap.codec.api.MessageDecorator) AddFuture(org.apache.directory.ldap.client.api.future.AddFuture) SearchFuture(org.apache.directory.ldap.client.api.future.SearchFuture)

Example 9 with BindFuture

use of org.apache.directory.ldap.client.api.future.BindFuture in project directory-ldap-api by apache.

the class LdapNetworkConnection method bind.

/**
 * Bind to the server using a GssApiRequest object.
 *
 * @param request The GssApiRequest POJO containing all the needed parameters
 * @return A LdapResponse containing the result
 * @throws LdapException if some error occurred
 */
public BindResponse bind(SaslGssApiRequest request) throws LdapException {
    if (request == null) {
        String msg = I18n.msg(I18n.MSG_03204_NULL_REQUEST);
        LOG.debug(msg);
        throw new IllegalArgumentException(msg);
    }
    BindFuture bindFuture = bindAsync(request);
    // Get the result from the future
    try {
        // Read the response, waiting for it if not available immediately
        // Get the response, blocking
        BindResponse bindResponse = bindFuture.get(timeout, TimeUnit.MILLISECONDS);
        if (bindResponse == null) {
            // We didn't received anything : this is an error
            if (LOG.isErrorEnabled()) {
                LOG.error(I18n.err(I18n.ERR_03203_OP_FAILED_TIMEOUT, "Bind"));
            }
            throw new LdapException(TIME_OUT_ERROR);
        }
        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));
            }
        }
        return bindResponse;
    } catch (Exception ie) {
        // Catch all other exceptions
        LOG.error(NO_RESPONSE_ERROR, ie);
        throw new LdapException(NO_RESPONSE_ERROR, ie);
    }
}
Also used : BindFuture(org.apache.directory.ldap.client.api.future.BindFuture) BindResponse(org.apache.directory.api.ldap.model.message.BindResponse) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException) InvalidConnectionException(org.apache.directory.ldap.client.api.exception.InvalidConnectionException) LdapOperationException(org.apache.directory.api.ldap.model.exception.LdapOperationException) LdapAuthenticationException(org.apache.directory.api.ldap.model.exception.LdapAuthenticationException) MessageEncoderException(org.apache.directory.api.ldap.codec.api.MessageEncoderException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) DecoderException(org.apache.directory.api.asn1.DecoderException) LdapNoPermissionException(org.apache.directory.api.ldap.model.exception.LdapNoPermissionException) LdapOtherException(org.apache.directory.api.ldap.model.exception.LdapOtherException) ProtocolEncoderException(org.apache.mina.filter.codec.ProtocolEncoderException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 10 with BindFuture

use of org.apache.directory.ldap.client.api.future.BindFuture in project directory-ldap-api by apache.

the class LdapNetworkConnection method bind.

/**
 * Bind to the server using a SaslExternalRequest object.
 *
 * @param request The SaslExternalRequest POJO containing all the needed parameters
 * @return A LdapResponse containing the result
 * @throws LdapException if some error occurred
 */
public BindResponse bind(SaslExternalRequest request) throws LdapException {
    if (request == null) {
        String msg = I18n.msg(I18n.MSG_03204_NULL_REQUEST);
        LOG.debug(msg);
        throw new IllegalArgumentException(msg);
    }
    BindFuture bindFuture = bindAsync(request);
    // Get the result from the future
    try {
        // Read the response, waiting for it if not available immediately
        // Get the response, blocking
        BindResponse bindResponse = bindFuture.get(timeout, TimeUnit.MILLISECONDS);
        if (bindResponse == null) {
            // We didn't received anything : this is an error
            if (LOG.isErrorEnabled()) {
                LOG.error(I18n.err(I18n.ERR_03203_OP_FAILED_TIMEOUT, "Bind"));
            }
            throw new LdapException(TIME_OUT_ERROR);
        }
        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));
            }
        }
        return bindResponse;
    } catch (Exception ie) {
        // Catch all other exceptions
        LOG.error(NO_RESPONSE_ERROR, ie);
        throw new LdapException(NO_RESPONSE_ERROR, ie);
    }
}
Also used : BindFuture(org.apache.directory.ldap.client.api.future.BindFuture) BindResponse(org.apache.directory.api.ldap.model.message.BindResponse) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException) InvalidConnectionException(org.apache.directory.ldap.client.api.exception.InvalidConnectionException) LdapOperationException(org.apache.directory.api.ldap.model.exception.LdapOperationException) LdapAuthenticationException(org.apache.directory.api.ldap.model.exception.LdapAuthenticationException) MessageEncoderException(org.apache.directory.api.ldap.codec.api.MessageEncoderException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) DecoderException(org.apache.directory.api.asn1.DecoderException) LdapNoPermissionException(org.apache.directory.api.ldap.model.exception.LdapNoPermissionException) LdapOtherException(org.apache.directory.api.ldap.model.exception.LdapOtherException) ProtocolEncoderException(org.apache.mina.filter.codec.ProtocolEncoderException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Aggregations

BindFuture (org.apache.directory.ldap.client.api.future.BindFuture)12 IOException (java.io.IOException)10 ConnectException (java.net.ConnectException)10 UnresolvedAddressException (java.nio.channels.UnresolvedAddressException)10 LdapOtherException (org.apache.directory.api.ldap.model.exception.LdapOtherException)10 InvalidConnectionException (org.apache.directory.ldap.client.api.exception.InvalidConnectionException)10 DecoderException (org.apache.directory.api.asn1.DecoderException)9 MessageEncoderException (org.apache.directory.api.ldap.codec.api.MessageEncoderException)9 CursorException (org.apache.directory.api.ldap.model.cursor.CursorException)9 LdapAuthenticationException (org.apache.directory.api.ldap.model.exception.LdapAuthenticationException)9 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)9 LdapInvalidDnException (org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)9 LdapNoPermissionException (org.apache.directory.api.ldap.model.exception.LdapNoPermissionException)9 LdapOperationException (org.apache.directory.api.ldap.model.exception.LdapOperationException)9 BindResponse (org.apache.directory.api.ldap.model.message.BindResponse)9 ProtocolEncoderException (org.apache.mina.filter.codec.ProtocolEncoderException)9 Message (org.apache.directory.api.ldap.model.message.Message)2 AddFuture (org.apache.directory.ldap.client.api.future.AddFuture)2 CompareFuture (org.apache.directory.ldap.client.api.future.CompareFuture)2 DeleteFuture (org.apache.directory.ldap.client.api.future.DeleteFuture)2