From 19af6ec9772150402caca8594e4f8c5b2943d3f7 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Thu, 12 Sep 2013 14:30:13 -0400
Subject: [PATCH] Connected token database to LDAP.

The TokenDatabase has been reimplemented using LDAPDatabase. The
TokenRecord has been modified to specify the object class and
attribute mappings.

Ticket #652
---
 .../src/org/dogtagpki/server/tps/TPSSubsystem.java | 10 ++++-
 .../dogtagpki/server/tps/token/TokenDatabase.java  | 51 ++++++++++------------
 .../dogtagpki/server/tps/token/TokenRecord.java    | 21 +++++++--
 .../dogtagpki/server/tps/token/TokenService.java   | 10 ++---
 4 files changed, 54 insertions(+), 38 deletions(-)

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 34edc58f46b134987333e155d33a11986ea63ad5..853e970c1acd51056857d409ed2378bb5873660a 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java
@@ -32,9 +32,11 @@ import com.netscape.certsrv.authority.IAuthority;
 import com.netscape.certsrv.base.EBaseException;
 import com.netscape.certsrv.base.IConfigStore;
 import com.netscape.certsrv.base.ISubsystem;
+import com.netscape.certsrv.dbs.IDBSubsystem;
 import com.netscape.certsrv.logging.ILogger;
 import com.netscape.certsrv.request.IRequestListener;
 import com.netscape.certsrv.request.IRequestQueue;
+import com.netscape.cmscore.dbs.DBSubsystem;
 
 /**
  * @author Endi S. Dewata <edewata@redhat.com>
@@ -54,7 +56,7 @@ public class TPSSubsystem implements IAuthority, ISubsystem {
     public AuthenticatorDatabase authenticatorDatabase = new AuthenticatorDatabase();
     public ConnectionDatabase connectionDatabase = new ConnectionDatabase();
     public TPSCertDatabase certDatabase = new TPSCertDatabase();
-    public TokenDatabase tokenDatabase = new TokenDatabase();
+    public TokenDatabase tokenDatabase;
 
     @Override
     public String getId() {
@@ -70,6 +72,12 @@ public class TPSSubsystem implements IAuthority, ISubsystem {
     public void init(ISubsystem owner, IConfigStore config) throws EBaseException {
         this.owner = owner;
         this.config = config;
+
+        IDBSubsystem dbSubsystem = DBSubsystem.getInstance();
+        IConfigStore cs = CMS.getConfigStore();
+
+        String tokenDatabaseDN = cs.getString("tokendb.baseDN");
+        tokenDatabase = new TokenDatabase(dbSubsystem, tokenDatabaseDN);
     }
 
     @Override
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenDatabase.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenDatabase.java
index a4c8e34c5cb289378c51aac94a7121ea4dfe874a..d16e4fc2522b13d141ef76920cc0009b30b442a7 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenDatabase.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenDatabase.java
@@ -20,48 +20,41 @@ package org.dogtagpki.server.tps.token;
 
 import java.util.Date;
 
-import com.netscape.cmscore.dbs.Database;
+import com.netscape.certsrv.base.EBaseException;
+import com.netscape.certsrv.dbs.IDBSubsystem;
+import com.netscape.cmscore.dbs.LDAPDatabase;
 
 /**
- * This class implements in-memory token database. In the future this
- * will be replaced with LDAP database.
- *
  * @author Endi S. Dewata
  */
