From ddb0c37389b2a02432c53f207d07b1095a220b0d Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Wed, 7 Aug 2013 00:00:59 -0400
Subject: [PATCH] Added TPS activities resource.

New REST services and clients have been added for TPS activities.
The activity database is currently implemented as in-memory database
with some sample data. Later it will be converted into LDAP database.

Ticket #652
---
 .../TPSClient.java => logging/ActivityClient.java} |  27 ++-
 .../certsrv/logging/ActivityCollection.java        |  38 ++++
 .../com/netscape/certsrv/logging/ActivityData.java | 208 +++++++++++++++++++++
 .../netscape/certsrv/logging/ActivityResource.java |  44 +++++
 .../src/com/netscape/certsrv/tps/TPSClient.java    |   2 +
 .../src/com/netscape/cmstools/cli/TPSCLI.java      |   2 +
 .../com/netscape/cmstools/logging/ActivityCLI.java |  83 ++++++++
 .../netscape/cmstools/logging/ActivityFindCLI.java |  94 ++++++++++
 .../netscape/cmstools/logging/ActivityShowCLI.java |  56 ++++++
 .../dogtagpki/tps/logging/ActivityDatabase.java    |  70 +++++++
 .../org/dogtagpki/tps/logging/ActivityRecord.java  | 173 +++++++++++++++++
 .../org/dogtagpki/tps/logging/ActivityService.java | 142 ++++++++++++++
 .../org/dogtagpki/tps/server/TPSApplication.java   |   4 +
 .../src/org/dogtagpki/tps/server/TPSSubsystem.java |   6 +
 14 files changed, 942 insertions(+), 7 deletions(-)
 copy base/common/src/com/netscape/certsrv/{tps/TPSClient.java => logging/ActivityClient.java} (57%)
 create mode 100644 base/common/src/com/netscape/certsrv/logging/ActivityCollection.java
 create mode 100644 base/common/src/com/netscape/certsrv/logging/ActivityData.java
 create mode 100644 base/common/src/com/netscape/certsrv/logging/ActivityResource.java
 create mode 100644 base/java-tools/src/com/netscape/cmstools/logging/ActivityCLI.java
 create mode 100644 base/java-tools/src/com/netscape/cmstools/logging/ActivityFindCLI.java
 create mode 100644 base/java-tools/src/com/netscape/cmstools/logging/ActivityShowCLI.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/tps/logging/ActivityDatabase.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/tps/logging/ActivityRecord.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/tps/logging/ActivityService.java

diff --git a/base/common/src/com/netscape/certsrv/tps/TPSClient.java b/base/common/src/com/netscape/certsrv/logging/ActivityClient.java
similarity index 57%
copy from base/common/src/com/netscape/certsrv/tps/TPSClient.java
copy to base/common/src/com/netscape/certsrv/logging/ActivityClient.java
index 7b35ab208b4bd99eb5eff67ca3c87a1c4512449b..cbaeac5de5433c299d6b3fe38cf33ff6f0695e34 100644
--- a/base/common/src/com/netscape/certsrv/tps/TPSClient.java
+++ b/base/common/src/com/netscape/certsrv/logging/ActivityClient.java
@@ -15,25 +15,38 @@
 //(C) 2013 Red Hat, Inc.
 //All rights reserved.
 //--- END COPYRIGHT BLOCK ---
-package com.netscape.certsrv.tps;
+package com.netscape.certsrv.logging;
 
 import java.net.URISyntaxException;
 
+import com.netscape.certsrv.client.Client;
 import com.netscape.certsrv.client.PKIClient;
-import com.netscape.certsrv.client.SubsystemClient;
-import com.netscape.certsrv.token.TokenClient;
 
 /**
  * @author Endi S. Dewata
  */
