From 32b0eb6f4943382f2428169a60d98cac07aad80a Mon Sep 17 00:00:00 2001 From: Erlend Klakegg Bergheim Date: Mon, 3 Sep 2018 19:37:12 +0200 Subject: [PATCH] Support for ObjectStorage in ModeDetector. --- .../java/no/difi/vefa/peppol/mode/Mode.java | 24 ++++++++++++++++--- .../vefa/peppol/security/ModeDetector.java | 7 +++--- .../util/DifiCertificateValidator.java | 8 +++---- pom.xml | 2 +- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/peppol-mode/src/main/java/no/difi/vefa/peppol/mode/Mode.java b/peppol-mode/src/main/java/no/difi/vefa/peppol/mode/Mode.java index 83569298..2af55aa8 100644 --- a/peppol-mode/src/main/java/no/difi/vefa/peppol/mode/Mode.java +++ b/peppol-mode/src/main/java/no/difi/vefa/peppol/mode/Mode.java @@ -24,6 +24,7 @@ import no.difi.vefa.peppol.common.lang.PeppolLoadingException; import java.lang.reflect.InvocationTargetException; +import java.util.Map; public class Mode { @@ -82,21 +83,38 @@ public Config getConfig() { } @SuppressWarnings({"unchecked", "unused"}) - public T initiate(String key, Class type) throws PeppolLoadingException { + public T initiate(String key, Class type, Map objectStorage) throws PeppolLoadingException { try { - return (T) initiate(Class.forName(getString(key))); + return (T) initiate(Class.forName(getString(key)), objectStorage); } catch (ClassNotFoundException e) { throw new PeppolLoadingException(String.format("Unable to initiate '%s'", getString(key)), e); } } + @SuppressWarnings({"unchecked", "unused"}) + public T initiate(String key, Class type) throws PeppolLoadingException { + return initiate(key, type, null); + } + public T initiate(Class cls) throws PeppolLoadingException { + return initiate(cls, null); + } + + public T initiate(Class cls, Map objectStorage) throws PeppolLoadingException { try { + try { + return cls.getConstructor(Mode.class, Map.class).newInstance(this, objectStorage); + } catch (NoSuchMethodException e) { + // No action + } + try { return cls.getConstructor(Mode.class).newInstance(this); } catch (NoSuchMethodException e) { - return cls.newInstance(); + // No action } + + return cls.newInstance(); } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { throw new PeppolLoadingException(String.format("Unable to initiate '%s'", cls), e); } diff --git a/peppol-security/src/main/java/no/difi/vefa/peppol/security/ModeDetector.java b/peppol-security/src/main/java/no/difi/vefa/peppol/security/ModeDetector.java index ebe7c847..b606e9d1 100644 --- a/peppol-security/src/main/java/no/difi/vefa/peppol/security/ModeDetector.java +++ b/peppol-security/src/main/java/no/difi/vefa/peppol/security/ModeDetector.java @@ -30,21 +30,22 @@ import org.slf4j.LoggerFactory; import java.security.cert.X509Certificate; +import java.util.Map; public class ModeDetector { private static final Logger LOGGER = LoggerFactory.getLogger(ModeDetector.class); public static Mode detect(X509Certificate certificate) throws PeppolLoadingException { - return detect(certificate, ConfigFactory.load()); + return detect(certificate, ConfigFactory.load(), null); } - public static Mode detect(X509Certificate certificate, Config config) throws PeppolLoadingException { + public static Mode detect(X509Certificate certificate, Config config, Map objectStorage) throws PeppolLoadingException { for (String token : config.getObject("mode").keySet()) { if (!"default".equals(token)) { try { Mode mode = Mode.of(config, token); - mode.initiate("security.validator.class", CertificateValidator.class) + mode.initiate("security.validator.class", CertificateValidator.class, objectStorage) .validate(Service.ALL, certificate); return mode; } catch (PeppolSecurityException e) { diff --git a/peppol-security/src/main/java/no/difi/vefa/peppol/security/util/DifiCertificateValidator.java b/peppol-security/src/main/java/no/difi/vefa/peppol/security/util/DifiCertificateValidator.java index 996cd1fc..c11cad37 100644 --- a/peppol-security/src/main/java/no/difi/vefa/peppol/security/util/DifiCertificateValidator.java +++ b/peppol-security/src/main/java/no/difi/vefa/peppol/security/util/DifiCertificateValidator.java @@ -40,6 +40,10 @@ public class DifiCertificateValidator implements CertificateValidator { private Mode mode; + public DifiCertificateValidator(Mode mode) throws PeppolLoadingException { + this(mode, null); + } + public DifiCertificateValidator(Mode mode, Map objectStorage) throws PeppolLoadingException { this.mode = mode; @@ -52,10 +56,6 @@ public DifiCertificateValidator(Mode mode, Map objectStorage) th } } - public DifiCertificateValidator(Mode mode) throws PeppolLoadingException { - this(mode, null); - } - @Override public void validate(Service service, X509Certificate certificate) throws PeppolSecurityException { try { diff --git a/pom.xml b/pom.xml index a633b0e7..cb27fc6f 100644 --- a/pom.xml +++ b/pom.xml @@ -152,7 +152,7 @@ no.difi.commons commons-certvalidator - 2.1.1 + 2.2.0 no.difi.commons