Search in sources :

Example 6 with MappingException

use of org.gluu.site.ldap.persistence.exception.MappingException in project oxCore by GluuFederation.

the class LdapEntryManager method findEntries.

public <T> List<T> findEntries(String baseDN, Class<T> entryClass, Filter filter, SearchScope scope, String[] ldapReturnAttributes, BatchOperation<T> batchOperation, int startIndex, int searchLimit, int sizeLimit) {
    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.search(baseDN, searchFilter, scope, batchOperation, startIndex, searchLimit, sizeLimit, null, 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 = createEntities(entryClass, propertiesAnnotations, searchResult.getSearchEntries().toArray(new SearchResultEntry[searchResult.getSearchEntries().size()]));
    // Default sort if needed
    sortEntriesIfNeeded(entryClass, entries);
    return entries;
}
Also used : EmptyEntryPersistenceException(org.gluu.site.ldap.persistence.exception.EmptyEntryPersistenceException) EntryPersistenceException(org.gluu.site.ldap.persistence.exception.EntryPersistenceException) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) EmptyEntryPersistenceException(org.gluu.site.ldap.persistence.exception.EmptyEntryPersistenceException) MappingException(org.gluu.site.ldap.persistence.exception.MappingException) AuthenticationException(org.gluu.site.ldap.persistence.exception.AuthenticationException) InvalidArgumentException(org.gluu.site.ldap.persistence.exception.InvalidArgumentException) ParseException(java.text.ParseException) EntryPersistenceException(org.gluu.site.ldap.persistence.exception.EntryPersistenceException) ConnectionException(org.gluu.site.ldap.exception.ConnectionException) MappingException(org.gluu.site.ldap.persistence.exception.MappingException)

Example 7 with MappingException

use of org.gluu.site.ldap.persistence.exception.MappingException in project oxCore by GluuFederation.

the class AbstractEntryManager method contains.

public boolean contains(Object entry) {
    if (entry == null) {
        throw new MappingException("Entry to persist is null");
    }
    // Check entry class
    Class<?> entryClass = entry.getClass();
    checkEntryClass(entryClass, false);
    String[] objectClasses = getObjectClasses(entry, entryClass);
    List<PropertyAnnotation> propertiesAnnotations = getEntryPropertyAnnotations(entryClass);
    Object dnValue = getDNValue(entry, entryClass);
    List<AttributeData> attributes = getAttributesListForPersist(entry, propertiesAnnotations);
    String[] ldapReturnAttributes = getLdapAttributes(null, propertiesAnnotations, false);
    return contains(dnValue.toString(), attributes, objectClasses, ldapReturnAttributes);
}
Also used : LdapJsonObject(org.gluu.site.ldap.persistence.annotation.LdapJsonObject) MappingException(org.gluu.site.ldap.persistence.exception.MappingException)

Example 8 with MappingException

use of org.gluu.site.ldap.persistence.exception.MappingException in project oxCore by GluuFederation.

the class AbstractEntryManager method getEntrySortBy.

protected String[] getEntrySortBy(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, LdapEntry.class);
    if (annotation == null) {
        return null;
    }
    return ((LdapEntry) annotation).sortBy();
}
Also used : LdapEntry(org.gluu.site.ldap.persistence.annotation.LdapEntry) Annotation(java.lang.annotation.Annotation) MappingException(org.gluu.site.ldap.persistence.exception.MappingException)

Example 9 with MappingException

use of org.gluu.site.ldap.persistence.exception.MappingException in project oxCore by GluuFederation.

the class AbstractEntryManager method getAttribute.

