org.opends.admin.ads.util
Class ConnectionUtils

java.lang.Object
  extended by org.opends.admin.ads.util.ConnectionUtils

public class ConnectionUtils
extends java.lang.Object

Class providing some utilities to create LDAP connections using JNDI and to manage entries retrieved using JNDI.


Method Summary
static boolean canConnectAsAdministrativeUser(java.lang.String ldapUrl, java.lang.String dn, java.lang.String pwd)
          Method used to know if we can connect as administrator in a server with a given password and dn.
static boolean connectedAsAdministrativeUser(javax.naming.ldap.InitialLdapContext ctx)
          Method used to know if we are connected as administrator in a server with a given InitialLdapContext.
static javax.naming.ldap.InitialLdapContext createLdapContext(java.lang.String ldapURL, java.lang.String dn, java.lang.String pwd, int timeout, java.util.Hashtable<java.lang.String,java.lang.String> env)
          Creates a clear LDAP connection and returns the corresponding LdapContext.
static javax.naming.ldap.InitialLdapContext createLdapsContext(java.lang.String ldapsURL, java.lang.String dn, java.lang.String pwd, int timeout, java.util.Hashtable<java.lang.String,java.lang.String> env, javax.net.ssl.TrustManager trustManager, javax.net.ssl.KeyManager keyManager)
          Creates an LDAPS connection and returns the corresponding LdapContext.
static javax.naming.ldap.InitialLdapContext createStartTLSContext(java.lang.String ldapURL, java.lang.String dn, java.lang.String pwd, int timeout, java.util.Hashtable<java.lang.String,java.lang.String> env, javax.net.ssl.TrustManager trustManager, javax.net.ssl.KeyManager keyManager, javax.net.ssl.HostnameVerifier verifier)
          Creates an LDAP+StartTLS connection and returns the corresponding LdapContext.
static java.lang.String getBindDN(javax.naming.ldap.InitialLdapContext ctx)
          Returns the bind DN used in the provided InitialLdapContext.
static java.lang.String getBindPassword(javax.naming.ldap.InitialLdapContext ctx)
          Returns the password used in the provided InitialLdapContext.
static int getDefaultLDAPTimeout()
          Returns the default LDAP timeout in milliseconds when we try to connect to a server.
static java.lang.String getFirstValue(javax.naming.directory.SearchResult entry, java.lang.String attrName)
          Returns the String representation of the first value of an attribute in a LDAP entry.
static java.lang.String getHostName(javax.naming.ldap.InitialLdapContext ctx)
          Returns the host name used in the provided InitialLdapContext.
static java.lang.String getHostNameForLdapUrl(java.lang.String host)
          Returns the String that can be used to represent a given host name in a LDAP URL.
static java.lang.String getHostPort(javax.naming.ldap.InitialLdapContext ctx)
          Returns the host port representation of the server to which this context is connected.
static java.lang.String getLdapUrl(javax.naming.ldap.InitialLdapContext ctx)
          Returns the LDAP URL used in the provided InitialLdapContext.
static java.lang.String getLDAPUrl(java.lang.String host, int port, boolean useSSL)
          Returns the LDAP URL for the provided parameters.
static int getPort(javax.naming.ldap.InitialLdapContext ctx)
          Returns the port number used in the provided InitialLdapContext.
static java.util.Set<java.lang.String> getValues(javax.naming.directory.SearchResult entry, java.lang.String attrName)
          Returns a Set with the String representation of the values of an attribute in a LDAP entry.
static boolean isCertificateException(java.lang.Throwable t)
          Tells whether the provided Throwable was caused because of a problem with a certificate while trying to establish a connection.
static boolean isSSL(javax.naming.ldap.InitialLdapContext ctx)
          Tells whether we are using SSL in the provided InitialLdapContext.
static boolean isStartTLS(javax.naming.ldap.InitialLdapContext ctx)
          Tells whether we are using StartTLS in the provided InitialLdapContext.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

createLdapContext

public static javax.naming.ldap.InitialLdapContext createLdapContext(java.lang.String ldapURL,
                                                                     java.lang.String dn,
                                                                     java.lang.String pwd,
                                                                     int timeout,
                                                                     java.util.Hashtable<java.lang.String,java.lang.String> env)
                                                              throws javax.naming.NamingException
