aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/gv/egiz/moazs/preprocess/ConfigUtil.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/at/gv/egiz/moazs/preprocess/ConfigUtil.java')
-rw-r--r--src/main/java/at/gv/egiz/moazs/preprocess/ConfigUtil.java62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/main/java/at/gv/egiz/moazs/preprocess/ConfigUtil.java b/src/main/java/at/gv/egiz/moazs/preprocess/ConfigUtil.java
index f49132f..ec7b7c8 100644
--- a/src/main/java/at/gv/egiz/moazs/preprocess/ConfigUtil.java
+++ b/src/main/java/at/gv/egiz/moazs/preprocess/ConfigUtil.java
@@ -5,15 +5,19 @@ import at.gv.zustellung.app2mzs.xsd.*;
import org.springframework.stereotype.Component;
import java.math.BigInteger;
+import java.util.List;
import java.util.Map;
+import java.util.stream.Stream;
import static at.gv.zustellung.app2mzs.xsd.ClientType.clientTypeBuilder;
import static at.gv.zustellung.app2mzs.xsd.ConfigType.configTypeBuilder;
+import static at.gv.zustellung.app2mzs.xsd.CustomHTTPHeaderType.*;
import static at.gv.zustellung.app2mzs.xsd.ForwardResponseToServiceType.forwardResponseToServiceTypeBuilder;
import static at.gv.zustellung.app2mzs.xsd.KeyStoreType.keyStoreTypeBuilder;
import static at.gv.zustellung.app2mzs.xsd.MsgResponseSinksType.msgResponseSinksTypeBuilder;
import static at.gv.zustellung.app2mzs.xsd.SSLType.SSLTypeBuilder;
import static at.gv.zustellung.app2mzs.xsd.SaveResponseToFileType.saveResponseToFileTypeBuilder;
+import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
@Component
@@ -41,6 +45,7 @@ public class ConfigUtil {
public static final String FORWARD_RESPONSE_TO_SERVICE_KEY = "forward-response-to-service";
public static final String MZS_CLIENT_KEY = "mzs-client";
public static final String SERVICE_TIMEOUT_KEY = "service-timeout";
+ public static final String CUSTOM_HTTP_HEADERS_KEY = "custom-http-headers";
/**
@@ -95,11 +100,16 @@ public class ConfigUtil {
SSLType ssl = sslParams.isEmpty()
? null : buildSSL(sslParams);
+ var customHeaderParams = filterMapByPrefix(clientParams, CUSTOM_HTTP_HEADERS_KEY);
+ var customHeaders = customHeaderParams.isEmpty()
+ ? null : convertMapToHeaders(customHeaderParams);
+
return clientTypeBuilder()
.withURL(url)
.withSSL(ssl)
.withConnectionTimeout(connectionTimeout)
.withReceiveTimeout(receiveTimeout)
+ .withCustomHTTPHeader(customHeaders)
.build();
}
@@ -243,6 +253,10 @@ public class ConfigUtil {
builder.withReceiveTimeout(primary.getReceiveTimeout());
}
+ if (primary.getCustomHTTPHeader() != null) {
+ builder.withCustomHTTPHeader(merge(primary.getCustomHTTPHeader(), fallback.getCustomHTTPHeader()));
+ }
+
return builder.build();
}
@@ -282,6 +296,26 @@ public class ConfigUtil {
}
+ private List<CustomHTTPHeaderType> merge(List<CustomHTTPHeaderType> primary, List<CustomHTTPHeaderType> fallback) {
+
+ if (fallback == null) return primary;
+
+ var primaryMap = convertHeadersToMap(primary);
+ var fallbackMap = convertHeadersToMap(fallback);
+
+ Map<String, String> mergedMap = Stream.of(fallbackMap, primaryMap)
+ .flatMap(map -> map.entrySet().stream())
+ .collect(toMap(
+ Map.Entry::getKey,
+ entry -> entry.getValue().get(0),
+ (v1, v2) -> v2
+ ));
+
+ return convertMapToHeaders(mergedMap);
+
+ }
+
+
private MsgResponseSinksType merge(MsgResponseSinksType primary, MsgResponseSinksType fallback) {
if (fallback == null) {
@@ -340,4 +374,32 @@ public class ConfigUtil {
}
+ /**
+ * Convert a Map that is indexed by header names into a List of CustomHTTPHeaders.
+ * @param customHeaderMap map of headers, indexed by header names.
+ * @return List of CustomHTTPHeaders
+ */
+ public List<CustomHTTPHeaderType> convertMapToHeaders(Map<String, String> customHeaderMap) {
+ return customHeaderMap.entrySet().stream()
+ .map(e -> customHTTPHeaderTypeBuilder()
+ .withName(e.getKey())
+ .withValue(e.getValue())
+ .build())
+ .collect(toList());
+ }
+
+
+ /**
+ * Convert a List of CustomHTTPHeaders into a Map that is indexed by the header names. Values are wrapped in Lists.
+ * @param customHeaders List of CustomHTTPHeaders
+ * @return Map of headers, indexed by header names.
+ */
+ public Map<String, List<String>> convertHeadersToMap(List<CustomHTTPHeaderType> customHeaders) {
+ return customHeaders
+ .stream()
+ .collect(toMap(
+ h -> h.getName(),
+ h -> List.of(h.getValue())));
+ }
+
}