From 0eced7181eed723c6ad30e39ab56c0135242cc84 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Sun, 4 May 2014 18:22:37 -0400
Subject: [PATCH] Added filter to GroupMemberProcessor.findGroupMembers().

The GroupMemberProcessor.findGroupMembers() has been modified to
accept an additional parameter to filter the group members to be
returned. The CLI has been modified accordingly.

Ticket #920
---
 .../com/netscape/certsrv/group/GroupClient.java    |  8 ++++--
 .../com/netscape/certsrv/group/GroupResource.java  |  1 +
 .../cmstools/group/GroupMemberFindCLI.java         |  9 ++++---
 .../cms/servlet/admin/GroupMemberProcessor.java    | 30 +++++++++++++---------
 .../org/dogtagpki/server/rest/GroupService.java    |  6 +++--
 5 files changed, 34 insertions(+), 20 deletions(-)

diff --git a/base/common/src/com/netscape/certsrv/group/GroupClient.java b/base/common/src/com/netscape/certsrv/group/GroupClient.java
index f516e9842cb0c7d9fb1862ce6404213ed0106af3..80e48e8099e073b73958a1de62487110411023b8 100644
--- a/base/common/src/com/netscape/certsrv/group/GroupClient.java
+++ b/base/common/src/com/netscape/certsrv/group/GroupClient.java
@@ -65,8 +65,12 @@ public class GroupClient extends Client {
         client.getEntity(response, Void.class);
     }
 
