From daf19a535970b6f193f0979589aaa959c912e701 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Tue, 24 Sep 2013 10:12:27 -0400
Subject: [PATCH] Added TPS profile resource.

A new REST service and clients have been added to manage the profiles
in the TPS configuration file.

Ticket #652
---
 .../src/com/netscape/certsrv/tps/TPSClient.java    |   2 +
 .../certsrv/tps/profile/ProfileClient.java         |  70 +++++++
 .../certsrv/tps/profile/ProfileCollection.java     |  38 ++++
 .../netscape/certsrv/tps/profile/ProfileData.java  | 232 +++++++++++++++++++++
 .../certsrv/tps/profile/ProfileResource.java       |  71 +++++++
 .../src/com/netscape/cmstools/cli/TPSCLI.java      |   2 +
 .../cmstools/tps/profile/ProfileAddCLI.java        |  96 +++++++++
 .../netscape/cmstools/tps/profile/ProfileCLI.java  |  73 +++++++
 .../cmstools/tps/profile/ProfileFindCLI.java       |  94 +++++++++
 .../cmstools/tps/profile/ProfileModifyCLI.java     | 101 +++++++++
 .../cmstools/tps/profile/ProfileRemoveCLI.java     |  53 +++++
 .../cmstools/tps/profile/ProfileShowCLI.java       |  87 ++++++++
 .../org/dogtagpki/server/tps/TPSApplication.java   |   4 +
 .../src/org/dogtagpki/server/tps/TPSSubsystem.java |   7 +
 .../server/tps/profile/ProfileDatabase.java        | 131 ++++++++++++
 .../server/tps/profile/ProfileRecord.java          | 112 ++++++++++
 .../server/tps/profile/ProfileService.java         | 219 +++++++++++++++++++
 17 files changed, 1392 insertions(+)
 create mode 100644 base/common/src/com/netscape/certsrv/tps/profile/ProfileClient.java
 create mode 100644 base/common/src/com/netscape/certsrv/tps/profile/ProfileCollection.java
 create mode 100644 base/common/src/com/netscape/certsrv/tps/profile/ProfileData.java
 create mode 100644 base/common/src/com/netscape/certsrv/tps/profile/ProfileResource.java
 create mode 100644 base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileAddCLI.java
 create mode 100644 base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileCLI.java
 create mode 100644 base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileFindCLI.java
 create mode 100644 base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileModifyCLI.java
 create mode 100644 base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileRemoveCLI.java
 create mode 100644 base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileShowCLI.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/profile/ProfileDatabase.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/profile/ProfileRecord.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/profile/ProfileService.java

diff --git a/base/common/src/com/netscape/certsrv/tps/TPSClient.java b/base/common/src/com/netscape/certsrv/tps/TPSClient.java
index b35893505d304f987ed1f0fe25ebfe7affca087b..42724259e1a0a54352e2334c7d31b559fc0f376f 100644
--- a/base/common/src/com/netscape/certsrv/tps/TPSClient.java
+++ b/base/common/src/com/netscape/certsrv/tps/TPSClient.java
@@ -27,6 +27,7 @@ import com.netscape.certsrv.tps.authenticator.AuthenticatorClient;
 import com.netscape.certsrv.tps.cert.TPSCertClient;
 import com.netscape.certsrv.tps.config.ConfigClient;
 import com.netscape.certsrv.tps.connection.ConnectionClient;
+import com.netscape.certsrv.tps.profile.ProfileClient;
 import com.netscape.certsrv.tps.token.TokenClient;
 import com.netscape.certsrv.user.UserClient;
 
@@ -47,6 +48,7 @@ public class TPSClient extends SubsystemClient {
         addClient(new ConfigClient(client, name));
         addClient(new ConnectionClient(client, name));
         addClient(new GroupClient(client, name));
+        addClient(new ProfileClient(client, name));
         addClient(new TokenClient(client, name));
         addClient(new UserClient(client, name));
     }
