Search in sources :

Example 1 with LDAPException

use of com.unboundid.ldap.sdk.LDAPException in project oxCore by GluuFederation.

the class LdifDataUtility method checkIfSerrverHasEntryFromLDIFFile.

/**
	 * Check if DS has at least one DN simular to specified in ldif file.
	 * 
	 * @param connection
	 *            Connection to LDAP server
	 * @param ldifFileName
	 *            LDIF file
	 * @return true if server contains at least one DN simular to specified in
	 *         ldif file.
	 */
public boolean checkIfSerrverHasEntryFromLDIFFile(LDAPConnection connection, String ldifFileName) {
    // Set up the LDIF reader that will be used to read the changes to apply
    LDIFReader ldifReader = createLdifReader(ldifFileName);
    if (ldifReader == null) {
        return true;
    }
    // Check all ldif entries
    while (true) {
        // Read the next change to process.
        Entry entry = null;
        try {
            entry = ldifReader.readEntry();
        } catch (LDIFException le) {
            log.error("Malformed ldif record", le);
            if (!le.mayContinueReading()) {
                return true;
            }
        } catch (IOException ioe) {
            log.error("I/O error encountered while reading a change record", ioe);
            return true;
        }
        // changes to be processed.
        if (entry == null) {
            break;
        }
        // Search entry in the server.
        try {
            SearchResult sr = connection.search(entry.getDN(), SearchScope.BASE, "objectClass=*");
            if ((sr != null) && (sr.getEntryCount() > 0)) {
                return true;
            }
        } catch (LDAPException le) {
            if (le.getResultCode() != ResultCode.NO_SUCH_OBJECT) {
                log.error("Failed to search ldif record", le);
                return true;
            }
        }
    }
    disposeLdifReader(ldifReader);
    return false;
}
Also used : Entry(com.unboundid.ldap.sdk.Entry) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry) LDAPException(com.unboundid.ldap.sdk.LDAPException) LDIFReader(com.unboundid.ldif.LDIFReader) LDIFException(com.unboundid.ldif.LDIFException) SearchResult(com.unboundid.ldap.sdk.SearchResult) IOException(java.io.IOException)

Example 2 with LDAPException

use of com.unboundid.ldap.sdk.LDAPException in project oxCore by GluuFederation.

the class LdifDataUtility method deleteEntryWithAllSubs.

/**
	 * Remove base entry with all sub entries
	 * 
	 * @param connection
	 *            Connection to LDAP server
	 * @param baseDN
	 *            Base DN entry
	 * @return The result code for the processing that was performed.
	 */
public ResultCode deleteEntryWithAllSubs(LDAPConnection connection, String baseDN) {
    ResultCode resultCode = ResultCode.SUCCESS;
    SearchResult searchResult = null;
    try {
        searchResult = connection.search(baseDN, SearchScope.SUB, "objectClass=*");
        if ((searchResult == null) || (searchResult.getEntryCount() == 0)) {
            return ResultCode.LOCAL_ERROR;
        }
    } catch (LDAPSearchException le) {
        log.error("Failed to search subordinate entries", le);
        return ResultCode.LOCAL_ERROR;
    }
    LinkedList<String> dns = new LinkedList<String>();
    for (SearchResultEntry entry : searchResult.getSearchEntries()) {
        dns.add(entry.getDN());
    }
    ListIterator<String> listIterator = dns.listIterator(dns.size());
    while (listIterator.hasPrevious()) {
        try {
            connection.delete(listIterator.previous());
        } catch (LDAPException le) {
            log.error("Failed to delete entry", le);
            resultCode = ResultCode.LOCAL_ERROR;
            break;
        }
    }
    return resultCode;
}
Also used : LDAPException(com.unboundid.ldap.sdk.LDAPException) LDAPSearchException(com.unboundid.ldap.sdk.LDAPSearchException) SearchResult(com.unboundid.ldap.sdk.SearchResult) ResultCode(com.unboundid.ldap.sdk.ResultCode) LinkedList(java.util.LinkedList) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Example 3 with LDAPException

use of com.unboundid.ldap.sdk.LDAPException in project oxCore by GluuFederation.

the class LdifDataUtility method getAttributeResultEntryLDIF.

public List<SearchResultEntry> getAttributeResultEntryLDIF(LDAPConnection connection, List<String> patterns, String baseDN) {
    List<SearchResultEntry> searchResultEntryList = new ArrayList<SearchResultEntry>();
    try {
        for (String pattern : patterns) {
            String[] targetArray = new String[] { pattern };
            Filter inumFilter = Filter.createSubstringFilter("inum", null, targetArray, null);
            Filter searchFilter = Filter.createORFilter(inumFilter);
            SearchResultEntry sr = connection.searchForEntry(baseDN, SearchScope.SUB, searchFilter, null);
            searchResultEntryList.add(sr);
        }
        return searchResultEntryList;
    } catch (LDAPException le) {
        if (le.getResultCode() != ResultCode.NO_SUCH_OBJECT) {
            log.error("Failed to search ldif record", le);
            return null;
        }
    }
    return null;
}
Also used : LDAPException(com.unboundid.ldap.sdk.LDAPException) Filter(com.unboundid.ldap.sdk.Filter) ArrayList(java.util.ArrayList) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Example 4 with LDAPException

