Class LdapClient


  • public final class LdapClient
    extends Object
    This class acts as a simplified access point into the OpenDJ LDAP SDK. Whilst it is possible for scripts to access the OpenDJ LDAP SDK APIs directly, this class simplifies the most common use cases by exposes fields and methods for:
    • creating and caching LDAP connections
    • parsing DNs and LDAP filters
    • simple access to LDAP scopes.
    • Field Detail

      • TRANSACTIONID_OPTION

        public static final Option<TransactionId> TRANSACTIONID_OPTION
        The option to pass the TransactionId to LdapConnection.
    • Method Detail

      • getInstance

        public static LdapClient getInstance()
        Returns the unique instance of an LdapClient.
        Returns:
        The unique instance of an LdapClient.
      • defaultOptions

        public static Options defaultOptions​(Context context)
        Setup the default options to create a LdapClient and adds the transactionId if any in the context's chain.
        Parameters:
        context - the context's chain
        Returns:
        the default options with the current transactionId set if any in the context's chain
      • getScope

        public Map<String,​org.forgerock.opendj.ldap.SearchScope> getScope()
        Returns the SearchScope available.
        Returns:
        the SearchScope available.
      • connect

        public LdapConnection connect​(String host,
                                      int port)
                               throws org.forgerock.opendj.ldap.LdapException
        Returns an LDAP connection for the specified LDAP server. The returned connection must be closed once the caller has completed its transaction. Connections are cached between calls using a connection pool.
        Parameters:
        host - The LDAP server host name.
        port - The LDAP server port.
        Returns:
        An LDAP connection for the specified LDAP server.
        Throws:
        org.forgerock.opendj.ldap.LdapException - If an error occurred while connecting to the LDAP server.
      • connect

        public LdapConnection connect​(String host,
                                      int port,
                                      Options options)
                               throws org.forgerock.opendj.ldap.LdapException
        Returns an LDAP connection for the specified LDAP server using the provided LDAP options. The returned connection must be closed once the caller has completed its transaction. Connections are cached between calls using a connection pool. The LDAP options may be used for configuring SSL parameters and timeouts.

        NOTE: if a connection has already been obtained to the specified LDAP server then a cached connection will be returned and the LDAP options will be ignored.

        Parameters:
        host - The LDAP server host name.
        port - The LDAP server port.
        options - The LDAP options.
        Returns:
        An LDAP connection for the specified LDAP server.
        Throws:
        org.forgerock.opendj.ldap.LdapException - If an error occurred while connecting to the LDAP server.
      • dn

        public String dn​(String template,
                         Object... attributeValues)
        Formats an LDAP distinguished name using the provided template and attribute values. Values will be safely escaped in order to avoid potential injection attacks.
        Parameters:
        template - The DN template.
        attributeValues - The attribute values to be substituted into the template.
        Returns:
        The formatted template parsed as a DN.
        Throws:
        org.forgerock.i18n.LocalizedIllegalArgumentException - If the formatted template is not a valid LDAP string representation of a DN.
        See Also:
        Dn.format(String, Object...)
      • filter

        public String filter​(String template,
                             Object... assertionValues)
        Formats an LDAP filter using the provided template and assertion values. Values will be safely escaped in order to avoid potential injection attacks.
        Parameters:
        template - The filter template.
        assertionValues - The assertion values to be substituted into the template.
        Returns:
        The formatted template parsed as a Filter.
        Throws:
        org.forgerock.i18n.LocalizedIllegalArgumentException - If the formatted template is not a valid LDAP string representation of a filter.
        See Also:
        Filter.format(String, Object...)