Search in sources :

Example 36 with MappingException

use of io.jans.orm.exception.MappingException in project jans by JanssenProject.

the class BaseEntryManager method getEntrySortByProperties.

protected String[] getEntrySortByProperties(Class<?> entryClass) {
    if (entryClass == null) {
        throw new MappingException("Entry class is null");
    }
    // Check if entry is LDAP Entry
    List<Annotation> entryAnnotations = ReflectHelper.getClassAnnotations(entryClass, LDAP_ENTRY_TYPE_ANNOTATIONS);
    Annotation annotation = ReflectHelper.getAnnotationByType(entryAnnotations, DataEntry.class);
    if (annotation == null) {
        return null;
    }
    return ((DataEntry) annotation).sortBy();
}
Also used : DataEntry(io.jans.orm.annotation.DataEntry) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation) Annotation(java.lang.annotation.Annotation) MappingException(io.jans.orm.exception.MappingException)

Example 37 with MappingException

use of io.jans.orm.exception.MappingException in project jans by JanssenProject.

the class BaseEntryManager method getEntrySortByNames.

protected String[] getEntrySortByNames(Class<?> entryClass) {
    if (entryClass == null) {
        throw new MappingException("Entry class is null");
    }
    // Check if entry is LDAP Entry
    List<Annotation> entryAnnotations = ReflectHelper.getClassAnnotations(entryClass, LDAP_ENTRY_TYPE_ANNOTATIONS);
    Annotation annotation = ReflectHelper.getAnnotationByType(entryAnnotations, DataEntry.class);
    if (annotation == null) {
        return null;
    }
    return ((DataEntry) annotation).sortByName();
}
Also used : DataEntry(io.jans.orm.annotation.DataEntry) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation) Annotation(java.lang.annotation.Annotation) MappingException(io.jans.orm.exception.MappingException)

Example 38 with MappingException

use of io.jans.orm.exception.MappingException in project jans by JanssenProject.

the class SpannerEntryManager method createEntities.

protected <T> List<T> createEntities(Class<T> entryClass, List<PropertyAnnotation> propertiesAnnotations, ParsedKey baseDn, EntryData... searchResultEntries) {
    List<T> result = new ArrayList<T>(searchResultEntries.length);
    Map<String, List<AttributeData>> entriesAttributes = new LinkedHashMap<String, List<AttributeData>>(100);
    int count = 0;
    for (int i = 0; i < searchResultEntries.length; i++) {
        count++;
        EntryData entryData = searchResultEntries[i];
        AttributeData attributeDataDn = entryData.getAttributeDate(SpannerOperationService.DN);
        if ((attributeDataDn == null) || (attributeDataDn.getValue() == null)) {
            throw new MappingException("Failed to convert EntryData to Entry because DN is missing");
        }
        entriesAttributes.put(attributeDataDn.getValue().toString(), entryData.getAttributeData());
        // Remove reference to allow java clean up object
        searchResultEntries[i] = null;
        // Allow java to clean up temporary objects
        if (count >= 100) {
            List<T> currentResult = createEntities(entryClass, propertiesAnnotations, entriesAttributes);
            result.addAll(currentResult);
            entriesAttributes = new LinkedHashMap<String, List<AttributeData>>(100);
            count = 0;
        }
    }
    List<T> currentResult = createEntities(entryClass, propertiesAnnotations, entriesAttributes);
    result.addAll(currentResult);
    return result;
}
Also used : EntryData(io.jans.orm.model.EntryData) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) AttributeData(io.jans.orm.model.AttributeData) LinkedHashMap(java.util.LinkedHashMap) MappingException(io.jans.orm.exception.MappingException)

Example 39 with MappingException

use of io.jans.orm.exception.MappingException in project jans by JanssenProject.

the class LdapEntryManager method findEntries.