-public class TokenDatabase extends Database<TokenRecord> {
+public class TokenDatabase extends LDAPDatabase<TokenRecord> {
 
-    public TokenDatabase() {
-        super("Token");
-
-        // add sample records
-        try {
-            TokenRecord record1 = new TokenRecord();
-            record1.setID("token1");
-            record1.setUserID("user1");
-            record1.setStatus("ENABLED");
-            addRecord(record1);
-
-            TokenRecord record2 = new TokenRecord();
-            record2.setID("token2");
-            record2.setUserID("user2");
-            record2.setStatus("DISABLED");
-            addRecord(record2);
-
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
+    public TokenDatabase(IDBSubsystem dbSubsystem, String baseDN) throws EBaseException {
+        super("Token", dbSubsystem, baseDN, TokenRecord.class);
     }
 
-    public void addRecord(TokenRecord tokenRecord) throws Exception {
+    @Override
+    public void addRecord(String id, TokenRecord tokenRecord) throws Exception {
         tokenRecord.setStatus("ENABLED");
         tokenRecord.setCreateTimestamp(new Date());
 
-        addRecord(tokenRecord.getID(), tokenRecord);
+        super.addRecord(id, tokenRecord);
     }
 
-    public void updateRecord(TokenRecord tokenRecord) throws Exception {
+    @Override
+    public void updateRecord(String id, TokenRecord tokenRecord) throws Exception {
         tokenRecord.setModifyTimestamp(new Date());
 
-        updateRecord(tokenRecord.getID(), tokenRecord);
+        super.updateRecord(id, tokenRecord);
+    }
+
+    @Override
+    public String createDN(String id) {
+        return "cn=" + id + "," + baseDN;
+    }
+
+    @Override
+    public String createFilter(String filter) {
+        return "(id=*)";
     }
 }
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenRecord.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenRecord.java
index ff3af5776809cc51f616c90d56b630d01f175dc2..bf0d1b1205e09defb7684fa29b5f03fc8957026d 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenRecord.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenRecord.java
@@ -20,10 +20,17 @@ package org.dogtagpki.server.tps.token;
 
 import java.util.Date;
 
+import com.netscape.cmscore.dbs.DBAttribute;
+import com.netscape.cmscore.dbs.DBObjectClasses;
+import com.netscape.cmscore.dbs.DBRecord;
+
 /**
  * @author Endi S. Dewata
  */
-public class TokenRecord {
+@DBObjectClasses({ "top", "tokenRecord" })
+public class TokenRecord extends DBRecord {
+
+    private static final long serialVersionUID = 1L;
 
     String id;
     String userID;
@@ -34,14 +41,16 @@ public class TokenRecord {
     Date createTimestamp;
     Date modifyTimestamp;
 
-    public String getID() {
+    @DBAttribute("cn")
+    public String getId() {
         return id;
     }
 
-    public void setID(String id) {
+    public void setId(String id) {
         this.id = id;
     }
 
+    @DBAttribute("tokenUserID")
     public String getUserID() {
         return userID;
     }
@@ -50,6 +59,7 @@ public class TokenRecord {
         this.userID = userID;
     }
 
+    @DBAttribute("tokenStatus")
     public String getStatus() {
         return status;
     }
@@ -58,6 +68,7 @@ public class TokenRecord {
         this.status = status;
     }
 
+    @DBAttribute("tokenReason")
     public String getReason() {
         return reason;
     }
@@ -66,6 +77,7 @@ public class TokenRecord {
         this.reason = reason;
     }
 
+    @DBAttribute("tokenAppletID")
     public String getAppletID() {
         return appletID;
     }
@@ -74,6 +86,7 @@ public class TokenRecord {
         this.appletID = appletID;
     }
 
+    @DBAttribute("keyInfo")
     public String getKeyInfo() {
         return keyInfo;
     }
@@ -82,6 +95,7 @@ public class TokenRecord {
         this.keyInfo = keyInfo;
     }
 
+    @DBAttribute("dateOfCreate")
     public Date getCreateTimestamp() {
         return createTimestamp;
     }
@@ -90,6 +104,7 @@ public class TokenRecord {
         this.createTimestamp = createTimestamp;
     }
 
+    @DBAttribute("dateOfModify")
     public Date getModifyTimestamp() {
         return modifyTimestamp;
     }
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenService.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenService.java
index 3cf3b9e4e29f38090ab975f7462d427bbe5a8f77..e84e7c5c85726249ed6f77a3ac85137dc6084483 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenService.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenService.java
@@ -51,7 +51,7 @@ public class TokenService extends PKIService implements TokenResource {
     public TokenData createTokenData(TokenRecord tokenRecord) {
 
         TokenData tokenData = new TokenData();
-        tokenData.setID(tokenRecord.getID());
+        tokenData.setID(tokenRecord.getId());
         tokenData.setUserID(tokenRecord.getUserID());
         tokenData.setStatus(tokenRecord.getStatus());
         tokenData.setReason(tokenRecord.getReason());
@@ -60,7 +60,7 @@ public class TokenService extends PKIService implements TokenResource {
         tokenData.setCreateTimestamp(tokenRecord.getCreateTimestamp());
         tokenData.setModifyTimestamp(tokenRecord.getModifyTimestamp());
 
-        String tokenID = tokenRecord.getID();
+        String tokenID = tokenRecord.getId();
         try {
             tokenID = URLEncoder.encode(tokenID, "UTF-8");
         } catch (UnsupportedEncodingException e) {
@@ -77,7 +77,7 @@ public class TokenService extends PKIService implements TokenResource {
     public TokenRecord createTokenRecord(TokenData tokenData) {
 
         TokenRecord tokenRecord = new TokenRecord();
-        tokenRecord.setID(tokenData.getID());
+        tokenRecord.setId(tokenData.getID());
         tokenRecord.setUserID(tokenData.getUserID());
         tokenRecord.setStatus(tokenData.getStatus());
         tokenRecord.setReason(tokenData.getReason());
@@ -162,7 +162,7 @@ public class TokenService extends PKIService implements TokenResource {
             TPSSubsystem subsystem = (TPSSubsystem)CMS.getSubsystem(TPSSubsystem.ID);
             TokenDatabase database = subsystem.getTokenDatabase();
 
-            database.addRecord(createTokenRecord(tokenData));
+            database.addRecord(tokenData.getID(), createTokenRecord(tokenData));
             tokenData = createTokenData(database.getRecord(tokenData.getID()));
 
             return Response
@@ -188,7 +188,7 @@ public class TokenService extends PKIService implements TokenResource {
 
             TokenRecord tokenRecord = database.getRecord(tokenID);
             tokenRecord.setUserID(tokenData.getUserID());
-            database.updateRecord(tokenData.getID(), tokenRecord);
+            database.updateRecord(tokenID, tokenRecord);
 
             tokenData = createTokenData(database.getRecord(tokenID));
 
-- 
1.8.1.4

