Search in sources :

Example 1 with DefaultConfigurableBinaryAttributeDetector

use of org.apache.directory.api.ldap.codec.api.DefaultConfigurableBinaryAttributeDetector 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 2 with DefaultConfigurableBinaryAttributeDetector

use of org.apache.directory.api.ldap.codec.api.DefaultConfigurableBinaryAttributeDetector in project directory-ldap-api by apache.

the class LdapNetworkConnection method buildConfig.

private static LdapConnectionConfig buildConfig(String server, int port, boolean useSsl) {
    LdapConnectionConfig config = new LdapConnectionConfig();
    config.setUseSsl(useSsl);
    if (port != -1) {
        config.setLdapPort(port);
    } else {
        if (useSsl) {
            config.setLdapPort(config.getDefaultLdapsPort());
        } else {
            config.setLdapPort(config.getDefaultLdapPort());
        }
    }
    // Default to localhost if null
    if (Strings.isEmpty(server)) {
        config.setLdapHost(Network.LOOPBACK_HOSTNAME);
    } else {
        config.setLdapHost(server);
    }
    config.setBinaryAttributeDetector(new DefaultConfigurableBinaryAttributeDetector());
    return config;
}
Also used : DefaultConfigurableBinaryAttributeDetector(org.apache.directory.api.ldap.codec.api.DefaultConfigurableBinaryAttributeDetector)

Aggregations

DefaultConfigurableBinaryAttributeDetector (org.apache.directory.api.ldap.codec.api.DefaultConfigurableBinaryAttributeDetector)2 IOException (java.io.IOException)1 ConnectException (java.net.ConnectException)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketAddress (java.net.SocketAddress)1 UnresolvedAddressException (java.nio.channels.UnresolvedAddressException)1 BinaryAttributeDetector (org.apache.directory.api.ldap.codec.api.BinaryAttributeDetector)1 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)1 MessageDecorator (org.apache.directory.api.ldap.codec.api.MessageDecorator)1 SchemaBinaryAttributeDetector (org.apache.directory.api.ldap.codec.api.SchemaBinaryAttributeDetector)1 LdapOtherException (org.apache.directory.api.ldap.model.exception.LdapOtherException)1 Message (org.apache.directory.api.ldap.model.message.Message)1 InvalidConnectionException (org.apache.directory.ldap.client.api.exception.InvalidConnectionException)1 AddFuture (org.apache.directory.ldap.client.api.future.AddFuture)1 BindFuture (org.apache.directory.ldap.client.api.future.BindFuture)1 CompareFuture (org.apache.directory.ldap.client.api.future.CompareFuture)1 DeleteFuture (org.apache.directory.ldap.client.api.future.DeleteFuture)1 ExtendedFuture (org.apache.directory.ldap.client.api.future.ExtendedFuture)1 ModifyDnFuture (org.apache.directory.ldap.client.api.future.ModifyDnFuture)1 ModifyFuture (org.apache.directory.ldap.client.api.future.ModifyFuture)1