Creates a clear LDAP connection and returns the corresponding LdapContext. This methods uses the specified parameters to create a JNDI environment hashtable and creates an InitialLdapContext instance.

Parameters:
ldapURL - the target LDAP URL
dn - passed as Context.SECURITY_PRINCIPAL if not null
pwd - passed as Context.SECURITY_CREDENTIALS if not null
timeout - passed as com.sun.jndi.ldap.connect.timeout if > 0
env - null or additional environment properties
Returns:
the created InitialLdapContext.
Throws:
javax.naming.NamingException - the exception thrown when instantiating InitialLdapContext
See Also:
Context, InitialLdapContext

createLdapsContext

public static javax.naming.ldap.InitialLdapContext createLdapsContext(java.lang.String ldapsURL,
                                                                      java.lang.String dn,
                                                                      java.lang.String pwd,
                                                                      int timeout,
                                                                      java.util.Hashtable<java.lang.String,java.lang.String> env,
                                                                      javax.net.ssl.TrustManager trustManager,
                                                                      javax.net.ssl.KeyManager keyManager)
                                                               throws javax.naming.NamingException
Creates an LDAPS connection and returns the corresponding LdapContext. This method uses the TrusteSocketFactory class so that the specified trust manager gets called during the SSL handshake. If trust manager is null, certificates are not verified during SSL handshake.

Parameters:
ldapsURL - the target *LDAPS* URL.
dn - passed as Context.SECURITY_PRINCIPAL if not null.
pwd - passed as Context.SECURITY_CREDENTIALS if not null.
timeout - passed as com.sun.jndi.ldap.connect.timeout if > 0.
env - null or additional environment properties.
trustManager - null or the trust manager to be invoked during SSL negociation.
keyManager - null or the key manager to be invoked during SSL negociation.
Returns:
the established connection with the given parameters.
Throws:
javax.naming.NamingException - the exception thrown when instantiating InitialLdapContext.
See Also:
Context, InitialLdapContext, TrustedSocketFactory

createStartTLSContext

public static javax.naming.ldap.InitialLdapContext createStartTLSContext(java.lang.String ldapURL,
                                                                         java.lang.String dn,
                                                                         java.lang.String pwd,
                                                                         int timeout,
                                                                         java.util.Hashtable<java.lang.String,java.lang.String> env,
                                                                         javax.net.ssl.TrustManager trustManager,
                                                                         javax.net.ssl.KeyManager keyManager,
                                                                         javax.net.ssl.HostnameVerifier verifier)
                                                                  throws javax.naming.NamingException
Creates an LDAP+StartTLS connection and returns the corresponding LdapContext. This method first creates an LdapContext with anonymous bind. Then it requests a StartTlsRequest extended operation. The StartTlsResponse is setup with the specified hostname verifier. Negotiation is done using a TrustSocketFactory so that the specified TrustManager gets called during the SSL handshake. If trust manager is null, certificates are not checked during SSL handshake.

Parameters:
ldapURL - the target *LDAP* URL.
dn - passed as Context.SECURITY_PRINCIPAL if not null.
pwd - passed as Context.SECURITY_CREDENTIALS if not null.
timeout - passed as com.sun.jndi.ldap.connect.timeout if > 0.
env - null or additional environment properties.
trustManager - null or the trust manager to be invoked during SSL negociation.
keyManager - null or the key manager to be invoked during SSL negociation.
verifier - null or the hostname verifier to be setup in the StartTlsResponse.
Returns:
the established connection with the given parameters.
Throws:
javax.naming.NamingException - the exception thrown when instantiating InitialLdapContext.
See Also:
Context, InitialLdapContext, StartTlsRequest, StartTlsResponse, TrustedSocketFactory

getLdapUrl

public static java.lang.String getLdapUrl(javax.naming.ldap.InitialLdapContext ctx)
Returns the LDAP URL used in the provided InitialLdapContext.

Parameters:
ctx - the context to analyze.
Returns:
the LDAP URL used in the provided InitialLdapContext.

getHostName

public static java.lang.String getHostName(javax.naming.ldap.InitialLdapContext ctx)
Returns the host name used in the provided InitialLdapContext.

Parameters:
ctx - the context to analyze.
Returns:
the host name used in the provided InitialLdapContext.

getPort

public static int getPort(javax.naming.ldap.InitialLdapContext ctx)
Returns the port number used in the provided InitialLdapContext.

