[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [Pki-devel] [PATCH] 675 Added property file for token state and transition labels.



On 2/3/2016 10:31 PM, Endi Sukma Dewata wrote:
On 2/3/2016 12:45 PM, Endi Sukma Dewata wrote:
The labels for token states and transitions are now stored in
token-states.properties. The labels will be loaded when the UI
is initialized. The dialog box for changing token status will
now show the transition labels.

The property file later can be moved into the theme package to
allow customization.

https://fedorahosted.org/pki/ticket/1289
https://fedorahosted.org/pki/ticket/1291

New patch attached. Fixed some typos.

New patch attached. The token-states.properties has been moved into /usr/share/pki/tps/conf and can be customized for each instance.

--
Endi S. Dewata
>From b9204bb8edde24b1dd905fa970472514e546e08f Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata redhat com>
Date: Wed, 3 Feb 2016 19:20:31 +0100
Subject: [PATCH] Added resource bundle for token state labels.

The labels for token states and the transitions are now stored
in token-states.properties. The default file will be stored
in the /usr/share/pki/tps/conf, but it can be overriden by
copying and customizing the file into <instance>/tps/conf.

When the UI retrieves the token data the labels for the current
state and the valid transitions will be loaded from the file
and returned to the UI. The UI will show the transition labels
in the dropdown list for changing token status.

https://fedorahosted.org/pki/ticket/1289
https://fedorahosted.org/pki/ticket/1291
---
 .../com/netscape/certsrv/tps/token/TokenData.java  | 23 ++++++++----
 .../com/netscape/cmstools/tps/token/TokenCLI.java  | 20 +++++++++-
 .../com/netscape/cms/servlet/base/PKIService.java  | 41 +++++++++++++++++++++
 base/server/share/webapps/pki/js/pki-ui.js         | 22 +++++++++--
 base/tps/shared/conf/token-states.properties       | 21 +++++++++++
 base/tps/shared/webapps/tps/js/token.js            | 43 +++++++++++-----------
 base/tps/shared/webapps/tps/ui/token.html          |  2 +-
 base/tps/shared/webapps/tps/ui/tokens.html         |  2 +-
 .../dogtagpki/server/tps/rest/TokenService.java    | 24 ++++++++++--
 9 files changed, 159 insertions(+), 39 deletions(-)
 create mode 100644 base/tps/shared/conf/token-states.properties

diff --git a/base/common/src/com/netscape/certsrv/tps/token/TokenData.java b/base/common/src/com/netscape/certsrv/tps/token/TokenData.java
index 9947bf658100b23ac57d63fe4b6b32f34373a658..a4bddcad820fca0e15afbca59fb82738095c0c30 100644
--- a/base/common/src/com/netscape/certsrv/tps/token/TokenData.java
+++ b/base/common/src/com/netscape/certsrv/tps/token/TokenData.java
@@ -51,13 +51,18 @@ public class TokenData {
         }
     }
 
+    public static class TokenStatusData {
+        public TokenStatus name;
+        public String label;
+    }
+
     String id;
     String tokenID;
     String userID;
     String type;
 
-    TokenStatus status;
-    Collection<TokenStatus> nextStates;
+    TokenStatusData status;
+    Collection<TokenStatusData> nextStates;
 
     String appletID;
     String keyInfo;
@@ -104,20 +109,20 @@ public class TokenData {
     }
 
     @XmlElement(name="Status")
