Search in sources :

Example 36 with SearchResult

use of com.unboundid.ldap.sdk.SearchResult 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 37 with SearchResult

use of com.unboundid.ldap.sdk.SearchResult 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 38 with SearchResult

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

the class LdapOperationsServiceImpl method searchVirtualListView.

/*
     * (non-Javadoc)
     *
     * @see
     * org.gluu.site.ldap.PlatformOperationFacade#searchVirtualListView(java.lang.
     * String, com.unboundid.ldap.sdk.Filter, org.xdi.ldap.model.SearchScope, int,
     * int, java.lang.String, org.xdi.ldap.model.SortOrder,
     * org.xdi.ldap.model.VirtualListViewResponse, java.lang.String)
     */
@Deprecated
public SearchResult searchVirtualListView(String dn, Filter filter, SearchScope scope, int startIndex, int count, String sortBy, SortOrder sortOrder, ListViewResponse vlvResponse, String... attributes) throws Exception {
    if (StringHelper.equalsIgnoreCase(dn, "o=gluu")) {
        (new Exception()).printStackTrace();
    }
    SearchRequest searchRequest;
    if (attributes == null) {
        searchRequest = new SearchRequest(dn, scope, filter);
    } else {
        searchRequest = new SearchRequest(dn, scope, filter, attributes);
    }
    // startIndex and count should be "cleansed" before arriving here
    int targetOffset = startIndex;
    int beforeCount = 0;
    int afterCount = (count > 0) ? (count - 1) : 0;
    int contentCount = 0;
    boolean reverseOrder = false;
    if (sortOrder != null) {
        reverseOrder = sortOrder.equals(SortOrder.DESCENDING) ? true : false;
    }
    // Note that the VLV control always requires the server-side sort control.
    searchRequest.setControls(new ServerSideSortRequestControl(new SortKey(sortBy, reverseOrder)), new VirtualListViewRequestControl(targetOffset, beforeCount, afterCount, contentCount, null));
    SearchResult searchResult = getConnectionPool().search(searchRequest);
    /*
         * for (SearchResultEntry searchResultEntry : searchResult.getSearchEntries()) {
         * log.info("##### searchResultEntry = " + searchResultEntry.toString()); }
         */
    // LDAPTestUtils.assertHasControl(searchResult,
    // VirtualListViewResponseControl.VIRTUAL_LIST_VIEW_RESPONSE_OID);
    VirtualListViewResponseControl vlvResponseControl = VirtualListViewResponseControl.get(searchResult);
    // Get results info
    vlvResponse.setItemsPerPage(searchResult.getEntryCount());
    vlvResponse.setTotalResults(vlvResponseControl.getContentCount());
    vlvResponse.setStartIndex(vlvResponseControl.getTargetPosition());
    return searchResult;
}
Also used : SearchRequest(com.unboundid.ldap.sdk.SearchRequest) ServerSideSortRequestControl(com.unboundid.ldap.sdk.controls.ServerSideSortRequestControl) VirtualListViewResponseControl(com.unboundid.ldap.sdk.controls.VirtualListViewResponseControl) VirtualListViewRequestControl(com.unboundid.ldap.sdk.controls.VirtualListViewRequestControl) SortKey(com.unboundid.ldap.sdk.controls.SortKey) SearchResult(com.unboundid.ldap.sdk.SearchResult) 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)

Example 39 with SearchResult

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

the class LdapOperationsServiceImpl method searchSearchResult.

/*
     * (non-Javadoc)
     *
     * @see org.gluu.site.ldap.PlatformOperationFacade#searchSearchResult(java.lang.
     * String, com.unboundid.ldap.sdk.Filter, org.xdi.ldap.model.SearchScope, int,
     * int, int, java.lang.String, org.xdi.ldap.model.SortOrder,
     * org.xdi.ldap.model.VirtualListViewResponse, java.lang.String)
     */