private AttributeData getAttribute(String propertyName, String ldapAttributeName, Getter propertyValueGetter, Object entry, boolean jsonObject) {
    Object propertyValue = propertyValueGetter.get(entry);
    if (propertyValue == null) {
        return null;
    }
    String[] attributeValues = new String[1];
    if (propertyValue instanceof String) {
        attributeValues[0] = StringHelper.toString(propertyValue);
    } else if (propertyValue instanceof Boolean) {
        attributeValues[0] = propertyValue.toString();
    } else if (propertyValue instanceof Integer) {
        attributeValues[0] = propertyValue.toString();
    } else if (propertyValue instanceof Long) {
        attributeValues[0] = propertyValue.toString();
    } else if (propertyValue instanceof Date) {
        attributeValues[0] = encodeGeneralizedTime((Date) propertyValue);
    } else if (propertyValue instanceof String[]) {
        attributeValues = (String[]) propertyValue;
    } else if (propertyValue instanceof List<?>) {
        attributeValues = new String[((List<?>) propertyValue).size()];
        int index = 0;
        for (Object tmpPropertyValue : (List<?>) propertyValue) {
            if (jsonObject) {
                attributeValues[index++] = convertJsonToString(tmpPropertyValue);
            } else {
                attributeValues[index++] = StringHelper.toString(tmpPropertyValue);
            }
        }
    } else if (propertyValue instanceof LdapEnum) {
        attributeValues[0] = ((LdapEnum) propertyValue).getValue();
    } else if (propertyValue instanceof LdapEnum[]) {
        LdapEnum[] propertyValues = (LdapEnum[]) propertyValue;
        attributeValues = new String[propertyValues.length];
        for (int i = 0; i < propertyValues.length; i++) {
            attributeValues[i] = (propertyValues[i] == null) ? null : propertyValues[i].getValue();
        }
    } else if (jsonObject) {
        attributeValues[0] = convertJsonToString(propertyValue);
    } else {
        throw new MappingException("Entry property '" + propertyName + "' should has getter with String, String[], Boolean, Integer, Long, Date, List, LdapEnum or LdapEnum[] return type or has annotation LdapJsonObject");
    }
    if (log.isDebugEnabled()) {
        log.debug(String.format("Property: %s, LdapProperty: %s, PropertyValue: %s", propertyName, ldapAttributeName, Arrays.toString(attributeValues)));
    }
    if (attributeValues.length == 0) {
        attributeValues = new String[] {};
    } else if ((attributeValues.length == 1) && StringHelper.isEmpty(attributeValues[0])) {
        return null;
    }
    return new AttributeData(ldapAttributeName, attributeValues);
}
Also used : MappingException(org.gluu.site.ldap.persistence.exception.MappingException) LdapEnum(org.gluu.site.ldap.persistence.annotation.LdapEnum) LdapJsonObject(org.gluu.site.ldap.persistence.annotation.LdapJsonObject) LdapAttributesList(org.gluu.site.ldap.persistence.annotation.LdapAttributesList)

Example 10 with MappingException

use of org.gluu.site.ldap.persistence.exception.MappingException in project oxCore by GluuFederation.

the class AbstractEntryManager method setCustomObjectClasses.

protected void setCustomObjectClasses(Object entry, Class<?> entryClass, String[] objectClasses) {
    List<PropertyAnnotation> customObjectAnnotations = getEntryCustomObjectClassAnnotations(entryClass);
    for (PropertyAnnotation propertiesAnnotation : customObjectAnnotations) {
        String propertyName = propertiesAnnotation.getPropertyName();
        Setter setter = getSetter(entryClass, propertyName);
        if (setter == null) {
            throw new MappingException("Entry should has setter for property " + propertyName);
        }
        AttributeData attribute = new AttributeData(propertyName, objectClasses);
        setPropertyValue(propertyName, setter, entry, attribute, false);
        break;
    }
}
Also used : Setter(org.gluu.site.ldap.persistence.property.Setter) MappingException(org.gluu.site.ldap.persistence.exception.MappingException)

Aggregations

MappingException (org.gluu.site.ldap.persistence.exception.MappingException)21 LdapJsonObject (org.gluu.site.ldap.persistence.annotation.LdapJsonObject)10 EntryPersistenceException (org.gluu.site.ldap.persistence.exception.EntryPersistenceException)9 EmptyEntryPersistenceException (org.gluu.site.ldap.persistence.exception.EmptyEntryPersistenceException)6 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)5 Annotation (java.lang.annotation.Annotation)5 ParseException (java.text.ParseException)5 ConnectionException (org.gluu.site.ldap.exception.ConnectionException)5 AuthenticationException (org.gluu.site.ldap.persistence.exception.AuthenticationException)5 InvalidArgumentException (org.gluu.site.ldap.persistence.exception.InvalidArgumentException)5 LdapAttributesList (org.gluu.site.ldap.persistence.annotation.LdapAttributesList)4 Getter (org.gluu.site.ldap.persistence.property.Getter)4 LdapAttribute (org.gluu.site.ldap.persistence.annotation.LdapAttribute)3 LdapEnum (org.gluu.site.ldap.persistence.annotation.LdapEnum)2 Setter (org.gluu.site.ldap.persistence.property.Setter)2 SimplePagedResultsControl (com.unboundid.ldap.sdk.controls.SimplePagedResultsControl)1 Method (java.lang.reflect.Method)1 AttributeModificationType (org.gluu.site.ldap.persistence.AttributeDataModification.AttributeModificationType)1 LdapEntry (org.gluu.site.ldap.persistence.annotation.LdapEntry)1