aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/gv/egiz
diff options
context:
space:
mode:
authorChristof Rabensteiner <christof.rabensteiner@iaik.tugraz.at>2019-05-15 09:16:04 +0200
committerChristof Rabensteiner <christof.rabensteiner@iaik.tugraz.at>2019-05-15 09:16:04 +0200
commit87f4be0b8834e6090137f2db2154bf50bcfcf646 (patch)
tree31a8e0183f94f231adba381a655b54a3b48039f2 /src/main/java/at/gv/egiz
parente70ec39488c0688d8144fa529d58d60ae9564fbd (diff)
downloadmoa-zs-87f4be0b8834e6090137f2db2154bf50bcfcf646.tar.gz
moa-zs-87f4be0b8834e6090137f2db2154bf50bcfcf646.tar.bz2
moa-zs-87f4be0b8834e6090137f2db2154bf50bcfcf646.zip
Integrate Config Profiles + Refactoring
Config Profile Integration: - Merge generated Configs from ConfigProfileGenerator with the default Config; Reason: Merging profile Configs with default Config asap ensures that profile Configs are "as complete as possible" while allowing profile Configs to "on-demand"-override values from the default Config. - Adapt Augmenter to work with Config profiles: Select a Config on the basis of on msg:DeliveryRequest/Config/ProfileId, fall back to 'default' Config. - Schema change: Make ProfileID optional. Refactoring: - Move Config-merging code from Augmenter into dedicated 'ConfigProfileMerger' class. - ConfigProfileGenerator: Clarify stream pipeline and comply with coding standard w.r.t. constant members. - Add Augmenter test cases for profile selection.
Diffstat (limited to 'src/main/java/at/gv/egiz')
-rw-r--r--src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileGenerator.java54
-rw-r--r--src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileMerger.java42
-rw-r--r--src/main/java/at/gv/egiz/moazs/preprocess/DeliveryRequestAugmenter.java46
3 files changed, 90 insertions, 52 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
index cba776d..a0cf766 100644
--- a/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileGenerator.java
+++ b/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileGenerator.java
@@ -13,22 +13,28 @@ 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.*;
import static java.util.stream.Collectors.toMap;
@Component
public class ConfigProfileGenerator {
- @Autowired
- private Environment env;
+ private static final String PROFILE_PREFIX = "delivery-request-configuration-profiles.";
+ private static final String TNVZ_REQUEST_KEY = "perform-query-person-request";
+ private static final String MSG_URL_KEY = "msg.url";
- private final String profilePrefix = "delivery-request-configuration-profiles.";
- private final String tnvzRequestKey = "perform-query-person-request";
- private final String msgUrlKey = "msg.url";
+ private final Environment env;
+ private final ConfigProfileMerger merger;
+
+ @Autowired
+ public ConfigProfileGenerator(Environment env, ConfigProfileMerger merger) {
+ this.env = env;
+ this.merger = merger;
+ }
public Map<String, ConfigType> generate() {
MutablePropertySources propSrcs = ((AbstractEnvironment) env).getPropertySources();
@@ -37,25 +43,29 @@ public class ConfigProfileGenerator {
.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()
+ .filter(this::isConfigurationProfileProperty)
+ .map(this::removePrefix)
+ .collect(groupingBy(this::keepPrefix, mapping(this::removePrefix, toSet())));
+
+ Map<String, ConfigType> profiles = groupedKeys.entrySet().stream()
.collect(toMap(Entry::getKey, this::createConfig));
+
+ var defaultProfile = profiles.get("default");
+
+ return profiles.entrySet().stream()
+ .collect(toUnmodifiableMap(Entry::getKey, e -> merger.merge(e.getValue(), defaultProfile)));
+ }
+
+ private boolean isConfigurationProfileProperty(String propName) {
+ return propName.startsWith(PROFILE_PREFIX);
}
private String keepPrefix(String name) {
- return name.substring(0, name.indexOf("."));
+ return name.substring(0, name.indexOf('.'));
}
private String removePrefix(String name) {
- return name.substring(name.indexOf(".") + 1);
+ return name.substring(name.indexOf('.') + 1);
}
private ConfigType createConfig(Entry<String, Set<String>> entry) {
@@ -66,17 +76,17 @@ public class ConfigProfileGenerator {
entry.getValue().stream()
.forEach(key -> {
- var assembledKey = profilePrefix + "." + profile + "." + key;
+ var assembledKey = PROFILE_PREFIX + '.' + profile + '.' + key;
var value = env.getProperty(assembledKey);
values.put(key, value);
});
var server = serverTypeBuilder()
- .withZUSEUrlID(values.get(msgUrlKey))
+ .withZUSEUrlID(values.get(MSG_URL_KEY))
.build();
- Boolean performQueryPersonRequest = values.get(tnvzRequestKey) == null
- ? null : Boolean.getBoolean(values.get(tnvzRequestKey));
+ Boolean performQueryPersonRequest = values.get(TNVZ_REQUEST_KEY) == null
+ ? null : Boolean.getBoolean(values.get(TNVZ_REQUEST_KEY));
return configTypeBuilder()
.withPerformQueryPersonRequest(performQueryPersonRequest)
diff --git a/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileMerger.java b/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileMerger.java
new file mode 100644
index 0000000..f25ec12
--- /dev/null
+++ b/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileMerger.java
@@ -0,0 +1,42 @@
+package at.gv.egiz.moazs.preprocess;
+
+import at.gv.zustellung.app2mzs.xsd.ConfigType;
+import at.gv.zustellung.app2mzs.xsd.ServerType;
+import org.springframework.stereotype.Component;
+
+import static at.gv.zustellung.app2mzs.xsd.ConfigType.configTypeBuilder;
+import static at.gv.zustellung.app2mzs.xsd.ServerType.serverTypeBuilder;
+
+@Component
+public class ConfigProfileMerger {
+
+ public 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/preprocess/DeliveryRequestAugmenter.java b/src/main/java/at/gv/egiz/moazs/preprocess/DeliveryRequestAugmenter.java
index 623af38..7211f6d 100644
--- a/src/main/java/at/gv/egiz/moazs/preprocess/DeliveryRequestAugmenter.java
+++ b/src/main/java/at/gv/egiz/moazs/preprocess/DeliveryRequestAugmenter.java
@@ -3,23 +3,23 @@ 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;
+import static at.gv.egiz.moazs.util.NullCoalesce.coalesce;
@Component
public class DeliveryRequestAugmenter {
- private final ConfigType defaultMzsConfig;
+ private final ConfigProfileMerger merger;
+ private final Map<String, ConfigType> configs;
@Autowired
- public DeliveryRequestAugmenter(Map<String, ConfigType> deliveryRequestConfigs) {
- this.defaultMzsConfig = deliveryRequestConfigs.get("default");
+ public DeliveryRequestAugmenter(Map<String, ConfigType> deliveryRequestConfigs, ConfigProfileMerger merger) {
+ this.configs = deliveryRequestConfigs;
+ this.merger = merger;
}
/**
@@ -30,40 +30,26 @@ public class DeliveryRequestAugmenter {
*/
public DeliveryRequestType augment(DeliveryRequestType request) {
- var config = merge(request.getConfig(), defaultMzsConfig);
+ var requestConfig = request.getConfig();
+
+ var fallbackConfig = determineProfileFromProfileId(requestConfig);
+
+ var config = merger.merge(requestConfig, fallbackConfig);
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());
- }
+ private ConfigType determineProfileFromProfileId(ConfigType requestConfig) {
+ if (requestConfig == null || requestConfig.getProfileID() == null) {
+ return configs.get("default");
}
- return builder.build();
+ var id = requestConfig.getProfileID();
+ return coalesce(configs.get(id), configs.get("default")).get();
}
- 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();
- }
}