From b7084e32bb89655ead73bd0d50616fa1f5bb8491 Mon Sep 17 00:00:00 2001
From: sin <sin@localhost>
Date: Tue, 14 Apr 2009 15:56:32 +0000
Subject: [PATCH] issue#2623:ldapsearch: wrong error message when a wrong sort order is specified

---
 opends/src/server/org/opends/server/backends/jeb/EntryContainer.java |   71 ++++++++++++++++++++++++++---------
 1 files changed, 53 insertions(+), 18 deletions(-)

diff --git a/opends/src/server/org/opends/server/backends/jeb/EntryContainer.java b/opends/src/server/org/opends/server/backends/jeb/EntryContainer.java
index 71e3171..834889b 100644
--- a/opends/src/server/org/opends/server/backends/jeb/EntryContainer.java
+++ b/opends/src/server/org/opends/server/backends/jeb/EntryContainer.java
@@ -875,6 +875,22 @@
     .getRequestControl(PagedResultsControl.DECODER);
     ServerSideSortRequestControl sortRequest = searchOperation
     .getRequestControl(ServerSideSortRequestControl.DECODER);
+    if(sortRequest != null && !sortRequest.containsSortKeys()
+            && sortRequest.isCritical())
+    {
+      /**
+         If the control's criticality field is true then the server SHOULD do
+         the following: return unavailableCriticalExtension as a return code
+         in the searchResultDone message; include the sortKeyResponseControl in
+         the searchResultDone message, and not send back any search result
+         entries.
+       */
+      searchOperation.addResponseControl(
+            new ServerSideSortResponseControl(
+                LDAPResultCode.NO_SUCH_ATTRIBUTE, null));
+      searchOperation.setResultCode(ResultCode.UNAVAILABLE_CRITICAL_EXTENSION);
+      return;
+    }
     VLVRequestControl vlvRequest = searchOperation
     .getRequestControl(VLVRequestControl.DECODER);
 
@@ -1043,28 +1059,47 @@
 
       if (sortRequest != null)
       {
-        try
-        {
-          entryIDList = EntryIDSetSorter.sort(this, entryIDList,
-              searchOperation,
-              sortRequest.getSortOrder(),
-              vlvRequest);
-          searchOperation.addResponseControl(
-              new ServerSideSortResponseControl(LDAPResultCode.SUCCESS, null));
-        }
-        catch (DirectoryException de)
-        {
-          searchOperation.addResponseControl(
-              new ServerSideSortResponseControl(
-                  de.getResultCode().getIntValue(), null));
-
-          if (sortRequest.isCritical())
+          try
           {
-            throw de;
+            //If the sort key is not present, the sorting will generate the
+            //default ordering. VLV search request goes through as if
+            //this sort key was not found in the user entry.
+            entryIDList = EntryIDSetSorter.sort(this, entryIDList,
+                searchOperation,
+                sortRequest.getSortOrder(),
+                vlvRequest);
+            if(sortRequest.containsSortKeys())
+            {
+              searchOperation.addResponseControl(
+                new ServerSideSortResponseControl(
+                                              LDAPResultCode.SUCCESS, null));
+            }
+            else
+            {
+              /*
+                There is no sort key associated with the sort control. Since it
+                came here it means that the critificality is false so let the
+                server return all search results unsorted and include the
+                sortKeyResponseControl inthe searchResultDone message.
+              */
+              searchOperation.addResponseControl(
+                      new ServerSideSortResponseControl
+                                (LDAPResultCode.NO_SUCH_ATTRIBUTE, null));
+            }
+          }
+          catch (DirectoryException de)
+          {
+            searchOperation.addResponseControl(
+                new ServerSideSortResponseControl(
+                    de.getResultCode().getIntValue(), null));
+
+            if (sortRequest.isCritical())
+            {
+              throw de;
+            }
           }
         }
       }
-    }
 
     // If requested, construct and return a fictitious entry containing
     // debug information, and no other entries.

--
Gitblit v1.10.0