-public class TPSClient extends SubsystemClient {
+public class ActivityClient extends Client {
 
-    public TPSClient(PKIClient client) throws Exception {
-        super(client, "tps");
+    public ActivityResource resource;
+
+    public ActivityClient(PKIClient client) throws URISyntaxException {
+        this(client, client.getSubsystem());
+    }
+
+    public ActivityClient(PKIClient client, String subsystem) throws URISyntaxException {
+        super(client, "activity");
         init();
     }
 
     public void init() throws URISyntaxException {
-        addClient(new TokenClient(client, name));
+        resource = createProxy(ActivityResource.class);
+    }
+
+    public ActivityCollection findActivities(Integer start, Integer size) {
+        return resource.findActivities(start, size);
+    }
+
+    public ActivityData getActivity(String tokenID) {
+        return resource.getActivity(tokenID);
     }
 }
diff --git a/base/common/src/com/netscape/certsrv/logging/ActivityCollection.java b/base/common/src/com/netscape/certsrv/logging/ActivityCollection.java
new file mode 100644
index 0000000000000000000000000000000000000000..d6f998843a57666e09e679bed49d9490288b8f41
--- /dev/null
+++ b/base/common/src/com/netscape/certsrv/logging/ActivityCollection.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.logging;
+
+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="Activities")
+public class ActivityCollection extends DataCollection<ActivityData> {
+
+    @XmlElementRef
+    public Collection<ActivityData> getEntries() {
+        return super.getEntries();
+    }
+}
diff --git a/base/common/src/com/netscape/certsrv/logging/ActivityData.java b/base/common/src/com/netscape/certsrv/logging/ActivityData.java
new file mode 100644
index 0000000000000000000000000000000000000000..0624cc73bc8a240577c3d29036977c0349b06900
--- /dev/null
+++ b/base/common/src/com/netscape/certsrv/logging/ActivityData.java
@@ -0,0 +1,208 @@
+// --- 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.logging;
+
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.util.Date;
+
+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 org.jboss.resteasy.plugins.providers.atom.Link;
+
+/**
+ * @author Endi S. Dewata
+ */
+@XmlRootElement(name="Activity")
+public class ActivityData {
+
+    public static Marshaller marshaller;
+    public static Unmarshaller unmarshaller;
+
+    static {
+        try {
+            marshaller = JAXBContext.newInstance(ActivityData.class).createMarshaller();
+            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
+            unmarshaller = JAXBContext.newInstance(ActivityData.class).createUnmarshaller();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    String id;
+    String tokenID;
+    String userID;
+    String ip;
+    String operation;
+    String result;
+    Date date;
+
+    Link link;
+
+    @XmlAttribute(name="id")
+    public String getID() {
+        return id;
+    }
+
+    public void setID(String id) {
+        this.id = id;
+    }
+
+    @XmlElement(name="TokenID")
+    public String getTokenID() {
+        return tokenID;
+    }
+
+    public void setTokenID(String tokenID) {
+        this.tokenID = tokenID;
+    }
+
+    @XmlElement(name="UserID")
+    public String getUserID() {
+        return userID;
+    }
+
+    public void setUserID(String userID) {
+        this.userID = userID;
+    }
+
+    @XmlElement(name="IP")
+    public String getIp() {
+        return ip;
+    }
+
+    public void setIp(String ip) {
+        this.ip = ip;
+    }
+
+    @XmlElement(name="Operation")
+    public String getOperation() {
+        return operation;
+    }
+
+    public void setOperation(String operation) {
+        this.operation = operation;
+    }
+
+    @XmlElement(name="Result")
+    public String getResult() {
+        return result;
+    }
+
+    public void setResult(String result) {
+        this.result = result;
+    }
+
+    @XmlElement(name="Date")
+    public Date getDate() {
+        return date;
+    }
+
+    public void setDate(Date date) {
+        this.date = date;
+    }
+
+    @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 + ((userID == null) ? 0 : userID.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;
+        ActivityData other = (ActivityData) 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 (userID == null) {
+            if (other.userID != null)
+                return false;
+        } else if (!userID.equals(other.userID))
+            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 ActivityData valueOf(String string) throws Exception {
+        try {
+            return (ActivityData)unmarshaller.unmarshal(new StringReader(string));
+        } catch (Exception e) {
+            return null;
+        }
+    }
+
+    public static void main(String args[]) throws Exception {
+
+        ActivityData before = new ActivityData();
+        before.setID("activity1");
+        before.setTokenID("TOKEN1234");
+        before.setUserID("user1");
+        before.setIp("192.168.1.1");
+        before.setOperation("enroll");
+        before.setResult("success");
+        before.setDate(new Date());
+
+        String string = before.toString();
+        System.out.println(string);
+
+        ActivityData after = ActivityData.valueOf(string);
+        System.out.println(before.equals(after));
+    }
+}
diff --git a/base/common/src/com/netscape/certsrv/logging/ActivityResource.java b/base/common/src/com/netscape/certsrv/logging/ActivityResource.java
new file mode 100644
index 0000000000000000000000000000000000000000..6ecf41dd247c6c47fcb39b133f42a94bf885252e
--- /dev/null
+++ b/base/common/src/com/netscape/certsrv/logging/ActivityResource.java
@@ -0,0 +1,44 @@
+// --- 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.logging;
+
+import javax.ws.rs.GET;
+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;
+
+
+/**
+ * @author Endi S. Dewata
+ */
+@Path("activities")
+public interface ActivityResource {
+
+    @GET
+    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    public ActivityCollection findActivities(
+            @QueryParam("start") Integer start,
+            @QueryParam("size") Integer size);
+
+    @GET
+    @Path("{activityID}")
+    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    public ActivityData getActivity(@PathParam("activityID") String activityID);
+}
diff --git a/base/common/src/com/netscape/certsrv/tps/TPSClient.java b/base/common/src/com/netscape/certsrv/tps/TPSClient.java
index 7b35ab208b4bd99eb5eff67ca3c87a1c4512449b..38aff46bda819013c30f0912def788031ef03182 100644
--- a/base/common/src/com/netscape/certsrv/tps/TPSClient.java
+++ b/base/common/src/com/netscape/certsrv/tps/TPSClient.java
@@ -21,6 +21,7 @@ import java.net.URISyntaxException;
 
 import com.netscape.certsrv.client.PKIClient;
 import com.netscape.certsrv.client.SubsystemClient;
+import com.netscape.certsrv.logging.ActivityClient;
 import com.netscape.certsrv.token.TokenClient;
 
 /**
@@ -34,6 +35,7 @@ public class TPSClient extends SubsystemClient {
     }
 
     public void init() throws URISyntaxException {
+        addClient(new ActivityClient(client, name));
         addClient(new TokenClient(client, name));
     }
 }
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 1f10e959a31bed16a440d1867e4f0909ab98ccb5..4c9e501adf5bd04efe46f68b4fee5ec5853803cb 100644
--- a/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java
@@ -20,6 +20,7 @@ package com.netscape.cmstools.cli;
 
 import com.netscape.certsrv.client.Client;
 import com.netscape.certsrv.tps.TPSClient;
+import com.netscape.cmstools.logging.ActivityCLI;
 import com.netscape.cmstools.token.TokenCLI;
 
 /**
@@ -32,6 +33,7 @@ public class TPSCLI extends SubsystemCLI {
     public TPSCLI(MainCLI mainCLI) {
         super("tps", "TPS management commands", mainCLI);
 
+        addModule(new ActivityCLI(this));
         addModule(new TokenCLI(this));
     }
 
diff --git a/base/java-tools/src/com/netscape/cmstools/logging/ActivityCLI.java b/base/java-tools/src/com/netscape/cmstools/logging/ActivityCLI.java
new file mode 100644
index 0000000000000000000000000000000000000000..4a9bf38280049a1152936a2a3192c5b33374b39a
--- /dev/null
+++ b/base/java-tools/src/com/netscape/cmstools/logging/ActivityCLI.java
@@ -0,0 +1,83 @@
+// --- 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.logging;
+
+import java.util.Arrays;
+
+import org.jboss.resteasy.plugins.providers.atom.Link;
+
+import com.netscape.certsrv.logging.ActivityClient;
+import com.netscape.certsrv.logging.ActivityData;
+import com.netscape.cmstools.cli.CLI;
+
+/**
+ * @author Endi S. Dewata
+ */
+public class ActivityCLI extends CLI {
+
+    public ActivityClient activityClient;
+
+    public ActivityCLI(CLI parent) {
+        super("activity", "Activity management commands", parent);
+
+        addModule(new ActivityFindCLI(this));
+        addModule(new ActivityShowCLI(this));
+    }
+
+    public void execute(String[] args) throws Exception {
+
+        client = parent.getClient();
+        activityClient = (ActivityClient)parent.getClient("activity");
+
+        if (args.length == 0) {
+            printHelp();
+            System.exit(1);
+        }
+
+        String command = args[0];
+        String[] commandArgs = Arrays.copyOfRange(args, 1, args.length);
+
+        if (command == null) {
+            printHelp();
+            System.exit(1);
+        }
+
+        CLI module = getModule(command);
+        if (module != null) {
+            module.execute(commandArgs);
+
+        } else {
+            System.err.println("Error: Invalid command \"" + command + "\"");
+            printHelp();
+            System.exit(1);
+        }
+    }
+
+    public static void printActivity(ActivityData activity) {
+        System.out.println("  Activity ID: " + activity.getID());
+        if (activity.getTokenID() != null) System.out.println("  Token ID: " + activity.getTokenID());
+        if (activity.getUserID() != null) System.out.println("  User ID: " + activity.getUserID());
+        if (activity.getDate() != null) System.out.println("  Date: " + activity.getDate());
+
+        Link link = activity.getLink();
+        if (verbose && link != null) {
+            System.out.println("  Link: " + link.getHref());
+        }
+    }
+}
diff --git a/base/java-tools/src/com/netscape/cmstools/logging/ActivityFindCLI.java b/base/java-tools/src/com/netscape/cmstools/logging/ActivityFindCLI.java
new file mode 100644
index 0000000000000000000000000000000000000000..0808a0a7059073f1ae0c3d5ba27dd120b54e44b0
--- /dev/null
+++ b/base/java-tools/src/com/netscape/cmstools/logging/ActivityFindCLI.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.logging;
+
+import java.util.Collection;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+
+import com.netscape.certsrv.logging.ActivityCollection;
+import com.netscape.certsrv.logging.ActivityData;
+import com.netscape.cmstools.cli.CLI;
+import com.netscape.cmstools.cli.MainCLI;
+
+/**
+ * @author Endi S. Dewata
+ */
+public class ActivityFindCLI extends CLI {
+
+    public ActivityCLI activityCLI;
+
+    public ActivityFindCLI(ActivityCLI activityCLI) {
+        super("find", "Find activities", activityCLI);
+        this.activityCLI = activityCLI;
+    }
+
+    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);
+
+        ActivityCollection result = activityCLI.activityClient.findActivities(start, size);
+        Collection<ActivityData> activities = result.getEntries();
+
+        MainCLI.printMessage(activities.size() + " activity(s) matched");
+
+        boolean first = true;
+
+        for (ActivityData activityData : activities) {
+
+            if (first) {
+                first = false;
+            } else {
+                System.out.println();
+            }
+
+            ActivityCLI.printActivity(activityData);
+        }
+
+        MainCLI.printMessage("Number of entries returned " + activities.size());
+    }
+}
diff --git a/base/java-tools/src/com/netscape/cmstools/logging/ActivityShowCLI.java b/base/java-tools/src/com/netscape/cmstools/logging/ActivityShowCLI.java
new file mode 100644
index 0000000000000000000000000000000000000000..6fa49c90c86006c681687b6a913cb75d46d1f419
--- /dev/null
+++ b/base/java-tools/src/com/netscape/cmstools/logging/ActivityShowCLI.java
@@ -0,0 +1,56 @@
+// --- 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.logging;
+
+import com.netscape.certsrv.logging.ActivityData;
+import com.netscape.cmstools.cli.CLI;
+import com.netscape.cmstools.cli.MainCLI;
+
+/**
+ * @author Endi S. Dewata
+ */
+public class ActivityShowCLI extends CLI {
+
+    public ActivityCLI activityCLI;
+
+    public ActivityShowCLI(ActivityCLI activityCLI) {
+        super("show", "Show activity", activityCLI);
+        this.activityCLI = activityCLI;
+    }
+
+    public void printHelp() {
+        formatter.printHelp(getFullName() + " <Activity ID>", options);
+    }
+
+    public void execute(String[] args) throws Exception {
+
+        if (args.length != 1) {
+            printHelp();
+            System.exit(1);
+        }
+
+        String activityID = args[0];
+
+        ActivityData activityData = activityCLI.activityClient.getActivity(activityID);
+
+        MainCLI.printMessage("Activity \"" + activityID + "\"");
+
+        ActivityCLI.printActivity(activityData);
+    }
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/tps/logging/ActivityDatabase.java b/base/tps-tomcat/src/org/dogtagpki/tps/logging/ActivityDatabase.java
new file mode 100644
index 0000000000000000000000000000000000000000..28c0935da415be56f1a20bddbe192f8fe185b3a9
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/tps/logging/ActivityDatabase.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 org.dogtagpki.tps.logging;
+
+import java.util.Date;
+
+import com.netscape.cmscore.dbs.Database;
+
+/**
+ * This class implements in-memory activity database. In the future this
+ * will be replaced with LDAP database.
+ *
+ * @author Endi S. Dewata
+ */
+public class ActivityDatabase extends Database<ActivityRecord> {
+
+    public ActivityDatabase() {
+        super("Activity");
+
+        // add sample records
+        try {
+            ActivityRecord record1 = new ActivityRecord();
+            record1.setID("activity1");
+            record1.setTokenID("token1");
+            record1.setUserID("user1");
+            record1.setIp("192.168.1.1");
+            record1.setOperation("enroll");
+            record1.setResult("success");
+            addRecord(record1);
+
+            ActivityRecord record2 = new ActivityRecord();
+            record2.setID("activity2");
+            record2.setTokenID("token2");
+            record2.setUserID("user2");
+            record2.setIp("192.168.1.2");
+            record2.setOperation("enroll");
+            record2.setResult("failed");
+            addRecord(record2);
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    public void addRecord(ActivityRecord activityRecord) throws Exception {
+        activityRecord.setDate(new Date());
+
+        addRecord(activityRecord.getID(), activityRecord);
+    }
+
+    public void updateRecord(ActivityRecord activityRecord) throws Exception {
+        updateRecord(activityRecord.getID(), activityRecord);
+    }
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/tps/logging/ActivityRecord.java b/base/tps-tomcat/src/org/dogtagpki/tps/logging/ActivityRecord.java
new file mode 100644
index 0000000000000000000000000000000000000000..d4b4793a90103b2669c818a5c2de5803683da712
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/tps/logging/ActivityRecord.java
@@ -0,0 +1,173 @@
+// --- 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.tps.logging;
+
+import java.util.Date;
+
+import com.netscape.certsrv.token.TokenData;
+
+/**
+ * @author Endi S. Dewata
+ */
+public class ActivityRecord {
+
+    String id;
+    String tokenID;
+    String userID;
+    String ip;
+    String operation;
+    String result;
+    Date date;
+
+    public String getID() {
+        return id;
+    }
+
+    public void setID(String id) {
+        this.id = id;
+    }
+
+    public String getTokenID() {
+        return tokenID;
+    }
+
+    public void setTokenID(String tokenID) {
+        this.tokenID = tokenID;
+    }
+
+    public String getUserID() {
+        return userID;
+    }
+
+    public void setUserID(String userID) {
+        this.userID = userID;
+    }
+
+    public String getIp() {
+        return ip;
+    }
+
+    public void setIp(String ip) {
+        this.ip = ip;
+    }
+
+    public String getOperation() {
+        return operation;
+    }
+
+    public void setOperation(String operation) {
+        this.operation = operation;
+    }
+
+    public String getResult() {
+        return result;
+    }
+
+    public void setResult(String result) {
+        this.result = result;
+    }
+
+    public Date getDate() {
+        return date;
+    }
+
+    public void setDate(Date date) {
+        this.date = date;
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((date == null) ? 0 : date.hashCode());
+        result = prime * result + ((id == null) ? 0 : id.hashCode());
+        result = prime * result + ((ip == null) ? 0 : ip.hashCode());
+        result = prime * result + ((operation == null) ? 0 : operation.hashCode());
+        result = prime * result + ((this.result == null) ? 0 : this.result.hashCode());
+        result = prime * result + ((tokenID == null) ? 0 : tokenID.hashCode());
+        result = prime * result + ((userID == null) ? 0 : userID.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;
+        ActivityRecord other = (ActivityRecord) obj;
+        if (date == null) {
+            if (other.date != null)
+                return false;
+        } else if (!date.equals(other.date))
+            return false;
+        if (id == null) {
+            if (other.id != null)
+                return false;
+        } else if (!id.equals(other.id))
+            return false;
+        if (ip == null) {
+            if (other.ip != null)
+                return false;
+        } else if (!ip.equals(other.ip))
+            return false;
+        if (operation == null) {
+            if (other.operation != null)
+                return false;
+        } else if (!operation.equals(other.operation))
+            return false;
+        if (result == null) {
+            if (other.result != null)
+                return false;
+        } else if (!result.equals(other.result))
+            return false;
+        if (tokenID == null) {
+            if (other.tokenID != null)
+                return false;
+        } else if (!tokenID.equals(other.tokenID))
+            return false;
+        if (userID == null) {
+            if (other.userID != null)
+                return false;
+        } else if (!userID.equals(other.userID))
+            return false;
+        return true;
+    }
+
+    public static void main(String args[]) throws Exception {
+
+        TokenData before = new TokenData();
+        before.setID("token1");
+        before.setUserID("user1");
+        before.setStatus("revoked");
+        before.setReason("lost");
+        before.setAppletID("APPLET1234");
+        before.setKeyInfo("key info");
+        before.setCreateTimestamp(new Date());
+        before.setModifyTimestamp(new Date());
+
+        String string = before.toString();
+        System.out.println(string);
+
+        TokenData after = TokenData.valueOf(string);
+        System.out.println(before.equals(after));
+    }
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/tps/logging/ActivityService.java b/base/tps-tomcat/src/org/dogtagpki/tps/logging/ActivityService.java
new file mode 100644
index 0000000000000000000000000000000000000000..50a18bdea173c16d6758ac7faefcce317d4c38e0
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/tps/logging/ActivityService.java
@@ -0,0 +1,142 @@
+// --- 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.tps.logging;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URI;
+import java.net.URLEncoder;
+import java.util.Iterator;
+
+import org.dogtagpki.tps.server.TPSSubsystem;
+import org.jboss.resteasy.plugins.providers.atom.Link;
+
+import com.netscape.certsrv.base.PKIException;
+import com.netscape.certsrv.logging.ActivityCollection;
+import com.netscape.certsrv.logging.ActivityData;
+import com.netscape.certsrv.logging.ActivityResource;
+import com.netscape.cms.servlet.base.PKIService;
+
+/**
+ * @author Endi S. Dewata
+ */
+public class ActivityService extends PKIService implements ActivityResource {
+
+    public final static int DEFAULT_SIZE = 20;
+
+    public ActivityService() {
+        System.out.println("ActivityService.<init>()");
+    }
+
+    public ActivityData createActivityData(ActivityRecord activityRecord) {
+
+        ActivityData activityData = new ActivityData();
+        activityData.setID(activityRecord.getID());
+        activityData.setTokenID(activityRecord.getTokenID());
+        activityData.setUserID(activityRecord.getUserID());
+        activityData.setDate(activityRecord.getDate());
+
+        String activityID = activityRecord.getID();
+        try {
+            activityID = URLEncoder.encode(activityID, "UTF-8");
+        } catch (UnsupportedEncodingException e) {
+            e.printStackTrace();
+            throw new PKIException(e.getMessage());
+        }
+
+        URI uri = uriInfo.getBaseUriBuilder().path(ActivityResource.class).path("{activityID}").build(activityID);
+        activityData.setLink(new Link("self", uri));
+
+        return activityData;
+    }
+
+    public ActivityRecord createActivityRecord(ActivityData activityData) {
+
+        ActivityRecord activityRecord = new ActivityRecord();
+        activityRecord.setID(activityData.getID());
+        activityRecord.setTokenID(activityData.getTokenID());
+        activityRecord.setUserID(activityData.getUserID());
+        activityRecord.setDate(activityData.getDate());
+
+        return activityRecord;
+    }
+
+    @Override
+    public ActivityCollection findActivities(Integer start, Integer size) {
+
+        System.out.println("ActivityService.findActivities()");
+
+        try {
+            start = start == null ? 0 : start;
+            size = size == null ? DEFAULT_SIZE : size;
+
+            TPSSubsystem subsystem = TPSSubsystem.getInstance();
+            ActivityDatabase database = subsystem.getActivityDatabase();
+
+            Iterator<ActivityRecord> activities = database.getRecords().iterator();
+
+            ActivityCollection response = new ActivityCollection();
+
+            int i = 0;
+
+            // skip to the start of the page
+            for ( ; i<start && activities.hasNext(); i++) activities.next();
+
+            // return entries up to the page size
+            for ( ; i<start+size && activities.hasNext(); i++) {
+                response.addEntry(createActivityData(activities.next()));
+            }
+
+            // count the total entries
+            for ( ; activities.hasNext(); i++) activities.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 ActivityData getActivity(String activityID) {
+
+        System.out.println("ActivityService.getActivity(\"" + activityID + "\")");
+
+        try {
+            TPSSubsystem subsystem = TPSSubsystem.getInstance();
+            ActivityDatabase database = subsystem.getActivityDatabase();
+
+            return createActivityData(database.getRecord(activityID));
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new PKIException(e.getMessage());
+        }
+    }
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/tps/server/TPSApplication.java b/base/tps-tomcat/src/org/dogtagpki/tps/server/TPSApplication.java
index fc3c18ee21bab32ec744e15705087dd73ea1560b..39562cf48d2fca7b0a3a697d782da755418daf2a 100644
--- a/base/tps-tomcat/src/org/dogtagpki/tps/server/TPSApplication.java
+++ b/base/tps-tomcat/src/org/dogtagpki/tps/server/TPSApplication.java
@@ -22,6 +22,7 @@ import java.util.Set;
 
 import javax.ws.rs.core.Application;
 
+import org.dogtagpki.tps.logging.ActivityService;
 import org.dogtagpki.tps.token.TokenService;
 
 import com.netscape.certsrv.base.PKIException;
@@ -65,6 +66,9 @@ public class TPSApplication extends Application {
         // tokens
         classes.add(TokenService.class);
 
+        // activities
+        classes.add(ActivityService.class);
+
         // exception mapper
         classes.add(PKIException.Mapper.class);
 
diff --git a/base/tps-tomcat/src/org/dogtagpki/tps/server/TPSSubsystem.java b/base/tps-tomcat/src/org/dogtagpki/tps/server/TPSSubsystem.java
index ef92c7284c314f5d35ab62332e01ed59785ca151..ac42c2030e8df88c3246b1c43eba733dc7201721 100644
--- a/base/tps-tomcat/src/org/dogtagpki/tps/server/TPSSubsystem.java
+++ b/base/tps-tomcat/src/org/dogtagpki/tps/server/TPSSubsystem.java
@@ -17,6 +17,7 @@
 // --- END COPYRIGHT BLOCK ---
 package org.dogtagpki.tps.server;
 
+import org.dogtagpki.tps.logging.ActivityDatabase;
 import org.dogtagpki.tps.token.TokenDatabase;
 import org.mozilla.jss.CryptoManager;
 import org.mozilla.jss.CryptoManager.NotInitializedException;
@@ -47,6 +48,7 @@ public class TPSSubsystem implements IAuthority, ISubsystem {
     public IConfigStore config;
 
     public TokenDatabase tokenDatabase = new TokenDatabase();
+    public ActivityDatabase activityDatabase = new ActivityDatabase();
 
     public static TPSSubsystem getInstance() {
         return INSTANCE;
@@ -117,6 +119,10 @@ public class TPSSubsystem implements IAuthority, ISubsystem {
         return tokenDatabase;
     }
 
+    public ActivityDatabase getActivityDatabase() {
+        return activityDatabase;
+    }
+
     public org.mozilla.jss.crypto.X509Certificate getSubsystemCert() throws EBaseException, NotInitializedException,
             ObjectNotFoundException, TokenException {
         IConfigStore cs = CMS.getConfigStore();
-- 
1.8.1.4

