Skip to content

Commit

Permalink
Support for ObjectStorage in ModeDetector.
Browse files Browse the repository at this point in the history
  • Loading branch information
klakegg committed Sep 3, 2018
1 parent 65904ca commit 32b0eb6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
24 changes: 21 additions & 3 deletions peppol-mode/src/main/java/no/difi/vefa/peppol/mode/Mode.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import no.difi.vefa.peppol.common.lang.PeppolLoadingException;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

public class Mode {

Expand Down Expand Up @@ -82,21 +83,38 @@ public Config getConfig() {
}

@SuppressWarnings({"unchecked", "unused"})
public <T> T initiate(String key, Class<T> type) throws PeppolLoadingException {
public <T> T initiate(String key, Class<T> type, Map<String, Object> 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> T initiate(String key, Class<T> type) throws PeppolLoadingException {
return initiate(key, type, null);
}

public <T> T initiate(Class<T> cls) throws PeppolLoadingException {
return initiate(cls, null);
}

public <T> T initiate(Class<T> cls, Map<String, Object> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> objectStorage) throws PeppolLoadingException {
this.mode = mode;

Expand All @@ -52,10 +56,6 @@ public DifiCertificateValidator(Mode mode, Map<String, Object> objectStorage) th
}
}

public DifiCertificateValidator(Mode mode) throws PeppolLoadingException {
this(mode, null);
}

@Override
public void validate(Service service, X509Certificate certificate) throws PeppolSecurityException {
try {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
<dependency>
<groupId>no.difi.commons</groupId>
<artifactId>commons-certvalidator</artifactId>
<version>2.1.1</version>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>no.difi.commons</groupId>
Expand Down

0 comments on commit 32b0eb6

Please sign in to comment.