Search in sources :

Example 16 with EntryPersistenceException

use of org.gluu.persist.exception.mapping.EntryPersistenceException in project oxTrust by GluuFederation.

the class CleanUpTest method cleanUpGroups.

/**
 * Test search
 *
 * @throws Exception
 */
// @Test
@Parameters(value = "test.keep.persons")
public void cleanUpGroups(String usedGroups) throws Exception {
    System.out.println("cleanup person Test initialted ");
    assertNotNull(usedGroups);
    List<String> usedGroupsList = Arrays.asList(StringHelper.split(usedGroups, ",", true, false));
    System.out.println("Used Groups: " + usedGroupsList);
    int groupsResultSetSize = 50;
    int countResults = 0;
    int countRemoved = 0;
    boolean existsMoreGroups = true;
    while (existsMoreGroups && countResults < 10000) {
        List<GluuGroup> groups = groupsService.getAllGroups();
        existsMoreGroups = groups.size() == groupsResultSetSize;
        countResults += groups.size();
        assertNotNull(groups);
        System.out.println("Found groups: " + groups.size());
        System.out.println("Total groups: " + countResults);
        for (GluuGroup group : groups) {
            // String clientId = person.getClientId();
            if (!usedGroupsList.contains(group.getInum())) {
                try {
                    groupsService.removeGroup(group);
                    countRemoved++;
                } catch (EntryPersistenceException ex) {
                    System.out.println("Failed to remove group: " + ex.getMessage());
                }
            }
        }
    }
    System.out.println("Removed Persons: " + countRemoved);
}
Also used : EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) GluuGroup(org.gluu.oxtrust.model.GluuGroup) Parameters(org.testng.annotations.Parameters)

Example 17 with EntryPersistenceException

use of org.gluu.persist.exception.mapping.EntryPersistenceException in project oxCore by GluuFederation.

the class LdapEntryManager method countEntries.

@Override
public <T> int countEntries(String baseDN, Class<T> entryClass, Filter filter) {
    if (StringHelper.isEmptyString(baseDN)) {
        throw new MappingException("Base DN to find entries is null");
    }
    // Check entry class
    checkEntryClass(entryClass, false);
    String[] objectClasses = getTypeObjectClasses(entryClass);
    // Don't load attributes
    String[] ldapReturnAttributes = new String[] { "" };
    // Find entries
    Filter searchFilter;
    if (objectClasses.length > 0) {
        searchFilter = addObjectClassFilter(filter, objectClasses);
    } else {
        searchFilter = filter;
    }
    CountBatchOperation<T> batchOperation = new CountBatchOperation<T>();
    try {
        LdapBatchOperationWraper<T> batchOperationWraper = new LdapBatchOperationWraper<T>(batchOperation);
        ldapOperationService.search(baseDN, toLdapFilter(searchFilter), toLdapSearchScope(SearchScope.SUB), batchOperationWraper, 0, 100, 0, null, ldapReturnAttributes);
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to calucalte count of entries with baseDN: %s, filter: %s", baseDN, searchFilter), ex);
    }
    return batchOperation.getCountEntries();
}
Also used : Filter(org.gluu.search.filter.Filter) EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) SearchException(org.gluu.persist.exception.operation.SearchException) AuthenticationException(org.gluu.persist.exception.operation.AuthenticationException) MappingException(org.gluu.persist.exception.mapping.MappingException) SearchScopeException(org.gluu.persist.exception.operation.SearchScopeException) ParseException(java.text.ParseException) EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) ConnectionException(org.gluu.persist.exception.operation.ConnectionException) MappingException(org.gluu.persist.exception.mapping.MappingException)

Example 18 with EntryPersistenceException

use of org.gluu.persist.exception.mapping.EntryPersistenceException in project oxCore by GluuFederation.

the class LdapEntryManager method merge.

