From e9a34212c4589e448c5210165d6c4ea8bae35bba Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Fri, 14 Feb 2014 12:46:49 -0500
Subject: [PATCH] Updated REST interface for groups.

The REST interface for groups has been modified to return Response
objects to allow better handling of server responses.

Ticket #554
---
 .../com/netscape/certsrv/group/GroupClient.java    | 18 ++++++----
 .../com/netscape/certsrv/group/GroupResource.java  | 18 ++++++----
 .../netscape/cms/servlet/admin/GroupService.java   | 40 ++++++++++++----------
 3 files changed, 45 insertions(+), 31 deletions(-)

diff --git a/base/common/src/com/netscape/certsrv/group/GroupClient.java b/base/common/src/com/netscape/certsrv/group/GroupClient.java
index c5ff9796a643fcf85fc132b1adffac639440c780..4823d4ed08f0668588e39b12198d4c9e425ac821 100644
--- a/base/common/src/com/netscape/certsrv/group/GroupClient.java
+++ b/base/common/src/com/netscape/certsrv/group/GroupClient.java
@@ -41,11 +41,13 @@ public class GroupClient extends Client {
     }
 
     public GroupCollection findGroups(String groupIDFilter, Integer start, Integer size) {
-        return groupClient.findGroups(groupIDFilter, start, size);
+        Response response = groupClient.findGroups(groupIDFilter, start, size);
+        return client.getEntity(response, GroupCollection.class);
     }
 
     public GroupData getGroup(String groupID) {
-        return groupClient.getGroup(groupID);
+        Response response = groupClient.getGroup(groupID);
+        return client.getEntity(response, GroupData.class);
     }
 
     public GroupData addGroup(GroupData groupData) {
@@ -59,15 +61,18 @@ public class GroupClient extends Client {
     }
 
     public void removeGroup(String groupID) {
-        groupClient.removeGroup(groupID);
+        Response response = groupClient.removeGroup(groupID);
+        client.getEntity(response, Void.class);
     }
 
     public GroupMemberCollection findGroupMembers(String groupID, Integer start, Integer size) {
-        return groupClient.findGroupMembers(groupID, start, size);
+        Response response = groupClient.findGroupMembers(groupID, start, size);
+        return client.getEntity(response, GroupMemberCollection.class);
     }
 
     public GroupMemberData getGroupMember(String groupID, String memberID) {
-        return groupClient.getGroupMember(groupID, memberID);
+        Response response = groupClient.getGroupMember(groupID, memberID);
+        return client.getEntity(response, GroupMemberData.class);
     }
 
     public GroupMemberData addGroupMember(String groupID, String memberID) {
@@ -76,6 +81,7 @@ public class GroupClient extends Client {
     }
 
     public void removeGroupMember(String groupID, String memberID) {
-        groupClient.removeGroupMember(groupID, memberID);
+        Response response = groupClient.removeGroupMember(groupID, memberID);
+        client.getEntity(response, Void.class);
     }
 }
diff --git a/base/common/src/com/netscape/certsrv/group/GroupResource.java b/base/common/src/com/netscape/certsrv/group/GroupResource.java
index aa26bebb1eaf5e550527917d21d4caf11f3fbc8b..f1dfe40261ff4297248721aa7f17f819aee3402b 100644
--- a/base/common/src/com/netscape/certsrv/group/GroupResource.java
+++ b/base/common/src/com/netscape/certsrv/group/GroupResource.java
@@ -44,8 +44,9 @@ import com.netscape.certsrv.base.PATCH;
 public interface GroupResource {
 
     @GET
+    @ClientResponseType(entityType=GroupCollection.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public GroupCollection findGroups(
+    public Response findGroups(
             @QueryParam("filter") String filter,
             @QueryParam("start") Integer start,
             @QueryParam("size") Integer size);
@@ -58,8 +59,9 @@ public interface GroupResource {
 
     @GET
     @Path("{groupID}")
+    @ClientResponseType(entityType=GroupData.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public GroupData getGroup(@PathParam("groupID") String groupID);
+    public Response getGroup(@PathParam("groupID") String groupID);
 
     @PATCH
     @Path("{groupID}")
@@ -70,13 +72,15 @@ public interface GroupResource {
 
     @DELETE
     @Path("{groupID}")
+    @ClientResponseType(entityType=Void.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public void removeGroup(@PathParam("groupID") String groupID);
+    public Response removeGroup(@PathParam("groupID") String groupID);
 
     @GET
     @Path("{groupID}/members")
+    @ClientResponseType(entityType=GroupMemberCollection.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public GroupMemberCollection findGroupMembers(
+    public Response findGroupMembers(
             @PathParam("groupID") String groupID,
             @QueryParam("start") Integer start,
             @QueryParam("size") Integer size);
@@ -90,11 +94,13 @@ public interface GroupResource {
 
     @GET
     @Path("{groupID}/members/{memberID}")
+    @ClientResponseType(entityType=GroupMemberData.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public GroupMemberData getGroupMember(@PathParam("groupID") String groupID, @PathParam("memberID") String memberID);
+    public Response getGroupMember(@PathParam("groupID") String groupID, @PathParam("memberID") String memberID);
 
     @DELETE
     @Path("{groupID}/members/{memberID}")
+    @ClientResponseType(entityType=Void.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public void removeGroupMember(@PathParam("groupID") String groupID, @PathParam("memberID") String memberID);
+    public Response removeGroupMember(@PathParam("groupID") String groupID, @PathParam("memberID") String memberID);
 }
diff --git a/base/server/cms/src/com/netscape/cms/servlet/admin/GroupService.java b/base/server/cms/src/com/netscape/cms/servlet/admin/GroupService.java
index 4998212d566f1ac61b4334e1466e3e5dd0e0dd97..4fc4a7cfc11a4893439e5404aeaf4b390f32f140 100644
--- a/base/server/cms/src/com/netscape/cms/servlet/admin/GroupService.java
+++ b/base/server/cms/src/com/netscape/cms/servlet/admin/GroupService.java
@@ -42,7 +42,6 @@ import com.netscape.certsrv.common.OpDef;
 import com.netscape.certsrv.common.ScopeDef;
 import com.netscape.certsrv.group.GroupCollection;
 import com.netscape.certsrv.group.GroupData;
-import com.netscape.certsrv.group.GroupMemberCollection;
 import com.netscape.certsrv.group.GroupMemberData;
 import com.netscape.certsrv.group.GroupNotFoundException;
 import com.netscape.certsrv.group.GroupResource;
@@ -102,7 +101,7 @@ public class GroupService extends PKIService implements GroupResource {
      * ui/admin-protocol-definition.html#user-admin
      */
     @Override
-    public GroupCollection findGroups(String filter, Integer start, Integer size) {
+    public Response findGroups(String filter, Integer start, Integer size) {
         try {
             filter = StringUtils.isEmpty(filter) ? "*" : "*"+LDAPUtil.escapeFilter(filter)+"*";
             start = start == null ? 0 : start;
@@ -136,7 +135,7 @@ public class GroupService extends PKIService implements GroupResource {
                 response.addLink(new Link("next", uri));
             }
 
-            return response;
+            return createOKResponse(response);
 
         } catch (Exception e) {
             throw new PKIException(getUserMessage("CMS_INTERNAL_ERROR", headers));
@@ -150,7 +149,11 @@ public class GroupService extends PKIService implements GroupResource {
      * ui/admin-protocol-definition.html#user-admin
      */
     @Override
-    public GroupData getGroup(String groupID) {
+    public Response getGroup(String groupID) {
+        return createOKResponse(getGroupData(groupID));
+    }
+
+    public GroupData getGroupData(String groupID) {
 
         try {
             if (groupID == null) {
@@ -217,12 +220,9 @@ public class GroupService extends PKIService implements GroupResource {
             auditAddGroup(groupID, groupData, ILogger.SUCCESS);
 
             // read the data back
-            groupData = getGroup(groupID);
+            groupData = getGroupData(groupID);
 
-            return Response
-                    .created(groupData.getLink().getHref())
-                    .entity(groupData)
-                    .build();
+            return createCreatedResponse(groupData, groupData.getLink().getHref());
 
         } catch (PKIException e) {
             auditAddGroup(groupID, groupData, ILogger.FAILURE);
@@ -280,11 +280,9 @@ public class GroupService extends PKIService implements GroupResource {
             auditModifyGroup(groupID, groupData, ILogger.SUCCESS);
 
             // read the data back
-            groupData = getGroup(groupID);
+            groupData = getGroupData(groupID);
 
-            return Response
-                    .ok(groupData)
-                    .build();
+            return createOKResponse(groupData);
 
         } catch (PKIException e) {
             auditModifyGroup(groupID, groupData, ILogger.FAILURE);
@@ -310,7 +308,7 @@ public class GroupService extends PKIService implements GroupResource {
      * </ul>
      */
     @Override
-    public void removeGroup(String groupID) {
+    public Response removeGroup(String groupID) {
 
         // ensure that any low-level exceptions are reported
         // to the signed audit log and stored as failures
@@ -325,6 +323,8 @@ public class GroupService extends PKIService implements GroupResource {
 
             auditDeleteGroup(groupID, ILogger.SUCCESS);
 
+            return createNoContentResponse();
+
         } catch (PKIException e) {
             auditDeleteGroup(groupID, ILogger.FAILURE);
             throw e;
@@ -336,14 +336,14 @@ public class GroupService extends PKIService implements GroupResource {
     }
 
     @Override
-    public GroupMemberCollection findGroupMembers(String groupID, Integer start, Integer size) {
+    public Response findGroupMembers(String groupID, Integer start, Integer size) {
 
         if (groupID == null) throw new BadRequestException("Group ID is null.");
 
         try {
             GroupMemberProcessor processor = new GroupMemberProcessor(getLocale(headers));
             processor.setUriInfo(uriInfo);
-            return processor.findGroupMembers(groupID, start, size);
+            return createOKResponse(processor.findGroupMembers(groupID, start, size));
 
         } catch (PKIException e) {
             throw e;
@@ -355,7 +355,7 @@ public class GroupService extends PKIService implements GroupResource {
     }
 
     @Override
-    public GroupMemberData getGroupMember(String groupID, String memberID) {
+    public Response getGroupMember(String groupID, String memberID) {
 
         if (groupID == null) throw new BadRequestException("Group ID is null.");
         if (memberID == null) throw new BadRequestException("Member ID is null.");
@@ -363,7 +363,7 @@ public class GroupService extends PKIService implements GroupResource {
         try {
             GroupMemberProcessor processor = new GroupMemberProcessor(getLocale(headers));
             processor.setUriInfo(uriInfo);
-            return processor.getGroupMember(groupID, memberID);
+            return createOKResponse(processor.getGroupMember(groupID, memberID));
 
         } catch (PKIException e) {
             throw e;
@@ -402,7 +402,7 @@ public class GroupService extends PKIService implements GroupResource {
     }
 
     @Override
-    public void removeGroupMember(String groupID, String memberID) {
+    public Response removeGroupMember(String groupID, String memberID) {
 
         if (groupID == null) throw new BadRequestException("Group ID is null.");
         if (memberID == null) throw new BadRequestException("Member ID is null.");
@@ -412,6 +412,8 @@ public class GroupService extends PKIService implements GroupResource {
             processor.setUriInfo(uriInfo);
             processor.removeGroupMember(groupID, memberID);
 
+            return createNoContentResponse();
+
         } catch (PKIException e) {
             throw e;
 
-- 
1.8.4.2

