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

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

Ticket #652
---
 .../src/com/netscape/certsrv/tps/TPSClient.java    |   2 +
 .../certsrv/tps/profile/ProfileMappingClient.java  |  70 +++++++
 .../tps/profile/ProfileMappingCollection.java      |  38 ++++
 .../certsrv/tps/profile/ProfileMappingData.java    | 232 +++++++++++++++++++++
 .../tps/profile/ProfileMappingResource.java        |  71 +++++++
 .../netscape/cmstools/tps/profile/ProfileCLI.java  |   2 +
 .../cmstools/tps/profile/ProfileMappingAddCLI.java |  96 +++++++++
 .../{ProfileCLI.java => ProfileMappingCLI.java}    |  34 +--
 .../tps/profile/ProfileMappingFindCLI.java         |  94 +++++++++
 .../tps/profile/ProfileMappingModifyCLI.java       | 101 +++++++++
 .../tps/profile/ProfileMappingRemoveCLI.java       |  53 +++++
 .../tps/profile/ProfileMappingShowCLI.java         |  87 ++++++++
 .../org/dogtagpki/server/tps/TPSApplication.java   |   2 +
 .../src/org/dogtagpki/server/tps/TPSSubsystem.java |   7 +
 .../server/tps/profile/ProfileMappingDatabase.java | 132 ++++++++++++
 .../server/tps/profile/ProfileMappingRecord.java   | 112 ++++++++++
 .../server/tps/profile/ProfileMappingService.java  | 219 +++++++++++++++++++
 17 files changed, 1335 insertions(+), 17 deletions(-)
 create mode 100644 base/common/src/com/netscape/certsrv/tps/profile/ProfileMappingClient.java
 create mode 100644 base/common/src/com/netscape/certsrv/tps/profile/ProfileMappingCollection.java
 create mode 100644 base/common/src/com/netscape/certsrv/tps/profile/ProfileMappingData.java
 create mode 100644 base/common/src/com/netscape/certsrv/tps/profile/ProfileMappingResource.java
 create mode 100644 base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingAddCLI.java
 copy base/java-tools/src/com/netscape/cmstools/tps/profile/{ProfileCLI.java => ProfileMappingCLI.java} (56%)
 create mode 100644 base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingFindCLI.java
 create mode 100644 base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingModifyCLI.java
 create mode 100644 base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingRemoveCLI.java
 create mode 100644 base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingShowCLI.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/profile/ProfileMappingDatabase.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/profile/ProfileMappingRecord.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/profile/ProfileMappingService.java

diff --git a/base/common/src/com/netscape/certsrv/tps/TPSClient.java b/base/common/src/com/netscape/certsrv/tps/TPSClient.java
index 42724259e1a0a54352e2334c7d31b559fc0f376f..1c4cd3860e7625fc8e23878a60690e0ab151b140 100644
--- a/base/common/src/com/netscape/certsrv/tps/TPSClient.java
+++ b/base/common/src/com/netscape/certsrv/tps/TPSClient.java
@@ -28,6 +28,7 @@ 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.profile.ProfileMappingClient;
 import com.netscape.certsrv.tps.token.TokenClient;
 import com.netscape.certsrv.user.UserClient;
 
@@ -49,6 +50,7 @@ public class TPSClient extends SubsystemClient {
         addClient(new ConnectionClient(client, name));
         addClient(new GroupClient(client, name));
         addClient(new ProfileClient(client, name));
+        addClient(new ProfileMappingClient(client, name));
         addClient(new TokenClient(client, name));
         addClient(new UserClient(client, name));
     }
