Search in sources :

Example 1 with InvalidSimplePageControlException

use of io.jans.orm.ldap.exception.InvalidSimplePageControlException in project jans by JanssenProject.

the class LdapOperationServiceImpl method scrollSimplePagedResultsControl.

private SimplePagedResponse scrollSimplePagedResultsControl(LDAPConnection ldapConnection, String dn, Filter filter, SearchScope scope, Control[] controls, int start) throws LDAPException, InvalidSimplePageControlException {
    SearchRequest searchRequest = new SearchRequest(dn, scope, filter, "dn");
    int currentStartIndex = start;
    ASN1OctetString cookie = null;
    SearchResult searchResult = null;
    do {
        int pageSize = Math.min(currentStartIndex, 100);
        searchRequest.setControls(new Control[] { new SimplePagedResultsControl(pageSize, cookie, true) });
        setControls(searchRequest, controls);
        searchResult = ldapConnection.search(searchRequest);
        currentStartIndex -= searchResult.getEntryCount();
        try {
            SimplePagedResultsControl c = SimplePagedResultsControl.get(searchResult);
            if (c != null) {
                cookie = c.getCookie();
            }
        } catch (LDAPException ex) {
            LOG.error("Error while accessing cookie", ex);
            throw new InvalidSimplePageControlException(ex.getResultCode(), "Error while accessing cookie");
        }
    } while ((cookie != null) && (cookie.getValueLength() > 0) && (currentStartIndex > 0));
    return new SimplePagedResponse(cookie, searchResult);
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) SearchRequest(com.unboundid.ldap.sdk.SearchRequest) LDAPException(com.unboundid.ldap.sdk.LDAPException) SearchResult(com.unboundid.ldap.sdk.SearchResult) SimplePagedResultsControl(com.unboundid.ldap.sdk.controls.SimplePagedResultsControl) InvalidSimplePageControlException(io.jans.orm.ldap.exception.InvalidSimplePageControlException)

Example 2 with InvalidSimplePageControlException

use of io.jans.orm.ldap.exception.InvalidSimplePageControlException in project jans by JanssenProject.

the class LdapOperationServiceImpl method searchImpl.

private <T> SearchResult searchImpl(String dn, Filter filter, SearchScope scope, LdapBatchOperationWraper<T> batchOperationWraper, int start, int searchLimit, int count, 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. This can be very slow
        if (StringHelper.equalsIgnoreCase(dn, "o=jans")) {
            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 = count > 0;
    if (useSizeLimit) {
        // Use paged result to limit search
        searchLimit = count;
    }
    SearchResult searchResult = null;
    List<SearchResult> searchResultList = new ArrayList<SearchResult>();
    List<SearchResultEntry> searchResultEntries = new ArrayList<SearchResultEntry>();
    List<SearchResultReference> searchResultReferences = new ArrayList<SearchResultReference>();
    if ((searchLimit > 0) || (start > 0)) {
        if (searchLimit == 0) {
            // Default page size
            searchLimit = 100;
        }
        boolean collectSearchResult;
        LDAPConnection ldapConnection = null;
        try {
            ldapConnection = getConnectionPool().getConnection();
            ASN1OctetString cookie = null;
            SimplePagedResponse simplePagedResponse = null;
            if (start > 0) {
                try {
                    simplePagedResponse = scrollSimplePagedResultsControl(ldapConnection, dn, filter, scope, controls, start);
                    cookie = simplePagedResponse.getCookie();
                } catch (InvalidSimplePageControlException ex) {
                    throw new LDAPSearchException(ex.getResultCode(), "Failed to scroll to specified start", ex);
                } catch (LDAPException ex) {
                    throw new LDAPSearchException(ex.getResultCode(), "Failed to scroll to specified start", ex);
                }
            }
            if ((cookie != null) && (cookie.getValueLength() == 0)) {
                SearchResult searchResultTemp = simplePagedResponse.getLastSearchResult();
                return new SearchResult(searchResultTemp.getMessageID(), searchResultTemp.getResultCode(), searchResultTemp.getDiagnosticMessage(), searchResultTemp.getMatchedDN(), searchResultTemp.getReferralURLs(), searchResultEntries, searchResultReferences, searchResultEntries.size(), searchResultReferences.size(), searchResultTemp.getResponseControls());
            }
            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 start", 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) LDAPSearchException(com.unboundid.ldap.sdk.LDAPSearchException) SearchException(io.jans.orm.exception.operation.SearchException) SearchResult(com.unboundid.ldap.sdk.SearchResult) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) MappingException(io.jans.orm.exception.MappingException) LDAPSearchException(com.unboundid.ldap.sdk.LDAPSearchException) DuplicateEntryException(io.jans.orm.exception.operation.DuplicateEntryException) ConnectionException(io.jans.orm.exception.operation.ConnectionException) InvalidSimplePageControlException(io.jans.orm.ldap.exception.InvalidSimplePageControlException) LDAPException(com.unboundid.ldap.sdk.LDAPException) SearchException(io.jans.orm.exception.operation.SearchException) AuthenticationException(io.jans.orm.exception.AuthenticationException) LDAPException(com.unboundid.ldap.sdk.LDAPException) LDAPSearchException(com.unboundid.ldap.sdk.LDAPSearchException) InvalidSimplePageControlException(io.jans.orm.ldap.exception.InvalidSimplePageControlException) SimplePagedResultsControl(com.unboundid.ldap.sdk.controls.SimplePagedResultsControl) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Aggregations

ASN1OctetString (com.unboundid.asn1.ASN1OctetString)2 LDAPException (com.unboundid.ldap.sdk.LDAPException)2 SearchRequest (com.unboundid.ldap.sdk.SearchRequest)2 SearchResult (com.unboundid.ldap.sdk.SearchResult)2 SimplePagedResultsControl (com.unboundid.ldap.sdk.controls.SimplePagedResultsControl)2 InvalidSimplePageControlException (io.jans.orm.ldap.exception.InvalidSimplePageControlException)2 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)1 LDAPSearchException (com.unboundid.ldap.sdk.LDAPSearchException)1 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)1 SearchResultReference (com.unboundid.ldap.sdk.SearchResultReference)1 AuthenticationException (io.jans.orm.exception.AuthenticationException)1 MappingException (io.jans.orm.exception.MappingException)1 ConnectionException (io.jans.orm.exception.operation.ConnectionException)1 DuplicateEntryException (io.jans.orm.exception.operation.DuplicateEntryException)1 SearchException (io.jans.orm.exception.operation.SearchException)1 ArrayList (java.util.ArrayList)1