@Override
public <T> List<T> findEntries(String baseDN, Class<T> entryClass, Filter filter, SearchScope scope, String[] ldapReturnAttributes, BatchOperation<T> batchOperation, int start, int count, int chunkSize) {
    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 = getAttributes(null, propertiesAnnotations, false);
    }
    // Find entries
    Filter searchFilter;
    if (objectClasses.length > 0) {
        searchFilter = addObjectClassFilter(filter, objectClasses);
    } else {
        searchFilter = filter;
    }
    SearchResult searchResult = null;
    try {
        LdapBatchOperationWraper<T> batchOperationWraper = new LdapBatchOperationWraper<T>(batchOperation, this, entryClass, propertiesAnnotations);
        searchResult = getOperationService().search(baseDN, toLdapFilter(searchFilter), toLdapSearchScope(scope), batchOperationWraper, start, chunkSize, count, null, currentLdapReturnAttributes);
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to find entries with baseDN: %s, filter: %s", baseDN, searchFilter), ex);
    }
    if (!ResultCode.SUCCESS.equals(searchResult.getResultCode())) {
        throw new EntryPersistenceException(String.format("Failed to find entries with baseDN: %s, filter: %s", baseDN, searchFilter));
    }
    if (searchResult.getEntryCount() == 0) {
        return new ArrayList<T>(0);
    }
    List<T> entries = createEntities(entryClass, propertiesAnnotations, searchResult.getSearchEntries().toArray(new SearchResultEntry[searchResult.getSearchEntries().size()]));
    // Default sort if needed
    sortEntriesIfNeeded(entryClass, entries);
    return entries;
}
Also used : ArrayList(java.util.ArrayList) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) SearchResult(com.unboundid.ldap.sdk.SearchResult) MappingException(io.jans.orm.exception.MappingException) ParseException(java.text.ParseException) EntryDeleteException(io.jans.orm.exception.EntryDeleteException) SearchScopeException(io.jans.orm.exception.operation.SearchScopeException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) ConnectionException(io.jans.orm.exception.operation.ConnectionException) SearchException(io.jans.orm.exception.operation.SearchException) AuthenticationException(io.jans.orm.exception.AuthenticationException) MappingException(io.jans.orm.exception.MappingException) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation) Filter(io.jans.orm.search.filter.Filter) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Example 40 with MappingException

use of io.jans.orm.exception.MappingException in project jans by JanssenProject.

the class LdapEntryManager method authenticate.

@Override
public <T> boolean authenticate(String baseDN, Class<T> entryClass, String userName, String password) {
    if (StringHelper.isEmptyString(baseDN)) {
        throw new MappingException("Base DN to count entries is null");
    }
    // Check entry class
    checkEntryClass(entryClass, false);
    String[] objectClasses = getTypeObjectClasses(entryClass);
    // Find entries
    Filter searchFilter = Filter.createEqualityFilter(LdapOperationService.UID, userName);
    if (objectClasses.length > 0) {
        searchFilter = addObjectClassFilter(searchFilter, objectClasses);
    }
    SearchScope scope = SearchScope.SUB;
    try {
        SearchResult searchResult = getOperationService().search(baseDN, toLdapFilter(searchFilter), toLdapSearchScope(scope), null, 0, 1, 1, null, LdapOperationService.UID_ARRAY);
        if ((searchResult == null) || (searchResult.getEntryCount() != 1)) {
            return false;
        }
        String bindDn = searchResult.getSearchEntries().get(0).getDN();
        return getOperationService().authenticate(bindDn, password, null);
    } catch (ConnectionException ex) {
        throw new AuthenticationException(String.format("Failed to authenticate user: %s", userName), ex);
    } catch (SearchScopeException ex) {
        throw new AuthenticationException(String.format("Failed to convert scope: %s", scope), ex);
    } catch (SearchException ex) {
        throw new AuthenticationException(String.format("Failed to find user DN: %s", userName), ex);
    }
}
Also used : Filter(io.jans.orm.search.filter.Filter) AuthenticationException(io.jans.orm.exception.AuthenticationException) SearchScope(io.jans.orm.model.SearchScope) SearchException(io.jans.orm.exception.operation.SearchException) SearchResult(com.unboundid.ldap.sdk.SearchResult) ConnectionException(io.jans.orm.exception.operation.ConnectionException) MappingException(io.jans.orm.exception.MappingException) SearchScopeException(io.jans.orm.exception.operation.SearchScopeException)

Aggregations

MappingException (io.jans.orm.exception.MappingException)43 PropertyAnnotation (io.jans.orm.reflect.property.PropertyAnnotation)27 EntryPersistenceException (io.jans.orm.exception.EntryPersistenceException)19 SearchException (io.jans.orm.exception.operation.SearchException)18 AuthenticationException (io.jans.orm.exception.AuthenticationException)16 Filter (io.jans.orm.search.filter.Filter)16 AttributeData (io.jans.orm.model.AttributeData)15 EntryDeleteException (io.jans.orm.exception.EntryDeleteException)14 JsonObject (io.jans.orm.annotation.JsonObject)12 ArrayList (java.util.ArrayList)11 SearchScopeException (io.jans.orm.exception.operation.SearchScopeException)7 Annotation (java.lang.annotation.Annotation)7 SearchResult (com.unboundid.ldap.sdk.SearchResult)6 ConnectionException (io.jans.orm.exception.operation.ConnectionException)6 EntryData (io.jans.orm.model.EntryData)6 DateTimeParseException (java.time.format.DateTimeParseException)6 List (java.util.List)6 Getter (io.jans.orm.reflect.property.Getter)5 ParseException (java.text.ParseException)5 DataEntry (io.jans.orm.annotation.DataEntry)4