private LdapNetworkConnection createLDAPConnection(LDAPConfiguration config,
            String url) throws GuacamoleException {

        // Parse provided LDAP URL
        LdapUrl ldapUrl;
        try {
            ldapUrl = new LdapUrl(url);
        }
        catch (LdapException e) {
            logger.debug("Cannot connect to LDAP URL \"{}\": URL is invalid.", url, e);
            throw new GuacamoleServerException("Invalid LDAP URL.", e);
        }

        // Retrieve hostname from URL, bailing out if no hostname is present
        String host = ldapUrl.getHost();
        if (host == null || host.isEmpty()) {
            logger.debug("Cannot connect to LDAP URL \"{}\": no hostname is present.", url);
            throw new GuacamoleServerException("LDAP URL contains no hostname.");
        }

        // Parse encryption method from URL scheme
        EncryptionMethod encryptionMethod = EncryptionMethod.NONE;
        if (LdapUrl.LDAPS_SCHEME.equals(ldapUrl.getScheme()))
            encryptionMethod = EncryptionMethod.SSL;

        // Use STARTTLS for otherwise unencrypted ldap:// URLs if the main
        // LDAP connection requires STARTTLS
        else if (config.getEncryptionMethod() == EncryptionMethod.STARTTLS) {
            logger.debug("Using STARTTLS for LDAP URL \"{}\" as the main LDAP "
                    + "connection described in guacamole.properties is "
                    + "configured to use STARTTLS.", url);
            encryptionMethod = EncryptionMethod.STARTTLS;
        }

        // If no post is specified within the URL, use the default port
        // dictated by the encryption method
        int port = ldapUrl.getPort();
        if (port < 1)
            port = encryptionMethod.DEFAULT_PORT;

        return createLDAPConnection(host, port, encryptionMethod,
                config.getSslProtocol(), config.getNetworkTimeout());

    }
Creates a new instance of LdapNetworkConnection, configured as required
to use whichever encryption method, hostname, and port are specified
within the given LDAP URL. The returned LdapNetworkConnection is
configured for use but is not yet connected nor bound to the LDAP
server. It will not be bound until a bind operation is explicitly
requested, and will not be connected until it is used in an LDAP
operation (such as a bind).

@param config
    The configuration of the LDAP server being queried.

@param url
    The LDAP URL containing the details which should be used to connect
    to the LDAP server.

@return
    A new LdapNetworkConnection instance which has already been
    configured to use the encryption method, hostname, and port
    specified within the given LDAP URL.

@throws GuacamoleException
    If the given URL is not a valid LDAP URL, or if the encryption
    method indicated by the URL is known but not actually implemented (a
    bug).
extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/LDAPConnectionService.java