Search in sources :

Example 1 with SearchException

use of org.gluu.persist.exception.operation.SearchException in project oxCore by GluuFederation.

the class LdapEntryManager method removeSubtreeThroughIteration.

private void removeSubtreeThroughIteration(String dn) {
    SearchResult searchResult = null;
    try {
        searchResult = this.ldapOperationService.search(dn, toLdapFilter(Filter.createPresenceFilter("objectClass")), 0, 0, null, "dn");
        if (!ResultCode.SUCCESS.equals(searchResult.getResultCode())) {
            throw new EntryPersistenceException(String.format("Failed to find sub-entries of entry '%s' for removal", dn));
        }
    } catch (SearchException ex) {
        throw new EntryPersistenceException(String.format("Failed to find sub-entries of entry '%s' for removal", dn), ex);
    }
    List<String> removeEntriesDn = new ArrayList<String>(searchResult.getEntryCount());
    for (SearchResultEntry searchResultEntry : searchResult.getSearchEntries()) {
        removeEntriesDn.add(searchResultEntry.getDN());
    }
    Collections.sort(removeEntriesDn, LINE_LENGHT_COMPARATOR);
    for (String removeEntryDn : removeEntriesDn) {
        remove(removeEntryDn);
    }
}
Also used : ArrayList(java.util.ArrayList) EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) SearchException(org.gluu.persist.exception.operation.SearchException) SearchResult(com.unboundid.ldap.sdk.SearchResult) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Example 2 with SearchException

use of org.gluu.persist.exception.operation.SearchException in project oxCore by GluuFederation.

the class LdapEntryManager method contains.

@Override
protected boolean contains(String baseDN, Filter filter, String[] objectClasses, String[] ldapReturnAttributes) {
    if (StringHelper.isEmptyString(baseDN)) {
        throw new MappingException("Base DN to check contain entries is null");
    }
    // Create filter
    Filter searchFilter;
    if (objectClasses.length > 0) {
        searchFilter = addObjectClassFilter(filter, objectClasses);
    } else {
        searchFilter = filter;
    }
    SearchResult searchResult = null;
    try {
        searchResult = this.ldapOperationService.search(baseDN, toLdapFilter(searchFilter), 1, 1, null, ldapReturnAttributes);
        if ((searchResult == null) || !ResultCode.SUCCESS.equals(searchResult.getResultCode())) {
            throw new EntryPersistenceException(String.format("Failed to find entry with baseDN: %s, filter: %s", baseDN, searchFilter));
        }
    } catch (SearchException ex) {
        if (!(ResultCode.NO_SUCH_OBJECT_INT_VALUE == ex.getResultCode())) {
            throw new EntryPersistenceException(String.format("Failed to find entry with baseDN: %s, filter: %s", baseDN, searchFilter), ex);
        }
    }
    return (searchResult != null) && (searchResult.getEntryCount() > 0);
}
Also used : Filter(org.gluu.search.filter.Filter) EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) SearchException(org.gluu.persist.exception.operation.SearchException) SearchResult(com.unboundid.ldap.sdk.SearchResult) MappingException(org.gluu.persist.exception.mapping.MappingException)

Example 3 with SearchException

use of org.gluu.persist.exception.operation.SearchException in project oxCore by GluuFederation.

the class LdapFilterConverter method convertToLdapFilter.

public com.unboundid.ldap.sdk.Filter convertToLdapFilter(Filter genericFilter) throws SearchException {
    FilterType type = genericFilter.getType();
    if (FilterType.RAW == type) {
        try {
            return com.unboundid.ldap.sdk.Filter.create(genericFilter.getFilterString());
        } catch (com.unboundid.ldap.sdk.LDAPException ex) {
            throw new SearchException("Failed to parse RAW Ldap filter", ex, ex.getResultCode().intValue());
        }
    }
    if ((FilterType.NOT == type) || (FilterType.AND == type) || (FilterType.OR == type)) {
        Filter[] genericFilters = genericFilter.getFilters();
        com.unboundid.ldap.sdk.Filter[] ldapFilters = new com.unboundid.ldap.sdk.Filter[genericFilters.length];
        if (genericFilters != null) {
            for (int i = 0; i < genericFilters.length; i++) {
                ldapFilters[i] = convertToLdapFilter(genericFilters[i]);
            }
            if (FilterType.NOT == type) {
                return com.unboundid.ldap.sdk.Filter.createNOTFilter(ldapFilters[0]);
            } else if (FilterType.AND == type) {
                return com.unboundid.ldap.sdk.Filter.createANDFilter(ldapFilters);
            } else if (FilterType.OR == type) {
                return com.unboundid.ldap.sdk.Filter.createORFilter(ldapFilters);
            }
        }
    }
    if (FilterType.EQUALITY == type) {
        return com.unboundid.ldap.sdk.Filter.createEqualityFilter(genericFilter.getAttributeName(), genericFilter.getAssertionValue());
    }
    if (FilterType.LESS_OR_EQUAL == type) {
        return com.unboundid.ldap.sdk.Filter.createLessOrEqualFilter(genericFilter.getAttributeName(), genericFilter.getAssertionValue());
    }
    if (FilterType.GREATER_OR_EQUAL == type) {
        return com.unboundid.ldap.sdk.Filter.createGreaterOrEqualFilter(genericFilter.getAttributeName(), genericFilter.getAssertionValue());
    }
    if (FilterType.PRESENCE == type) {
        return com.unboundid.ldap.sdk.Filter.createPresenceFilter(genericFilter.getAttributeName());
    }
    if (FilterType.APPROXIMATE_MATCH == type) {
        return com.unboundid.ldap.sdk.Filter.createApproximateMatchFilter(genericFilter.getAttributeName(), genericFilter.getAssertionValue());
    }
    if (FilterType.SUBSTRING == type) {
        return com.unboundid.ldap.sdk.Filter.createSubstringFilter(genericFilter.getAttributeName(), genericFilter.getSubInitial(), genericFilter.getSubAny(), genericFilter.getSubFinal());
    }
    throw new SearchException(String.format("Unknown filter type '%s'", type), com.unboundid.ldap.sdk.ResultCode.PROTOCOL_ERROR_INT_VALUE);
}
Also used : FilterType(org.gluu.search.filter.FilterType) Filter(org.gluu.search.filter.Filter) SearchException(org.gluu.persist.exception.operation.SearchException)

