package at.gv.egiz.moazs.preprocess; import at.gv.egiz.moazs.util.StringUtils; import at.gv.zustellung.app2mzs.xsd.*; import org.springframework.stereotype.Component; import java.math.BigInteger; import java.util.Map; 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.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.toMap; @Component public class ConfigUtil { public static final String TNVZ_REQUEST_KEY = "perform-query-person-request"; public static final String MSG_CLIENT_KEY = "msg-client"; public static final String TNVZ_CLIENT_KEY = "tnvz-client"; public static final String URL_KEY = "url"; public static final String SSL_KEY = "ssl"; public static final String TRUST_ALL_KEY = "trust-all"; public static final String LAX_HOSTNAME_VERIFICATION_KEY = "lax-hostname-verification"; public static final String KEYSTORE_KEY = "keystore"; public static final String TRUSTSTORE_KEY = "truststore"; public static final String FILENAME_KEY = "filename"; public static final String FILETYPE_KEY = "filetype"; public static final String PASSWORD_KEY = "password"; public static final String RECEIVE_TIMEOUT_KEY = "receive-timeout"; public static final String CONNECTION_TIMEOUT_KEY = "connection-timeout"; public static final String MSG_RESPONSE_SINKS_KEY = "msg-response-sinks"; public static final String LOG_RESPONSE_KEY = "log-response"; public static final String SAVE_RESPONSE_TO_FILE_KEY = "save-response-to-file"; public static final String ACTIVE_KEY = "active"; public static final String SAVE_RESPONSE_TO_FILE_PATH_KEY = "path"; 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"; /** * Convert a map into a Config object. * * @param values: Map with well-defined indexes and values * @return Config */ public ConfigType convert(Map values) { Boolean performQueryPersonRequest = values.get(TNVZ_REQUEST_KEY) == null ? null : Boolean.getBoolean(values.get(TNVZ_REQUEST_KEY)); var msgClientParams = filterMapByPrefix(values, MSG_CLIENT_KEY); ClientType msgClient = msgClientParams.isEmpty() ? null : buildClient(msgClientParams); var tnvzClientParams = filterMapByPrefix(values, TNVZ_CLIENT_KEY); ClientType tnvzClient = tnvzClientParams.isEmpty() ? null : buildClient(tnvzClientParams); var msgResponseSinksParams = filterMapByPrefix(values, MSG_RESPONSE_SINKS_KEY); MsgResponseSinksType sinks = msgResponseSinksParams.isEmpty() ? null : buildMsgResponseSinks(msgResponseSinksParams); var serviceTimeout = bigIntOrNull(values.get(SERVICE_TIMEOUT_KEY)); return ConfigType.configTypeBuilder() .withPerformQueryPersonRequest(performQueryPersonRequest) .withMSGClient(msgClient) .withTNVZClient(tnvzClient) .withMsgResponseSinks(sinks) .withServiceTimeout(serviceTimeout) .build(); } private Map filterMapByPrefix(Map values, String prefix) { return values.entrySet().stream() .filter(entry -> entry.getKey().startsWith(prefix)) .collect(toMap(e -> StringUtils.removePrefix(e.getKey()), Map.Entry::getValue)); } private ClientType buildClient(Map clientParams) { var url = clientParams.get(URL_KEY); var connectionTimeout = bigIntOrNull(clientParams.get(CONNECTION_TIMEOUT_KEY)); var receiveTimeout = bigIntOrNull(clientParams.get(RECEIVE_TIMEOUT_KEY)); var sslParams = filterMapByPrefix(clientParams, SSL_KEY); SSLType ssl = sslParams.isEmpty() ? null : buildSSL(sslParams); return clientTypeBuilder() .withURL(url) .withSSL(ssl) .withConnectionTimeout(connectionTimeout) .withReceiveTimeout(receiveTimeout) .build(); } private SSLType buildSSL(Map sslParams) { var keyStoreParams = filterMapByPrefix(sslParams, KEYSTORE_KEY); KeyStoreType keyStore = keyStoreParams.isEmpty() ? null : buildKeyStore(keyStoreParams); var trustStoreParams = filterMapByPrefix(sslParams, TRUSTSTORE_KEY); KeyStoreType trustStore = trustStoreParams.isEmpty() ? null : buildKeyStore(trustStoreParams); var trustAll = booleanOrNull(sslParams.get(TRUST_ALL_KEY)); var laxHostNameVerification = booleanOrNull(sslParams.get(LAX_HOSTNAME_VERIFICATION_KEY)); return SSLTypeBuilder() .withKeyStore(keyStore) .withTrustStore(trustStore) .withTrustAll(trustAll) .withLaxHostNameVerification(laxHostNameVerification) .build(); } private KeyStoreType buildKeyStore(Map params) { return keyStoreTypeBuilder() .withFileName(params.get(FILENAME_KEY)) .withFileType(params.get(FILETYPE_KEY)) .withPassword(params.get(PASSWORD_KEY)) .build(); } private MsgResponseSinksType buildMsgResponseSinks(Map params) { var logResponse = booleanOrNull(params.get(LOG_RESPONSE_KEY)); var saveResponse = buildSaveResponse(filterMapByPrefix(params, SAVE_RESPONSE_TO_FILE_KEY)); var forwardResponse = buildForwardResponse(filterMapByPrefix(params, FORWARD_RESPONSE_TO_SERVICE_KEY)); return msgResponseSinksTypeBuilder() .withLogResponse(logResponse) .withSaveResponseToFile(saveResponse) .withForwardResponseToService(forwardResponse) .build(); } private SaveResponseToFileType buildSaveResponse(Map params) { var isActive = booleanOrNull(params.get(ACTIVE_KEY)); var path = params.get(SAVE_RESPONSE_TO_FILE_PATH_KEY); return saveResponseToFileTypeBuilder() .withActive(isActive) .withPath(path) .build(); } private ForwardResponseToServiceType buildForwardResponse(Map params) { var isActive = booleanOrNull(params.get(ACTIVE_KEY)); var mzsClientParams = filterMapByPrefix(params, MZS_CLIENT_KEY); ClientType mzsClient = mzsClientParams.isEmpty() ? null : buildClient(mzsClientParams); return forwardResponseToServiceTypeBuilder() .withActive(isActive) .withMzsClient(mzsClient) .build(); } private Boolean booleanOrNull(String value) { return value == null ? null : Boolean.getBoolean(value); } private BigInteger bigIntOrNull(String value) { return value == null ? null : new BigInteger(value); } /** * Combine properties of two Configs; {@code primary} overrides {@code fallback}. * * @param primary * @param fallback * @return Merged Config */ public ConfigType merge(ConfigType primary, ConfigType fallback) { var builder = configTypeBuilder(fallback); if (primary.isPerformQueryPersonRequest() != null) { builder.withPerformQueryPersonRequest(primary.isPerformQueryPersonRequest()); } if (primary.getMSGClient() != null) { builder.withMSGClient(merge(primary.getMSGClient(), fallback.getMSGClient())); } if (primary.getTNVZClient() != null) { builder.withMSGClient(merge(primary.getTNVZClient(), fallback.getTNVZClient())); } if (primary.getMsgResponseSinks() != null) { builder.withMsgResponseSinks(merge(primary.getMsgResponseSinks(), fallback.getMsgResponseSinks())); } if (primary.getServiceTimeout() != null) { builder.withServiceTimeout(primary.getServiceTimeout()); } return builder.build(); } private ClientType merge(ClientType primary, ClientType fallback) { if (fallback == null) { return primary; } var builder = clientTypeBuilder(fallback); if (primary.getURL() != null) { builder.withURL(primary.getURL()); } if (primary.getSSL() != null) { builder.withSSL(merge(primary.getSSL(), fallback.getSSL())); } if (primary.getConnectionTimeout() != null) { builder.withConnectionTimeout(primary.getConnectionTimeout()); } if (primary.getReceiveTimeout() != null) { builder.withConnectionTimeout(primary.getReceiveTimeout()); } return builder.build(); } private SSLType merge(SSLType primary, SSLType fallback) { if (fallback == null) { return primary; } var builder = SSLTypeBuilder(fallback); if (primary.getKeyStore() != null) { builder.withKeyStore(merge(primary.getKeyStore(), fallback.getKeyStore())); } if (primary.getTrustStore() != null) { builder.withKeyStore(merge(primary.getTrustStore(), fallback.getTrustStore())); } if (primary.isLaxHostNameVerification() != null) { builder.withLaxHostNameVerification(primary.isLaxHostNameVerification()); } if (primary.isTrustAll() != null) { builder.withLaxHostNameVerification(primary.isTrustAll()); } return builder.build(); } private KeyStoreType merge(KeyStoreType primary, KeyStoreType fallback) { if (primary.getFileName() != null && primary.getFileType() != null && primary.getPassword() != null) return primary; return fallback; } private MsgResponseSinksType merge(MsgResponseSinksType primary, MsgResponseSinksType fallback) { if (fallback == null) { return primary; } var builder = msgResponseSinksTypeBuilder(fallback); if (primary.isLogResponse() != null) { builder.withLogResponse(primary.isLogResponse()); } if (primary.getSaveResponseToFile() != null) { builder.withSaveResponseToFile(merge(primary.getSaveResponseToFile(), fallback.getSaveResponseToFile())); } if (primary.getForwardResponseToService() != null) { builder.withForwardResponseToService(merge(primary.getForwardResponseToService(), fallback.getForwardResponseToService())); } return builder.build(); } private SaveResponseToFileType merge(SaveResponseToFileType primary, SaveResponseToFileType fallback) { if (fallback == null) return primary; var builder = saveResponseToFileTypeBuilder(fallback); if (primary.isActive() != null) { builder.withActive(primary.isActive()); } if (primary.getPath() != null) { builder.withPath(primary.getPath()); } return builder.build(); } private ForwardResponseToServiceType merge(ForwardResponseToServiceType primary, ForwardResponseToServiceType fallback) { if (fallback == null) return primary; var builder = forwardResponseToServiceTypeBuilder(fallback); if (primary.isActive() != null) { builder.withActive(primary.isActive()); } if (primary.getMzsClient() != null) { builder.withMzsClient(merge(primary.getMzsClient(), fallback.getMzsClient())); } return builder.build(); } }