aboutsummaryrefslogtreecommitdiff
path: root/pdf-as-lib/src/main/java/at/knowcenter/wag/egov/egiz/sig/SignatureTypes.java
diff options
context:
space:
mode:
Diffstat (limited to 'pdf-as-lib/src/main/java/at/knowcenter/wag/egov/egiz/sig/SignatureTypes.java')
-rw-r--r--pdf-as-lib/src/main/java/at/knowcenter/wag/egov/egiz/sig/SignatureTypes.java167
1 files changed, 159 insertions, 8 deletions
diff --git a/pdf-as-lib/src/main/java/at/knowcenter/wag/egov/egiz/sig/SignatureTypes.java b/pdf-as-lib/src/main/java/at/knowcenter/wag/egov/egiz/sig/SignatureTypes.java
index 783512c..a4d71fd 100644
--- a/pdf-as-lib/src/main/java/at/knowcenter/wag/egov/egiz/sig/SignatureTypes.java
+++ b/pdf-as-lib/src/main/java/at/knowcenter/wag/egov/egiz/sig/SignatureTypes.java
@@ -26,7 +26,9 @@
package at.knowcenter.wag.egov.egiz.sig;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -45,6 +47,118 @@ import at.knowcenter.wag.exactparser.ByteArrayUtils;
public class SignatureTypes
{
+ /**
+ * Defines all supported states for {@link SignatureTypes} (signature profiles). Signature types can be enabled
+ * ("on"), can be set to support signature only ("sign_only"), to verification only ("verify_only") or can be
+ * disabled ("off" or any other value not covered by other enum values).
+ *
+ * @author Datentechnik Innovation GmbH
+ */
+ public enum State {
+
+ /**
+ * Enables a signature profile.
+ */
+ ON ("on", "yes", "true", "enabled"),
+
+ /**
+ * Disables a signature profile.
+ */
+ OFF (),
+
+ /**
+ * Restricts the signature profile so that is can only be used for verification purposes and not for signature.
+ */
+ VERIFY_ONLY ("verify_only", "verify-only", "verifyonly", "verify only", "verify"),
+
+ /**
+ * Allows the signature profile to be used for signature but not for verification.
+ */
+ SIGN_ONLY ("sign_only", "sign-only", "signonly", "sign only", "sign");
+
+ /**
+ * Sets the default state when no valid value was provided.
+ */
+ private static final State DEFAULT = OFF;
+
+ /**
+ * States that allow signatures.
+ */
+ private static final State[] CAN_SIGN = { ON, SIGN_ONLY };
+
+ /**
+ * States that allow verification.
+ */
+ private static final State[] CAN_VERIFY = { ON, VERIFY_ONLY };
+
+ private String[] keyWords;
+
+ private State(String... keyWords) {
+ this.keyWords = keyWords;
+ }
+
+ /**
+ * Returns a valid State from a given {@code keyWord}. If the {@code keyWord} cannot be matched to a certain
+ * state, the default State {@link #OFF} is returned.
+ *
+ * @param keyWord
+ * A valid keyword like "on", "sign_only"...
+ * @return The enum State.
+ */
+ public static State fromString(String keyWord) {
+ if (keyWord == null) {
+ return DEFAULT;
+ }
+ try {
+ return valueOf(keyWord.toUpperCase());
+ } catch (IllegalArgumentException e) {
+ for (State candidate : values()) {
+ for (String candidateKeyWord : candidate.keyWords) {
+ if (keyWord.equalsIgnoreCase(candidateKeyWord)) {
+ return candidate;
+ }
+ }
+ }
+ return DEFAULT;
+ }
+ }
+
+ /**
+ * Returns {@code true} when the current state is one of the given candidate {@code states}.
+ *
+ * @param states
+ * The candidate states.
+ * @return {@code true} when the current state is one of the given candidate states, {@code false} if not.
+ */
+ public boolean in(State... states) {
+ if (states != null) {
+ for (State state : states) {
+ if (this == state) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Returns if the respective state allows signatures.
+ * @return {@code true} if signatures are allowed, {@code false} if not.
+ */
+ public boolean canSign() {
+ return in(CAN_SIGN);
+ }
+
+ /**
+ * Returns if the respective state allows verification.
+ * @return {@code true} if verification is allowed, {@code false} if not.
+ */
+ public boolean canVerify() {
+ return in(CAN_VERIFY);
+ }
+
+ }
+
// 03.11.2010 changed by exthex - commented unneeded setDefaultStyles method to reduce confusion
/**
@@ -70,8 +184,8 @@ public class SignatureTypes
/**
* The state value activating an signature definition
*/
- private static final String STATE_ON = "on";
-
+// public static final String STATE_ON = "on";
+
// /**
// * The state value de activating an signature definition
// */
@@ -403,10 +517,12 @@ public class SignatureTypes
if (settings_ != null)
{
ArrayList types = settings_.getKeys(TYPES);
- for (int type_idx = 0; type_idx < types.size(); type_idx++)
- {
- String type = (String) types.get(type_idx);
- addSignatureType(type);
+ if (types != null) {
+ for (int type_idx = 0; type_idx < types.size(); type_idx++)
+ {
+ String type = (String) types.get(type_idx);
+ addSignatureType(type);
+ }
}
}
}
@@ -420,8 +536,9 @@ public class SignatureTypes
* @param typeName
*/
public void addSignatureType(String typeName) {
-
- if (STATE_ON.equals(settings_.getSetting(TYPES + "." + typeName, null)))
+
+// if (STATE_ON.equals(settings_.getSetting(TYPES + "." + typeName, null)))
+ if (State.fromString(settings_.getSetting(TYPES + "." + typeName, null)) != State.OFF)
{
SignatureTypeDefinition sig_type_def;
try
@@ -446,6 +563,23 @@ public class SignatureTypes
return this.typeDefMap_.keySet();
}
+ /**
+ * Returns a set of identifiers for profiles than can be used for signature, i.e. profiles that are either enabled
+ * ("on") or set to "sign_only").
+ *
+ * @return A set of signature profile/type identifiers.
+ */
+ @SuppressWarnings("unchecked")
+ public Set<String> getSignatureTypesForSignature() {
+ Set<String> filteredResult = new HashSet<String>();
+ for (String signatureProfileId : (Set<String>) typeDefMap_.keySet()) {
+ if (State.fromString(settings_.getSetting(TYPES + "." + signatureProfileId, null)).canSign()) {
+ filteredResult.add(signatureProfileId);
+ }
+ }
+ return filteredResult;
+ }
+
/**
* @return a list of signature type definitions
*/
@@ -453,6 +587,23 @@ public class SignatureTypes
{
return new ArrayList(this.typeDefMap_.values());
}
+
+ /**
+ * Returns a (filtered) list of signature type definitions useable for verification. Those definitions for profiles
+ * that are not allowed to be used for verification are filtered.
+ *
+ * @return A filtered list of signature type definitions.
+ */
+ @SuppressWarnings("unchecked")
+ public List<SignatureTypeDefinition> getSignatureTypeDefinitionsForVerification() {
+ List<SignatureTypeDefinition> filteredResult = new ArrayList<SignatureTypeDefinition>(typeDefMap_.size());
+ for (String signatureProfileId : (Set<String>) typeDefMap_.keySet()) {
+ if (State.fromString(settings_.getSetting(TYPES + "." + signatureProfileId, null)).canVerify()) {
+ filteredResult.add((SignatureTypeDefinition) typeDefMap_.get(signatureProfileId));
+ }
+ }
+ return filteredResult;
+ }
/**
* This method returns the corresponding signature type definition to a given