diff --git a/base/common/src/com/netscape/certsrv/tps/profile/ProfileMappingClient.java b/base/common/src/com/netscape/certsrv/tps/profile/ProfileMappingClient.java
new file mode 100644
index 0000000000000000000000000000000000000000..dd02825c87b00177dc3533a7b6ededdcda49c692
--- /dev/null
+++ b/base/common/src/com/netscape/certsrv/tps/profile/ProfileMappingClient.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 ProfileMappingClient extends Client {
+
+    public ProfileMappingResource resource;
+
+    public ProfileMappingClient(PKIClient client) throws URISyntaxException {
+        this(client, client.getSubsystem());
+    }
+
+    public ProfileMappingClient(PKIClient client, String subsystem) throws URISyntaxException {
+        super(client, subsystem, "profile-mapping");
+        init();
+    }
+
+    public void init() throws URISyntaxException {
+        resource = createProxy(ProfileMappingResource.class);
+    }
+
+    public ProfileMappingCollection findProfileMappings(Integer start, Integer size) {
+        return resource.findProfileMappings(start, size);
+    }
+
+    public ProfileMappingData getProfileMapping(String profileMappingID) {
+        return resource.getProfileMapping(profileMappingID);
+    }
+
+    public ProfileMappingData addProfileMapping(ProfileMappingData profileMappingData) {
+        @SuppressWarnings("unchecked")
+        ClientResponse<ProfileMappingData> response = (ClientResponse<ProfileMappingData>)resource.addProfileMapping(profileMappingData);
+        return client.getEntity(response);
+    }
+
+    public ProfileMappingData updateProfileMapping(String profileMappingID, ProfileMappingData profileMappingData) {
+        @SuppressWarnings("unchecked")
+        ClientResponse<ProfileMappingData> response = (ClientResponse<ProfileMappingData>)resource.updateProfileMapping(profileMappingID, profileMappingData);
+        return client.getEntity(response);
+    }
+
+    public void removeProfileMapping(String profileMappingID) {
+        resource.removeProfileMapping(profileMappingID);
+    }
+}
diff --git a/base/common/src/com/netscape/certsrv/tps/profile/ProfileMappingCollection.java b/base/common/src/com/netscape/certsrv/tps/profile/ProfileMappingCollection.java
new file mode 100644
index 0000000000000000000000000000000000000000..9f3bbbb9463785a376182ad0fb487ebcb371cbd4
--- /dev/null
+++ b/base/common/src/com/netscape/certsrv/tps/profile/ProfileMappingCollection.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="ProfileMappings")
+public class ProfileMappingCollection extends DataCollection<ProfileMappingData> {
+
+    @XmlElementRef
+    public Collection<ProfileMappingData> getEntries() {
+        return super.getEntries();
+    }
+}
diff --git a/base/common/src/com/netscape/certsrv/tps/profile/ProfileMappingData.java b/base/common/src/com/netscape/certsrv/tps/profile/ProfileMappingData.java
new file mode 100644
index 0000000000000000000000000000000000000000..2ca955b4e9eed5da5112ca25028cf3a688acd0ce
--- /dev/null
+++ b/base/common/src/com/netscape/certsrv/tps/profile/ProfileMappingData.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="ProfileMapping")
+public class ProfileMappingData {
+
+    public static Marshaller marshaller;
+    public static Unmarshaller unmarshaller;
+
+    static {
+        try {
+            marshaller = JAXBContext.newInstance(ProfileMappingData.class).createMarshaller();
+            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
+            unmarshaller = JAXBContext.newInstance(ProfileMappingData.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;
+        ProfileMappingData other = (ProfileMappingData) 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 ProfileMappingData valueOf(String string) throws Exception {
+        try {
+            return (ProfileMappingData)unmarshaller.unmarshal(new StringReader(string));
+        } catch (Exception e) {
+            return null;
+        }
+    }
+
+    public static void main(String args[]) throws Exception {
+
+        ProfileMappingData before = new ProfileMappingData();
+        before.setID("profileMapping1");
+        before.setStatus("ENABLED");
+        before.setProperty("param1", "value1");
+        before.setProperty("param2", "value2");
+
+        String string = before.toString();
+        System.out.println(string);
+
+        ProfileMappingData after = ProfileMappingData.valueOf(string);
+        System.out.println(before.equals(after));
+    }
+}
diff --git a/base/common/src/com/netscape/certsrv/tps/profile/ProfileMappingResource.java b/base/common/src/com/netscape/certsrv/tps/profile/ProfileMappingResource.java
new file mode 100644
index 0000000000000000000000000000000000000000..20e3603107879b181ea1232812b6206dd9a4dc2e
--- /dev/null
+++ b/base/common/src/com/netscape/certsrv/tps/profile/ProfileMappingResource.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("profile-mappings")
+public interface ProfileMappingResource {
+
+    @GET
+    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    public ProfileMappingCollection findProfileMappings(
+            @QueryParam("start") Integer start,
+            @QueryParam("size") Integer size);
+
+    @GET
+    @Path("{profileMappingID}")
+    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    public ProfileMappingData getProfileMapping(@PathParam("profileMappingID") String profileMappingID);
+
+    @POST
+    @ClientResponseType(entityType=ProfileMappingData.class)
+    @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    public Response addProfileMapping(ProfileMappingData profileMappingData);
+
+    @PUT
+    @Path("{profileMappingID}")
+    @ClientResponseType(entityType=ProfileMappingData.class)
+    @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    public Response updateProfileMapping(
+            @PathParam("profileMappingID") String profileMappingID,
+            ProfileMappingData profileMappingData);
+
+    @DELETE
+    @Path("{profileMappingID}")
+    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    public void removeProfileMapping(@PathParam("profileMappingID") String profileMappingID);
+}
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
index 8dfde38b438895ceddeca4a02715b7a8a2fd31aa..8b02f2e577979a94b435c60b0577fc08b455a3f2 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileCLI.java
@@ -42,6 +42,8 @@ public class ProfileCLI extends CLI {
         addModule(new ProfileModifyCLI(this));
         addModule(new ProfileRemoveCLI(this));
         addModule(new ProfileShowCLI(this));
+
+        addModule(new ProfileMappingCLI(this));
     }
 
     public void execute(String[] args) throws Exception {
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingAddCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingAddCLI.java
new file mode 100644
index 0000000000000000000000000000000000000000..277fc4efe7b1a7e6c48ba7b4d104d19f2b00db9b
--- /dev/null
+++ b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingAddCLI.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.ProfileMappingData;
+import com.netscape.cmstools.cli.CLI;
+import com.netscape.cmstools.cli.MainCLI;
+
+/**
+ * @author Endi S. Dewata
+ */
+public class ProfileMappingAddCLI extends CLI {
+
+    public ProfileMappingCLI profileMappingCLI;
+
+    public ProfileMappingAddCLI(ProfileMappingCLI profileMappingCLI) {
+        super("add", "Add profile mapping", profileMappingCLI);
+        this.profileMappingCLI = profileMappingCLI;
+    }
+
+    public void printHelp() {
+        formatter.printHelp(getFullName() + " <ProfileMapping ID> [OPTIONS...]", options);
+    }
+
+    public void execute(String[] args) throws Exception {
+
+        Option option = new Option(null, "input", true, "Input file containing profile mapping 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");
+
+        ProfileMappingData profileMappingData;
+
+        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);
+            }
+
+            profileMappingData = ProfileMappingData.valueOf(sw.toString());
+        }
+
+        profileMappingData = profileMappingCLI.profileMappingClient.addProfileMapping(profileMappingData);
+
+        MainCLI.printMessage("Added profile mapping \"" + profileMappingData.getID() + "\"");
+
+        ProfileMappingCLI.printProfileMappingData(profileMappingData, 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/ProfileMappingCLI.java
similarity index 56%
copy from base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileCLI.java
copy to base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingCLI.java
index 8dfde38b438895ceddeca4a02715b7a8a2fd31aa..6f7e6c5d9b6f44d6357f87965cd963373af234de 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingCLI.java
@@ -23,49 +23,49 @@ 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.certsrv.tps.profile.ProfileMappingClient;
+import com.netscape.certsrv.tps.profile.ProfileMappingData;
 import com.netscape.cmstools.cli.CLI;
 
 /**
  * @author Endi S. Dewata
  */
-public class ProfileCLI extends CLI {
+public class ProfileMappingCLI extends CLI {
 
-    public ProfileClient profileClient;
+    public ProfileMappingClient profileMappingClient;
 
-    public ProfileCLI(CLI parent) {
-        super("profile", "Profile management commands", parent);
+    public ProfileMappingCLI(ProfileCLI profileCLI) {
+        super("mapping", "Profile mapping management commands", profileCLI);
 
-        addModule(new ProfileAddCLI(this));
-        addModule(new ProfileFindCLI(this));
-        addModule(new ProfileModifyCLI(this));
-        addModule(new ProfileRemoveCLI(this));
-        addModule(new ProfileShowCLI(this));
+        addModule(new ProfileMappingAddCLI(this));
+        addModule(new ProfileMappingFindCLI(this));
+        addModule(new ProfileMappingModifyCLI(this));
+        addModule(new ProfileMappingRemoveCLI(this));
+        addModule(new ProfileMappingShowCLI(this));
     }
 
     public void execute(String[] args) throws Exception {
 
         client = parent.getClient();
-        profileClient = (ProfileClient)parent.getClient("profile");
+        profileMappingClient = (ProfileMappingClient)parent.getClient("profile-mapping");
 
         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());
+    public static void printProfileMappingData(ProfileMappingData profileMappingData, boolean showProperties) throws IOException {
+        System.out.println("  Profile Mapping ID: " + profileMappingData.getID());
+        if (profileMappingData.getStatus() != null) System.out.println("  Status: " + profileMappingData.getStatus());
 
         if (showProperties) {
             System.out.println("  Properties:");
-            Map<String, String> properties = profileData.getProperties();
+            Map<String, String> properties = profileMappingData.getProperties();
             for (String name : properties.keySet()) {
                 String value = properties.get(name);
                 System.out.println("    " + name + ": " + value);
             }
         }
 
-        Link link = profileData.getLink();
+        Link link = profileMappingData.getLink();
         if (verbose && link != null) {
             System.out.println("  Link: " + link.getHref());
         }
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingFindCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingFindCLI.java
new file mode 100644
index 0000000000000000000000000000000000000000..d96268be72295a457ebece8261100635ed62fee9
--- /dev/null
+++ b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingFindCLI.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.ProfileMappingCollection;
+import com.netscape.certsrv.tps.profile.ProfileMappingData;
+import com.netscape.cmstools.cli.CLI;
+import com.netscape.cmstools.cli.MainCLI;
+
+/**
+ * @author Endi S. Dewata
+ */
+public class ProfileMappingFindCLI extends CLI {
+
+    public ProfileMappingCLI profileMappingCLI;
+
+    public ProfileMappingFindCLI(ProfileMappingCLI profileMappingCLI) {
+        super("find", "Find profile mappings", profileMappingCLI);
+        this.profileMappingCLI = profileMappingCLI;
+    }
+
+    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);
+
+        ProfileMappingCollection result = profileMappingCLI.profileMappingClient.findProfileMappings(start, size);
+        Collection<ProfileMappingData> profileMappings = result.getEntries();
+
+        MainCLI.printMessage(profileMappings.size() + " profile mapping(s) matched");
+
+        boolean first = true;
+
+        for (ProfileMappingData profileMappingData : profileMappings) {
+
+            if (first) {
+                first = false;
+            } else {
+                System.out.println();
+            }
+
+            ProfileMappingCLI.printProfileMappingData(profileMappingData, false);
+        }
+
+        MainCLI.printMessage("Number of entries returned " + profileMappings.size());
+    }
+}
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingModifyCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingModifyCLI.java
new file mode 100644
index 0000000000000000000000000000000000000000..4561695ff6df1c3ed068e58f90b4f391f4afb3b0
--- /dev/null
+++ b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingModifyCLI.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.ProfileMappingData;
+import com.netscape.cmstools.cli.CLI;
+import com.netscape.cmstools.cli.MainCLI;
+
+/**
+ * @author Endi S. Dewata
+ */
+public class ProfileMappingModifyCLI extends CLI {
+
+    public ProfileMappingCLI profileMappingCLI;
+
+    public ProfileMappingModifyCLI(ProfileMappingCLI profileMappingCLI) {
+        super("mod", "Modify profile mapping", profileMappingCLI);
+        this.profileMappingCLI = profileMappingCLI;
+    }
+
+    public void printHelp() {
+        formatter.printHelp(getFullName() + " <Profile Mapping 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 mapping 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 profileMappingID = cmdArgs[0];
+        String status = cmd.getOptionValue("status");
+        String input = cmd.getOptionValue("input");
+
+        ProfileMappingData profileMappingData;
+
+        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);
+            }
+
+            profileMappingData = ProfileMappingData.valueOf(sw.toString());
+        }
+
+        profileMappingData = profileMappingCLI.profileMappingClient.updateProfileMapping(profileMappingID, profileMappingData);
+
+        MainCLI.printMessage("Modified profile mapping \"" + profileMappingID + "\"");
+
+        ProfileMappingCLI.printProfileMappingData(profileMappingData, true);
+    }
+}
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingRemoveCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingRemoveCLI.java
new file mode 100644
index 0000000000000000000000000000000000000000..f64c12c8371e8b7cc4faf43e969e986f5ef1a142
--- /dev/null
+++ b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingRemoveCLI.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 ProfileMappingRemoveCLI extends CLI {
+
+    public ProfileMappingCLI profileMappingCLI;
+
+    public ProfileMappingRemoveCLI(ProfileMappingCLI profileMappingCLI) {
+        super("del", "Remove profile mapping", profileMappingCLI);
+        this.profileMappingCLI = profileMappingCLI;
+    }
+
+    public void printHelp() {
+        formatter.printHelp(getFullName() + " <Profile Mapping ID>", options);
+    }
+
+    public void execute(String[] args) throws Exception {
+
+        if (args.length != 1) {
+            printHelp();
+            System.exit(1);
+        }
+
+        String profileMappingID = args[0];
+
+        profileMappingCLI.profileMappingClient.removeProfileMapping(profileMappingID);
+
+        MainCLI.printMessage("Deleted profile mapping \"" + profileMappingID + "\"");
+    }
+}
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingShowCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingShowCLI.java
new file mode 100644
index 0000000000000000000000000000000000000000..c715a3edc58f3c9dfca7029836f09946201d675f
--- /dev/null
+++ b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingShowCLI.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.ProfileMappingData;
+import com.netscape.cmstools.cli.CLI;
+import com.netscape.cmstools.cli.MainCLI;
+
+/**
+ * @author Endi S. Dewata
+ */
+public class ProfileMappingShowCLI extends CLI {
+
+    public ProfileMappingCLI profileMappingCLI;
+
+    public ProfileMappingShowCLI(ProfileMappingCLI profileMappingCLI) {
+        super("show", "Show profileMapping", profileMappingCLI);
+        this.profileMappingCLI = profileMappingCLI;
+    }
+
+    public void printHelp() {
+        formatter.printHelp(getFullName() + " <Profile Mapping ID>", options);
+    }
+
+    public void execute(String[] args) throws Exception {
+
+        Option option = new Option(null, "output", true, "Output file to store profile mapping 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 profileMappingID = args[0];
+        String output = cmd.getOptionValue("output");
+
+        ProfileMappingData profileMappingData = profileMappingCLI.profileMappingClient.getProfileMapping(profileMappingID);
+
+        if (output == null) {
+            MainCLI.printMessage("ProfileMapping \"" + profileMappingID + "\"");
+            ProfileMappingCLI.printProfileMappingData(profileMappingData, true);
+
+        } else {
+            try (PrintWriter out = new PrintWriter(new FileWriter(output))) {
+                out.println(profileMappingData);
+            }
+            MainCLI.printMessage("Stored profile mapping \"" + profileMappingID + "\" 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 104baa9789c1bc7138d6a68142951992496ff553..83fa3aa1f5f79dba8b5ef91dd90fa2f61c41ef2d 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.ProfileMappingService;
 import org.dogtagpki.server.tps.profile.ProfileService;
 import org.dogtagpki.server.tps.token.TokenService;
 
@@ -85,6 +86,7 @@ public class TPSApplication extends Application {
 
         // profiles
         classes.add(ProfileService.class);
+        classes.add(ProfileMappingService.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 8b077a762435bbc40d4676efa8754b605bc99a71..3201e0bd1ae77deb1ffc20611a672a60b1c62b6c 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java
@@ -23,6 +23,7 @@ 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.profile.ProfileMappingDatabase;
 import org.dogtagpki.server.tps.token.TokenDatabase;
 import org.mozilla.jss.CryptoManager;
 import org.mozilla.jss.CryptoManager.NotInitializedException;
@@ -60,6 +61,7 @@ public class TPSSubsystem implements IAuthority, ISubsystem {
     public ConfigDatabase configDatabase;
     public ConnectionDatabase connectionDatabase;
     public ProfileDatabase profileDatabase;
+    public ProfileMappingDatabase profileMappingDatabase;
     public TokenDatabase tokenDatabase;
 
     @Override
@@ -93,6 +95,7 @@ public class TPSSubsystem implements IAuthority, ISubsystem {
         authenticatorDatabase = new AuthenticatorDatabase();
         connectionDatabase = new ConnectionDatabase();
         profileDatabase = new ProfileDatabase();
+        profileMappingDatabase = new ProfileMappingDatabase();
 }
 
     @Override
@@ -164,6 +167,10 @@ public class TPSSubsystem implements IAuthority, ISubsystem {
         return profileDatabase;
     }
 
+    public ProfileMappingDatabase getProfileMappingDatabase() {
+        return profileMappingDatabase;
+    }
+
     public TokenDatabase getTokenDatabase() {
         return tokenDatabase;
     }
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/profile/ProfileMappingDatabase.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/profile/ProfileMappingDatabase.java
new file mode 100644
index 0000000000000000000000000000000000000000..a550f222d738560d87e37ff325a1fa4a5305dadd
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/profile/ProfileMappingDatabase.java
@@ -0,0 +1,132 @@
+// --- 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 profileMappings in CS.cfg.
+ *
+ * @author Endi S. Dewata
+ */
+public class ProfileMappingDatabase extends Database<ProfileMappingRecord> {
+
+    public ProfileMappingDatabase() {
+        super("Profile Mapping");
+    }
+
+    public ProfileMappingRecord createProfileMappingRecord(ConfigDatabase configDatabase, ConfigRecord configRecord, String profileMappingID) {
+        ProfileMappingRecord profileMappingRecord = new ProfileMappingRecord();
+        profileMappingRecord.setID(profileMappingID);
+        Map<String, String> properties = configDatabase.getProperties(configRecord, profileMappingID);
+        profileMappingRecord.setProperties(properties);
+        return profileMappingRecord;
+    }
+
+    @Override
+    public Collection<ProfileMappingRecord> getRecords() throws Exception {
+
+        Collection<ProfileMappingRecord> result = new ArrayList<ProfileMappingRecord>();
+        ConfigDatabase configDatabase = new ConfigDatabase();
+        ConfigRecord configRecord = configDatabase.getRecord("Profile_Mappings");
+
+        for (String profileMappingID : configRecord.getKeys()) {
+            ProfileMappingRecord profileMappingRecord = createProfileMappingRecord(configDatabase, configRecord, profileMappingID);
+            result.add(profileMappingRecord);
+        }
+
+        return result;
+    }
+
+    @Override
+    public ProfileMappingRecord getRecord(String profileMappingID) throws Exception {
+
+        ConfigDatabase configDatabase = new ConfigDatabase();
+        ConfigRecord configRecord = configDatabase.getRecord("Profile_Mappings");
+
+        return createProfileMappingRecord(configDatabase, configRecord, profileMappingID);
+    }
+
+
+    @Override
+    public void addRecord(String profileMappingID, ProfileMappingRecord profileMappingRecord) throws Exception {
+
+        CMS.debug("ProfileMappingDatabase.addRecord(\"" + profileMappingID + "\")");
+        ConfigDatabase configDatabase = new ConfigDatabase();
+        ConfigRecord configRecord = configDatabase.getRecord("Profile_Mappings");
+
+        // validate new properties
+        Map<String, String> properties = profileMappingRecord.getProperties();
+        configDatabase.validateProperties(configRecord, profileMappingID, properties);
+
+        // add new profileMapping
+        configRecord.addKey(profileMappingID);
+        configDatabase.updateRecord("Profile_Mappings", configRecord);
+
+        // store new properties
+        configDatabase.addProperties(configRecord, profileMappingID, properties);
+
+        configDatabase.commit();
+    }
+
+    @Override
+    public void updateRecord(String profileMappingID, ProfileMappingRecord profileMappingRecord) throws Exception {
+
+        CMS.debug("ProfileMappingDatabase.updateRecord(\"" + profileMappingID + "\")");
+        ConfigDatabase configDatabase = new ConfigDatabase();
+        ConfigRecord configRecord = configDatabase.getRecord("Profile_Mappings");
+
+        // validate new properties
+        Map<String, String> properties = profileMappingRecord.getProperties();
+        configDatabase.validateProperties(configRecord, profileMappingID, properties);
+
+        // remove old properties
+        configDatabase.removeProperties(configRecord, profileMappingID);
+
+        // add new properties
+        configDatabase.addProperties(configRecord, profileMappingID, properties);
+
+        configDatabase.commit();
+    }
+
+    @Override
+    public void removeRecord(String profileMappingID) throws Exception {
+
+        CMS.debug("ProfileMappingDatabase.removeRecord(\"" + profileMappingID + "\")");
+        ConfigDatabase configDatabase = new ConfigDatabase();
+        ConfigRecord configRecord = configDatabase.getRecord("Profile_Mappings");
+
+        // remove properties
+        configDatabase.removeProperties(configRecord, profileMappingID);
+
+        // remove profileMapping
+        configRecord.removeKey(profileMappingID);
+        configDatabase.updateRecord("Profile_Mappings", configRecord);
+
+        configDatabase.commit();
+    }
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/profile/ProfileMappingRecord.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/profile/ProfileMappingRecord.java
new file mode 100644
index 0000000000000000000000000000000000000000..6ba29dc9d2719c3f063c2c683c78412154b8b788
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/profile/ProfileMappingRecord.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 ProfileMappingRecord {
+
+    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;
+        ProfileMappingRecord other = (ProfileMappingRecord) 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/ProfileMappingService.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/profile/ProfileMappingService.java
new file mode 100644
index 0000000000000000000000000000000000000000..d23485ddd8cccfa573dbd2b1978dbf041e9fdcb4
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/profile/ProfileMappingService.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.ProfileMappingCollection;
+import com.netscape.certsrv.tps.profile.ProfileMappingData;
+import com.netscape.certsrv.tps.profile.ProfileMappingResource;
+import com.netscape.cms.servlet.base.PKIService;
+
+/**
+ * @author Endi S. Dewata
+ */
+public class ProfileMappingService extends PKIService implements ProfileMappingResource {
+
+    @Context
+    private UriInfo uriInfo;
+
+    @Context
+    private HttpHeaders headers;
+
+    @Context
+    private Request request;
+
+    @Context
+    private HttpServletRequest servletRequest;
+
+    public final static int DEFAULT_SIZE = 20;
+
+    public ProfileMappingService() {
+        CMS.debug("ProfileMappingService.<init>()");
+    }
+
+    public ProfileMappingData createProfileMappingData(ProfileMappingRecord profileMappingRecord) throws UnsupportedEncodingException {
+
+        String profileMappingID = profileMappingRecord.getID();
+
+        ProfileMappingData profileMappingData = new ProfileMappingData();
+        profileMappingData.setID(profileMappingID);
+        profileMappingData.setStatus(profileMappingRecord.getStatus());
+        profileMappingData.setProperties(profileMappingRecord.getProperties());
+
+        profileMappingID = URLEncoder.encode(profileMappingID, "UTF-8");
+        URI uri = uriInfo.getBaseUriBuilder().path(ProfileMappingResource.class).path("{profileMappingID}").build(profileMappingID);
+        profileMappingData.setLink(new Link("self", uri));
+
+        return profileMappingData;
+    }
+
+    public ProfileMappingRecord createProfileMappingRecord(ProfileMappingData profileMappingData) {
+
+        ProfileMappingRecord profileMappingRecord = new ProfileMappingRecord();
+        profileMappingRecord.setID(profileMappingData.getID());
+        profileMappingRecord.setStatus(profileMappingData.getStatus());
+        profileMappingRecord.setProperties(profileMappingData.getProperties());
+
+        return profileMappingRecord;
+    }
+
+    @Override
+    public ProfileMappingCollection findProfileMappings(Integer start, Integer size) {
+
+        CMS.debug("ProfileMappingService.findProfileMappings()");
+
+        try {
+            start = start == null ? 0 : start;
+            size = size == null ? DEFAULT_SIZE : size;
+
+            TPSSubsystem subsystem = (TPSSubsystem)CMS.getSubsystem(TPSSubsystem.ID);
+            ProfileMappingDatabase database = subsystem.getProfileMappingDatabase();
+
+            Iterator<ProfileMappingRecord> profileMappings = database.getRecords().iterator();
+
+            ProfileMappingCollection response = new ProfileMappingCollection();
+
+            int i = 0;
+
+            // skip to the start of the page
+            for ( ; i<start && profileMappings.hasNext(); i++) profileMappings.next();
+
+            // return entries up to the page size
+            for ( ; i<start+size && profileMappings.hasNext(); i++) {
+                response.addEntry(createProfileMappingData(profileMappings.next()));
+            }
+
+            // count the total entries
+            for ( ; profileMappings.hasNext(); i++) profileMappings.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 ProfileMappingData getProfileMapping(String profileMappingID) {
+
+        CMS.debug("ProfileMappingService.getProfileMapping(\"" + profileMappingID + "\")");
+
+        try {
+            TPSSubsystem subsystem = (TPSSubsystem)CMS.getSubsystem(TPSSubsystem.ID);
+            ProfileMappingDatabase database = subsystem.getProfileMappingDatabase();
+
+            return createProfileMappingData(database.getRecord(profileMappingID));
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new PKIException(e.getMessage());
+        }
+    }
+
+    @Override
+    public Response addProfileMapping(ProfileMappingData profileMappingData) {
+
+        CMS.debug("ProfileMappingService.addProfileMapping(\"" + profileMappingData.getID() + "\")");
+
+        try {
+            TPSSubsystem subsystem = (TPSSubsystem)CMS.getSubsystem(TPSSubsystem.ID);
+            ProfileMappingDatabase database = subsystem.getProfileMappingDatabase();
+
+            database.addRecord(profileMappingData.getID(), createProfileMappingRecord(profileMappingData));
+            profileMappingData = createProfileMappingData(database.getRecord(profileMappingData.getID()));
+
+            return Response
+                    .created(profileMappingData.getLink().getHref())
+                    .entity(profileMappingData)
+                    .type(MediaType.APPLICATION_XML)
+                    .build();
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new PKIException(e.getMessage());
+        }
+    }
+
+    @Override
+    public Response updateProfileMapping(String profileMappingID, ProfileMappingData profileMappingData) {
+
+        CMS.debug("ProfileMappingService.updateProfileMapping(\"" + profileMappingID + "\")");
+
+        try {
+            TPSSubsystem subsystem = (TPSSubsystem)CMS.getSubsystem(TPSSubsystem.ID);
+            ProfileMappingDatabase database = subsystem.getProfileMappingDatabase();
+
+            database.updateRecord(profileMappingData.getID(), createProfileMappingRecord(profileMappingData));
+            profileMappingData = createProfileMappingData(database.getRecord(profileMappingID));
+
+            return Response
+                    .ok(profileMappingData)
+                    .type(MediaType.APPLICATION_XML)
+                    .build();
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new PKIException(e.getMessage());
+        }
+    }
+
+    @Override
+    public void removeProfileMapping(String profileMappingID) {
+
+        CMS.debug("ProfileMappingService.removeProfileMapping(\"" + profileMappingID + "\")");
+
+        try {
+            TPSSubsystem subsystem = (TPSSubsystem)CMS.getSubsystem(TPSSubsystem.ID);
+            ProfileMappingDatabase database = subsystem.getProfileMappingDatabase();
+            database.removeRecord(profileMappingID);
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new PKIException(e.getMessage());
+        }
+    }
+}
-- 
1.8.1.4