-    public GroupMemberCollection findGroupMembers(String groupID, Integer start, Integer size) {
-        Response response = groupClient.findGroupMembers(groupID, start, size);
+    public GroupMemberCollection findGroupMembers(
+            String groupID,
+            String filter,
+            Integer start,
+            Integer size) {
+        Response response = groupClient.findGroupMembers(groupID, filter, start, size);
         return client.getEntity(response, GroupMemberCollection.class);
     }
 
diff --git a/base/common/src/com/netscape/certsrv/group/GroupResource.java b/base/common/src/com/netscape/certsrv/group/GroupResource.java
index aff584fe66d47dc64532fbc1a91cf31ef3664671..27fa90b75fceff4c21a582a54709af41699c45e9 100644
--- a/base/common/src/com/netscape/certsrv/group/GroupResource.java
+++ b/base/common/src/com/netscape/certsrv/group/GroupResource.java
@@ -71,6 +71,7 @@ public interface GroupResource {
     @ClientResponseType(entityType=GroupMemberCollection.class)
     public Response findGroupMembers(
             @PathParam("groupID") String groupID,
+            @QueryParam("filter") String filter,
             @QueryParam("start") Integer start,
             @QueryParam("size") Integer size);
 
diff --git a/base/java-tools/src/com/netscape/cmstools/group/GroupMemberFindCLI.java b/base/java-tools/src/com/netscape/cmstools/group/GroupMemberFindCLI.java
index 41f310a5f15e1ab9f3d69820051660e3541e4a20..7a4f217f009cdb5102a00011ecb445ce2e11a352 100644
--- a/base/java-tools/src/com/netscape/cmstools/group/GroupMemberFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/group/GroupMemberFindCLI.java
@@ -44,7 +44,7 @@ public class GroupMemberFindCLI extends CLI {
     }
 
     public void printHelp() {
-        formatter.printHelp(getFullName() + " <Group ID> [OPTIONS...]", options);
+        formatter.printHelp(getFullName() + " <Group ID> [FILTER] [OPTIONS...]", options);
     }
 
     public void createOptions() {
@@ -78,13 +78,14 @@ public class GroupMemberFindCLI extends CLI {
 
         String[] cmdArgs = cmd.getArgs();
 
-        if (cmdArgs.length != 1) {
-            System.err.println("Error: No Group ID specified.");
+        if (cmdArgs.length < 1 || cmdArgs.length > 2) {
+            System.err.println("Error: Incorrect number of arguments specified.");
             printHelp();
             System.exit(-1);
         }
 
         String groupID = cmdArgs[0];
+        String filter = cmdArgs.length < 2 ? null : cmdArgs[1];
 
         String s = cmd.getOptionValue("start");
         Integer start = s == null ? null : Integer.valueOf(s);
@@ -92,7 +93,7 @@ public class GroupMemberFindCLI extends CLI {
         s = cmd.getOptionValue("size");
         Integer size = s == null ? null : Integer.valueOf(s);
 
-        GroupMemberCollection response = groupMemberCLI.groupClient.findGroupMembers(groupID, start, size);
+        GroupMemberCollection response = groupMemberCLI.groupClient.findGroupMembers(groupID, filter, start, size);
 
         MainCLI.printMessage(response.getTotal() + " entries matched");
         if (response.getTotal() == 0) return;
diff --git a/base/server/cms/src/com/netscape/cms/servlet/admin/GroupMemberProcessor.java b/base/server/cms/src/com/netscape/cms/servlet/admin/GroupMemberProcessor.java
index 78f602acdff35fb00202832e336cef505fca1193..3a89ce8dced7f7be83c0d615927fe93a6676f3df 100644
--- a/base/server/cms/src/com/netscape/cms/servlet/admin/GroupMemberProcessor.java
+++ b/base/server/cms/src/com/netscape/cms/servlet/admin/GroupMemberProcessor.java
@@ -19,7 +19,9 @@ package com.netscape.cms.servlet.admin;
 
 import java.net.URI;
 import java.net.URLEncoder;
+import java.util.ArrayList;
 import java.util.Enumeration;
+import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 
@@ -94,7 +96,10 @@ public class GroupMemberProcessor extends Processor {
         return groupMemberData;
     }
 
-    public GroupMemberCollection findGroupMembers(String groupID, Integer start, Integer size) {
+    public GroupMemberCollection findGroupMembers(String groupID, String filter, Integer start, Integer size) {
+
+        CMS.debug("GroupMemberProcessor.findGroupMembers(" + groupID + ", " + filter + ")");
+
         try {
             start = start == null ? 0 : start;
             size = size == null ? DEFAULT_SIZE : size;
@@ -113,28 +118,29 @@ public class GroupMemberProcessor extends Processor {
             GroupMemberCollection response = new GroupMemberCollection();
 
             Enumeration<String> members = group.getMemberNames();
+            List<String> results = new ArrayList<String>();
 
-            int i = 0;
-
-            // skip to the start of the page
-            for ( ; i<start && members.hasMoreElements(); i++) members.nextElement();
-
-            // return entries up to the page size
-            for ( ; i<start+size && members.hasMoreElements(); i++) {
+            // filter members
+            while (members.hasMoreElements()) {
                 String memberID = members.nextElement();
+                if (filter == null || memberID.contains(filter)) results.add(memberID);
+            }
+
+            // return entries in the page
+            for (int i = start; i < start+size && i < results.size(); i++) {
+                String memberID = results.get(i);
                 response.addEntry(createGroupMemberData(groupID, memberID));
             }
 
-            // count the total entries
-            for ( ; members.hasMoreElements(); i++) members.nextElement();
-            response.setTotal(i);
+            // return the total entries
+            response.setTotal(results.size());
 
             if (start > 0) {
                 URI uri = uriInfo.getRequestUriBuilder().replaceQueryParam("start", Math.max(start-size, 0)).build();
                 response.addLink(new Link("prev", uri));
             }
 
-            if (start+size < i) {
+            if (start+size < results.size()) {
                 URI uri = uriInfo.getRequestUriBuilder().replaceQueryParam("start", start+size).build();
                 response.addLink(new Link("next", uri));
             }
diff --git a/base/server/cms/src/org/dogtagpki/server/rest/GroupService.java b/base/server/cms/src/org/dogtagpki/server/rest/GroupService.java
index fdfbbd9660a7f97bc75528e2dce6ffb049e4f31b..3e98d3940bbe194c937df956d2f32cd439581a37 100644
--- a/base/server/cms/src/org/dogtagpki/server/rest/GroupService.java
+++ b/base/server/cms/src/org/dogtagpki/server/rest/GroupService.java
@@ -336,14 +336,16 @@ public class GroupService extends PKIService implements GroupResource {
     }
 
     @Override
-    public Response findGroupMembers(String groupID, Integer start, Integer size) {
+    public Response findGroupMembers(String groupID, String filter, Integer start, Integer size) {
+
+        CMS.debug("GroupService.findGroupMembers(" + groupID + ", " + filter + ")");
 
         if (groupID == null) throw new BadRequestException("Group ID is null.");
 
         try {
             GroupMemberProcessor processor = new GroupMemberProcessor(getLocale(headers));
             processor.setUriInfo(uriInfo);
-            return createOKResponse(processor.findGroupMembers(groupID, start, size));
+            return createOKResponse(processor.findGroupMembers(groupID, filter, start, size));
 
         } catch (PKIException e) {
             throw e;
-- 
1.8.4.2