diff --git a/base/common/src/com/netscape/certsrv/tps/profile/ProfileClient.java b/base/common/src/com/netscape/certsrv/tps/profile/ProfileClient.java
new file mode 100644
index 0000000000000000000000000000000000000000..bc989e293c5f84020d46d213f740cf2c1f556c94
--- /dev/null
+++ b/base/common/src/com/netscape/certsrv/tps/profile/ProfileClient.java
@@ -0,0 +1,70 @@
+//--- BEGIN COPYRIGHT BLOCK ---
+//This program is free software; you can redistribute it and/or modify
+//it under the terms of the GNU General Public License as published by
+//the Free Software Foundation; version 2 of the License.
+//
+//This program is distributed in the hope that it will be useful,
+//but WITHOUT ANY WARRANTY; without even the implied warranty of
+//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//GNU General Public License for more details.
+//
+//You should have received a copy of the GNU General Public License along
+//with this program; if not, write to the Free Software Foundation, Inc.,
+//51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+//(C) 2013 Red Hat, Inc.
+//All rights reserved.
+//--- END COPYRIGHT BLOCK ---
+package com.netscape.certsrv.tps.profile;
+
+import java.net.URISyntaxException;
+
+import org.jboss.resteasy.client.ClientResponse;
+
+import com.netscape.certsrv.client.Client;
+import com.netscape.certsrv.client.PKIClient;
+
+/**
+ * @author Endi S. Dewata
+ */
+public class ProfileClient extends Client {
+
+    public ProfileResource resource;
+
+    public ProfileClient(PKIClient client) throws URISyntaxException {
+        this(client, client.getSubsystem());
+    }
+
+    public ProfileClient(PKIClient client, String subsystem) throws URISyntaxException {
+        super(client, subsystem, "profile");
+        init();
+    }
+
+    public void init() throws URISyntaxException {
+        resource = createProxy(ProfileResource.class);
+    }
+
+    public ProfileCollection findProfiles(Integer start, Integer size) {
+        return resource.findProfiles(start, size);
+    }
+
+    public ProfileData getProfile(String profileID) {
+        return resource.getProfile(profileID);
+    }
+
+    public ProfileData addProfile(ProfileData profileData) {
+        @SuppressWarnings("unchecked")
+        ClientResponse<ProfileData> response = (ClientResponse<ProfileData>)resource.addProfile(profileData);
+        return client.getEntity(response);
+    }
+
+    public ProfileData updateProfile(String profileID, ProfileData profileData) {
+        @SuppressWarnings("unchecked")
+        ClientResponse<ProfileData> response = (ClientResponse<ProfileData>)resource.updateProfile(profileID, profileData);
+        return client.getEntity(response);
+    }
+
+    public void removeProfile(String profileID) {
+        resource.removeProfile(profileID);
+    }
+}
diff --git a/base/common/src/com/netscape/certsrv/tps/profile/ProfileCollection.java b/base/common/src/com/netscape/certsrv/tps/profile/ProfileCollection.java
new file mode 100644
index 0000000000000000000000000000000000000000..a802cb680c73c0894a225b50434fed44870fcb39
--- /dev/null
+++ b/base/common/src/com/netscape/certsrv/tps/profile/ProfileCollection.java
@@ -0,0 +1,38 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+
+package com.netscape.certsrv.tps.profile;
+
+import java.util.Collection;
+
+import javax.xml.bind.annotation.XmlElementRef;
+import javax.xml.bind.annotation.XmlRootElement;
+
+import com.netscape.certsrv.base.DataCollection;
+
+/**
+ * @author Endi S. Dewata
+ */
+@XmlRootElement(name="Profiles")
+public class ProfileCollection extends DataCollection<ProfileData> {
+
+    @XmlElementRef
+    public Collection<ProfileData> getEntries() {
+        return super.getEntries();
+    }
+}
diff --git a/base/common/src/com/netscape/certsrv/tps/profile/ProfileData.java b/base/common/src/com/netscape/certsrv/tps/profile/ProfileData.java
new file mode 100644
index 0000000000000000000000000000000000000000..c4bbc037e189bd689d528e928e1d5515be49836c
--- /dev/null
+++ b/base/common/src/com/netscape/certsrv/tps/profile/ProfileData.java
@@ -0,0 +1,232 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+
+package com.netscape.certsrv.tps.profile;
+
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlValue;
+import javax.xml.bind.annotation.adapters.XmlAdapter;
+import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
+
+import org.jboss.resteasy.plugins.providers.atom.Link;
+
+/**
+ * @author Endi S. Dewata
+ */
+@XmlRootElement(name="Profile")
+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();
+        }
+    }
+
+    String id;
+    String status;
+    Map<String, String> properties = new LinkedHashMap<String, String>();
+
+    Link link;
+
+    @XmlAttribute(name="id")
+    public String getID() {
+        return id;
+    }
+
+    public void setID(String id) {
+        this.id = id;
+    }
+
+    @XmlElement(name="Status")
+    public String getStatus() {
+        return status;
+    }
+
+    public void setStatus(String status) {
+        this.status = status;
+    }
+
+    @XmlElement(name="Properties")
+    @XmlJavaTypeAdapter(MapAdapter.class)
+    public Map<String, String> getProperties() {
+        return properties;
+    }
+
+    public void setProperties(Map<String, String> properties) {
+        this.properties.clear();
+        this.properties.putAll(properties);
+    }
+
+    public Collection<String> getPropertyNames() {
+        return properties.keySet();
+    }
+
+    public String getProperty(String name) {
+        return properties.get(name);
+    }
+
+    public void setProperty(String name, String value) {
+        properties.put(name, value);
+    }
+
+    public String removeProperty(String name) {
+        return properties.remove(name);
+    }
+
+    public static class MapAdapter extends XmlAdapter<PropertyList, Map<String, String>> {
+
+        public PropertyList marshal(Map<String, String> map) {
+            PropertyList list = new PropertyList();
+            for (Map.Entry<String, String> entry : map.entrySet()) {
+                Property property = new Property();
+                property.name = entry.getKey();
+                property.value = entry.getValue();
+                list.properties.add(property);
+            }
+            return list;
+        }
+
+        public Map<String, String> unmarshal(PropertyList list) {
+            Map<String, String> map = new LinkedHashMap<String, String>();
+            for (Property property : list.properties) {
+                map.put(property.name, property.value);
+            }
+            return map;
+        }
+    }
+
+    public static class PropertyList {
+        @XmlElement(name="Property")
+        public List<Property> properties = new ArrayList<Property>();
+    }
+
+    public static class Property {
+
+        @XmlAttribute
+        public String name;
+
+        @XmlValue
+        public String value;
+    }
+
+    @XmlElement(name="Link")
+    public Link getLink() {
+        return link;
+    }
+
+    public void setLink(Link link) {
+        this.link = link;
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((id == null) ? 0 : id.hashCode());
+        result = prime * result + ((link == null) ? 0 : link.hashCode());
+        result = prime * result + ((properties == null) ? 0 : properties.hashCode());
+        result = prime * result + ((status == null) ? 0 : status.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        ProfileData other = (ProfileData) obj;
+        if (id == null) {
+            if (other.id != null)
+                return false;
+        } else if (!id.equals(other.id))
+            return false;
+        if (link == null) {
+            if (other.link != null)
+                return false;
+        } else if (!link.equals(other.link))
+            return false;
+        if (properties == null) {
+            if (other.properties != null)
+                return false;
+        } else if (!properties.equals(other.properties))
+            return false;
+        if (status == null) {
+            if (other.status != null)
+                return false;
+        } else if (!status.equals(other.status))
+            return false;
+        return true;
+    }
+
+    public String toString() {
+        try {
+            StringWriter sw = new StringWriter();
+            marshaller.marshal(this, sw);
+            return sw.toString();
+
+        } catch (Exception e) {
+            return super.toString();
+        }
+    }
+
+    public static ProfileData valueOf(String string) throws Exception {
+        try {
+            return (ProfileData)unmarshaller.unmarshal(new StringReader(string));
+        } catch (Exception e) {
+            return null;
+        }
+    }
+
+    public static void main(String args[]) throws Exception {
+
+        ProfileData before = new ProfileData();
+        before.setID("profile1");
+        before.setStatus("ENABLED");
+        before.setProperty("param1", "value1");
+        before.setProperty("param2", "value2");
+
+        String string = before.toString();
+        System.out.println(string);
+
+        ProfileData after = ProfileData.valueOf(string);
+        System.out.println(before.equals(after));
+    }
+}
diff --git a/base/common/src/com/netscape/certsrv/tps/profile/ProfileResource.java b/base/common/src/com/netscape/certsrv/tps/profile/ProfileResource.java
new file mode 100644
index 0000000000000000000000000000000000000000..e3a2caa178a18dabf8889565d19911d90c21ecd6
--- /dev/null
+++ b/base/common/src/com/netscape/certsrv/tps/profile/ProfileResource.java
@@ -0,0 +1,71 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package com.netscape.certsrv.tps.profile;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+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;
+
+
+/**
+ * @author Endi S. Dewata
+ */
+@Path("profiles")
+public interface ProfileResource {
+
+    @GET
+    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    public ProfileCollection findProfiles(
+            @QueryParam("start") Integer start,
+            @QueryParam("size") Integer size);
+
+    @GET
+    @Path("{profileID}")
+    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    public ProfileData getProfile(@PathParam("profileID") String profileID);
+
+    @POST
+    @ClientResponseType(entityType=ProfileData.class)
+    @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    public Response addProfile(ProfileData profileData);
+
+    @PUT
+    @Path("{profileID}")
+    @ClientResponseType(entityType=ProfileData.class)
+    @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    public Response updateProfile(
+            @PathParam("profileID") String profileID,
+            ProfileData profileData);
+
+    @DELETE
+    @Path("{profileID}")
+    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    public void removeProfile(@PathParam("profileID") String profileID);
+}
diff --git a/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java b/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java
index e29c6c4efc02a6d75f7ced8f9e69736ad4d3f77f..09596fae7718528affa381192036c949515585c7 100644
--- a/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java
@@ -26,6 +26,7 @@ import com.netscape.cmstools.tps.authenticator.AuthenticatorCLI;
 import com.netscape.cmstools.tps.cert.TPSCertCLI;
 import com.netscape.cmstools.tps.config.ConfigCLI;
 import com.netscape.cmstools.tps.connection.ConnectionCLI;
