mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Matthew Swift
20.10.2012 db786032bf45be89c4a893281911364d158cfb6e
opendj3/opendj-rest2ldap/src/main/java/org/forgerock/opendj/rest2ldap/SimpleAttributeMapper.java
@@ -26,26 +26,27 @@
import org.forgerock.json.fluent.JsonPointer;
import org.forgerock.json.fluent.JsonValue;
import org.forgerock.json.resource.ResultHandler;
import org.forgerock.json.resource.ServerContext;
import org.forgerock.opendj.ldap.Attribute;
import org.forgerock.opendj.ldap.ByteString;
import org.forgerock.opendj.ldap.Entry;
import org.forgerock.opendj.ldap.Function;
import org.forgerock.opendj.ldap.Functions;
import org.forgerock.resource.provider.Context;
/**
 *
 */
public class SimpleAttributeMapper implements AttributeMapper {
    private final String ldapAttributeName;
    private final String jsonAttributeName;
    private final String normalizedJsonAttributeName;
    private boolean forceSingleValued = false;
    private Object defaultValue = null;
    private boolean isReadOnly = false;
    private Function<ByteString, ?, Void> decoder = null;
    private Object defaultValue = null;
    private boolean forceSingleValued = false;
    private boolean isReadOnly = false;
    private final String jsonAttributeName;
    private final String ldapAttributeName;
    private final String normalizedJsonAttributeName;
    /**
     * Creates a new simple attribute mapper which maps a single LDAP attribute
@@ -54,7 +55,7 @@
     * @param attributeName
     *            The name of the simple JSON and LDAP attribute.
     */
    public SimpleAttributeMapper(String attributeName) {
    public SimpleAttributeMapper(final String attributeName) {
        this(attributeName, attributeName);
    }
@@ -67,54 +68,38 @@
     * @param ldapAttributeName
     *            The name of the LDAP attribute.
     */
    public SimpleAttributeMapper(String jsonAttributeName, String ldapAttributeName) {
    public SimpleAttributeMapper(final String jsonAttributeName, final String ldapAttributeName) {
        this.jsonAttributeName = jsonAttributeName;
        this.ldapAttributeName = ldapAttributeName;
        this.normalizedJsonAttributeName = toLowerCase(jsonAttributeName);
    }
    public SimpleAttributeMapper withDefaultValue(Object defaultValue) {
        this.defaultValue = defaultValue;
        return this;
    }
    public SimpleAttributeMapper isReadOnly(boolean readOnly) {
        this.isReadOnly = readOnly;
        return this;
    }
    public SimpleAttributeMapper forceSingleValued(boolean singleValued) {
    public SimpleAttributeMapper forceSingleValued(final boolean singleValued) {
        this.forceSingleValued = singleValued;
        return this;
    }
    public SimpleAttributeMapper withDecoder(Function<ByteString, ?, Void> f) {
        this.decoder = f;
        return this;
    }
    /**
     * {@inheritDoc}
     */
    public void getLDAPAttributes(JsonPointer jsonAttribute, Set<String> ldapAttributes) {
    public void getLDAPAttributes(final JsonPointer jsonAttribute, final Set<String> ldapAttributes) {
        if (attributeMatchesPointer(jsonAttribute)) {
            ldapAttributes.add(ldapAttributeName);
        }
    }
    private boolean attributeMatchesPointer(JsonPointer resourceAttribute) {
        return resourceAttribute.isEmpty()
                || toLowerCase(resourceAttribute.get(0)).equals(normalizedJsonAttributeName);
    public SimpleAttributeMapper isReadOnly(final boolean readOnly) {
        this.isReadOnly = readOnly;
        return this;
    }
    /**
     * {@inheritDoc}
     */
    public void toJson(Context c, Entry e,
            final AttributeMapperCompletionHandler<Map<String, Object>> h) {
        Attribute a = e.getAttribute(ldapAttributeName);
    public void toJson(final ServerContext c, final Entry e, final ResultHandler<Map<String, Object>> h) {
        final Attribute a = e.getAttribute(ldapAttributeName);
        if (a != null) {
            Function<ByteString, ?, Void> f =
            final Function<ByteString, ?, Void> f =
                    decoder == null ? Functions.fixedFunction(byteStringToJson(), a) : decoder;
            final Object value;
            if (forceSingleValued || a.getAttributeDescription().getAttributeType().isSingleValue()) {
@@ -122,17 +107,32 @@
            } else {
                value = a.parse().asSetOf(f, defaultValue);
            }
            Map<String, Object> result = Collections.singletonMap(jsonAttributeName, value);
            h.onSuccess(result);
            final Map<String, Object> result = Collections.singletonMap(jsonAttributeName, value);
            h.handleResult(result);
        }
    }
    /**
     * {@inheritDoc}
     */
    public void toLDAP(Context c, JsonValue v, AttributeMapperCompletionHandler<List<Attribute>> h) {
    public void toLDAP(final ServerContext c, final JsonValue v, final ResultHandler<List<Attribute>> h) {
        // TODO Auto-generated method stub
    }
    public SimpleAttributeMapper withDecoder(final Function<ByteString, ?, Void> f) {
        this.decoder = f;
        return this;
    }
    public SimpleAttributeMapper withDefaultValue(final Object defaultValue) {
        this.defaultValue = defaultValue;
        return this;
    }
    private boolean attributeMatchesPointer(final JsonPointer resourceAttribute) {
        return resourceAttribute.isEmpty()
                || toLowerCase(resourceAttribute.get(0)).equals(normalizedJsonAttributeName);
    }
}