>From 8c55952d16d986f79ad55c098d80266c35012cc2 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Sat, 23 Apr 2016 00:50:08 +0200
Subject: [PATCH] Refactored TokenStatus enumeration.

The TokenStatus enumeration has been converted into a class to
allow overriding the TokenStatus.valueOf() to provide backward
compatibility.

https://fedorahosted.org/pki/ticket/2286
---
 .../netscape/certsrv/tps/token/TokenClient.java    |   2 +-
 .../netscape/certsrv/tps/token/TokenResource.java  |   2 +-
 .../netscape/certsrv/tps/token/TokenStatus.java    | 130 ++++++++++++++++++---
 .../netscape/cmstools/tps/token/TokenFindCLI.java  |   8 +-
 .../org/dogtagpki/server/tps/engine/TPSEngine.java |   2 +-
 .../dogtagpki/server/tps/rest/TokenService.java    |  31 ++---
 6 files changed, 132 insertions(+), 43 deletions(-)

diff --git a/base/common/src/com/netscape/certsrv/tps/token/TokenClient.java b/base/common/src/com/netscape/certsrv/tps/token/TokenClient.java
index 63e0edcde173524f2833cdd2ca317bb0d05f8af1..9c43893bdb1bb0b8590a4de590db18935e18f2bd 100644
--- a/base/common/src/com/netscape/certsrv/tps/token/TokenClient.java
+++ b/base/common/src/com/netscape/certsrv/tps/token/TokenClient.java
@@ -45,7 +45,7 @@ public class TokenClient extends Client {
             String tokenID,
             String userID,
             String type,
-            String status,
+            TokenStatus status,
             Integer start,
             Integer size) {
 
diff --git a/base/common/src/com/netscape/certsrv/tps/token/TokenResource.java b/base/common/src/com/netscape/certsrv/tps/token/TokenResource.java
index 4c1bc39aa0264f5f4f01077e3642bc6650b81aa7..c37b02704ca6ab88846c8ab28052de2ef51b0c4a 100644
--- a/base/common/src/com/netscape/certsrv/tps/token/TokenResource.java
+++ b/base/common/src/com/netscape/certsrv/tps/token/TokenResource.java
@@ -48,7 +48,7 @@ public interface TokenResource {
             @QueryParam("tokenID") String tokenID,
             @QueryParam("userID") String userID,
             @QueryParam("type") String type,
-            @QueryParam("status") String status,
+            @QueryParam("status") TokenStatus status,
             @QueryParam("start") Integer start,
             @QueryParam("size") Integer size);
 
diff --git a/base/common/src/com/netscape/certsrv/tps/token/TokenStatus.java b/base/common/src/com/netscape/certsrv/tps/token/TokenStatus.java
index b85f9eee290bec685d526b535345ee076928cf2f..18c1d9302c0529279f303c9613d17383004078be 100644
--- a/base/common/src/com/netscape/certsrv/tps/token/TokenStatus.java
+++ b/base/common/src/com/netscape/certsrv/tps/token/TokenStatus.java
@@ -18,41 +18,133 @@
 
 package com.netscape.certsrv.tps.token;
 
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
 
+import javax.xml.bind.annotation.adapters.XmlAdapter;
+import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
+
+import com.netscape.certsrv.tps.token.TokenStatus.TokenStatusAdapter;
+
+
 /**
  * @author Endi S. Dewata
  */
-public enum TokenStatus {
+@XmlJavaTypeAdapter(TokenStatusAdapter.class)
+public class TokenStatus {
 
-    UNINITIALIZED(0),
-    DAMAGED(1),
-    PERM_LOST(2),
-    TEMP_LOST(3),
-    ACTIVE(4),
-    TEMP_LOST_PERM_LOST(5),
-    TERMINATED(6);
+    public static class TokenStatusAdapter extends XmlAdapter<String, TokenStatus> {
 
-    static Map<Integer, TokenStatus> map = new HashMap<Integer, TokenStatus>();
+        public String marshal(TokenStatus status) {
+            return status.name;
+        }
 
+        public TokenStatus unmarshal(String status) {
+            return TokenStatus.valueOf(status);
+        }
+    }
+
+    static Map<String, TokenStatus> instancesByName = new HashMap<String, TokenStatus>();
+    static Map<Integer, TokenStatus> instancesByValue = new HashMap<Integer, TokenStatus>();
+
+    public final static TokenStatus UNINITIALIZED       = new TokenStatus("UNINITIALIZED", 0);
+    public final static TokenStatus DAMAGED             = new TokenStatus("DAMAGED", 1);
+    public final static TokenStatus PERM_LOST           = new TokenStatus("PERM_LOST", 2);
+    public final static TokenStatus TEMP_LOST           = new TokenStatus("TEMP_LOST", 3);
+    public final static TokenStatus ACTIVE              = new TokenStatus("ACTIVE", 4);
+    public final static TokenStatus TEMP_LOST_PERM_LOST = new TokenStatus("TEMP_LOST_PERM_LOST", 5);
+    public final static TokenStatus TERMINATED          = new TokenStatus("TERMINATED", 6);
+
+    String name;
     Integer value;
 
-    static {
-        for (TokenStatus state : TokenStatus.values()) {
-            map.put(state.value, state);
+    public TokenStatus() {
+        // required for JAXB
+    }
+
+    public TokenStatus(String name, Integer value) {
+        this.name = name;
+        this.value = value;
+
+        instancesByName.put(name, this);
+        instancesByValue.put(value, this);
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Integer getValue() {
+        return value;
+    }
+
+    public void setValue(Integer value) {
+        this.value = value;
+    }
+
+    public String toString() {
+        return name;
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((name == null) ? 0 : name.hashCode());
+        result = prime * result + ((value == null) ? 0 : value.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;
+        TokenStatus other = (TokenStatus) obj;
+        if (name == null) {
+            if (other.name != null)
+                return false;
+        } else if (!name.equals(other.name))
+            return false;
+        if (value == null) {
+            if (other.value != null)
+                return false;
+        } else if (!value.equals(other.value))
+            return false;
+        return true;
+    }
+
+    public static Collection<TokenStatus> values() {
+        return instancesByName.values();
+    }
+
+    public static TokenStatus valueOf(String name) {
+
+        TokenStatus status = instancesByName.get(name);
+
+        if (status == null) {
+            throw new IllegalArgumentException("Invalid token status name: " + name);
         }
-    }
 
-    TokenStatus(Integer value) {
-        this.value = value;
+        return status;
     }
 
     public static TokenStatus fromInt(Integer value) {
-        return map.get(value);
-    }
 
-    public int toInt() {
-        return value.intValue();
+        TokenStatus status = instancesByValue.get(value);
+
+        if (status == null) {
+            throw new IllegalArgumentException("Invalid token status value: " + value);
+        }
+
+        return status;
     }
 }
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/token/TokenFindCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/token/TokenFindCLI.java
index cfe7c2fe3b2a906367f5863296588de8ede81860..477d6d926c507ca5dc972ef434a0d85d09042791 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/token/TokenFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/token/TokenFindCLI.java
@@ -26,6 +26,7 @@ import org.apache.commons.cli.Option;
 
 import com.netscape.certsrv.tps.token.TokenCollection;
 import com.netscape.certsrv.tps.token.TokenData;
+import com.netscape.certsrv.tps.token.TokenStatus;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -97,7 +98,12 @@ public class TokenFindCLI extends CLI {
         String tokenID = cmd.getOptionValue("token");
         String userID = cmd.getOptionValue("user");
         String type = cmd.getOptionValue("type");
-        String status = cmd.getOptionValue("status");
+        String statusStr = cmd.getOptionValue("status");
+
+        TokenStatus status = null;
+        if (statusStr != null) {
+            status = TokenStatus.valueOf(statusStr);
+        }
 
         String s = cmd.getOptionValue("start");
         Integer start = s == null ? null : Integer.valueOf(s);
diff --git a/base/tps/src/org/dogtagpki/server/tps/engine/TPSEngine.java b/base/tps/src/org/dogtagpki/server/tps/engine/TPSEngine.java
index bc9d12c19c480fec7814059c3e23ce2e2a72306d..a577a76199a51e37f8bbcd7b7477f39aad85c8e8 100644
--- a/base/tps/src/org/dogtagpki/server/tps/engine/TPSEngine.java
+++ b/base/tps/src/org/dogtagpki/server/tps/engine/TPSEngine.java
@@ -620,7 +620,7 @@ public class TPSEngine {
 
         }
 
-        String transition = oldState.toInt() + ":" + newState.toInt();
+        String transition = oldState.getValue() + ":" + newState.getValue();
 
         CMS.debug("TPSEngine.isOperationTransistionAllowed: checking for transition: " + transition);
 
diff --git a/base/tps/src/org/dogtagpki/server/tps/rest/TokenService.java b/base/tps/src/org/dogtagpki/server/tps/rest/TokenService.java
index 78a503994283302b50fc771379d4760e8df0974a..312b09934282d0e508db868ac27a675d817c1238 100644
--- a/base/tps/src/org/dogtagpki/server/tps/rest/TokenService.java
+++ b/base/tps/src/org/dogtagpki/server/tps/rest/TokenService.java
@@ -90,14 +90,12 @@ public class TokenService extends PKIService implements TokenResource {
 
         auditModParams.put("UserID", tokenRecord.getUserID());
 
-        switch (tokenState) {
-        case UNINITIALIZED:
+        if (tokenState == TokenStatus.UNINITIALIZED) {
             tokenRecord.setStatus("uninitialized");
             newStatus = "uninitialized";
             tokenRecord.setReason(null);
-            break;
 
-        case ACTIVE:
+        } else if (tokenState == TokenStatus.ACTIVE) {
             if (tokenRecord.getTokenStatus() == TokenStatus.TEMP_LOST) {
                 // unrevoke certs
                 tps.tdb.unRevokeCertsByCUID(tokenRecord.getId(), ipAddress, remoteUser);
@@ -107,10 +105,7 @@ public class TokenService extends PKIService implements TokenResource {
             newStatus = "active";
             tokenRecord.setReason(null);
 
-            break;
-
-        case PERM_LOST:
-        case TEMP_LOST_PERM_LOST:
+        } else if (tokenState == TokenStatus.PERM_LOST || tokenState == TokenStatus.TEMP_LOST_PERM_LOST) {
             tokenRecord.setStatus("lost");
             newStatus = "lost";
             tokenRecord.setReason("keyCompromise");
@@ -118,9 +113,8 @@ public class TokenService extends PKIService implements TokenResource {
 
             //revoke certs
             tps.tdb.revokeCertsByCUID(tokenRecord.getId(), "keyCompromise", ipAddress, remoteUser);
-            break;
 
-        case DAMAGED:
+        } else if (tokenState == TokenStatus.DAMAGED) {
             tokenRecord.setStatus("lost");
             newStatus = "lost";
             tokenRecord.setReason("destroyed");
@@ -129,9 +123,7 @@ public class TokenService extends PKIService implements TokenResource {
             //revoke certs
             tps.tdb.revokeCertsByCUID(tokenRecord.getId(), "destroyed", ipAddress, remoteUser);
 
-            break;
-
-        case TEMP_LOST:
+        } else if (tokenState == TokenStatus.TEMP_LOST) {
             tokenRecord.setStatus("lost");
             newStatus = "lost";
             tokenRecord.setReason("onHold");
@@ -139,9 +131,8 @@ public class TokenService extends PKIService implements TokenResource {
 
             // put certs onHold
             tps.tdb.revokeCertsByCUID(tokenRecord.getId(), "onHold", ipAddress, remoteUser);
-            break;
 
-        case TERMINATED:
+        } else if (tokenState == TokenStatus.TERMINATED) {
             String reason = "terminated";
             String origStatus2 = tokenRecord.getStatus();
             String origReason2 = tokenRecord.getReason();
@@ -157,9 +148,8 @@ public class TokenService extends PKIService implements TokenResource {
 
             //revoke certs
             tps.tdb.revokeCertsByCUID(tokenRecord.getId(), reason, ipAddress, remoteUser);
-            break;
 
-        default:
+        } else {
             PKIException e = new PKIException("Unsupported token state: " + tokenState);
             auditTokenStateChange(ILogger.FAILURE, oldStatus,
                     newStatus, oldReason, newReason,
@@ -225,7 +215,7 @@ public class TokenService extends PKIService implements TokenResource {
             String tokenID,
             String userID,
             String type,
-            String status,
+            TokenStatus status,
             Integer start,
             Integer size) {
 
@@ -249,8 +239,8 @@ public class TokenService extends PKIService implements TokenResource {
             attributes.put("type", type);
         }
 
-        if (StringUtils.isNotEmpty(status)) {
-            attributes.put("status", status);
+        if (status != null) {
+            attributes.put("status", status.toString());
         }
 
         start = start == null ? 0 : start;
@@ -603,6 +593,7 @@ public class TokenService extends PKIService implements TokenResource {
                     "Token ID is null.");
             throw new BadRequestException("Token ID is null.");
         }
+
         auditModParams.put("tokenID", tokenID);
         if (tokenStatus == null) {
             auditConfigTokenGeneral(ILogger.FAILURE, method, null,
-- 
2.5.5

