>From 320ca68175088848cefb7c30c83e81f76aa80896 Mon Sep 17 00:00:00 2001
From: Ade Lee <alee@redhat.com>
Date: Mon, 29 Jul 2013 12:23:03 -0400
Subject: [PATCH] Fix various issues with Profile Interface

1. Fixed REST API as per review.
2. Add output for profile-show and profile-find
---
 .../src/com/netscape/cms/servlet/test/CATest.java  |  2 +
 base/ca/shared/webapps/ca/WEB-INF/web.xml          | 10 ++++
 .../netscape/certsrv/profile/ProfileDataInfo.java  | 21 +++++++++
 .../netscape/certsrv/profile/ProfileResource.java  | 12 ++---
 .../cms/servlet/profile/ProfileService.java        | 11 +++--
 .../com/netscape/cmstools/profile/ProfileCLI.java  | 54 +++++++++++++++++++++-
 .../netscape/cmstools/profile/ProfileShowCLI.java  | 21 ++++-----
 7 files changed, 106 insertions(+), 25 deletions(-)

diff --git a/base/ca/functional/src/com/netscape/cms/servlet/test/CATest.java b/base/ca/functional/src/com/netscape/cms/servlet/test/CATest.java
index 3134f581466cef6e796b8d376dbcc6ad228e2cfd..0268c2d7d851cb94db67991a6f1baf163f69b3d1 100644
--- a/base/ca/functional/src/com/netscape/cms/servlet/test/CATest.java
+++ b/base/ca/functional/src/com/netscape/cms/servlet/test/CATest.java
@@ -415,6 +415,8 @@ public class CATest {
         log(" \n Profile Information: \n");
         log("ProfileURL: " + info.getProfileURL());
         log("ProfileID: " + info.getProfileId());
+        log("ProfileName: " + info.getProfileName());
+        log("ProfileDescription: " + info.getProfileDescription());
     }
 
     private static void printProfileData(ProfileData info) {
diff --git a/base/ca/shared/webapps/ca/WEB-INF/web.xml b/base/ca/shared/webapps/ca/WEB-INF/web.xml
index 57bdae62e47313a69b769e722492e4cec582f35f..32f5786a13c35f9e60c89ca5125377ab712a3a8d 100644
--- a/base/ca/shared/webapps/ca/WEB-INF/web.xml
+++ b/base/ca/shared/webapps/ca/WEB-INF/web.xml
@@ -2461,6 +2461,16 @@
         </user-data-constraint>
     </security-constraint>
 
+    <security-constraint>
+        <web-resource-collection>
+            <web-resource-name>Profile Services</web-resource-name>
+            <url-pattern>/rest/profiles/*</url-pattern>
+        </web-resource-collection>
+        <user-data-constraint>
+            <transport-guarantee>CONFIDENTIAL</transport-guarantee>
+        </user-data-constraint>
+    </security-constraint>
+
     <login-config>
         <realm-name>Certificate Authority</realm-name>
     </login-config>
diff --git a/base/common/src/com/netscape/certsrv/profile/ProfileDataInfo.java b/base/common/src/com/netscape/certsrv/profile/ProfileDataInfo.java
index 22062309d979ab9430de8e9d4f4d2e8fbd1bf19b..c01d55d16ac63d02cae45bbf76ddc4aefc21e75a 100644
--- a/base/common/src/com/netscape/certsrv/profile/ProfileDataInfo.java
+++ b/base/common/src/com/netscape/certsrv/profile/ProfileDataInfo.java
@@ -36,6 +36,12 @@ public class ProfileDataInfo {
     @XmlElement
     protected String profileId;
 
+    @XmlElement
+    protected String profileName;
+
+    @XmlElement
+    protected String profileDescription;
+
     public ProfileDataInfo() {
         // required for JAXB (defaults)
     }
@@ -65,4 +71,19 @@ public class ProfileDataInfo {
         return profileId;
     }
 
+    public String getProfileName() {
+        return profileName;
+    }
+
+    public void setProfileName(String profileName) {
+        this.profileName = profileName;
+    }
+
+    public String getProfileDescription() {
+        return profileDescription;
+    }
+
+    public void setProfileDescription(String profileDescription) {
+        this.profileDescription = profileDescription;
+    }
 }
diff --git a/base/common/src/com/netscape/certsrv/profile/ProfileResource.java b/base/common/src/com/netscape/certsrv/profile/ProfileResource.java
index 317215afb943d76819cce7073ae2261743d597fd..5c733aa592ea4896637d9f6496140ee4259232e5 100644
--- a/base/common/src/com/netscape/certsrv/profile/ProfileResource.java
+++ b/base/common/src/com/netscape/certsrv/profile/ProfileResource.java
@@ -13,41 +13,39 @@ import javax.ws.rs.core.MediaType;
 
 import com.netscape.certsrv.authentication.AuthMethodMapping;
 
-@Path("")
+@Path("profiles")
 public interface ProfileResource {
 
     @GET
-    @Path("profiles")
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     public ProfileDataInfos listProfiles();
 
     @GET
-    @Path("profiles/{id}")
+    @Path("{id}")
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     public ProfileData retrieveProfile(@PathParam("id") String id);
 
     @POST
-    @Path("admin/profiles")
     @AuthMethodMapping("admin")
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     public void createProfile(ProfileData data);
 
     @POST
-    @Path("agent/profiles/{id}")
+    @Path("{id}")
     @AuthMethodMapping("agent")
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     public void modifyProfileState(@PathParam("id") String id, @QueryParam("action") String action);
 
     @PUT
-    @Path("admin/profiles/{id}")
+    @Path("{id}")
     @AuthMethodMapping("admin")
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     public void modifyProfile(@PathParam("id") String id, ProfileData data);
 
     @DELETE
-    @Path("admin/profiles/{id}")
+    @Path("{id}")
     @AuthMethodMapping("admin")
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     public void deleteProfile(@PathParam("id") String id);
diff --git a/base/common/src/com/netscape/cms/servlet/profile/ProfileService.java b/base/common/src/com/netscape/cms/servlet/profile/ProfileService.java
index e8adc9b2a051baaa2d1009ba31785c25f60bc6a5..734a527d847fbfa86554fa3d1141024ce248c74a 100644
--- a/base/common/src/com/netscape/cms/servlet/profile/ProfileService.java
+++ b/base/common/src/com/netscape/cms/servlet/profile/ProfileService.java
@@ -275,13 +275,14 @@ public class ProfileService extends PKIService implements ProfileResource {
         ret = new ProfileDataInfo();
 
         ret.setProfileId(profileId);
+        ret.setProfileName(profile.getName(getLocale()));
+        ret.setProfileDescription(profile.getDescription(getLocale()));
 
         UriBuilder profileBuilder = uriInfo.getBaseUriBuilder();
+        URI uri = profileBuilder.path(ProfileResource.class).path("{id}").
+                build(profileId);
 
-        // TODO - fix this once we figure out the auth problem
-        //URI uri = profileBuilder.path(ProfileResource.class).path("{id}").
-        //        build(profileId);
-
+        /*
         URI uri = null;
         if (visibleOnly) {
             uri = profileBuilder.path(ProfileResource.class).path("profiles").path("{id}")
@@ -289,7 +290,7 @@ public class ProfileService extends PKIService implements ProfileResource {
         } else {
             uri = profileBuilder.path(ProfileResource.class).path("agent").path("profiles")
                     .path("{id}").build(profileId);
-        }
+        }*/
 
         ret.setProfileURL(uri.toString());
 
diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileCLI.java
index 37b88bcb5c654c93d2e1b0a2fb353e8d1b556df6..1463b096eb572b43d8985da7b86d0863f5b20ac0 100644
--- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileCLI.java
@@ -3,8 +3,12 @@ package com.netscape.cmstools.profile;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
+import java.net.URI;
 import java.util.Arrays;
+import java.util.Locale;
+import java.util.Map;
 
+import javax.ws.rs.core.UriBuilder;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Marshaller;
@@ -12,9 +16,13 @@ import javax.xml.bind.Unmarshaller;
 
 import org.apache.commons.lang.StringUtils;
 
+import com.netscape.certsrv.profile.ProfileAttribute;
 import com.netscape.certsrv.profile.ProfileClient;
 import com.netscape.certsrv.profile.ProfileData;
 import com.netscape.certsrv.profile.ProfileDataInfo;
+import com.netscape.certsrv.profile.ProfileInput;
+import com.netscape.certsrv.profile.ProfileOutput;
+import com.netscape.certsrv.profile.ProfileResource;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -87,11 +95,53 @@ public class ProfileCLI extends CLI {
     public static void printProfileDataInfo(ProfileDataInfo info) {
         System.out.println("Profile ID:  " + info.getProfileId());
         System.out.println("Profile URL: " + info.getProfileURL());
+        System.out.println("Profile Name: " + info.getProfileName());
+        System.out.println("Profile Description: " + info.getProfileDescription());
     }
 
-    public static void printProfile(ProfileData profileData) {
-        // TODO Auto-generated method stub
+    public static void printProfile(ProfileData data, URI baseUri) {
 
+        UriBuilder profileBuilder = UriBuilder.fromUri(baseUri);
+        URI uri = profileBuilder.path(ProfileResource.class).path("{id}").
+                build(data.getId());
+
+        System.out.println("Profile ID:  " + data.getId());
+        System.out.println("Profile URL: " + uri.toString());
+        System.out.println("Profile Name: " + data.getName());
+        System.out.println("Profile Description: " + data.getDescription() + "\n");
+
+        System.out.println("Profile Inputs:  " + data.getId());
+        int count =0;
+        for (Map.Entry<String, ProfileInput> entry: data.getInputs().entrySet()) {
+            ProfileInput input = entry.getValue();
+            System.out.println("Input " + count + " Id: " + entry.getKey());
+            System.out.println("Input " + count + " Name: " + input.getName());
+            System.out.println("Input " + count + " Class: " + input.getClassId());
+            for (ProfileAttribute attr: input.getAttrs()) {
+                System.out.println("Input " + count + " Attribute Name: " + attr.getName());
+                System.out.println("Input " + count + " Attribute Description: " +
+                    attr.getDescriptor().getDescription(Locale.getDefault()));
+                System.out.println("Input " + count + " Attribute Syntax: " +
+                    attr.getDescriptor().getSyntax());
+            }
+            count ++;
+        }
+
+        count = 0;
+        for (Map.Entry<String, ProfileOutput> entry: data.getOutputs().entrySet()) {
+            ProfileOutput output = entry.getValue();
+            System.out.println("Output " + count + " Id: " + entry.getKey());
+            System.out.println("Output " + count + " Name: " + output.getName());
+            System.out.println("Output " + count + " Class: " + output.getClassId());
+            for (ProfileAttribute attr: output.getAttrs()) {
+                System.out.println("Output " + count + " Attribute Name: " + attr.getName());
+                System.out.println("Output " + count + " Attribute Description: " +
+                    attr.getDescriptor().getDescription(Locale.getDefault()));
+                System.out.println("Output " + count + " Attribute Syntax: " +
+                    attr.getDescriptor().getSyntax());
+            }
+            count ++;
+        }
     }
 
     public static void saveProfileToFile(String filename, ProfileData data)
diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileShowCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileShowCLI.java
index de83c1eb95474abd891cf6a5c8ccf5c99d460226..c3f8675f2e11a870d5d19020a2432a39bc1fb033 100644
--- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileShowCLI.java
@@ -48,24 +48,23 @@ public class ProfileShowCLI extends CLI {
         String filename = null;
         if (cmd.hasOption("output")) {
             filename = cmd.getOptionValue("output");
-        } else {
-            System.err.println("Error: Missing output file name.");
-            printHelp();
-            System.exit(-1);
-        }
 
-        if (filename == null || filename.trim().length() == 0) {
-            System.err.println("Error: Missing output file name.");
-            printHelp();
-            System.exit(-1);
+            if (filename == null || filename.trim().length() == 0) {
+                System.err.println("Error: Missing output file name.");
+                printHelp();
+                System.exit(-1);
+            }
         }
 
         ProfileData profileData = parent.client.retrieveProfile(profileId);
 
         MainCLI.printMessage("Profile \"" + profileId + "\"");
 
-        ProfileCLI.printProfile(profileData);
-        ProfileCLI.saveProfileToFile(filename, profileData);
+        if (filename != null) {
+            ProfileCLI.saveProfileToFile(filename, profileData);
+        } else {
+            ProfileCLI.printProfile(profileData, parent.parent.config.getServerURI());
+        }
     }
 
 }
-- 
1.8.1.4

