>From c0df5e255849911cc03be030241877746c48086c Mon Sep 17 00:00:00 2001
From: Ade Lee <alee@redhat.com>
Date: Tue, 5 Nov 2013 12:12:52 -0500
Subject: [PATCH] Modify profile resource to return correct response for create
 or modify op

Ticket 749
---
 .../netscape/certsrv/profile/ProfileClient.java    | 16 ++++++++---
 .../com/netscape/certsrv/profile/ProfileData.java  | 16 +++++++++++
 .../netscape/certsrv/profile/ProfileResource.java  |  9 +++++--
 .../netscape/cmstools/profile/ProfileAddCLI.java   |  5 +++-
 .../cmstools/profile/ProfileModifyCLI.java         |  6 ++++-
 .../cms/servlet/profile/ProfileService.java        | 31 +++++++++++++++++-----
 6 files changed, 69 insertions(+), 14 deletions(-)

diff --git a/base/common/src/com/netscape/certsrv/profile/ProfileClient.java b/base/common/src/com/netscape/certsrv/profile/ProfileClient.java
index 64a53b209bc8304213d82eae8afb014fdda622f8..eefd0660e9349897d3b56a9d4b1fbdcb1189bc3f 100644
--- a/base/common/src/com/netscape/certsrv/profile/ProfileClient.java
+++ b/base/common/src/com/netscape/certsrv/profile/ProfileClient.java
@@ -19,6 +19,8 @@ package com.netscape.certsrv.profile;
 
 import java.net.URISyntaxException;
 
+import org.jboss.resteasy.client.ClientResponse;
+
 import com.netscape.certsrv.client.Client;
 import com.netscape.certsrv.client.PKIClient;
 
@@ -54,12 +56,18 @@ public class ProfileClient extends Client {
         profileClient.modifyProfileState(id, "disable");
     }
 