use of com.unboundid.ldap.sdk.LDAPException in project oxCore by GluuFederation.

the class LDAPConnectionProvider method createSSLConnectionPoolWithPreviousProtocols.

private LDAPConnectionPool createSSLConnectionPoolWithPreviousProtocols(SSLUtil sslUtil, BindRequest bindRequest, LDAPConnectionOptions connectionOptions, int maxConnections) throws LDAPException {
    for (int i = 1; i < SSL_PROTOCOLS.length; i++) {
        String protocol = SSL_PROTOCOLS[i];
        try {
            FailoverServerSet failoverSet = new FailoverServerSet(this.addresses, this.ports, sslUtil.createSSLSocketFactory(protocol), connectionOptions);
            LDAPConnectionPool connectionPool = new LDAPConnectionPool(failoverSet, bindRequest, maxConnections);
            log.info("Server supports: '" + protocol + "'");
            return connectionPool;
        } catch (GeneralSecurityException ex) {
            log.debug("Server not supports: '" + protocol + "'", ex);
        } catch (LDAPException ex) {
            // Error when LDAP server not supports specified encryption
            if (ex.getResultCode() != ResultCode.SERVER_DOWN) {
                throw ex;
            }
            log.debug("Server not supports: '" + protocol + "'", ex);
        }
    }
    return null;
}
Also used : LDAPConnectionPool(com.unboundid.ldap.sdk.LDAPConnectionPool) LDAPException(com.unboundid.ldap.sdk.LDAPException) GeneralSecurityException(java.security.GeneralSecurityException) FailoverServerSet(com.unboundid.ldap.sdk.FailoverServerSet)

Example 5 with LDAPException

use of com.unboundid.ldap.sdk.LDAPException in project oxCore by GluuFederation.

the class LDAPConnectionProvider method createConnectionPoolWithWaitImpl.

private LDAPConnectionPool createConnectionPoolWithWaitImpl(Properties props, FailoverServerSet failoverSet, BindRequest bindRequest, LDAPConnectionOptions connectionOptions, int maxConnections, SSLUtil sslUtil) throws LDAPException {
    String connectionPoolMaxWaitTime = props.getProperty("connection-pool-max-wait-time");
    int connectionPoolMaxWaitTimeSeconds = 30;
    if (StringHelper.isNotEmpty(connectionPoolMaxWaitTime)) {
        connectionPoolMaxWaitTimeSeconds = Integer.parseInt(connectionPoolMaxWaitTime);
    }
    log.debug("Using LDAP connection pool timeout: '" + connectionPoolMaxWaitTimeSeconds + "'");
    LDAPConnectionPool createdConnectionPool = null;
    LDAPException lastException = null;
    int attempt = 0;
    long currentTime = System.currentTimeMillis();
    long maxWaitTime = currentTime + connectionPoolMaxWaitTimeSeconds * 1000;
    do {
        attempt++;
        if (attempt > 0) {
            log.info("Attempting to create connection pool: " + attempt);
        }
        try {
            createdConnectionPool = createConnectionPoolImpl(failoverSet, bindRequest, connectionOptions, maxConnections, sslUtil);
            break;
        } catch (LDAPException ex) {
            if (ex.getResultCode().intValue() != ResultCode.CONNECT_ERROR_INT_VALUE) {
                throw ex;
            }
            lastException = ex;
        }
        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
            log.error("Exception happened in sleep", ex);
            return null;
        }
        currentTime = System.currentTimeMillis();
    } while (maxWaitTime > currentTime);
    if ((createdConnectionPool == null) && (lastException != null)) {
        throw lastException;
    }
    return createdConnectionPool;
}
Also used : LDAPConnectionPool(com.unboundid.ldap.sdk.LDAPConnectionPool) LDAPException(com.unboundid.ldap.sdk.LDAPException)

Aggregations

LDAPException (com.unboundid.ldap.sdk.LDAPException)59 SearchResult (com.unboundid.ldap.sdk.SearchResult)15 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)13 SearchRequest (com.unboundid.ldap.sdk.SearchRequest)11 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)11 IOException (java.io.IOException)11 ResultCode (com.unboundid.ldap.sdk.ResultCode)9 LDIFReader (com.unboundid.ldif.LDIFReader)8 GeneralSecurityException (java.security.GeneralSecurityException)8 DN (com.unboundid.ldap.sdk.DN)6 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)5 Entry (com.unboundid.ldap.sdk.Entry)5 Filter (com.unboundid.ldap.sdk.Filter)5 LDAPConnectionPool (com.unboundid.ldap.sdk.LDAPConnectionPool)5 ArrayList (java.util.ArrayList)5 LdifDataUtility (org.gluu.persist.ldap.impl.LdifDataUtility)5 InMemoryDirectoryServer (com.unboundid.ldap.listener.InMemoryDirectoryServer)4 InMemoryDirectoryServerConfig (com.unboundid.ldap.listener.InMemoryDirectoryServerConfig)4 BindResult (com.unboundid.ldap.sdk.BindResult)4 SimpleBindRequest (com.unboundid.ldap.sdk.SimpleBindRequest)4