Search in sources :

Example 1 with AttributeDescription

use of org.forgerock.opendj.ldap.AttributeDescription in project OpenAM by OpenRock.

the class LdapTokenAttributeConversionTest method shouldStripObjectClass.

@Test
public void shouldStripObjectClass() {
    // Given
    Entry entry = mock(Entry.class);
    Attribute attribute = mock(Attribute.class);
    given(entry.getAttribute(anyString())).willReturn(attribute);
    AttributeDescription description = AttributeDescription.valueOf("badger");
    given(attribute.getAttributeDescription()).willReturn(description);
    // When
    LdapTokenAttributeConversion.stripObjectClass(entry);
    // Then
    verify(entry).removeAttribute(description);
}
Also used : LinkedHashMapEntry(org.forgerock.opendj.ldap.LinkedHashMapEntry) Entry(org.forgerock.opendj.ldap.Entry) Attribute(org.forgerock.opendj.ldap.Attribute) AttributeDescription(org.forgerock.opendj.ldap.AttributeDescription) Test(org.testng.annotations.Test)

Example 2 with AttributeDescription

use of org.forgerock.opendj.ldap.AttributeDescription in project OpenAM by OpenRock.

the class LdapTokenAttributeConversion method stripObjectClass.

/**
     * Only strips out the ObjectClass if it is present.
     *
     * @param entry Non null Entry to process.
     *
     * @return The Entry reference passed in.
     */
public static Entry stripObjectClass(Entry entry) {
    Attribute attribute = entry.getAttribute(CoreTokenConstants.OBJECT_CLASS);
    if (attribute != null) {
        AttributeDescription description = attribute.getAttributeDescription();
        entry.removeAttribute(description);
    }
    return entry;
}
Also used : Attribute(org.forgerock.opendj.ldap.Attribute) AttributeDescription(org.forgerock.opendj.ldap.AttributeDescription)

Example 3 with AttributeDescription

use of org.forgerock.opendj.ldap.AttributeDescription in project OpenAM by OpenRock.

the class LdapTokenAttributeConversion method mapFromEntry.

/**
     * Convert an Entry into a more convenient Mapping of CoreTokenField to Object.
     *
     * This function is important because no every operation with LDAP needs to return a
     * fully initialised Token. Instead users may be interested in only certain
     * attributes of the Token, and choose to query just those as a performance enhancement.
     *
     * @param entry Non null entry to convert.
     *
     * @return A mapping of zero or more CoreTokenFields to Objects.
     */
public Map<CoreTokenField, Object> mapFromEntry(Entry entry) {
    stripObjectClass(entry);
    Map<CoreTokenField, Object> r = new LinkedHashMap<>();
    for (Attribute a : entry.getAllAttributes()) {
        AttributeDescription description = a.getAttributeDescription();
        CoreTokenField field = CoreTokenField.fromLDAPAttribute(description.toString());
        // Special case for Token Type
        if (CoreTokenField.TOKEN_TYPE.equals(field)) {
            String value = entry.parseAttribute(description).asString();
            r.put(field, TokenType.valueOf(value));
            continue;
        }
        if (CoreTokenFieldTypes.isCalendar(field)) {
            String dateString = entry.parseAttribute(description).asString();
            Calendar calendar = conversion.fromLDAPDate(dateString);
            r.put(field, calendar);
        } else if (CoreTokenFieldTypes.isString(field)) {
            String value = entry.parseAttribute(description).asString();
            if (EMPTY.equals(value)) {
                value = "";
            }
            r.put(field, value);
        } else if (CoreTokenFieldTypes.isInteger(field)) {
            Integer value = entry.parseAttribute(description).asInteger();
            r.put(field, value);
        } else if (CoreTokenFieldTypes.isByteArray(field)) {
            byte[] data = entry.parseAttribute(description).asByteString().toByteArray();
            r.put(field, data);
        } else {
            throw new IllegalStateException();
        }
    }
    return r;
}
Also used : Attribute(org.forgerock.opendj.ldap.Attribute) Calendar(java.util.Calendar) CoreTokenField(org.forgerock.openam.tokens.CoreTokenField) LinkedHashMap(java.util.LinkedHashMap) AttributeDescription(org.forgerock.opendj.ldap.AttributeDescription)

Aggregations

Attribute (org.forgerock.opendj.ldap.Attribute)3 AttributeDescription (org.forgerock.opendj.ldap.AttributeDescription)3 Calendar (java.util.Calendar)1 LinkedHashMap (java.util.LinkedHashMap)1 CoreTokenField (org.forgerock.openam.tokens.CoreTokenField)1 Entry (org.forgerock.opendj.ldap.Entry)1 LinkedHashMapEntry (org.forgerock.opendj.ldap.LinkedHashMapEntry)1 Test (org.testng.annotations.Test)1