-    public void createProfile(ProfileData data){
-        profileClient.createProfile(data);
+    public ProfileData createProfile(ProfileData data) {
+        @SuppressWarnings("unchecked")
+        ClientResponse<ProfileData> response =
+                (ClientResponse<ProfileData>) profileClient.createProfile(data);
+        return client.getEntity(response);
     }
 
-    public void modifyProfile(ProfileData data){
-        profileClient.modifyProfile(data.getId(), data);
+    public ProfileData modifyProfile(ProfileData data) {
+        @SuppressWarnings("unchecked")
+        ClientResponse<ProfileData> response =
+                (ClientResponse<ProfileData>) profileClient.modifyProfile(data.getId(), data);
+        return client.getEntity(response);
     }
 
     public void deleteProfile(String id) {
diff --git a/base/common/src/com/netscape/certsrv/profile/ProfileData.java b/base/common/src/com/netscape/certsrv/profile/ProfileData.java
index 619a39e67d3c891bf18d3964ac885b244d86a4a4..4b35fee2cdf4b1182b871631f3974b9672925e8a 100644
--- a/base/common/src/com/netscape/certsrv/profile/ProfileData.java
+++ b/base/common/src/com/netscape/certsrv/profile/ProfileData.java
@@ -27,6 +27,9 @@ import java.util.List;
 import java.util.Map;
 import java.util.Vector;
 
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlAttribute;
@@ -46,6 +49,19 @@ import org.jboss.resteasy.plugins.providers.atom.Link;
 @XmlAccessorType(XmlAccessType.FIELD)
 public class ProfileData {
 
+    public static Marshaller marshaller;
+    public static Unmarshaller unmarshaller;
+
+    static {
+        try {
+            marshaller = JAXBContext.newInstance(ProfileData.class).createMarshaller();
+            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
+            unmarshaller = JAXBContext.newInstance(ProfileData.class).createUnmarshaller();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
     @XmlAttribute
     protected String id;
 
diff --git a/base/common/src/com/netscape/certsrv/profile/ProfileResource.java b/base/common/src/com/netscape/certsrv/profile/ProfileResource.java
index b37b38f320c0b37102e6edad145bf3ffb39787ee..06dd785c3db2f0dc4feb78b78a2beecdd64d43d1 100644
--- a/base/common/src/com/netscape/certsrv/profile/ProfileResource.java
+++ b/base/common/src/com/netscape/certsrv/profile/ProfileResource.java
@@ -10,6 +10,9 @@ import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import org.jboss.resteasy.annotations.ClientResponseType;
 
 import com.netscape.certsrv.acls.ACLMapping;
 import com.netscape.certsrv.authentication.AuthMethodMapping;
@@ -30,10 +33,11 @@ public interface ProfileResource {
     public ProfileData retrieveProfile(@PathParam("id") String id);
 
     @POST
+    @ClientResponseType(entityType=ProfileData.class)
     @ACLMapping("profile.create")
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public void createProfile(ProfileData data);
+    public Response createProfile(ProfileData data);
 
     @POST
     @Path("{id}")
@@ -43,10 +47,11 @@ public interface ProfileResource {
 
     @PUT
     @Path("{id}")
+    @ClientResponseType(entityType=ProfileData.class)
     @ACLMapping("profile.modify")
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public void modifyProfile(@PathParam("id") String id, ProfileData data);
+    public Response modifyProfile(@PathParam("id") String id, ProfileData data);
 
     @DELETE
     @Path("{id}")
diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileAddCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileAddCLI.java
index 17f7998f9aa8a9ef12ed131bdc6c40f97a08fd8c..8e95b832b5b30a0d8e867e34426c0de880bb4412 100644
--- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileAddCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileAddCLI.java
@@ -51,8 +51,11 @@ public class ProfileAddCLI extends CLI {
 
         try {
             ProfileData data = ProfileCLI.readProfileFromFile(filename);
-            profileCLI.profileClient.createProfile(data);
+            data = profileCLI.profileClient.createProfile(data);
+
             MainCLI.printMessage("Added profile " + data.getId());
+
+            ProfileCLI.printProfile(data, profileCLI.getClient().getConfig().getServerURI());
         } catch (FileNotFoundException | JAXBException  e) {
             System.err.println("Error: " + e.getMessage());
             System.exit(-1);
diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileModifyCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileModifyCLI.java
index e1c5436724c5df627947744999faf6523794e7b6..9fce7d28831e06cf6cc40ffd2be93d9188eab043 100644
--- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileModifyCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileModifyCLI.java
@@ -51,8 +51,12 @@ public class ProfileModifyCLI extends CLI {
 
         try {
             ProfileData data = ProfileCLI.readProfileFromFile(filename);
-            profileCLI.profileClient.modifyProfile(data);
+            data = profileCLI.profileClient.modifyProfile(data);
+
             MainCLI.printMessage("Modified profile " + data.getId());
+
+            ProfileCLI.printProfile(data, profileCLI.getClient().getConfig().getServerURI());
+
         } catch (FileNotFoundException | JAXBException  e) {
             System.err.println("Error: " + e.getMessage());
             System.exit(-1);
diff --git a/base/server/cms/src/com/netscape/cms/servlet/profile/ProfileService.java b/base/server/cms/src/com/netscape/cms/servlet/profile/ProfileService.java
index edb8b22489fc7ddca3aae59dedcaaa869d75baff..7db0cddbe53c7ed157a073d6bf0b83539c74c68f 100644
--- a/base/server/cms/src/com/netscape/cms/servlet/profile/ProfileService.java
+++ b/base/server/cms/src/com/netscape/cms/servlet/profile/ProfileService.java
@@ -34,7 +34,9 @@ import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Request;
+import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriBuilder;
 import javax.ws.rs.core.UriInfo;
 
@@ -400,7 +402,8 @@ public class ProfileService extends PKIService implements ProfileResource {
         }
     }
 
-    public void createProfile(ProfileData data) {
+    @Override
+    public Response createProfile(ProfileData data) {
         if (ps == null) {
             CMS.debug("createProfile: ps is null");
             throw new PKIException("Error creating profile.  Profile Service not available");
@@ -445,6 +448,17 @@ public class ProfileService extends PKIService implements ProfileResource {
                     profileId,
                     ILogger.SUCCESS,
                     auditParams);
+
+            changeProfileData(data, profile);
+
+            ProfileData profileData = createProfileData(profileId);
+
+            return Response
+                    .created(profileData.getLink().getHref())
+                    .entity(profileData)
+                    .type(MediaType.APPLICATION_XML)
+                    .build();
+
         } catch (EBaseException | IOException e) {
             CMS.debug("createProfile: error in creating profile: " + e);
             e.printStackTrace();
@@ -458,11 +472,10 @@ public class ProfileService extends PKIService implements ProfileResource {
 
             throw new PKIException("Error in creating profile");
         }
-
-        changeProfileData(data, profile);
     }
 
-    public void modifyProfile(String profileId, ProfileData data) {
+    @Override
+    public Response modifyProfile(String profileId, ProfileData data) {
         if (ps == null) {
             CMS.debug("modifyProfile: ps is null");
             throw new PKIException("Error modifying profile.  Profile Service not available");
@@ -475,13 +488,19 @@ public class ProfileService extends PKIService implements ProfileResource {
                 throw new ProfileNotFoundException("Cannot modify profile `" + profileId +
                         "`.  Profile not found");
             }
+
+            changeProfileData(data, profile);
+
+            ProfileData profileData = createProfileData(profileId);
+            return Response
+                    .ok(profileData)
+                    .type(MediaType.APPLICATION_XML)
+                    .build();
         } catch (EBaseException e) {
             CMS.debug("modifyProfile: error obtaining profile `" + profileId + "`: " + e);
             e.printStackTrace();
             throw new PKIException("Error modifying profile.  Cannot obtain profile.");
         }
-
-        changeProfileData(data, profile);
     }
 
     private void changeProfileData(ProfileData data, IProfile profile) {
-- 
1.8.3.1