Parameters:
ctx - the context to analyze.
Returns:
the port number used in the provided InitialLdapContext.

getHostPort

public static java.lang.String getHostPort(javax.naming.ldap.InitialLdapContext ctx)
Returns the host port representation of the server to which this context is connected.

Parameters:
ctx - the context to analyze.
Returns:
the host port representation of the server to which this context is connected.

getBindDN

public static java.lang.String getBindDN(javax.naming.ldap.InitialLdapContext ctx)
Returns the bind DN used in the provided InitialLdapContext.

Parameters:
ctx - the context to analyze.
Returns:
the bind DN used in the provided InitialLdapContext.

getBindPassword

public static java.lang.String getBindPassword(javax.naming.ldap.InitialLdapContext ctx)
Returns the password used in the provided InitialLdapContext.

Parameters:
ctx - the context to analyze.
Returns:
the password used in the provided InitialLdapContext.

isSSL

public static boolean isSSL(javax.naming.ldap.InitialLdapContext ctx)
Tells whether we are using SSL in the provided InitialLdapContext.

Parameters:
ctx - the context to analyze.
Returns:
true if we are using SSL and false otherwise.

isStartTLS

public static boolean isStartTLS(javax.naming.ldap.InitialLdapContext ctx)
Tells whether we are using StartTLS in the provided InitialLdapContext.

Parameters:
ctx - the context to analyze.
Returns:
true if we are using StartTLS and false otherwise.

canConnectAsAdministrativeUser

public static boolean canConnectAsAdministrativeUser(java.lang.String ldapUrl,
                                                     java.lang.String dn,
                                                     java.lang.String pwd)
Method used to know if we can connect as administrator in a server with a given password and dn.

Parameters:
ldapUrl - the ldap URL of the server.
dn - the dn to be used.
pwd - the password to be used.
Returns:
true if we can connect and read the configuration and false otherwise.

connectedAsAdministrativeUser

public static boolean connectedAsAdministrativeUser(javax.naming.ldap.InitialLdapContext ctx)
Method used to know if we are connected as administrator in a server with a given InitialLdapContext.

Parameters:
ctx - the context.
Returns:
true if we are connected and read the configuration and false otherwise.

getDefaultLDAPTimeout

public static int getDefaultLDAPTimeout()
Returns the default LDAP timeout in milliseconds when we try to connect to a server.

Returns:
the default LDAP timeout in milliseconds when we try to connect to a server.

getHostNameForLdapUrl

public static java.lang.String getHostNameForLdapUrl(java.lang.String host)
Returns the String that can be used to represent a given host name in a LDAP URL. This method must be used when we have IPv6 addresses (the address in the LDAP URL must be enclosed with brackets).

Parameters:
host - the host name.
Returns:
the String that can be used to represent a given host name in a LDAP URL.

getLDAPUrl

public static java.lang.String getLDAPUrl(java.lang.String host,
                                          int port,
                                          boolean useSSL)
Returns the LDAP URL for the provided parameters.

Parameters:
host - the host name.
port - the LDAP port.
useSSL - whether to use SSL or not.
Returns:
the LDAP URL for the provided parameters.

isCertificateException

public static boolean isCertificateException(java.lang.Throwable t)
Tells whether the provided Throwable was caused because of a problem with a certificate while trying to establish a connection.

Parameters:
t - the Throwable to analyze.
Returns:
true if the provided Throwable was caused because of a problem with a certificate while trying to establish a connection and false otherwise.

getFirstValue

public static java.lang.String getFirstValue(javax.naming.directory.SearchResult entry,
                                             java.lang.String attrName)
                                      throws javax.naming.NamingException
Returns the String representation of the first value of an attribute in a LDAP entry.

Parameters:
entry - the entry.
attrName - the attribute name.
Returns:
the String representation of the first value of an attribute in a LDAP entry.
Throws:
javax.naming.NamingException - if there is an error processing the entry.

getValues

public static java.util.Set<java.lang.String> getValues(javax.naming.directory.SearchResult entry,
                                                        java.lang.String attrName)
                                                 throws javax.naming.NamingException
Returns a Set with the String representation of the values of an attribute in a LDAP entry. The returned Set will never be null.

Parameters:
entry - the entry.
attrName - the attribute name.
Returns:
a Set with the String representation of the values of an attribute in a LDAP entry.
Throws:
javax.naming.NamingException - if there is an error processing the entry.