From 1bc6e4214cb328452836b2955a95c3d17a98cf79 Mon Sep 17 00:00:00 2001
From: Gaetan Boismal <gaetan.boismal@forgerock.com>
Date: Fri, 27 Nov 2015 13:01:58 +0000
Subject: [PATCH] OPENDJ-1901 PR-22 rest2ldap fix reference searchFilter

---
 opendj-sdk/opendj-rest2ldap/src/main/java/org/forgerock/opendj/rest2ldap/ReferenceAttributeMapper.java |   19 ++++++++++++-------
 1 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/opendj-sdk/opendj-rest2ldap/src/main/java/org/forgerock/opendj/rest2ldap/ReferenceAttributeMapper.java b/opendj-sdk/opendj-rest2ldap/src/main/java/org/forgerock/opendj/rest2ldap/ReferenceAttributeMapper.java
index f259cb2..cf0fd1c 100644
--- a/opendj-sdk/opendj-rest2ldap/src/main/java/org/forgerock/opendj/rest2ldap/ReferenceAttributeMapper.java
+++ b/opendj-sdk/opendj-rest2ldap/src/main/java/org/forgerock/opendj/rest2ldap/ReferenceAttributeMapper.java
@@ -319,9 +319,11 @@
                                            // Combine values into a single JSON array.
                                            final List<Object> result = new ArrayList<>(value.size());
                                            for (final JsonValue e : value) {
-                                               result.add(e.getObject());
+                                               if (e != null) {
+                                                   result.add(e.getObject());
+                                               }
                                            }
-                                           return new JsonValue(result);
+                                           return result.isEmpty() ? null : new JsonValue(result);
                                        }
                                    }
                                });
@@ -344,7 +346,11 @@
         return requestState.getConnection().thenAsync(new AsyncFunction<Connection, JsonValue, ResourceException>() {
             @Override
             public Promise<JsonValue, ResourceException> apply(Connection connection) throws ResourceException {
-                return connection.readEntryAsync(dn, requestedLDAPAttributes)
+                final Filter searchFilter = filter != null ? filter : Filter.alwaysTrue();
+                final String[] attributes = requestedLDAPAttributes.toArray(new String[requestedLDAPAttributes.size()]);
+                final SearchRequest request = newSearchRequest(dn, SearchScope.BASE_OBJECT, searchFilter, attributes);
+
+                return connection.searchSingleEntryAsync(request)
                         .thenAsync(new AsyncFunction<SearchResultEntry, JsonValue, ResourceException>() {
                             @Override
                             public Promise<JsonValue, ResourceException> apply(final SearchResultEntry result) {
@@ -353,12 +359,11 @@
                         }, new AsyncFunction<LdapException, JsonValue, ResourceException>() {
                             @Override
                             public Promise<JsonValue, ResourceException> apply(final LdapException error) {
-                                if (!(error instanceof EntryNotFoundException)) {
-                                    return Promises.newExceptionPromise(asResourceException(error));
-                                } else {
-                                    // The referenced entry does not exist so ignore it since it cannot be mapped.
+                                if (error instanceof EntryNotFoundException) {
+                                    // Ignore missing entry since it cannot be mapped.
                                     return Promises.newResultPromise(null);
                                 }
+                                return Promises.newExceptionPromise(asResourceException(error));
                             }
                         });
             }

--
Gitblit v1.10.0