+import com.netscape.cmstools.tps.profile.ProfileCLI;
 import com.netscape.cmstools.tps.token.TokenCLI;
 import com.netscape.cmstools.user.UserCLI;
 
@@ -45,6 +46,7 @@ public class TPSCLI extends SubsystemCLI {
         addModule(new ConfigCLI(this));
         addModule(new ConnectionCLI(this));
         addModule(new GroupCLI(this));
+        addModule(new ProfileCLI(this));
         addModule(new TokenCLI(this));
         addModule(new UserCLI(this));
     }
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileAddCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileAddCLI.java
new file mode 100644
index 0000000000000000000000000000000000000000..d44ddc08e045351d8f7a88463a2748cd84bc8aba
--- /dev/null
+++ b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileAddCLI.java
@@ -0,0 +1,96 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+
+package com.netscape.cmstools.tps.profile;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+
+import com.netscape.certsrv.tps.profile.ProfileData;
+import com.netscape.cmstools.cli.CLI;
+import com.netscape.cmstools.cli.MainCLI;
+
+/**
+ * @author Endi S. Dewata
+ */
+public class ProfileAddCLI extends CLI {
+
+    public ProfileCLI profileCLI;
+
+    public ProfileAddCLI(ProfileCLI profileCLI) {
+        super("add", "Add profile", profileCLI);
+        this.profileCLI = profileCLI;
+    }
+
+    public void printHelp() {
+        formatter.printHelp(getFullName() + " <Profile ID> [OPTIONS...]", options);
+    }
+
+    public void execute(String[] args) throws Exception {
+
+        Option option = new Option(null, "input", true, "Input file containing profile properties.");
+        option.setArgName("file");
+        option.setRequired(true);
+        options.addOption(option);
+
+        CommandLine cmd = null;
+
+        try {
+            cmd = parser.parse(options, args);
+
+        } catch (Exception e) {
+            System.err.println("Error: " + e.getMessage());
+            printHelp();
+            System.exit(1);
+        }
+
+        String[] cmdArgs = cmd.getArgs();
+
+        if (cmdArgs.length != 0) {
+            printHelp();
+            System.exit(1);
+        }
+
+        String input = cmd.getOptionValue("input");
+
+        ProfileData profileData;
+
+        try (BufferedReader in = new BufferedReader(new FileReader(input));
+            StringWriter sw = new StringWriter();
+            PrintWriter out = new PrintWriter(sw, true)) {
+
+            String line;
+            while ((line = in.readLine()) != null) {
+                out.println(line);
+            }
+
+            profileData = ProfileData.valueOf(sw.toString());
+        }
+
+        profileData = profileCLI.profileClient.addProfile(profileData);
+
+        MainCLI.printMessage("Added profile \"" + profileData.getID() + "\"");
+
+        ProfileCLI.printProfileData(profileData, true);
+    }
+}
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileCLI.java
new file mode 100644
index 0000000000000000000000000000000000000000..8dfde38b438895ceddeca4a02715b7a8a2fd31aa
--- /dev/null
+++ b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileCLI.java
@@ -0,0 +1,73 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+
+package com.netscape.cmstools.tps.profile;
+
+import java.io.IOException;
+import java.util.Map;
+
+import org.jboss.resteasy.plugins.providers.atom.Link;
+
+import com.netscape.certsrv.tps.profile.ProfileClient;
+import com.netscape.certsrv.tps.profile.ProfileData;
+import com.netscape.cmstools.cli.CLI;
+
+/**
+ * @author Endi S. Dewata
+ */
+public class ProfileCLI extends CLI {
+
+    public ProfileClient profileClient;
+
+    public ProfileCLI(CLI parent) {
+        super("profile", "Profile management commands", parent);
+
+        addModule(new ProfileAddCLI(this));
+        addModule(new ProfileFindCLI(this));
+        addModule(new ProfileModifyCLI(this));
+        addModule(new ProfileRemoveCLI(this));
+        addModule(new ProfileShowCLI(this));
+    }
+
+    public void execute(String[] args) throws Exception {
+
+        client = parent.getClient();
+        profileClient = (ProfileClient)parent.getClient("profile");
+
+        super.execute(args);
+    }
+
+    public static void printProfileData(ProfileData profileData, boolean showProperties) throws IOException {
+        System.out.println("  Profile ID: " + profileData.getID());
+        if (profileData.getStatus() != null) System.out.println("  Status: " + profileData.getStatus());
+
+        if (showProperties) {
+            System.out.println("  Properties:");
+            Map<String, String> properties = profileData.getProperties();
+            for (String name : properties.keySet()) {
+                String value = properties.get(name);
+                System.out.println("    " + name + ": " + value);
+            }
+        }
+
+        Link link = profileData.getLink();
+        if (verbose && link != null) {
+            System.out.println("  Link: " + link.getHref());
+        }
+    }
+}
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileFindCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileFindCLI.java
new file mode 100644
index 0000000000000000000000000000000000000000..9f822f6ba5ad9f3045c41396527d578f96976552
--- /dev/null
+++ b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileFindCLI.java
@@ -0,0 +1,94 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+
+package com.netscape.cmstools.tps.profile;
+
+import java.util.Collection;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+
+import com.netscape.certsrv.tps.profile.ProfileCollection;
+import com.netscape.certsrv.tps.profile.ProfileData;
+import com.netscape.cmstools.cli.CLI;
+import com.netscape.cmstools.cli.MainCLI;
+
+/**
+ * @author Endi S. Dewata
+ */
+public class ProfileFindCLI extends CLI {
+
+    public ProfileCLI profileCLI;
+
+    public ProfileFindCLI(ProfileCLI profileCLI) {
+        super("find", "Find profiles", profileCLI);
+        this.profileCLI = profileCLI;
+    }
+
+    public void printHelp() {
+        formatter.printHelp(getFullName() + " [OPTIONS...]", options);
+    }
+
+    public void execute(String[] args) throws Exception {
+
+        Option option = new Option(null, "start", true, "Page start");
+        option.setArgName("start");
+        options.addOption(option);
+
+        option = new Option(null, "size", true, "Page size");
+        option.setArgName("size");
+        options.addOption(option);
+
+        CommandLine cmd = null;
+
+        try {
+            cmd = parser.parse(options, args);
+
+        } catch (Exception e) {
+            System.err.println("Error: " + e.getMessage());
+            printHelp();
+            System.exit(1);
+        }
+
+        String s = cmd.getOptionValue("start");
+        Integer start = s == null ? null : Integer.valueOf(s);
+
+        s = cmd.getOptionValue("size");
+        Integer size = s == null ? null : Integer.valueOf(s);
+
+        ProfileCollection result = profileCLI.profileClient.findProfiles(start, size);
+        Collection<ProfileData> profiles = result.getEntries();
+
+        MainCLI.printMessage(profiles.size() + " profile(s) matched");
+
+        boolean first = true;
+
+        for (ProfileData profileData : profiles) {
+
+            if (first) {
+                first = false;
+            } else {
+                System.out.println();
+            }
+
+            ProfileCLI.printProfileData(profileData, false);
+        }
+
+        MainCLI.printMessage("Number of entries returned " + profiles.size());
+    }
+}
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileModifyCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileModifyCLI.java
new file mode 100644
index 0000000000000000000000000000000000000000..124674aeb6debbb5139a8f83b0b77e9051b70b49
--- /dev/null
+++ b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileModifyCLI.java
@@ -0,0 +1,101 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+
+package com.netscape.cmstools.tps.profile;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+
+import com.netscape.certsrv.tps.profile.ProfileData;
+import com.netscape.cmstools.cli.CLI;
+import com.netscape.cmstools.cli.MainCLI;
+
+/**
+ * @author Endi S. Dewata
+ */
+public class ProfileModifyCLI extends CLI {
+
+    public ProfileCLI profileCLI;
+
+    public ProfileModifyCLI(ProfileCLI profileCLI) {
+        super("mod", "Modify profile", profileCLI);
+        this.profileCLI = profileCLI;
+    }
+
+    public void printHelp() {
+        formatter.printHelp(getFullName() + " <Profile ID> [OPTIONS...]", options);
+    }
+
+    public void execute(String[] args) throws Exception {
+
+        Option option = new Option(null, "status", true, "Status: ENABLED, DISABLED.");
+        option.setArgName("status");
+        options.addOption(option);
+
+        option = new Option(null, "input", true, "Input file containing profile properties.");
+        option.setArgName("file");
+        options.addOption(option);
+
+        CommandLine cmd = null;
+
+        try {
+            cmd = parser.parse(options, args);
+
+        } catch (Exception e) {
+            System.err.println("Error: " + e.getMessage());
+            printHelp();
+            System.exit(1);
+        }
+
+        String[] cmdArgs = cmd.getArgs();
+
+        if (cmdArgs.length != 1) {
+            printHelp();
+            System.exit(1);
+        }
+
+        String profileID = cmdArgs[0];
+        String status = cmd.getOptionValue("status");
+        String input = cmd.getOptionValue("input");
+
+        ProfileData profileData;
+
+        try (BufferedReader in = new BufferedReader(new FileReader(input));
+            StringWriter sw = new StringWriter();
+            PrintWriter out = new PrintWriter(sw, true)) {
+
+            String line;
+            while ((line = in.readLine()) != null) {
+                out.println(line);
+            }
+
+            profileData = ProfileData.valueOf(sw.toString());
+        }
+
+        profileData = profileCLI.profileClient.updateProfile(profileID, profileData);
+
+        MainCLI.printMessage("Modified profile \"" + profileID + "\"");
+
+        ProfileCLI.printProfileData(profileData, true);
+    }
+}
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileRemoveCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileRemoveCLI.java
new file mode 100644
index 0000000000000000000000000000000000000000..20913103f2099e9402e4f3515be526d4b7887f2b
--- /dev/null
+++ b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileRemoveCLI.java
@@ -0,0 +1,53 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+
+package com.netscape.cmstools.tps.profile;
+
+import com.netscape.cmstools.cli.CLI;
+import com.netscape.cmstools.cli.MainCLI;
+
+/**
+ * @author Endi S. Dewata
+ */
+public class ProfileRemoveCLI extends CLI {
+
+    public ProfileCLI profileCLI;
+
+    public ProfileRemoveCLI(ProfileCLI profileCLI) {
+        super("del", "Remove profile", profileCLI);
+        this.profileCLI = profileCLI;
+    }
+
+    public void printHelp() {
+        formatter.printHelp(getFullName() + " <Profile ID>", options);
+    }
+
+    public void execute(String[] args) throws Exception {
+
+        if (args.length != 1) {
+            printHelp();
+            System.exit(1);
+        }
+
+        String profileID = args[0];
+
+        profileCLI.profileClient.removeProfile(profileID);
+
+        MainCLI.printMessage("Deleted profile \"" + profileID + "\"");
+    }
+}
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileShowCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileShowCLI.java
new file mode 100644
index 0000000000000000000000000000000000000000..7c4c2fa1ebad7c3da9c98f2680d9ab3c1f0061fc
--- /dev/null
+++ b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileShowCLI.java
@@ -0,0 +1,87 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+
+package com.netscape.cmstools.tps.profile;
+
+import java.io.FileWriter;
+import java.io.PrintWriter;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+
+import com.netscape.certsrv.tps.profile.ProfileData;
+import com.netscape.cmstools.cli.CLI;
+import com.netscape.cmstools.cli.MainCLI;
+
+/**
+ * @author Endi S. Dewata
+ */
+public class ProfileShowCLI extends CLI {
+
+    public ProfileCLI profileCLI;
+
+    public ProfileShowCLI(ProfileCLI profileCLI) {
+        super("show", "Show profile", profileCLI);
+        this.profileCLI = profileCLI;
+    }
+
+    public void printHelp() {
+        formatter.printHelp(getFullName() + " <Profile ID>", options);
+    }
+
+    public void execute(String[] args) throws Exception {
+
+        Option option = new Option(null, "output", true, "Output file to store profile properties.");
+        option.setArgName("file");
+        options.addOption(option);
+
+        CommandLine cmd = null;
+
+        try {
+            cmd = parser.parse(options, args);
+
+        } catch (Exception e) {
+            System.err.println("Error: " + e.getMessage());
+            printHelp();
+            System.exit(1);
+        }
+
+        String[] cmdArgs = cmd.getArgs();
+
+        if (cmdArgs.length != 1) {
+            printHelp();
+            System.exit(1);
+        }
+
+        String profileID = args[0];
+        String output = cmd.getOptionValue("output");
+
+        ProfileData profileData = profileCLI.profileClient.getProfile(profileID);
+
+        if (output == null) {
+            MainCLI.printMessage("Profile \"" + profileID + "\"");
+            ProfileCLI.printProfileData(profileData, true);
+
+        } else {
+            try (PrintWriter out = new PrintWriter(new FileWriter(output))) {
+                out.println(profileData);
+            }
+            MainCLI.printMessage("Stored profile \"" + profileID + "\" into " + output);
+        }
+    }
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSApplication.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSApplication.java
index e90d8bbcc9bec2a2a30ae373412cc1359c624e33..104baa9789c1bc7138d6a68142951992496ff553 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSApplication.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSApplication.java
@@ -27,6 +27,7 @@ import org.dogtagpki.server.tps.cert.TPSCertService;
 import org.dogtagpki.server.tps.config.ConfigService;
 import org.dogtagpki.server.tps.connection.ConnectionService;
 import org.dogtagpki.server.tps.logging.ActivityService;
+import org.dogtagpki.server.tps.profile.ProfileService;
 import org.dogtagpki.server.tps.token.TokenService;
 
 import com.netscape.certsrv.base.PKIException;
@@ -82,6 +83,9 @@ public class TPSApplication extends Application {
         // connections
         classes.add(ConnectionService.class);
 
+        // profiles
+        classes.add(ProfileService.class);
+
         // tokens
         classes.add(TokenService.class);
 
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java
index 58ed122333af7b46536b9b9222a26ebb30e7a7a8..8b077a762435bbc40d4676efa8754b605bc99a71 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java
@@ -22,6 +22,7 @@ import org.dogtagpki.server.tps.cert.TPSCertDatabase;
 import org.dogtagpki.server.tps.config.ConfigDatabase;
 import org.dogtagpki.server.tps.connection.ConnectionDatabase;
 import org.dogtagpki.server.tps.logging.ActivityDatabase;
+import org.dogtagpki.server.tps.profile.ProfileDatabase;
 import org.dogtagpki.server.tps.token.TokenDatabase;
 import org.mozilla.jss.CryptoManager;
 import org.mozilla.jss.CryptoManager.NotInitializedException;
@@ -58,6 +59,7 @@ public class TPSSubsystem implements IAuthority, ISubsystem {
     public TPSCertDatabase certDatabase;
     public ConfigDatabase configDatabase;
     public ConnectionDatabase connectionDatabase;
+    public ProfileDatabase profileDatabase;
     public TokenDatabase tokenDatabase;
 
     @Override
@@ -90,6 +92,7 @@ public class TPSSubsystem implements IAuthority, ISubsystem {
         configDatabase = new ConfigDatabase();
         authenticatorDatabase = new AuthenticatorDatabase();
         connectionDatabase = new ConnectionDatabase();
+        profileDatabase = new ProfileDatabase();
 }
 
     @Override
@@ -157,6 +160,10 @@ public class TPSSubsystem implements IAuthority, ISubsystem {
         return connectionDatabase;
     }
 
+    public ProfileDatabase getProfileDatabase() {
+        return profileDatabase;
+    }
+
     public TokenDatabase getTokenDatabase() {
         return tokenDatabase;
     }
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/profile/ProfileDatabase.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/profile/ProfileDatabase.java
new file mode 100644
index 0000000000000000000000000000000000000000..0a125235b88a25420d8a92833c39e79345097def
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/profile/ProfileDatabase.java
@@ -0,0 +1,131 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+
+package org.dogtagpki.server.tps.profile;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Map;
+
+import org.dogtagpki.server.tps.config.ConfigDatabase;
+import org.dogtagpki.server.tps.config.ConfigRecord;
+
+import com.netscape.certsrv.apps.CMS;
+import com.netscape.cmscore.dbs.Database;
+
+/**
+ * This class provides access to the profiles in CS.cfg.
+ *
+ * @author Endi S. Dewata
+ */
+public class ProfileDatabase extends Database<ProfileRecord> {
+
+    public ProfileDatabase() {
+        super("Profile");
+    }
+
+    public ProfileRecord createProfileRecord(ConfigDatabase configDatabase, ConfigRecord configRecord, String profileID) {
+        ProfileRecord profileRecord = new ProfileRecord();
+        profileRecord.setID(profileID);
+        Map<String, String> properties = configDatabase.getProperties(configRecord, profileID);
+        profileRecord.setProperties(properties);
+        return profileRecord;
+    }
+
+    @Override
+    public Collection<ProfileRecord> getRecords() throws Exception {
+
+        Collection<ProfileRecord> result = new ArrayList<ProfileRecord>();
+        ConfigDatabase configDatabase = new ConfigDatabase();
+        ConfigRecord configRecord = configDatabase.getRecord("Profiles");
+
+        for (String profileID : configRecord.getKeys()) {
+            ProfileRecord profileRecord = createProfileRecord(configDatabase, configRecord, profileID);
+            result.add(profileRecord);
+        }
+
+        return result;
+    }
+
+    @Override
+    public ProfileRecord getRecord(String profileID) throws Exception {
+
+        ConfigDatabase configDatabase = new ConfigDatabase();
+        ConfigRecord configRecord = configDatabase.getRecord("Profiles");
+
+        return createProfileRecord(configDatabase, configRecord, profileID);
+    }
+
+    @Override
+    public void addRecord(String profileID, ProfileRecord profileRecord) throws Exception {
+
+        CMS.debug("ProfileDatabase.addRecord(\"" + profileID + "\")");
+        ConfigDatabase configDatabase = new ConfigDatabase();
+        ConfigRecord configRecord = configDatabase.getRecord("Profiles");
+
+        // validate new properties
+        Map<String, String> properties = profileRecord.getProperties();
+        configDatabase.validateProperties(configRecord, profileID, properties);
+
+        // add new profile
+        configRecord.addKey(profileID);
+        configDatabase.updateRecord("Profiles", configRecord);
+
+        // store new properties
+        configDatabase.addProperties(configRecord, profileID, properties);
+
+        configDatabase.commit();
+    }
+
+    @Override
+    public void updateRecord(String profileID, ProfileRecord profileRecord) throws Exception {
+
+        CMS.debug("ProfileDatabase.updateRecord(\"" + profileID + "\")");
+        ConfigDatabase configDatabase = new ConfigDatabase();
+        ConfigRecord configRecord = configDatabase.getRecord("Profiles");
+
+        // validate new properties
+        Map<String, String> properties = profileRecord.getProperties();
+        configDatabase.validateProperties(configRecord, profileID, properties);
+
+        // remove old properties
+        configDatabase.removeProperties(configRecord, profileID);
+
+        // add new properties
+        configDatabase.addProperties(configRecord, profileID, properties);
+
+        configDatabase.commit();
+    }
+
+    @Override
+    public void removeRecord(String profileID) throws Exception {
+
+        CMS.debug("ProfileDatabase.removeRecord(\"" + profileID + "\")");
+        ConfigDatabase configDatabase = new ConfigDatabase();
+        ConfigRecord configRecord = configDatabase.getRecord("Profiles");
+
+        // remove properties
+        configDatabase.removeProperties(configRecord, profileID);
+
+        // remove profile
+        configRecord.removeKey(profileID);
+        configDatabase.updateRecord("Profiles", configRecord);
+
+        configDatabase.commit();
+    }
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/profile/ProfileRecord.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/profile/ProfileRecord.java
new file mode 100644
index 0000000000000000000000000000000000000000..4a35841a31d6008e0a671afa8e398d004ed99cf4
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/profile/ProfileRecord.java
@@ -0,0 +1,112 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+
+package org.dogtagpki.server.tps.profile;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.TreeMap;
+
+
+/**
+ * @author Endi S. Dewata
+ */
+public class ProfileRecord {
+
+    String id;
+    String status;
+    Map<String, String> properties = new TreeMap<String, String>();
+
+    public String getID() {
+        return id;
+    }
+
+    public void setID(String id) {
+        this.id = id;
+    }
+
+    public String getStatus() {
+        return status;
+    }
+
+    public void setStatus(String status) {
+        this.status = status;
+    }
+
+    public Map<String, String> getProperties() {
+        return properties;
+    }
+
+    public void setProperties(Map<String, String> properties) {
+        this.properties.clear();
+        this.properties.putAll(properties);
+    }
+
+    public Collection<String> getPropertyNames() {
+        return properties.keySet();
+    }
+
+    public String getProperty(String name) {
+        return properties.get(name);
+    }
+
+    public void setProperty(String name, String value) {
+        properties.put(name, value);
+    }
+
+    public String removeProperty(String name) {
+        return properties.remove(name);
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((id == null) ? 0 : id.hashCode());
+        result = prime * result + ((properties == null) ? 0 : properties.hashCode());
+        result = prime * result + ((status == null) ? 0 : status.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        ProfileRecord other = (ProfileRecord) obj;
+        if (id == null) {
+            if (other.id != null)
+                return false;
+        } else if (!id.equals(other.id))
+            return false;
+        if (properties == null) {
+            if (other.properties != null)
+                return false;
+        } else if (!properties.equals(other.properties))
+            return false;
+        if (status == null) {
+            if (other.status != null)
+                return false;
+        } else if (!status.equals(other.status))
+            return false;
+        return true;
+    }
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/profile/ProfileService.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/profile/ProfileService.java
new file mode 100644
index 0000000000000000000000000000000000000000..4212eacb6f454bf0cb74e624df6eee3a940b7611
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/profile/ProfileService.java
@@ -0,0 +1,219 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+
+package org.dogtagpki.server.tps.profile;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URI;
+import java.net.URLEncoder;
+import java.util.Iterator;
+
+import javax.servlet.http.HttpServletRequest;
+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.UriInfo;
+
+import org.dogtagpki.server.tps.TPSSubsystem;
+import org.jboss.resteasy.plugins.providers.atom.Link;
+
+import com.netscape.certsrv.apps.CMS;
+import com.netscape.certsrv.base.PKIException;
+import com.netscape.certsrv.tps.profile.ProfileCollection;
+import com.netscape.certsrv.tps.profile.ProfileData;
+import com.netscape.certsrv.tps.profile.ProfileResource;
+import com.netscape.cms.servlet.base.PKIService;
+
+/**
+ * @author Endi S. Dewata
+ */
+public class ProfileService extends PKIService implements ProfileResource {
+
+    @Context
+    private UriInfo uriInfo;
+
+    @Context
+    private HttpHeaders headers;
+
+    @Context
+    private Request request;
+
+    @Context
+    private HttpServletRequest servletRequest;
+
+    public final static int DEFAULT_SIZE = 20;
+
+    public ProfileService() {
+        CMS.debug("ProfileService.<init>()");
+    }
+
+    public ProfileData createProfileData(ProfileRecord profileRecord) throws UnsupportedEncodingException {
+
+        String profileID = profileRecord.getID();
+
+        ProfileData profileData = new ProfileData();
+        profileData.setID(profileID);
+        profileData.setStatus(profileRecord.getStatus());
+        profileData.setProperties(profileRecord.getProperties());
+
+        profileID = URLEncoder.encode(profileID, "UTF-8");
+        URI uri = uriInfo.getBaseUriBuilder().path(ProfileResource.class).path("{profileID}").build(profileID);
+        profileData.setLink(new Link("self", uri));
+
+        return profileData;
+    }
+
+    public ProfileRecord createProfileRecord(ProfileData profileData) {
+
+        ProfileRecord profileRecord = new ProfileRecord();
+        profileRecord.setID(profileData.getID());
+        profileRecord.setStatus(profileData.getStatus());
+        profileRecord.setProperties(profileData.getProperties());
+
+        return profileRecord;
+    }
+
+    @Override
+    public ProfileCollection findProfiles(Integer start, Integer size) {
+
+        CMS.debug("ProfileService.findProfiles()");
+
+        try {
+            start = start == null ? 0 : start;
+            size = size == null ? DEFAULT_SIZE : size;
+
+            TPSSubsystem subsystem = (TPSSubsystem)CMS.getSubsystem(TPSSubsystem.ID);
+            ProfileDatabase database = subsystem.getProfileDatabase();
+
+            Iterator<ProfileRecord> profiles = database.getRecords().iterator();
+
+            ProfileCollection response = new ProfileCollection();
+
+            int i = 0;
+
+            // skip to the start of the page
+            for ( ; i<start && profiles.hasNext(); i++) profiles.next();
+
+            // return entries up to the page size
+            for ( ; i<start+size && profiles.hasNext(); i++) {
+                response.addEntry(createProfileData(profiles.next()));
+            }
+
+            // count the total entries
+            for ( ; profiles.hasNext(); i++) profiles.next();
+
+            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) {
+                URI uri = uriInfo.getRequestUriBuilder().replaceQueryParam("start", start+size).build();
+                response.addLink(new Link("next", uri));
+            }
+
+            return response;
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new PKIException(e.getMessage());
+        }
+    }
+
+    @Override
+    public ProfileData getProfile(String profileID) {
+
+        CMS.debug("ProfileService.getProfile(\"" + profileID + "\")");
+
+        try {
+            TPSSubsystem subsystem = (TPSSubsystem)CMS.getSubsystem(TPSSubsystem.ID);
+            ProfileDatabase database = subsystem.getProfileDatabase();
+
+            return createProfileData(database.getRecord(profileID));
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new PKIException(e.getMessage());
+        }
+    }
+
+    @Override
+    public Response addProfile(ProfileData profileData) {
+
+        CMS.debug("ProfileService.addProfile(\"" + profileData.getID() + "\")");
+
+        try {
+            TPSSubsystem subsystem = (TPSSubsystem)CMS.getSubsystem(TPSSubsystem.ID);
+            ProfileDatabase database = subsystem.getProfileDatabase();
+
+            database.addRecord(profileData.getID(), createProfileRecord(profileData));
+            profileData = createProfileData(database.getRecord(profileData.getID()));
+
+            return Response
+                    .created(profileData.getLink().getHref())
+                    .entity(profileData)
+                    .type(MediaType.APPLICATION_XML)
+                    .build();
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new PKIException(e.getMessage());
+        }
+    }
+
+    @Override
+    public Response updateProfile(String profileID, ProfileData profileData) {
+
+        CMS.debug("ProfileService.updateProfile(\"" + profileID + "\")");
+
+        try {
+            TPSSubsystem subsystem = (TPSSubsystem)CMS.getSubsystem(TPSSubsystem.ID);
+            ProfileDatabase database = subsystem.getProfileDatabase();
+
+            database.updateRecord(profileData.getID(), createProfileRecord(profileData));
+            profileData = createProfileData(database.getRecord(profileID));
+
+            return Response
+                    .ok(profileData)
+                    .type(MediaType.APPLICATION_XML)
+                    .build();
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new PKIException(e.getMessage());
+        }
+    }
+
+    @Override
+    public void removeProfile(String profileID) {
+
+        CMS.debug("ProfileService.removeProfile(\"" + profileID + "\")");
+
+        try {
+            TPSSubsystem subsystem = (TPSSubsystem)CMS.getSubsystem(TPSSubsystem.ID);
+            ProfileDatabase database = subsystem.getProfileDatabase();
+            database.removeRecord(profileID);
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new PKIException(e.getMessage());
+        }
+    }
+}
-- 
1.8.1.4

