From 58ffacee660a4eb792f49dd311a4e2ddda05afad Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Fri, 25 May 2012 22:12:32 +0000
Subject: [PATCH] Checkpoint local changes: add more attribute mapping support.

---
 opendj3/opendj-rest2ldap/src/main/java/org/forgerock/opendj/rest2ldap/SimpleAttributeMapper.java |   59 ++++++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 46 insertions(+), 13 deletions(-)

diff --git a/opendj3/opendj-rest2ldap/src/main/java/org/forgerock/opendj/rest2ldap/SimpleAttributeMapper.java b/opendj3/opendj-rest2ldap/src/main/java/org/forgerock/opendj/rest2ldap/SimpleAttributeMapper.java
index 3d3b5bf..0c92b63 100644
--- a/opendj3/opendj-rest2ldap/src/main/java/org/forgerock/opendj/rest2ldap/SimpleAttributeMapper.java
+++ b/opendj3/opendj-rest2ldap/src/main/java/org/forgerock/opendj/rest2ldap/SimpleAttributeMapper.java
@@ -16,7 +16,7 @@
 
 package org.forgerock.opendj.rest2ldap;
 
-import static org.forgerock.opendj.rest2ldap.Utils.attributeToJson;
+import static org.forgerock.opendj.rest2ldap.Utils.byteStringToJson;
 import static org.forgerock.opendj.rest2ldap.Utils.toLowerCase;
 
 import java.util.Collections;
@@ -27,7 +27,10 @@
 import org.forgerock.json.fluent.JsonPointer;
 import org.forgerock.json.fluent.JsonValue;
 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;
 
 /**
@@ -39,6 +42,11 @@
     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;
+
     /**
      * Creates a new simple attribute mapper which maps a single LDAP attribute
      * to an entry.
@@ -61,26 +69,44 @@
      */
     public SimpleAttributeMapper(String jsonAttributeName, String ldapAttributeName) {
         this.jsonAttributeName = jsonAttributeName;
-        this.normalizedJsonAttributeName = toLowerCase(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) {
+        this.forceSingleValued = singleValued;
+        return this;
+    }
+
+    public SimpleAttributeMapper withDecoder(Function<ByteString, ?, Void> f) {
+        this.decoder = f;
+        return this;
     }
 
     /**
      * {@inheritDoc}
      */
-    public void getLDAPAttributes(Set<String> ldapAttributes) {
-        ldapAttributes.add(ldapAttributeName);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public void getLDAPAttributes(Set<String> ldapAttributes, JsonPointer resourceAttribute) {
-        if (toLowerCase(resourceAttribute.leaf()).equals(normalizedJsonAttributeName)) {
+    public void getLDAPAttributes(JsonPointer jsonAttribute, Set<String> ldapAttributes) {
+        if (attributeMatchesPointer(jsonAttribute)) {
             ldapAttributes.add(ldapAttributeName);
         }
     }
 
+    private boolean attributeMatchesPointer(JsonPointer resourceAttribute) {
+        return resourceAttribute.isEmpty()
+                || toLowerCase(resourceAttribute.get(0)).equals(normalizedJsonAttributeName);
+    }
+
     /**
      * {@inheritDoc}
      */
@@ -88,8 +114,15 @@
             final AttributeMapperCompletionHandler<Map<String, Object>> h) {
         Attribute a = e.getAttribute(ldapAttributeName);
         if (a != null) {
-            Map<String, Object> result =
-                    Collections.singletonMap(jsonAttributeName, attributeToJson(a));
+            Function<ByteString, ?, Void> f =
+                    decoder == null ? Functions.fixedFunction(byteStringToJson(), a) : decoder;
+            final Object value;
+            if (forceSingleValued || a.getAttributeDescription().getAttributeType().isSingleValue()) {
+                value = a.parse().as(f, defaultValue);
+            } else {
+                value = a.parse().asSetOf(f, defaultValue);
+            }
+            Map<String, Object> result = Collections.singletonMap(jsonAttributeName, value);
             h.onSuccess(result);
         }
     }

--
Gitblit v1.10.0