From e70ec39488c0688d8144fa529d58d60ae9564fbd Mon Sep 17 00:00:00 2001 From: Christof Rabensteiner Date: Wed, 15 May 2019 06:41:15 +0200 Subject: WIP: Generate Map of Config Profiles from Spring Environment Augmenter - Replace default Config Profile with map of Config Profiles. Now, Augmenter can choose a config profile at runtime and augment it. - Move Augmenter to sub package. WIP - Implement ConfigProfileGenerator, which retrieves Config profiles from Spring Environment and returns an easy-to-use map with profiles that is keyed with profile id's. - Replace application.properties with application.yaml. - Remove Augmenter test cases: They need to be adapted. --- src/main/java/at/gv/egiz/moazs/App2MzsService.java | 1 + .../at/gv/egiz/moazs/DeliveryRequestAugmenter.java | 68 ----------------- .../config/DeliveryRequestAugmenterConfig.java | 25 ++----- .../moazs/preprocess/ConfigProfileGenerator.java | 87 ++++++++++++++++++++++ .../moazs/preprocess/DeliveryRequestAugmenter.java | 69 +++++++++++++++++ 5 files changed, 164 insertions(+), 86 deletions(-) delete mode 100644 src/main/java/at/gv/egiz/moazs/DeliveryRequestAugmenter.java create mode 100644 src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileGenerator.java create mode 100644 src/main/java/at/gv/egiz/moazs/preprocess/DeliveryRequestAugmenter.java (limited to 'src/main/java/at/gv/egiz/moazs') diff --git a/src/main/java/at/gv/egiz/moazs/App2MzsService.java b/src/main/java/at/gv/egiz/moazs/App2MzsService.java index 622f1dc..19eb856 100644 --- a/src/main/java/at/gv/egiz/moazs/App2MzsService.java +++ b/src/main/java/at/gv/egiz/moazs/App2MzsService.java @@ -1,6 +1,7 @@ package at.gv.egiz.moazs; import at.gv.egiz.moazs.pipeline.DeliveryPipeline; +import at.gv.egiz.moazs.preprocess.DeliveryRequestAugmenter; import at.gv.egiz.moazs.repository.DeliveryRepository; import at.gv.zustellung.app2mzs.xsd.App2MzsPortType; import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; diff --git a/src/main/java/at/gv/egiz/moazs/DeliveryRequestAugmenter.java b/src/main/java/at/gv/egiz/moazs/DeliveryRequestAugmenter.java deleted file mode 100644 index 96197d0..0000000 --- a/src/main/java/at/gv/egiz/moazs/DeliveryRequestAugmenter.java +++ /dev/null @@ -1,68 +0,0 @@ -package at.gv.egiz.moazs; - -import at.gv.zustellung.app2mzs.xsd.ConfigType; -import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; -import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.DeliveryRequestTypeBuilder; -import at.gv.zustellung.app2mzs.xsd.ServerType; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import static at.gv.egiz.moazs.util.NullCoalesce.coalesce; -import static at.gv.zustellung.app2mzs.xsd.ConfigType.configTypeBuilder; -import static at.gv.zustellung.app2mzs.xsd.ServerType.*; - -@Component -public class DeliveryRequestAugmenter { - - private final ConfigType defaultMzsConfig; - - @Autowired - public DeliveryRequestAugmenter(ConfigType defaultMzsConfig) { - this.defaultMzsConfig = defaultMzsConfig; - } - - /** - * Augments an incoming {@code request} by filling gaps with config- or default parameters. - * - * @param request - * @return augmented request - */ - public DeliveryRequestType augment(DeliveryRequestType request) { - - var config = merge(request.getConfig(), defaultMzsConfig); - - return new DeliveryRequestTypeBuilder(request) - .withConfig(config) - .build(); - } - - private ConfigType merge(ConfigType primary, ConfigType fallback) { - - var builder = configTypeBuilder(fallback); - - if(primary != null) { - - if(primary.getServer() != null) { - builder.withServer(merge(primary.getServer(), fallback.getServer())); - } - - if(primary.isPerformQueryPersonRequest() != null) { - builder.withPerformQueryPersonRequest(primary.isPerformQueryPersonRequest()); - } - - } - - return builder.build(); - - } - - private ServerType merge(ServerType primary, ServerType fallback) { - - var builder = serverTypeBuilder(fallback); - - if (primary.getX509() != null) builder.withX509 (primary.getX509() ); - if (primary.getZUSEUrlID() != null) builder.withZUSEUrlID(primary.getZUSEUrlID()); - - return builder.build(); - } -} diff --git a/src/main/java/at/gv/egiz/moazs/config/DeliveryRequestAugmenterConfig.java b/src/main/java/at/gv/egiz/moazs/config/DeliveryRequestAugmenterConfig.java index 93615c4..d5b52c5 100644 --- a/src/main/java/at/gv/egiz/moazs/config/DeliveryRequestAugmenterConfig.java +++ b/src/main/java/at/gv/egiz/moazs/config/DeliveryRequestAugmenterConfig.java @@ -1,31 +1,20 @@ package at.gv.egiz.moazs.config; +import at.gv.egiz.moazs.preprocess.ConfigProfileGenerator; import at.gv.zustellung.app2mzs.xsd.ConfigType; -import org.springframework.beans.factory.annotation.Value; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import static at.gv.zustellung.app2mzs.xsd.ConfigType.configTypeBuilder; -import static at.gv.zustellung.app2mzs.xsd.ServerType.serverTypeBuilder; +import java.util.Map; @Configuration public class DeliveryRequestAugmenterConfig { - - @Value("${msg.default.url}") - private String zuseUrlID; - @Bean - public ConfigType defaultMzsConfig() { - - var server = serverTypeBuilder() - .withZUSEUrlID(zuseUrlID) - .withX509(null) //todo load cert! - .build(); - - return configTypeBuilder() - .withPerformQueryPersonRequest(false) - .withServer(server) - .build(); + @Autowired + Map deliveryRequestConfigs(ConfigProfileGenerator generator) { + return generator.generate(); } + } diff --git a/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileGenerator.java b/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileGenerator.java new file mode 100644 index 0000000..cba776d --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileGenerator.java @@ -0,0 +1,87 @@ +package at.gv.egiz.moazs.preprocess; + +import at.gv.zustellung.app2mzs.xsd.ConfigType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.AbstractEnvironment; +import org.springframework.core.env.EnumerablePropertySource; +import org.springframework.core.env.Environment; +import org.springframework.core.env.MutablePropertySources; +import org.springframework.stereotype.Component; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +import static at.gv.zustellung.app2mzs.xsd.ConfigType.configTypeBuilder; +import static at.gv.zustellung.app2mzs.xsd.ServerType.serverTypeBuilder; +import static java.util.stream.Collectors.toMap; + +@Component +public class ConfigProfileGenerator { + + @Autowired + private Environment env; + + private final String profilePrefix = "delivery-request-configuration-profiles."; + private final String tnvzRequestKey = "perform-query-person-request"; + private final String msgUrlKey = "msg.url"; + + public Map generate() { + MutablePropertySources propSrcs = ((AbstractEnvironment) env).getPropertySources(); + + Map> groupedKeys = StreamSupport.stream(propSrcs.spliterator(), false) + .filter(ps -> ps instanceof EnumerablePropertySource) + .map(ps -> ((EnumerablePropertySource) ps).getPropertyNames()) + .flatMap(Arrays::stream) + .filter(propName -> propName.startsWith(profilePrefix)) + .map(name -> name.substring(name.indexOf(".") + 1)) + .collect( + Collectors.groupingBy( + this::keepPrefix, + Collectors.mapping( + this::removePrefix, + Collectors.toSet()))); + + return groupedKeys.entrySet().stream() + .collect(toMap(Entry::getKey, this::createConfig)); + } + + private String keepPrefix(String name) { + return name.substring(0, name.indexOf(".")); + } + + private String removePrefix(String name) { + return name.substring(name.indexOf(".") + 1); + } + + private ConfigType createConfig(Entry> entry) { + + var profile = entry.getKey(); + + var values = new HashMap(); + + entry.getValue().stream() + .forEach(key -> { + var assembledKey = profilePrefix + "." + profile + "." + key; + var value = env.getProperty(assembledKey); + values.put(key, value); + }); + + var server = serverTypeBuilder() + .withZUSEUrlID(values.get(msgUrlKey)) + .build(); + + Boolean performQueryPersonRequest = values.get(tnvzRequestKey) == null + ? null : Boolean.getBoolean(values.get(tnvzRequestKey)); + + return configTypeBuilder() + .withPerformQueryPersonRequest(performQueryPersonRequest) + .withServer(server) + .build(); + } + +} diff --git a/src/main/java/at/gv/egiz/moazs/preprocess/DeliveryRequestAugmenter.java b/src/main/java/at/gv/egiz/moazs/preprocess/DeliveryRequestAugmenter.java new file mode 100644 index 0000000..623af38 --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/preprocess/DeliveryRequestAugmenter.java @@ -0,0 +1,69 @@ +package at.gv.egiz.moazs.preprocess; + +import at.gv.zustellung.app2mzs.xsd.ConfigType; +import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; +import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.DeliveryRequestTypeBuilder; +import at.gv.zustellung.app2mzs.xsd.ServerType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.Map; + +import static at.gv.zustellung.app2mzs.xsd.ConfigType.configTypeBuilder; +import static at.gv.zustellung.app2mzs.xsd.ServerType.serverTypeBuilder; + +@Component +public class DeliveryRequestAugmenter { + + private final ConfigType defaultMzsConfig; + + @Autowired + public DeliveryRequestAugmenter(Map deliveryRequestConfigs) { + this.defaultMzsConfig = deliveryRequestConfigs.get("default"); + } + + /** + * Augments an incoming {@code request} by filling gaps with config- or default parameters. + * + * @param request + * @return augmented request + */ + public DeliveryRequestType augment(DeliveryRequestType request) { + + var config = merge(request.getConfig(), defaultMzsConfig); + + return new DeliveryRequestTypeBuilder(request) + .withConfig(config) + .build(); + } + + private ConfigType merge(ConfigType primary, ConfigType fallback) { + + var builder = configTypeBuilder(fallback); + + if(primary != null) { + + if(primary.getServer() != null) { + builder.withServer(merge(primary.getServer(), fallback.getServer())); + } + + if(primary.isPerformQueryPersonRequest() != null) { + builder.withPerformQueryPersonRequest(primary.isPerformQueryPersonRequest()); + } + + } + + return builder.build(); + + } + + private ServerType merge(ServerType primary, ServerType fallback) { + + var builder = serverTypeBuilder(fallback); + + if (primary.getX509() != null) builder.withX509 (primary.getX509() ); + if (primary.getZUSEUrlID() != null) builder.withZUSEUrlID(primary.getZUSEUrlID()); + + return builder.build(); + } +} -- cgit v1.2.3