@Override
public SearchResult searchSearchResult(String dn, Filter filter, SearchScope scope, int startIndex, int count, int searchLimit, String sortBy, SortOrder sortOrder, ListViewResponse vlvResponse, String... attributes) throws Exception {
    if (StringHelper.equalsIgnoreCase(dn, "o=gluu")) {
        (new Exception()).printStackTrace();
    }
    SearchRequest searchRequest;
    if (attributes == null) {
        searchRequest = new SearchRequest(dn, scope, filter);
    } else {
        searchRequest = new SearchRequest(dn, scope, filter, attributes);
    }
    List<SearchResult> searchResultList = new ArrayList<SearchResult>();
    List<SearchResultEntry> searchResultEntries = new ArrayList<SearchResultEntry>();
    List<SearchResultReference> searchResultReferences = new ArrayList<SearchResultReference>();
    searchRequest.setControls(new SimplePagedResultsControl(searchLimit));
    SearchResult searchResult = getConnectionPool().search(searchRequest);
    List<SearchResultEntry> resultSearchResultEntries = searchResult.getSearchEntries();
    int totalResults = resultSearchResultEntries.size();
    if (StringUtils.isNotEmpty(sortBy)) {
        boolean ascending = sortOrder == null || sortOrder.equals(SortOrder.ASCENDING);
        resultSearchResultEntries = sortListByAttributes(resultSearchResultEntries, SearchResultEntry.class, false, ascending, sortBy);
    }
    List<SearchResultEntry> searchResultEntryList = new ArrayList<SearchResultEntry>();
    if (startIndex <= totalResults) {
        int diff = (totalResults - startIndex);
        if (diff <= count) {
            count = (diff + 1) >= count ? count : (diff + 1);
        }
        int startZeroIndex = startIndex - 1;
        searchResultEntryList = resultSearchResultEntries.subList(startZeroIndex, startZeroIndex + count);
    }
    searchResultList.add(searchResult);
    searchResultEntries.addAll(searchResultEntryList);
    searchResultReferences.addAll(searchResult.getSearchReferences());
    SearchResult searchResultTemp = searchResultList.get(0);
    searchResult = new SearchResult(searchResultTemp.getMessageID(), searchResultTemp.getResultCode(), searchResultTemp.getDiagnosticMessage(), searchResultTemp.getMatchedDN(), searchResultTemp.getReferralURLs(), searchResultEntries, searchResultReferences, searchResultEntries.size(), searchResultReferences.size(), searchResultTemp.getResponseControls());
    // Get results info
    vlvResponse.setItemsPerPage(count);
    vlvResponse.setTotalResults(totalResults);
    vlvResponse.setStartIndex(startIndex);
    return searchResult;
}
Also used : SearchRequest(com.unboundid.ldap.sdk.SearchRequest) ArrayList(java.util.ArrayList) SearchResultReference(com.unboundid.ldap.sdk.SearchResultReference) SearchResult(com.unboundid.ldap.sdk.SearchResult) SimplePagedResultsControl(com.unboundid.ldap.sdk.controls.SimplePagedResultsControl) 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) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Aggregations

SearchResult (com.unboundid.ldap.sdk.SearchResult)39 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)24 LDAPException (com.unboundid.ldap.sdk.LDAPException)16 SearchRequest (com.unboundid.ldap.sdk.SearchRequest)14 SearchException (org.gluu.persist.exception.operation.SearchException)10 ArrayList (java.util.ArrayList)9 MappingException (org.gluu.persist.exception.mapping.MappingException)9 ConnectionException (org.gluu.persist.exception.operation.ConnectionException)8 Test (org.junit.Test)7 LdapConnection (com.gitblit.ldap.LdapConnection)6 EntryPersistenceException (org.gluu.persist.exception.mapping.EntryPersistenceException)6 BindResult (com.unboundid.ldap.sdk.BindResult)5 LDAPSearchException (com.unboundid.ldap.sdk.LDAPSearchException)5 TeamModel (com.gitblit.models.TeamModel)4 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)4 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)4 SimplePagedResultsControl (com.unboundid.ldap.sdk.controls.SimplePagedResultsControl)4 ParseException (java.text.ParseException)4 AuthenticationException (org.gluu.persist.exception.operation.AuthenticationException)4 SearchScopeException (org.gluu.persist.exception.operation.SearchScopeException)4