@Override
public void merge(String dn, List<AttributeDataModification> attributeDataModifications) {
    // Update entry
    try {
        List<Modification> modifications = new ArrayList<Modification>(attributeDataModifications.size());
        for (AttributeDataModification attributeDataModification : attributeDataModifications) {
            AttributeData attribute = attributeDataModification.getAttribute();
            AttributeData oldAttribute = attributeDataModification.getOldAttribute();
            String attributeName = null;
            String[] attributeValues = null;
            if (attribute != null) {
                attributeName = attribute.getName();
                attributeValues = attribute.getValues();
            }
            String oldAttributeName = null;
            String[] oldAttributeValues = null;
            if (oldAttribute != null) {
                oldAttributeName = oldAttribute.getName();
                oldAttributeValues = oldAttribute.getValues();
            }
            Modification modification = null;
            if (AttributeModificationType.ADD.equals(attributeDataModification.getModificationType())) {
                modification = createModification(ModificationType.ADD, attributeName, attributeValues);
            } else {
                if (AttributeModificationType.REMOVE.equals(attributeDataModification.getModificationType())) {
                    modification = createModification(ModificationType.DELETE, oldAttributeName, oldAttributeValues);
                } else if (AttributeModificationType.REPLACE.equals(attributeDataModification.getModificationType())) {
                    if (attributeValues.length == 1) {
                        modification = createModification(ModificationType.REPLACE, attributeName, attributeValues);
                    } else {
                        String[] oldValues = ArrayHelper.arrayClone(oldAttributeValues);
                        String[] newValues = ArrayHelper.arrayClone(attributeValues);
                        Arrays.sort(oldValues);
                        Arrays.sort(newValues);
                        boolean[] retainOldValues = new boolean[oldValues.length];
                        Arrays.fill(retainOldValues, false);
                        List<String> addValues = new ArrayList<String>();
                        List<String> removeValues = new ArrayList<String>();
                        // Add new values
                        for (String value : newValues) {
                            int idx = Arrays.binarySearch(oldValues, value, new Comparator<String>() {

                                @Override
                                public int compare(String o1, String o2) {
                                    return o1.toLowerCase().compareTo(o2.toLowerCase());
                                }
                            });
                            if (idx >= 0) {
                                // Old values array contains new value. Retain
                                // old value
                                retainOldValues[idx] = true;
                            } else {
                                // This is new value
                                addValues.add(value);
                            }
                        }
                        // Remove values which we don't have in new values
                        for (int i = 0; i < oldValues.length; i++) {
                            if (!retainOldValues[i]) {
                                removeValues.add(oldValues[i]);
                            }
                        }
                        if (removeValues.size() > 0) {
                            Modification removeModification = createModification(ModificationType.DELETE, attributeName, removeValues.toArray(new String[removeValues.size()]));
                            modifications.add(removeModification);
                        }
                        if (addValues.size() > 0) {
                            Modification addModification = createModification(ModificationType.ADD, attributeName, addValues.toArray(new String[addValues.size()]));
                            modifications.add(addModification);
                        }
                    }
                }
            }
            if (modification != null) {
                modifications.add(modification);
            }
        }
        if (modifications.size() > 0) {
            boolean result = this.ldapOperationService.updateEntry(dn, modifications);
            if (!result) {
                throw new EntryPersistenceException(String.format("Failed to update entry: %s", dn));
            }
        }
    } catch (ConnectionException ex) {
        throw new EntryPersistenceException(String.format("Failed to update entry: %s", dn), ex.getCause());
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to update entry: %s", dn), ex);
    }
}
Also used : AttributeDataModification(org.gluu.persist.model.AttributeDataModification) Modification(com.unboundid.ldap.sdk.Modification) ArrayList(java.util.ArrayList) EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) SearchException(org.gluu.persist.exception.operation.SearchException) AuthenticationException(org.gluu.persist.exception.operation.AuthenticationException) MappingException(org.gluu.persist.exception.mapping.MappingException) SearchScopeException(org.gluu.persist.exception.operation.SearchScopeException) ParseException(java.text.ParseException) EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) ConnectionException(org.gluu.persist.exception.operation.ConnectionException) Comparator(java.util.Comparator) AttributeDataModification(org.gluu.persist.model.AttributeDataModification) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) AttributeData(org.gluu.persist.model.AttributeData) ConnectionException(org.gluu.persist.exception.operation.ConnectionException)

Example 19 with EntryPersistenceException