Example 4 with SearchException

use of org.gluu.persist.exception.operation.SearchException in project oxTrust by GluuFederation.

the class CacheRefreshTimer method processImpl.

private void processImpl(CacheRefreshConfiguration cacheRefreshConfiguration, GluuConfiguration currentConfiguration) throws SearchException {
    CacheRefreshUpdateMethod updateMethod = getUpdateMethod(cacheRefreshConfiguration);
    // Prepare and check connections to LDAP servers
    LdapServerConnection[] sourceServerConnections = prepareLdapServerConnections(cacheRefreshConfiguration, cacheRefreshConfiguration.getSourceConfigs());
    LdapServerConnection inumDbServerConnection;
    if (cacheRefreshConfiguration.isDefaultInumServer()) {
        GluuLdapConfiguration ldapInumConfiguration = new GluuLdapConfiguration();
        ldapInumConfiguration.setConfigId("local_inum");
        ldapInumConfiguration.setBaseDNsStringsList(Arrays.asList(new String[] { OxTrustConstants.CACHE_REFRESH_DEFAULT_BASE_DN }));
        inumDbServerConnection = prepareLdapServerConnection(cacheRefreshConfiguration, ldapInumConfiguration, true);
    } else {
        inumDbServerConnection = prepareLdapServerConnection(cacheRefreshConfiguration, cacheRefreshConfiguration.getInumConfig());
    }
    boolean isVdsUpdate = CacheRefreshUpdateMethod.VDS.equals(updateMethod);
    LdapServerConnection targetServerConnection = null;
    if (isVdsUpdate) {
        targetServerConnection = prepareLdapServerConnection(cacheRefreshConfiguration, cacheRefreshConfiguration.getTargetConfig());
    }
    try {
        if ((sourceServerConnections == null) || (inumDbServerConnection == null) || (isVdsUpdate && (targetServerConnection == null))) {
            log.error("Skipping cache refresh due to invalid server configuration");
        } else {
            detectChangedEntries(cacheRefreshConfiguration, currentConfiguration, sourceServerConnections, inumDbServerConnection, targetServerConnection, updateMethod);
        }
    } finally {
        // Close connections to LDAP servers
        try {
            closeLdapServerConnection(sourceServerConnections);
        } catch (Exception e) {
        // Nothing can be done
        }
        if (!cacheRefreshConfiguration.isDefaultInumServer()) {
            try {
                closeLdapServerConnection(inumDbServerConnection);
            } catch (Exception e) {
            // Nothing can be done
            }
        }
        try {
            if (isVdsUpdate) {
                closeLdapServerConnection(targetServerConnection);
            }
        } catch (Exception e) {
        // Nothing can be done
        }
    }
    return;
}
Also used : GluuLdapConfiguration(org.gluu.model.ldap.GluuLdapConfiguration) EntryPersistenceException(org.gluu.persist.exception.EntryPersistenceException) SearchException(org.gluu.persist.exception.operation.SearchException) SocketException(java.net.SocketException) BasePersistenceException(org.gluu.persist.exception.BasePersistenceException)

Example 5 with SearchException

use of org.gluu.persist.exception.operation.SearchException 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)

Aggregations

SearchException (org.gluu.persist.exception.operation.SearchException)5 SearchResult (com.unboundid.ldap.sdk.SearchResult)3 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)2 ArrayList (java.util.ArrayList)2 EntryPersistenceException (org.gluu.persist.exception.mapping.EntryPersistenceException)2 MappingException (org.gluu.persist.exception.mapping.MappingException)2 Filter (org.gluu.search.filter.Filter)2 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)1 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)1 LDAPException (com.unboundid.ldap.sdk.LDAPException)1 LDAPSearchException (com.unboundid.ldap.sdk.LDAPSearchException)1 SearchRequest (com.unboundid.ldap.sdk.SearchRequest)1 SearchResultReference (com.unboundid.ldap.sdk.SearchResultReference)1 SimplePagedResultsControl (com.unboundid.ldap.sdk.controls.SimplePagedResultsControl)1 SocketException (java.net.SocketException)1 GluuLdapConfiguration (org.gluu.model.ldap.GluuLdapConfiguration)1 BasePersistenceException (org.gluu.persist.exception.BasePersistenceException)1 EntryPersistenceException (org.gluu.persist.exception.EntryPersistenceException)1 ConnectionException (org.gluu.persist.exception.operation.ConnectionException)1 DuplicateEntryException (org.gluu.persist.exception.operation.DuplicateEntryException)1