summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEndi S. Dewata <edewata@redhat.com>2016-04-23 00:50:08 +0200
committerEndi S. Dewata <edewata@redhat.com>2016-05-03 01:55:54 +0200
commit2b5f9e1630bae2e755093447b08c2236ce8db158 (patch)
treefb3f30683227f8bb02df12ece82b26273879e151
parent29cee52cfeb4b6c1b10f6ef4b4bdf91bffe0de7c (diff)
downloadpki-2b5f9e1630bae2e755093447b08c2236ce8db158.tar.gz
pki-2b5f9e1630bae2e755093447b08c2236ce8db158.tar.xz
pki-2b5f9e1630bae2e755093447b08c2236ce8db158.zip
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
-rw-r--r--base/common/src/com/netscape/certsrv/tps/token/TokenClient.java2
-rw-r--r--base/common/src/com/netscape/certsrv/tps/token/TokenResource.java2
-rw-r--r--base/common/src/com/netscape/certsrv/tps/token/TokenStatus.java136
-rw-r--r--base/java-tools/src/com/netscape/cmstools/tps/token/TokenFindCLI.java8
-rw-r--r--base/tps/src/org/dogtagpki/server/tps/engine/TPSEngine.java2
-rw-r--r--base/tps/src/org/dogtagpki/server/tps/rest/TokenService.java25
6 files changed, 140 insertions, 35 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 63e0edcde..9c43893bd 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 4c1bc39aa..c37b02704 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 b85f9eee2..b2f23dd9c 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,141 @@
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 {
+
+ public static class TokenStatusAdapter extends XmlAdapter<String, 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>();
- UNINITIALIZED(0),
- DAMAGED(1),
- PERM_LOST(2),
- TEMP_LOST(3),
- ACTIVE(4),
- TEMP_LOST_PERM_LOST(5),
- TERMINATED(6);
+ public final static int TOKEN_UNINITIALIZED = 0;
+ public final static int TOKEN_DAMAGED = 1;
+ public final static int TOKEN_PERM_LOST = 2;
+ public final static int TOKEN_TEMP_LOST = 3;
+ public final static int TOKEN_ACTIVE = 4;
+ public final static int TOKEN_TEMP_LOST_PERM_LOST = 5;
+ public final static int TOKEN_TERMINATED = 6;
- static Map<Integer, TokenStatus> map = new HashMap<Integer, TokenStatus>();
+ public final static TokenStatus UNINITIALIZED = new TokenStatus("UNINITIALIZED", TOKEN_UNINITIALIZED);
+ public final static TokenStatus DAMAGED = new TokenStatus("DAMAGED", TOKEN_DAMAGED);
+ public final static TokenStatus PERM_LOST = new TokenStatus("PERM_LOST", TOKEN_PERM_LOST);
+ public final static TokenStatus TEMP_LOST = new TokenStatus("TEMP_LOST", TOKEN_TEMP_LOST);
+ public final static TokenStatus ACTIVE = new TokenStatus("ACTIVE", TOKEN_ACTIVE);
+ public final static TokenStatus TEMP_LOST_PERM_LOST = new TokenStatus("TEMP_LOST_PERM_LOST", TOKEN_TEMP_LOST_PERM_LOST);
+ public final static TokenStatus TERMINATED = new TokenStatus("TERMINATED", TOKEN_TERMINATED);
+ String name;
Integer value;
- static {
- for (TokenStatus state : TokenStatus.values()) {
- map.put(state.value, state);
- }
+ public TokenStatus() {
+ // required for JAXB
}
- TokenStatus(Integer value) {
+ TokenStatus(String name, Integer value) {
+ this.name = name;
this.value = value;
+
+ instancesByName.put(name, this);
+ instancesByValue.put(value, this);
}
- public static TokenStatus fromInt(Integer value) {
- return map.get(value);
+ 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 int toInt() {
- return value.intValue();
+ 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);
+ }
+
+ return status;
+ }
+
+ public static TokenStatus fromInt(Integer value) {
+
+ 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 cfe7c2fe3..477d6d926 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 bc9d12c19..a577a7619 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 78a503994..46a3c12d6 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,14 @@ public class TokenService extends PKIService implements TokenResource {
auditModParams.put("UserID", tokenRecord.getUserID());
- switch (tokenState) {
- case UNINITIALIZED:
+ switch (tokenState.getValue()) {
+ case TokenStatus.TOKEN_UNINITIALIZED:
tokenRecord.setStatus("uninitialized");
newStatus = "uninitialized";
tokenRecord.setReason(null);
break;
- case ACTIVE:
+ case TokenStatus.TOKEN_ACTIVE:
if (tokenRecord.getTokenStatus() == TokenStatus.TEMP_LOST) {
// unrevoke certs
tps.tdb.unRevokeCertsByCUID(tokenRecord.getId(), ipAddress, remoteUser);
@@ -106,11 +106,10 @@ public class TokenService extends PKIService implements TokenResource {
tokenRecord.setStatus("active");
newStatus = "active";
tokenRecord.setReason(null);
-
break;
- case PERM_LOST:
- case TEMP_LOST_PERM_LOST:
+ case TokenStatus.TOKEN_PERM_LOST:
+ case TokenStatus.TOKEN_TEMP_LOST_PERM_LOST:
tokenRecord.setStatus("lost");
newStatus = "lost";
tokenRecord.setReason("keyCompromise");
@@ -120,7 +119,7 @@ public class TokenService extends PKIService implements TokenResource {
tps.tdb.revokeCertsByCUID(tokenRecord.getId(), "keyCompromise", ipAddress, remoteUser);
break;
- case DAMAGED:
+ case TokenStatus.TOKEN_DAMAGED:
tokenRecord.setStatus("lost");
newStatus = "lost";
tokenRecord.setReason("destroyed");
@@ -128,10 +127,9 @@ public class TokenService extends PKIService implements TokenResource {
//revoke certs
tps.tdb.revokeCertsByCUID(tokenRecord.getId(), "destroyed", ipAddress, remoteUser);
-
break;
- case TEMP_LOST:
+ case TokenStatus.TOKEN_TEMP_LOST:
tokenRecord.setStatus("lost");
newStatus = "lost";
tokenRecord.setReason("onHold");
@@ -141,7 +139,7 @@ public class TokenService extends PKIService implements TokenResource {
tps.tdb.revokeCertsByCUID(tokenRecord.getId(), "onHold", ipAddress, remoteUser);
break;
- case TERMINATED:
+ case TokenStatus.TOKEN_TERMINATED:
String reason = "terminated";
String origStatus2 = tokenRecord.getStatus();
String origReason2 = tokenRecord.getReason();
@@ -225,7 +223,7 @@ public class TokenService extends PKIService implements TokenResource {
String tokenID,
String userID,
String type,
- String status,
+ TokenStatus status,
Integer start,
Integer size) {
@@ -249,8 +247,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 +601,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,