aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/gv/egiz/moazs/preprocess
diff options
context:
space:
mode:
authorChristof Rabensteiner <christof.rabensteiner@iaik.tugraz.at>2019-05-15 06:41:15 +0200
committerChristof Rabensteiner <christof.rabensteiner@iaik.tugraz.at>2019-05-15 06:41:15 +0200
commite70ec39488c0688d8144fa529d58d60ae9564fbd (patch)
tree07ef8e905d56b5ab2142f7a787d98317debcb38d /src/main/java/at/gv/egiz/moazs/preprocess
parent1c252d7fc336831188c628ecdd39cd1b84b0cada (diff)
downloadmoa-zs-e70ec39488c0688d8144fa529d58d60ae9564fbd.tar.gz
moa-zs-e70ec39488c0688d8144fa529d58d60ae9564fbd.tar.bz2
moa-zs-e70ec39488c0688d8144fa529d58d60ae9564fbd.zip
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.
Diffstat (limited to 'src/main/java/at/gv/egiz/moazs/preprocess')
-rw-r--r--src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileGenerator.java87
-rw-r--r--src/main/java/at/gv/egiz/moazs/preprocess/DeliveryRequestAugmenter.java69
2 files changed, 156 insertions, 0 deletions
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<String, ConfigType> generate() {
+ MutablePropertySources propSrcs = ((AbstractEnvironment) env).getPropertySources();
+
+ Map<String, Set<String>> 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<String, Set<String>> entry) {
+
+ var profile = entry.getKey();
+
+ var values = new HashMap<String,String>();
+
+ 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<String, ConfigType> 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();
+ }
+}