From ed84c0a74ea4a0e3dd46257c018d2f91f96ce0a1 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Tue, 11 Feb 2014 12:19:00 -0500
Subject: [PATCH] Updated REST interface for users.

The REST interface for users has been modified to return Response objects
to allow better handling of server responses. Other REST interfaces will
be modified in subsequent patches. Once all interfaces are updated, the
client library can be updated to use RESTEasy 3.0 API. Clients using the
client library or XML/JSON directly are not affected by these changes.

Ticket #554
---
 .../src/com/netscape/certsrv/user/UserClient.java  | 24 +++++---
 .../com/netscape/certsrv/user/UserResource.java    | 24 +++++---
 .../netscape/cms/servlet/admin/UserService.java    | 69 +++++++++++-----------
 .../com/netscape/cms/servlet/base/PKIService.java  | 26 +++++++-
 4 files changed, 91 insertions(+), 52 deletions(-)

diff --git a/base/common/src/com/netscape/certsrv/user/UserClient.java b/base/common/src/com/netscape/certsrv/user/UserClient.java
index 33ed61d2aa4780b2255678d00e1a018750dd3301..ee9d9095038fff424c8d8cf92deb0c5b9055f5ff 100644
--- a/base/common/src/com/netscape/certsrv/user/UserClient.java
+++ b/base/common/src/com/netscape/certsrv/user/UserClient.java
@@ -41,11 +41,13 @@ public class UserClient extends Client {
     }
 
     public UserCollection findUsers(String filter, Integer start, Integer size) {
-        return userClient.findUsers(filter, start, size);
+        Response response = userClient.findUsers(filter, start, size);
+        return client.getEntity(response, UserCollection.class);
     }
 
     public UserData getUser(String userID) {
-        return userClient.getUser(userID);
+        Response response = userClient.getUser(userID);
+        return client.getEntity(response, UserData.class);
     }
 
     public UserData addUser(UserData userData) {
@@ -59,15 +61,18 @@ public class UserClient extends Client {
     }
 
     public void removeUser(String userID) {
-        userClient.removeUser(userID);
+        Response response = userClient.removeUser(userID);
+        client.getEntity(response, Void.class);
     }
 
     public UserCertCollection findUserCerts(String userID, Integer start, Integer size) {
-        return userClient.findUserCerts(userID, start, size);
+        Response response = userClient.findUserCerts(userID, start, size);
+        return client.getEntity(response, UserCertCollection.class);
     }
 
     public UserCertData getUserCert(String userID, String certID) {
-        return userClient.getUserCert(userID, certID);
+        Response response = userClient.getUserCert(userID, certID);
+        return client.getEntity(response, UserCertData.class);
     }
 
     public UserCertData addUserCert(String userID, UserCertData userCertData) {
@@ -76,11 +81,13 @@ public class UserClient extends Client {
     }
 
     public void removeUserCert(String userID, String certID) {
-        userClient.removeUserCert(userID, certID);
+        Response response = userClient.removeUserCert(userID, certID);
+        client.getEntity(response, Void.class);
     }
 
     public UserMembershipCollection findUserMemberships(String userID, Integer start, Integer size) {
-        return userClient.findUserMemberships(userID, start, size);
+        Response response = userClient.findUserMemberships(userID, start, size);
+        return client.getEntity(response, UserMembershipCollection.class);
     }
 
     public UserMembershipData addUserMembership(String userID, String groupID) {
@@ -89,6 +96,7 @@ public class UserClient extends Client {
     }
 
     public void removeUserMembership(String userD, String groupID) {
-        userClient.removeUserMembership(userD, groupID);
+        Response response = userClient.removeUserMembership(userD, groupID);
+        client.getEntity(response, Void.class);
     }
 }
diff --git a/base/common/src/com/netscape/certsrv/user/UserResource.java b/base/common/src/com/netscape/certsrv/user/UserResource.java
index 90b46ef607a0ee9ac69c2a340d882201c1d19745..cbda69a4cb68d298581bbc3db3fe3d5df5d08ab6 100644
--- a/base/common/src/com/netscape/certsrv/user/UserResource.java
+++ b/base/common/src/com/netscape/certsrv/user/UserResource.java
@@ -48,8 +48,9 @@ public interface UserResource {
     public static final String ALL_PROFILES = "All Profiles";
 
     @GET
+    @ClientResponseType(entityType=UserCollection.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public UserCollection findUsers(
+    public Response findUsers(
             @QueryParam("filter") String filter,
             @QueryParam("start") Integer start,
             @QueryParam("size") Integer size);
@@ -62,8 +63,9 @@ public interface UserResource {
 
     @GET
     @Path("{userID}")
+    @ClientResponseType(entityType=UserData.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public UserData getUser(@PathParam("userID") String userID);
+    public Response getUser(@PathParam("userID") String userID);
 
     @PUT
     @Path("{userID}")
@@ -81,13 +83,15 @@ public interface UserResource {
 
     @DELETE
     @Path("{userID}")
+    @ClientResponseType(entityType=Void.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public void removeUser(@PathParam("userID") String userID);
+    public Response removeUser(@PathParam("userID") String userID);
 
     @GET
     @Path("{userID}/certs")
+    @ClientResponseType(entityType=UserCertCollection.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public UserCertCollection findUserCerts(
+    public Response findUserCerts(
             @PathParam("userID") String userID,
             @QueryParam("start") Integer start,
             @QueryParam("size") Integer size);
@@ -102,18 +106,21 @@ public interface UserResource {
 
     @GET
     @Path("{userID}/certs/{certID}")
+    @ClientResponseType(entityType=UserCertData.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public UserCertData getUserCert(@PathParam("userID") String userID, @PathParam("certID") String certID);
+    public Response getUserCert(@PathParam("userID") String userID, @PathParam("certID") String certID);
 
     @DELETE
     @Path("{userID}/certs/{certID}")
+    @ClientResponseType(entityType=Void.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public void removeUserCert(@PathParam("userID") String userID, @PathParam("certID") String certID);
+    public Response removeUserCert(@PathParam("userID") String userID, @PathParam("certID") String certID);
 
     @GET
     @Path("{userID}/memberships")
+    @ClientResponseType(entityType=UserMembershipCollection.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public UserMembershipCollection findUserMemberships(
+    public Response findUserMemberships(
             @PathParam("userID") String userID,
             @QueryParam("start") Integer start,
             @QueryParam("size") Integer size);
@@ -127,6 +134,7 @@ public interface UserResource {
 
     @DELETE
     @Path("{userID}/memberships/{groupID}")
+    @ClientResponseType(entityType=Void.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public void removeUserMembership(@PathParam("userID") String userID, @PathParam("groupID") String groupID);
+    public Response removeUserMembership(@PathParam("userID") String userID, @PathParam("groupID") String groupID);
 }
diff --git a/base/server/cms/src/com/netscape/cms/servlet/admin/UserService.java b/base/server/cms/src/com/netscape/cms/servlet/admin/UserService.java
index 130103a87bd467f92b45c755047dec77aba1b974..70124b4f4fa21176b9741f2f6b5ac5381dd86a1a 100644
--- a/base/server/cms/src/com/netscape/cms/servlet/admin/UserService.java
+++ b/base/server/cms/src/com/netscape/cms/servlet/admin/UserService.java
@@ -131,7 +131,7 @@ public class UserService extends PKIService implements UserResource {
      * ui/admin-protocol-definition.html#user-admin
      */
     @Override
-    public UserCollection findUsers(String filter, Integer start, Integer size) {
+    public Response findUsers(String filter, Integer start, Integer size) {
         try {
             filter = StringUtils.isEmpty(filter) ? "*" : "*"+LDAPUtil.escapeFilter(filter)+"*";
             start = start == null ? 0 : start;
@@ -165,7 +165,7 @@ public class UserService extends PKIService implements UserResource {
                 response.addLink(new Link("next", uri));
             }
 
-            return response;
+            return createOKResponse(response);
 
         } catch (Exception e) {
             throw new PKIException(getUserMessage("CMS_INTERNAL_ERROR", headers));
@@ -182,7 +182,11 @@ public class UserService extends PKIService implements UserResource {
      * ui/admin-protocol-definition.html#user-admin
      */
     @Override
-    public UserData getUser(String userID) {
+    public Response getUser(String userID) {
+        return createOKResponse(getUserData(userID));
+    }
+
+    public UserData getUserData(String userID) {
         try {
             if (userID == null) {
                 log(ILogger.LL_FAILURE, CMS.getLogMessage("ADMIN_SRVLT_NULL_RS_ID"));
@@ -360,12 +364,9 @@ public class UserService extends PKIService implements UserResource {
             auditAddUser(userID, userData, ILogger.SUCCESS);
 
             // read the data back
-            userData = getUser(userID);
+            userData = getUserData(userID);
 
-            return Response
-                    .created(userData.getLink().getHref())
-                    .entity(userData)
-                    .build();
+            return createCreatedResponse(userData, userData.getLink().getHref());
 
         } catch (PKIException e) {
             auditAddUser(userID, userData, ILogger.FAILURE);
@@ -439,11 +440,9 @@ public class UserService extends PKIService implements UserResource {
             auditModifyUser(userID, userData, ILogger.SUCCESS);
 
             // read the data back
-            userData = getUser(userID);
+            userData = getUserData(userID);
 
-            return Response
-                    .ok(userData)
-                    .build();
+            return createOKResponse(userData);
 
         } catch (PKIException e) {
             auditModifyUser(userID, userData, ILogger.FAILURE);
@@ -530,11 +529,9 @@ public class UserService extends PKIService implements UserResource {
             auditModifyUser(userID, userData, ILogger.SUCCESS);
 
             // read the data back
-            userData = getUser(userID);
+            userData = getUserData(userID);
 
-            return Response
-                    .ok(userData)
-                    .build();
+            return createOKResponse(userData);
 
         } catch (PKIException e) {
             auditModifyUser(userID, userData, ILogger.FAILURE);
@@ -563,7 +560,7 @@ public class UserService extends PKIService implements UserResource {
      * </ul>
      */
     @Override
-    public void removeUser(String userID) {
+    public Response removeUser(String userID) {
 
         // ensure that any low-level exceptions are reported
         // to the signed audit log and stored as failures
@@ -588,6 +585,8 @@ public class UserService extends PKIService implements UserResource {
 
             auditDeleteUser(userID, ILogger.SUCCESS);
 
+            return createNoContentResponse();
+
         } catch (PKIException e) {
             auditDeleteUser(userID, ILogger.FAILURE);
             throw e;
@@ -626,7 +625,7 @@ public class UserService extends PKIService implements UserResource {
      * ui/admin-protocol-definition.html#user-admin
      */
     @Override
-    public UserCertCollection findUserCerts(String userID, Integer start, Integer size) {
+    public Response findUserCerts(String userID, Integer start, Integer size) {
         try {
             start = start == null ? 0 : start;
             size = size == null ? DEFAULT_SIZE : size;
@@ -678,7 +677,7 @@ public class UserService extends PKIService implements UserResource {
                 response.addLink(new Link("next", uri));
             }
 
-            return response;
+            return createOKResponse(response);
 
         } catch (PKIException e) {
             throw e;
@@ -689,7 +688,11 @@ public class UserService extends PKIService implements UserResource {
     }
 
     @Override
-    public UserCertData getUserCert(String userID, String certID) {
+    public Response getUserCert(String userID, String certID) {
+        return createOKResponse(getUserCertData(userID, certID));
+    }
+
+    public UserCertData getUserCertData(String userID, String certID) {
 
         if (certID == null) throw new BadRequestException("Certificate ID is null.");
 
@@ -786,7 +789,7 @@ public class UserService extends PKIService implements UserResource {
             // no cert is a success
             if (encoded == null) {
                 auditAddUserCert(userID, userCertData, ILogger.SUCCESS);
-                return Response.ok().build();
+                return createOKResponse();
             }
 
             // only one cert added per operation
@@ -918,12 +921,9 @@ public class UserService extends PKIService implements UserResource {
                 userCertData.setSubjectDN(cert.getSubjectDN().toString());
                 String certID = userCertData.getID();
 
-                userCertData = getUserCert(userID, URLEncoder.encode(certID, "UTF-8"));
+                userCertData = getUserCertData(userID, URLEncoder.encode(certID, "UTF-8"));
 
-                return Response
-                        .created(userCertData.getLink().getHref())
-                        .entity(userCertData)
-                        .build();
+                return createCreatedResponse(userCertData, userCertData.getLink().getHref());
 
             } catch (CertificateExpiredException e) {
                 log(ILogger.LL_FAILURE, CMS.getLogMessage("ADMIN_SRVLT_ADD_CERT_EXPIRED",
@@ -964,7 +964,7 @@ public class UserService extends PKIService implements UserResource {
      * </ul>
      */
     @Override
-    public void removeUserCert(String userID, String certID) {
+    public Response removeUserCert(String userID, String certID) {
 
         if (userID == null) throw new BadRequestException("User ID is null.");
         if (certID == null) throw new BadRequestException("Certificate ID is null.");
@@ -978,6 +978,8 @@ public class UserService extends PKIService implements UserResource {
         UserCertData userCertData = new UserCertData();
         userCertData.setID(certID);
         removeUserCert(userID, userCertData);
+
+        return createNoContentResponse();
     }
 
     public void removeUserCert(String userID, UserCertData userCertData) {
@@ -1036,7 +1038,7 @@ public class UserService extends PKIService implements UserResource {
     }
 
     @Override
-    public UserMembershipCollection findUserMemberships(String userID, Integer start, Integer size) {
+    public Response findUserMemberships(String userID, Integer start, Integer size) {
         try {
             start = start == null ? 0 : start;
             size = size == null ? DEFAULT_SIZE : size;
@@ -1081,7 +1083,7 @@ public class UserService extends PKIService implements UserResource {
                 response.addLink(new Link("next", uri));
             }
 
-            return response;
+            return createOKResponse(response);
 
         } catch (PKIException e) {
             throw e;
@@ -1109,10 +1111,7 @@ public class UserService extends PKIService implements UserResource {
 
             UserMembershipData userMembershipData = createUserMembershipData(userID, groupID);
 
-            return Response
-                    .created(userMembershipData.getLink().getHref())
-                    .entity(userMembershipData)
-                    .build();
+            return createCreatedResponse(userMembershipData, userMembershipData.getLink().getHref());
 
         } catch (PKIException e) {
             throw e;
@@ -1124,7 +1123,7 @@ public class UserService extends PKIService implements UserResource {
     }
 
     @Override
-    public void removeUserMembership(String userID, String groupID) {
+    public Response removeUserMembership(String userID, String groupID) {
 
         if (userID == null) throw new BadRequestException("User ID is null.");
         if (groupID == null) throw new BadRequestException("Group ID is null.");
@@ -1134,6 +1133,8 @@ public class UserService extends PKIService implements UserResource {
             processor.setUriInfo(uriInfo);
             processor.removeGroupMember(groupID, userID);
 
+            return createNoContentResponse();
+
         } catch (PKIException e) {
             throw e;
 
diff --git a/base/server/cms/src/com/netscape/cms/servlet/base/PKIService.java b/base/server/cms/src/com/netscape/cms/servlet/base/PKIService.java
index 23e540f0eb1809a4ec3901ed02166e917a809d5c..6550715f581666551ec1b9807b816a1b576b12d4 100644
--- a/base/server/cms/src/com/netscape/cms/servlet/base/PKIService.java
+++ b/base/server/cms/src/com/netscape/cms/servlet/base/PKIService.java
@@ -18,6 +18,7 @@
 package com.netscape.cms.servlet.base;
 
 import java.lang.reflect.Method;
+import java.net.URI;
 import java.security.cert.CertificateEncodingException;
 import java.util.HashMap;
 import java.util.List;
@@ -54,8 +55,29 @@ public class PKIService {
     public ILogger logger = CMS.getLogger();
     public IAuditor auditor = CMS.getAuditor();
 
-    public Response createOKResponse(Object object) {
-        return Response.ok(object).build();
+    public Response createOKResponse() {
+        return Response
+                .ok()
+                .build();
+    }
+
+    public Response createOKResponse(Object entity) {
+        return Response
+                .ok(entity)
+                .build();
+    }
+
+    public Response createCreatedResponse(Object entity, URI link) {
+        return Response
+                .created(link)
+                .entity(entity)
+                .build();
+    }
+
+    public Response createNoContentResponse() {
+        return Response
+                .noContent()
+                .build();
     }
 
     public Response sendConditionalGetResponse(int ctime, Object object, Request request) {
-- 
1.8.4.2

