Search in sources :

Example 46 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 47 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 48 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)

Example 49 with LDAPException

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

the class LdapOperationsServiceImpl method search.

/*
     * (non-Javadoc)
     *
     * @see org.gluu.site.ldap.PlatformOperationFacade#search(java.lang.String,
     * com.unboundid.ldap.sdk.Filter, org.xdi.ldap.model.SearchScope,
     * org.gluu.site.ldap.persistence.BatchOperation, int, int, int,
     * com.unboundid.ldap.sdk.Control[], java.lang.String)
     */
@Override
public <T> SearchResult search(String dn, Filter filter, SearchScope scope, LdapBatchOperationWraper<T> batchOperationWraper, int startIndex, int searchLimit, int sizeLimit, Control[] controls, String... attributes) throws SearchException {
    SearchRequest searchRequest;
    BatchOperation<T> ldapBatchOperation = null;
    if (batchOperationWraper != null) {
        ldapBatchOperation = (BatchOperation<T>) batchOperationWraper.getBatchOperation();
    }
    if (LOG.isTraceEnabled()) {
        // Find whole tree search
        if (StringHelper.equalsIgnoreCase(dn, "o=gluu")) {
            LOG.trace("Search in whole LDAP tree", new Exception());
        }
    }
    if (attributes == null) {
        searchRequest = new SearchRequest(dn, scope, filter);
    } else {
        searchRequest = new SearchRequest(dn, scope, filter, attributes);
    }
    boolean useSizeLimit = sizeLimit > 0;
    if (useSizeLimit) {
        // Use paged result to limit search
        searchLimit = sizeLimit;
    }
    SearchResult searchResult = null;
    List<SearchResult> searchResultList = new ArrayList<SearchResult>();
    List<SearchResultEntry> searchResultEntries = new ArrayList<SearchResultEntry>();
    List<SearchResultReference> searchResultReferences = new ArrayList<SearchResultReference>();
    if ((searchLimit > 0) || (startIndex > 0)) {
        if (searchLimit == 0) {
            // Default page size
            searchLimit = 100;
        }
        boolean collectSearchResult;
        LDAPConnection ldapConnection = null;
        try {
            ldapConnection = getConnectionPool().getConnection();
            ASN1OctetString cookie = null;
            if (startIndex > 0) {
                try {
                    cookie = scrollSimplePagedResultsControl(ldapConnection, dn, filter, scope, controls, startIndex);
                } catch (InvalidSimplePageControlException ex) {
                    throw new LDAPSearchException(ex.getResultCode(), "Failed to scroll to specified startIndex", ex);
                } catch (LDAPException ex) {
                    throw new LDAPSearchException(ex.getResultCode(), "Failed to scroll to specified startIndex", ex);
                }
            }
            do {
                collectSearchResult = true;
                searchRequest.setControls(new Control[] { new SimplePagedResultsControl(searchLimit, cookie) });
                setControls(searchRequest, controls);
                searchResult = ldapConnection.search(searchRequest);
                if (ldapBatchOperation != null) {
                    collectSearchResult = ldapBatchOperation.collectSearchResult(searchResult.getEntryCount());
                }
                if (collectSearchResult) {
                    searchResultList.add(searchResult);
                    searchResultEntries.addAll(searchResult.getSearchEntries());
                    searchResultReferences.addAll(searchResult.getSearchReferences());
                }
                if (ldapBatchOperation != null) {
                    List<T> entries = batchOperationWraper.createEntities(searchResult);
                    ldapBatchOperation.performAction(entries);
                }
                cookie = null;
                try {
                    SimplePagedResultsControl c = SimplePagedResultsControl.get(searchResult);
                    if (c != null) {
                        cookie = c.getCookie();
                    }
                } catch (LDAPException ex) {
                    LOG.error("Error while accessing cookies" + ex.getMessage());
                }
                if (useSizeLimit) {
                    break;
                }
            } while ((cookie != null) && (cookie.getValueLength() > 0));
        } catch (LDAPException ex) {
            throw new SearchException("Failed to scroll to specified startIndex", ex, ex.getResultCode().intValue());
        } finally {
            if (ldapConnection != null) {
                getConnectionPool().releaseConnection(ldapConnection);
            }
        }
        if (!collectSearchResult) {
            return new SearchResult(searchResult.getMessageID(), searchResult.getResultCode(), searchResult.getDiagnosticMessage(), searchResult.getMatchedDN(), searchResult.getReferralURLs(), searchResultEntries, searchResultReferences, searchResultEntries.size(), searchResultReferences.size(), searchResult.getResponseControls());
        }
        if (!searchResultList.isEmpty()) {
            SearchResult searchResultTemp = searchResultList.get(0);
            return new SearchResult(searchResultTemp.getMessageID(), searchResultTemp.getResultCode(), searchResultTemp.getDiagnosticMessage(), searchResultTemp.getMatchedDN(), searchResultTemp.getReferralURLs(), searchResultEntries, searchResultReferences, searchResultEntries.size(), searchResultReferences.size(), searchResultTemp.getResponseControls());
        }
    } else {
        setControls(searchRequest, controls);
        try {
            searchResult = getConnectionPool().search(searchRequest);
        } catch (LDAPSearchException ex) {
            throw new SearchException(ex.getMessage(), ex, ex.getResultCode().intValue());
        }
    }
    return searchResult;
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) SearchRequest(com.unboundid.ldap.sdk.SearchRequest) ArrayList(java.util.ArrayList) SearchResultReference(com.unboundid.ldap.sdk.SearchResultReference) SearchException(org.gluu.persist.exception.operation.SearchException) LDAPSearchException(com.unboundid.ldap.sdk.LDAPSearchException) SearchResult(com.unboundid.ldap.sdk.SearchResult) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) InvalidSimplePageControlException(org.gluu.persist.ldap.exception.InvalidSimplePageControlException) ConnectionException(org.gluu.persist.exception.operation.ConnectionException) SearchException(org.gluu.persist.exception.operation.SearchException) LDAPSearchException(com.unboundid.ldap.sdk.LDAPSearchException) MappingException(org.gluu.persist.exception.mapping.MappingException) LDAPException(com.unboundid.ldap.sdk.LDAPException) DuplicateEntryException(org.gluu.persist.exception.operation.DuplicateEntryException) LDAPException(com.unboundid.ldap.sdk.LDAPException) LDAPSearchException(com.unboundid.ldap.sdk.LDAPSearchException) InvalidSimplePageControlException(org.gluu.persist.ldap.exception.InvalidSimplePageControlException) SimplePagedResultsControl(com.unboundid.ldap.sdk.controls.SimplePagedResultsControl) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Example 50 with LDAPException

use of com.unboundid.ldap.sdk.LDAPException in project gitblit by gitblit.

the class LdapConnection method bind.

/**
 * Bind using the given credentials, by filling in the username in the given {@code bindPattern} to
 * create the DN.
 * @return A bind result, or null if binding failed.
 */
public BindResult bind(String bindPattern, String simpleUsername, String password) {
    BindResult result = null;
    try {
        String bindUser = StringUtils.replace(bindPattern, "${username}", escapeLDAPSearchFilter(simpleUsername));
        SimpleBindRequest request = new SimpleBindRequest(bindUser, password);
        result = conn.bind(request);
        userBindRequest = request;
        currentBindRequest = userBindRequest;
    } catch (LDAPException e) {
        logger.error("Error authenticating to LDAP with user account to search the directory.");
        logger.error("  Please check your settings for realm.ldap.bindpattern.");
        logger.debug("  Received exception when binding to LDAP", e);
        return null;
    }
    return result;
}
Also used : SimpleBindRequest(com.unboundid.ldap.sdk.SimpleBindRequest) LDAPException(com.unboundid.ldap.sdk.LDAPException) BindResult(com.unboundid.ldap.sdk.BindResult)

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