-    public TokenStatus getStatus() {
+    public TokenStatusData getStatus() {
         return status;
     }
 
-    public void setStatus(TokenStatus status) {
+    public void setStatus(TokenStatusData status) {
         this.status = status;
     }
 
     @XmlElement(name="NextStates")
-    public Collection<TokenStatus> getNextStates() {
+    public Collection<TokenStatusData> getNextStates() {
         return nextStates;
     }
 
-    public void setNextStates(Collection<TokenStatus> nextStates) {
+    public void setNextStates(Collection<TokenStatusData> nextStates) {
         this.nextStates = nextStates;
     }
 
@@ -288,7 +293,11 @@ public class TokenData {
         before.setID("token1");
         before.setUserID("user1");
         before.setType("userKey");
-        before.setStatus(TokenStatus.ACTIVE);
+
+        TokenStatusData statusData = new TokenStatusData();
+        statusData.name = TokenStatus.ACTIVE;
+        before.setStatus(statusData);
+
         before.setAppletID("APPLET1234");
         before.setKeyInfo("key info");
         before.setPolicy("FORCE_FORMAT=YES");
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/token/TokenCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/token/TokenCLI.java
index 328490a498f8bfbf56f2cef85767c35194e4218a..77ca40bd154d9ca8d517e0a664102db1df52044a 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/token/TokenCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/token/TokenCLI.java
@@ -18,11 +18,16 @@
 
 package com.netscape.cmstools.tps.token;
 
+import java.util.ArrayList;
+import java.util.Collection;
+
 import org.apache.commons.lang.StringUtils;
 import org.jboss.resteasy.plugins.providers.atom.Link;
 
 import com.netscape.certsrv.tps.token.TokenClient;
 import com.netscape.certsrv.tps.token.TokenData;
+import com.netscape.certsrv.tps.token.TokenData.TokenStatusData;
+import com.netscape.certsrv.tps.token.TokenStatus;
 import com.netscape.cmstools.cli.CLI;
 
 /**
@@ -54,8 +59,19 @@ public class TokenCLI extends CLI {
         System.out.println("  Token ID: " + token.getID());
         if (token.getUserID() != null) System.out.println("  User ID: " + token.getUserID());
         if (token.getType() != null) System.out.println("  Type: " + token.getType());
-        if (token.getStatus() != null) System.out.println("  Status: " + token.getStatus());
-        if (token.getNextStates() != null) System.out.println("  Next States: " + StringUtils.join(token.getNextStates(), ", "));
+
+        TokenStatusData status = token.getStatus();
+        if (status != null) System.out.println("  Status: " + status.name);
+
+        Collection<TokenStatusData> nextStates = token.getNextStates();
+        if (nextStates != null) {
+            Collection<TokenStatus> names = new ArrayList<TokenStatus>();
+            for (TokenStatusData nextState : nextStates) {
+                names.add(nextState.name);
+            }
+            System.out.println("  Next States: " + StringUtils.join(names, ", "));
+        }
+
         if (token.getAppletID() != null) System.out.println("  Applet ID: " + token.getAppletID());
         if (token.getKeyInfo() != null) System.out.println("  Key Info: " + token.getKeyInfo());
         if (token.getPolicy() != null) System.out.println("  Policy: " + token.getPolicy());
diff --git a/base/server/cms/src/com/netscape/cms/servlet/base/PKIService.java b/base/server/cms/src/com/netscape/cms/servlet/base/PKIService.java
index fe77fd567922a49641938cde99d533c091398b75..d2e55b5a3c42ee89f5784da12de860fd4e8880bf 100644
--- a/base/server/cms/src/com/netscape/cms/servlet/base/PKIService.java
+++ b/base/server/cms/src/com/netscape/cms/servlet/base/PKIService.java
@@ -17,14 +17,19 @@
 // --- END COPYRIGHT BLOCK ---
 package com.netscape.cms.servlet.base;
 
+import java.io.File;
 import java.lang.reflect.Method;
 import java.net.URI;
+import java.net.URL;
+import java.net.URLClassLoader;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
+import java.util.ResourceBundle;
 
+import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.FormParam;
 import javax.ws.rs.core.CacheControl;
@@ -77,10 +82,46 @@ public class PKIService {
     @Context
     protected HttpServletRequest servletRequest;
 
+    @Context
+    protected ServletContext servletContext;
 
     public ILogger logger = CMS.getLogger();
     public IAuditor auditor = CMS.getAuditor();
 
+    public String getInstanceDir() {
+        return System.getProperty("catalina.base");
+    }
+
+    public String getSubsystemName() {
+        // get web application path: /<subsystem>
+        String path = servletContext.getContextPath();
+
+        // get subsystem name by removing the / prefix from the path
+        return path.startsWith("/") ? path.substring(1) : path;
+    }
+
+    public String getSubsystemConfDir() {
+        return getInstanceDir() + File.separator + getSubsystemName() + File.separator + "conf";
+    }
+
+    public String getSharedSubsystemConfDir() {
+        return File.separator + "usr" + File.separator + "share" + File.separator + "pki" +
+                File.separator + getSubsystemName() + File.separator + "conf";
+    }
+
+    public ResourceBundle getResourceBundle(String name) throws Exception {
+
+        // Look in <instance>/<subsystem>/conf first,
+        // then fallback to /usr/share/pki/<subsystem>/conf.
+        URL[] urls = {
+                new File(getSubsystemConfDir()).toURI().toURL(),
+                new File(getSharedSubsystemConfDir()).toURI().toURL()
+        };
+
+        ClassLoader loader = new URLClassLoader(urls);
+        return ResourceBundle.getBundle(name, servletRequest.getLocale(), loader);
+    }
+
     public static MediaType resolveFormat(MediaType format) {
 
         if (format == null) return null;
diff --git a/base/server/share/webapps/pki/js/pki-ui.js b/base/server/share/webapps/pki/js/pki-ui.js
index c6e326a0c4b10a672e67a40fc2d21bfea4be6b43..0729895aa03c346bb634e662b06d0c9e1bc25203 100644
--- a/base/server/share/webapps/pki/js/pki-ui.js
+++ b/base/server/share/webapps/pki/js/pki-ui.js
@@ -456,8 +456,24 @@ var TableItem = Backbone.View.extend({
                 break;
             }
 
-            var name = RegExp.$1;
-            var value = self.get(name);
+            // get attribute name
+            var fullName = RegExp.$1;
+
+            // split attribute names
+            var names = fullName.split(".");
+
+            // get the value from the leaf object
+            var value;
+            for (var i=0; i<names.length; i++) {
+                var name = names[i];
+                if (i == 0) {
+                    value = self.get(name);
+                } else {
+                    value = value[name];
+                }
+                if (! value) break;
+            }
+
             if (value === undefined) value = "";
             if (value instanceof Date) value = value.toUTCString();
 
@@ -465,7 +481,7 @@ var TableItem = Backbone.View.extend({
             newContent += content.substring(0, index) + value;
 
             // process the remaining content
-            content = content.substring(index + name.length + 3);
+            content = content.substring(index + fullName.length + 3);
         }
 
         td.html(newContent);
diff --git a/base/tps/shared/conf/token-states.properties b/base/tps/shared/conf/token-states.properties
new file mode 100644
index 0000000000000000000000000000000000000000..f67688d4b8a80a6a8314edeb734e48af48ff357b
--- /dev/null
+++ b/base/tps/shared/conf/token-states.properties
@@ -0,0 +1,21 @@
+# Token states
+UNINITIALIZED       = Uninitialized
+ACTIVE              = Active
+TEMP_LOST           = Temporarily lost
+PERM_LOST           = Permanently lost
+DAMAGED             = Physically damaged
+TEMP_LOST_PERM_LOST = Temporarily lost then permanently lost
+TERMINATED          = Terminated
+
+# Token state transitions
+UNINITIALIZED.DAMAGED    = This token has been physically damaged.
+UNINITIALIZED.PERM_LOST  = This token has been permanently lost.
+UNINITIALIZED.TEMP_LOST  = This token has been temporarily lost.
+UNINITIALIZED.TERMINATED = This token has been terminated.
+TEMP_LOST.ACTIVE         = This temporarily lost token has been found.
+TEMP_LOST.PERM_LOST      = This temporarily lost token has become permanently lost.
+TEMP_LOST.TERMINATED     = This temporarily lost token has been terminated.
+ACTIVE.DAMAGED           = This token has been physically damaged.
+ACTIVE.PERM_LOST         = This token has been permanently lost.
+ACTIVE.TEMP_LOST         = This token has been temporarily lost.
+ACTIVE.TERMINATED        = This token has been terminated.
diff --git a/base/tps/shared/webapps/tps/js/token.js b/base/tps/shared/webapps/tps/js/token.js
index b4d5a717beb7e7c49e2641044881abaaa7c49b32..fb1c3ea4608312882eaa6d758fa18f6bd160f2e8 100644
--- a/base/tps/shared/webapps/tps/js/token.js
+++ b/base/tps/shared/webapps/tps/js/token.js
@@ -19,16 +19,6 @@
  * @author Endi S. Dewata
  */
 
-var TokenStatus = {
-    UNINITIALIZED: "Uninitialized",
-    ACTIVE: "Active",
-    TEMP_LOST: "Temporarily lost",
-    PERM_LOST: "Permanently lost",
-    DAMAGED: "Physically damaged",
-    TEMP_LOST_PERM_LOST: "Temporarily lost then permanently lost",
-    TERMINATED: "Terminated"
-};
-
 var TokenModel = Model.extend({
     urlRoot: "/tps/rest/tokens",
     parseResponse: function(response) {
@@ -38,7 +28,6 @@ var TokenModel = Model.extend({
             userID: response.UserID,
             type: response.Type,
             status: response.Status,
-            statusLabel: TokenStatus[response.Status],
             nextStates: response.NextStates,
             appletID: response.AppletID,
             keyInfo: response.KeyInfo,
@@ -53,7 +42,6 @@ var TokenModel = Model.extend({
             TokenID: attributes.tokenID,
             UserID: attributes.userID,
             Type: attributes.type,
-            Status: attributes.status,
             AppletID: attributes.appletID,
             KeyInfo: attributes.keyInfo,
             Policy: attributes.policy,
@@ -92,7 +80,6 @@ var TokenCollection = Collection.extend({
             userID: entry.UserID,
             type: entry.Type,
             status: entry.Status,
-            statusLabel: TokenStatus[entry.Status],
             nextStates: entry.NextStates,
             appletID: entry.AppletID,
             keyInfo: entry.KeyInfo,
@@ -114,19 +101,19 @@ var TokenDialog = Dialog.extend({
         }
 
         var select = input.empty();
-        var status = self.entry["status"];
+        var status = self.entry.status;
 
         $('<option/>', {
-            text: TokenStatus[status],
-            value: status,
+            text: status.label,
+            value: status.name,
             selected: true
         }).appendTo(select);
 
-        var nextStates = self.entry["nextStates"];
+        var nextStates = self.entry.nextStates;
         _.each(nextStates, function(nextState) {
             $('<option/>', {
-                text: TokenStatus[nextState],
-                value: nextState
+                text: nextState.label,
+                value: nextState.name
             }).appendTo(select);
         });
     }
@@ -212,7 +199,7 @@ var TokenPage = EntryPage.extend({
             self.$("label[name='modifyTimestamp']").hide();
             self.$("input[name='modifyTimestamp']").hide();
             self.$("label[name='status']").hide();
-            self.$("input[name='statusLabel']").hide();
+            self.$("input[name='status']").hide();
 
         } else {
             self.changeStatusAction.show();
@@ -227,8 +214,22 @@ var TokenPage = EntryPage.extend({
             self.$("label[name='modifyTimestamp']").show();
             self.$("input[name='modifyTimestamp']").show();
             self.$("label[name='status']").show();
-            self.$("input[name='statusLabel']").show();
+            self.$("input[name='status']").show();
         }
+    },
+    loadField: function(input) {
+        var self = this;
+
+        var name = input.attr("name");
+        if (name != "status") {
+            TokenPage.__super__.loadField.call(self, input);
+            return;
+        }
+
+        var value = self.entry.status;
+        if (value) value = value.label;
+        if (value === undefined) value = "";
+        input.val(value);
     }
 });
 
diff --git a/base/tps/shared/webapps/tps/ui/token.html b/base/tps/shared/webapps/tps/ui/token.html
index aaf18141424d972eb3312eec5819a8bc06e8420c..bc4b01d22caa137587e30c5aba10c22cbd67daa4 100644
--- a/base/tps/shared/webapps/tps/ui/token.html
+++ b/base/tps/shared/webapps/tps/ui/token.html
@@ -60,7 +60,7 @@
     <label name="modifyTimestamp">Modified</label>
     <input name="modifyTimestamp" readonly="readonly"><br>
     <label name="status">Status</label>
-    <input name="statusLabel" readonly="readonly"><br>
+    <input name="status" readonly="readonly"><br>
 </fieldset>
 </div>
 
diff --git a/base/tps/shared/webapps/tps/ui/tokens.html b/base/tps/shared/webapps/tps/ui/tokens.html
index ec89de958a17bba09b181e8bbe3d15b6c8287cad..b0782d3731e4664ef2bcff5474d640d130807fc8 100644
--- a/base/tps/shared/webapps/tps/ui/tokens.html
+++ b/base/tps/shared/webapps/tps/ui/tokens.html
@@ -56,7 +56,7 @@
         <td name="id"><a href="#tokens/${id}">${id}</a></td>
         <td name="userID">${userID}</td>
         <td name="type">${type}</td>
-        <td name="status"><a href="#tokens/${id}/status">${statusLabel}</a></td>
+        <td name="status"><a href="#tokens/${id}/status">${status.label}</a></td>
         <td name="appletID">${appletID}</td>
         <td name="keyInfo">${keyInfo}</td>
         <td name="policy">${policy}</td>
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 247aa911c809454891ae3f8a6dc358a965b96008..a188799b3098eefba49ae8d2160d2b3aafe98f75 100644
--- a/base/tps/src/org/dogtagpki/server/tps/rest/TokenService.java
+++ b/base/tps/src/org/dogtagpki/server/tps/rest/TokenService.java
@@ -21,11 +21,13 @@ package org.dogtagpki.server.tps.rest;
 import java.io.UnsupportedEncodingException;
 import java.net.URI;
 import java.net.URLEncoder;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.ResourceBundle;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.core.Context;
@@ -50,6 +52,7 @@ import com.netscape.certsrv.dbs.EDBException;
 import com.netscape.certsrv.ldap.LDAPExceptionConverter;
 import com.netscape.certsrv.tps.token.TokenCollection;
 import com.netscape.certsrv.tps.token.TokenData;
+import com.netscape.certsrv.tps.token.TokenData.TokenStatusData;
 import com.netscape.certsrv.tps.token.TokenResource;
 import com.netscape.certsrv.tps.token.TokenStatus;
 import com.netscape.cms.servlet.base.PKIService;
@@ -192,7 +195,9 @@ public class TokenService extends PKIService implements TokenResource {
 
     }
 
-    public TokenData createTokenData(TokenRecord tokenRecord) {
+    public TokenData createTokenData(TokenRecord tokenRecord) throws Exception {
+
+        ResourceBundle labels = getResourceBundle("token-states");
 
         TokenData tokenData = new TokenData();
         tokenData.setID(tokenRecord.getId());
@@ -200,9 +205,20 @@ public class TokenService extends PKIService implements TokenResource {
         tokenData.setUserID(tokenRecord.getUserID());
         tokenData.setType(tokenRecord.getType());
 
-        TokenStatus currentState = getTokenStatus(tokenRecord);
-        tokenData.setStatus(currentState);
-        tokenData.setNextStates(transitions.get(currentState));
+        TokenStatus status = getTokenStatus(tokenRecord);
+        TokenStatusData statusData = new TokenStatusData();
+        statusData.name = status;
+        statusData.label = labels.getString(status.toString());
+        tokenData.setStatus(statusData);
+
+        Collection<TokenStatusData> nextStates = new ArrayList<TokenStatusData>();
+        for (TokenStatus nextState : transitions.get(status)) {
+            TokenStatusData nextStateData = new TokenStatusData();
+            nextStateData.name = nextState;
+            nextStateData.label = labels.getString(status + "." + nextState);
+            nextStates.add(nextStateData);
+        }
+        tokenData.setNextStates(nextStates);
 
         tokenData.setAppletID(tokenRecord.getAppletID());
         tokenData.setKeyInfo(tokenRecord.getKeyInfo());
-- 
2.4.3


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]