use of org.gluu.persist.exception.mapping.EntryPersistenceException in project oxCore by GluuFederation.

the class LdapEntryManager method findEntriesVirtualListView.

@Deprecated
public <T> List<T> findEntriesVirtualListView(String baseDN, Class<T> entryClass, Filter filter, int startIndex, int count, String sortBy, SortOrder sortOrder, ListViewResponse vlvResponse, String[] ldapReturnAttributes) {
    if (StringHelper.isEmptyString(baseDN)) {
        throw new MappingException("Base DN to find entries is null");
    }
    // Check entry class
    checkEntryClass(entryClass, false);
    String[] objectClasses = getTypeObjectClasses(entryClass);
    List<PropertyAnnotation> propertiesAnnotations = getEntryPropertyAnnotations(entryClass);
    String[] currentLdapReturnAttributes = ldapReturnAttributes;
    if (ArrayHelper.isEmpty(currentLdapReturnAttributes)) {
        currentLdapReturnAttributes = getLdapAttributes(null, propertiesAnnotations, false);
    }
    // Find entries
    Filter searchFilter;
    if (objectClasses.length > 0) {
        searchFilter = addObjectClassFilter(filter, objectClasses);
    } else {
        searchFilter = filter;
    }
    SearchResult searchResult = null;
    try {
        searchResult = this.ldapOperationService.searchVirtualListView(baseDN, toLdapFilter(searchFilter), toLdapSearchScope(SearchScope.SUB), startIndex, count, sortBy, sortOrder, vlvResponse, currentLdapReturnAttributes);
        if (!ResultCode.SUCCESS.equals(searchResult.getResultCode())) {
            throw new EntryPersistenceException(String.format("Failed to find entries with baseDN: %s, filter: %s", baseDN, searchFilter));
        }
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to find entries with baseDN: %s, filter: %s", baseDN, searchFilter), ex);
    }
    if (searchResult.getEntryCount() == 0) {
        return new ArrayList<T>(0);
    }
    List<T> entries = createEntitiesVirtualListView(entryClass, propertiesAnnotations, searchResult.getSearchEntries().toArray(new SearchResultEntry[searchResult.getSearchEntries().size()]));
    return entries;
}
Also used : ArrayList(java.util.ArrayList) EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) SearchResult(com.unboundid.ldap.sdk.SearchResult) SearchException(org.gluu.persist.exception.operation.SearchException) AuthenticationException(org.gluu.persist.exception.operation.AuthenticationException) MappingException(org.gluu.persist.exception.mapping.MappingException) SearchScopeException(org.gluu.persist.exception.operation.SearchScopeException) ParseException(java.text.ParseException) EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) ConnectionException(org.gluu.persist.exception.operation.ConnectionException) MappingException(org.gluu.persist.exception.mapping.MappingException) PropertyAnnotation(org.gluu.persist.model.PropertyAnnotation) Filter(org.gluu.search.filter.Filter) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Aggregations

EntryPersistenceException (org.gluu.persist.exception.mapping.EntryPersistenceException)19 SearchException (org.gluu.persist.exception.operation.SearchException)11 MappingException (org.gluu.persist.exception.mapping.MappingException)10 ParseException (java.text.ParseException)9 AuthenticationException (org.gluu.persist.exception.operation.AuthenticationException)9 ConnectionException (org.gluu.persist.exception.operation.ConnectionException)9 SearchScopeException (org.gluu.persist.exception.operation.SearchScopeException)9 ArrayList (java.util.ArrayList)8 SearchResult (com.unboundid.ldap.sdk.SearchResult)7 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)7 Filter (org.gluu.search.filter.Filter)7 GluuCustomPerson (org.gluu.oxtrust.model.GluuCustomPerson)3 AttributeData (org.gluu.persist.model.AttributeData)3 PropertyAnnotation (org.gluu.persist.model.PropertyAnnotation)3 List (java.util.List)2 GluuGroup (org.gluu.oxtrust.model.GluuGroup)2 Parameters (org.testng.annotations.Parameters)2 Attribute (com.unboundid.ldap.sdk.Attribute)1 Modification (com.unboundid.ldap.sdk.Modification)1 SocketException (java.net.SocketException)1