From aecd14427d7fd7d54eb2894499d312414cbdb2f6 Mon Sep 17 00:00:00 2001 From: Giuseppe Baittiner Date: Thu, 7 Dec 2023 15:39:19 +0100 Subject: [PATCH] fix: refactor props --- .../statusmanager/client/IConfigClient.java | 9 +- .../client/impl/ConfigClient.java | 64 +++------- .../client/routes/ConfigClientRoutes.java | 20 +-- .../client/routes/base/ClientRoutes.java | 6 +- .../gtw/statusmanager/dto/ConfigItemDTO.java | 26 ++++ .../enums/ConfigItemTypeEnum.java | 6 + .../statusmanager/service/impl/ConfigSRV.java | 117 +++++++----------- 7 files changed, 113 insertions(+), 135 deletions(-) create mode 100644 src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/dto/ConfigItemDTO.java create mode 100644 src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/enums/ConfigItemTypeEnum.java diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/client/IConfigClient.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/client/IConfigClient.java index b01ff32..eb91561 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/client/IConfigClient.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/client/IConfigClient.java @@ -11,9 +11,10 @@ */ package it.finanze.sanita.fse2.ms.gtw.statusmanager.client; -public interface IConfigClient { +import it.finanze.sanita.fse2.ms.gtw.statusmanager.dto.ConfigItemDTO; +import it.finanze.sanita.fse2.ms.gtw.statusmanager.enums.ConfigItemTypeEnum; - Integer getExpirationDate(); - Boolean isCfOnIssuerAllowed(); - Boolean isSubjectPersistenceEnabled(); +public interface IConfigClient { + ConfigItemDTO getConfigurationItems(ConfigItemTypeEnum type); + String getProps(ConfigItemTypeEnum type, String props, String previous); } diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/client/impl/ConfigClient.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/client/impl/ConfigClient.java index 9f70fea..8de7377 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/client/impl/ConfigClient.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/client/impl/ConfigClient.java @@ -13,15 +13,14 @@ import it.finanze.sanita.fse2.ms.gtw.statusmanager.client.IConfigClient; import it.finanze.sanita.fse2.ms.gtw.statusmanager.client.routes.ConfigClientRoutes; +import it.finanze.sanita.fse2.ms.gtw.statusmanager.dto.ConfigItemDTO; +import it.finanze.sanita.fse2.ms.gtw.statusmanager.enums.ConfigItemTypeEnum; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.client.ResourceAccessException; import org.springframework.web.client.RestTemplate; -import static it.finanze.sanita.fse2.ms.gtw.statusmanager.client.routes.base.ClientRoutes.Config.*; - @Slf4j @Component public class ConfigClient implements IConfigClient { @@ -32,49 +31,6 @@ public class ConfigClient implements IConfigClient { @Autowired private ConfigClientRoutes routes; - @Override - public Integer getExpirationDate() { - int output = 5; - - String endpoint = routes.getStatusManagerConfig(PROPS_NAME_EXP_DAYS); - log.debug("{} - Executing request: {}", routes.identifier(), endpoint); - - if(isReachable()) { - ResponseEntity response = client.getForEntity(endpoint, String.class); - if(response.getBody() != null) output = Integer.parseInt(response.getBody()); - } - - return output; - } - - @Override - public Boolean isSubjectPersistenceEnabled() { - boolean output = false; - String endpoint = routes.getStatusManagerConfig(PROPS_NAME_SUBJECT); - log.debug("{} - Executing request: {}", routes.identifier(), endpoint); - - if (isReachable()){ - ResponseEntity response = client.getForEntity(endpoint, String.class); - if (response.getBody() != null) output = Boolean.parseBoolean(response.getBody()); - } - - return output; - } - - @Override - public Boolean isCfOnIssuerAllowed() { - boolean output = false; - String endpoint = routes.getStatusManagerConfig(PROPS_NAME_ISSUER_CF); - log.debug("{} - Executing request: {}", routes.identifier(), endpoint); - - if(isReachable()) { - ResponseEntity response = client.getForEntity(endpoint, String.class); - if(response.getBody() != null) output = Boolean.parseBoolean(response.getBody()); - } - - return output; - } - private boolean isReachable() { boolean out; try { @@ -85,4 +41,20 @@ private boolean isReachable() { } return out; } + + @Override + public ConfigItemDTO getConfigurationItems(ConfigItemTypeEnum type) { + return client.getForObject(routes.getConfigItems(type), ConfigItemDTO.class); + } + + @Override + public String getProps(ConfigItemTypeEnum type, String props, String previous) { + String out = previous; + String endpoint = routes.getConfigItem(type, props); + if (isReachable()) out = client.getForObject(endpoint, String.class); + if(out == null || !out.equals(previous)) { + log.info("[GTW-CFG] Property {} is set as {} (previously: {})", props, out, previous); + } + return out; + } } diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/client/routes/ConfigClientRoutes.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/client/routes/ConfigClientRoutes.java index a2f643b..30c8c5d 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/client/routes/ConfigClientRoutes.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/client/routes/ConfigClientRoutes.java @@ -11,6 +11,7 @@ */ package it.finanze.sanita.fse2.ms.gtw.statusmanager.client.routes; +import it.finanze.sanita.fse2.ms.gtw.statusmanager.enums.ConfigItemTypeEnum; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.web.util.UriComponentsBuilder; @@ -43,21 +44,22 @@ public String status() { .toUriString(); } - public String getStatusManagerConfig(String props) { - return getConfigItemByName(props, QP_TYPE_STATUS_MANAGER); - } - - public String getGenericProps(String props) { - return getConfigItemByName(props, QP_TYPE_GENERIC); - } - private String getConfigItemByName(String props, String type) { + public String getConfigItem(ConfigItemTypeEnum type, String props) { return base() .pathSegment(API_VERSION, API_CONFIG_ITEMS, API_PROPS) - .queryParam(QP_TYPE, type) + .queryParam(QP_TYPE, type.name()) .queryParam(QP_PROPS, props) .build() .toUriString(); } + public String getConfigItems(ConfigItemTypeEnum type) { + return base() + .pathSegment(API_VERSION, API_CONFIG_ITEMS) + .queryParam(QP_TYPE, type.name()) + .build() + .toUriString(); + } + } diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/client/routes/base/ClientRoutes.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/client/routes/base/ClientRoutes.java index 290bbaf..9627972 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/client/routes/base/ClientRoutes.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/client/routes/base/ClientRoutes.java @@ -47,13 +47,9 @@ public static final class Config { // QP public static final String QP_TYPE = "type"; public static final String QP_PROPS = "props"; - public static final String QP_TYPE_GENERIC = "GENERIC"; - public static final String QP_TYPE_STATUS_MANAGER = "STATUS_MANAGER"; // VALUES - public static final String PROPS_NAME_EXP_DAYS = "expiring_date_day"; + public static final String PROPS_NAME_EXP_DAYS = "expiring-date-day"; public static final String PROPS_NAME_ISSUER_CF = "issuer-cf-cleaning"; public static final String PROPS_NAME_SUBJECT = "subject-persistence"; - } - } diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/dto/ConfigItemDTO.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/dto/ConfigItemDTO.java new file mode 100644 index 0000000..21cd2d3 --- /dev/null +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/dto/ConfigItemDTO.java @@ -0,0 +1,26 @@ +package it.finanze.sanita.fse2.ms.gtw.statusmanager.dto; + +import lombok.Getter; +import lombok.Setter; + +import java.util.List; +import java.util.Map; + +@Getter +@Setter +public class ConfigItemDTO { + + private String traceId; + private String spanId; + + private List configurationItems; + + @Getter + @Setter + public static class ConfigDataItemDTO { + private String key; + private Map items; + } + +} + diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/enums/ConfigItemTypeEnum.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/enums/ConfigItemTypeEnum.java new file mode 100644 index 0000000..57da3f5 --- /dev/null +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/enums/ConfigItemTypeEnum.java @@ -0,0 +1,6 @@ +package it.finanze.sanita.fse2.ms.gtw.statusmanager.enums; + +public enum ConfigItemTypeEnum { + GENERIC, + STATUS_MANAGER +} diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/service/impl/ConfigSRV.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/service/impl/ConfigSRV.java index f969f7d..b8a08d0 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/service/impl/ConfigSRV.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/service/impl/ConfigSRV.java @@ -12,6 +12,8 @@ package it.finanze.sanita.fse2.ms.gtw.statusmanager.service.impl; import it.finanze.sanita.fse2.ms.gtw.statusmanager.client.IConfigClient; +import it.finanze.sanita.fse2.ms.gtw.statusmanager.dto.ConfigItemDTO; +import it.finanze.sanita.fse2.ms.gtw.statusmanager.enums.ConfigItemTypeEnum; import it.finanze.sanita.fse2.ms.gtw.statusmanager.service.IConfigSRV; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.tuple.Pair; @@ -22,9 +24,12 @@ import java.util.Date; import java.util.HashMap; +import java.util.List; import java.util.Map; import static it.finanze.sanita.fse2.ms.gtw.statusmanager.client.routes.base.ClientRoutes.Config.*; +import static it.finanze.sanita.fse2.ms.gtw.statusmanager.dto.ConfigItemDTO.*; +import static it.finanze.sanita.fse2.ms.gtw.statusmanager.enums.ConfigItemTypeEnum.STATUS_MANAGER; @Slf4j @Service @@ -32,108 +37,78 @@ public class ConfigSRV implements IConfigSRV { private static final Long DELTA_MS = 300000L; - private final Map> props; - @Autowired private IConfigClient client; + private final Map> props; + public ConfigSRV() { this.props = new HashMap<>(); } @EventListener(ApplicationStartedEvent.class) void initialize() { - refreshExpirationDate(); - refreshIsCfOnIssuerAllowed(); - refreshIsSubjectPersistenceEnabled(); - runningConfiguration(); - } - - private void refreshExpirationDate() { - int days = client.getExpirationDate(); - props.put(PROPS_NAME_EXP_DAYS, Pair.of(new Date().getTime(), days)); - } - - private void refreshIsSubjectPersistenceEnabled(){ - boolean out = client.isSubjectPersistenceEnabled(); - props.put(PROPS_NAME_SUBJECT, Pair.of(new Date().getTime(), out)); - } - - private void refreshIsCfOnIssuerAllowed() { - boolean out = client.isCfOnIssuerAllowed(); - props.put(PROPS_NAME_ISSUER_CF, Pair.of(new Date().getTime(), out)); + for(ConfigItemTypeEnum en : ConfigItemTypeEnum.values()) { + log.info("[GTW-CFG] Retrieving {} properties ...", en.name()); + ConfigItemDTO items = client.getConfigurationItems(en); + List opts = items.getConfigurationItems(); + for(ConfigDataItemDTO opt : opts) { + opt.getItems().forEach((key, value) -> { + log.info("[GTW-CFG] Property {} is set as {}", key, value); + props.put(key, Pair.of(new Date().getTime(), value)); + }); + } + } } @Override public Integer getExpirationDate() { - Pair pair = props.getOrDefault( - PROPS_NAME_EXP_DAYS, - Pair.of(0L, null) - ); - if (new Date().getTime() - pair.getKey() >= DELTA_MS) { + long lastUpdate = props.get(PROPS_NAME_EXP_DAYS).getKey(); + if (new Date().getTime() - lastUpdate >= DELTA_MS) { synchronized(PROPS_NAME_EXP_DAYS) { - refreshExpirationDate(); - verifyExpirationDate(pair); + if (new Date().getTime() - lastUpdate >= DELTA_MS) { + refresh(STATUS_MANAGER, PROPS_NAME_EXP_DAYS); + } } } - return (Integer) props.get(PROPS_NAME_EXP_DAYS).getValue(); + return Integer.parseInt( + props.get(PROPS_NAME_EXP_DAYS).getValue() + ); } @Override public Boolean isSubjectPersistenceEnabled() { - Pair pair = props.getOrDefault( - PROPS_NAME_SUBJECT, - Pair.of(0L, null) - ); - if (new Date().getTime() - pair.getKey() >= DELTA_MS) { + long lastUpdate = props.get(PROPS_NAME_SUBJECT).getKey(); + if (new Date().getTime() - lastUpdate >= DELTA_MS) { synchronized (PROPS_NAME_SUBJECT) { - refreshIsSubjectPersistenceEnabled(); - verifyIsSubjectPersistenceEnabled(pair); + if (new Date().getTime() - lastUpdate >= DELTA_MS) { + refresh(STATUS_MANAGER, PROPS_NAME_SUBJECT); + } } } - return (Boolean) props.get(PROPS_NAME_SUBJECT).getValue(); + return Boolean.parseBoolean( + props.get(PROPS_NAME_SUBJECT).getValue() + ); } @Override public Boolean isCfOnIssuerNotAllowed() { - Pair pair = props.getOrDefault( - PROPS_NAME_ISSUER_CF, - Pair.of(0L, null) - ); - if (new Date().getTime() - pair.getKey() >= DELTA_MS) { + long lastUpdate = props.get(PROPS_NAME_ISSUER_CF).getKey(); + if (new Date().getTime() - lastUpdate >= DELTA_MS) { synchronized(PROPS_NAME_ISSUER_CF) { - refreshIsCfOnIssuerAllowed(); - verifyIsCfOnIssuerAllowed(pair); + if (new Date().getTime() - lastUpdate >= DELTA_MS) { + refresh(STATUS_MANAGER, PROPS_NAME_ISSUER_CF); + } } } - return (Boolean) props.get(PROPS_NAME_ISSUER_CF).getValue(); - } - - private void runningConfiguration() { - props.forEach((id, pair) -> log.info("[GTW-CONFIG] key: {} | value: {}", id, pair.getValue())); - } - - private void verifyExpirationDate(Pair pair) { - int previous = (int) pair.getValue(); - int current = (int) props.get(PROPS_NAME_EXP_DAYS).getValue(); - if(previous != current) { - log.info("[GTW-CONFIG][UPDATE] key: {} | value: {} (previous: {})", PROPS_NAME_EXP_DAYS, current, previous); - } - } - - private void verifyIsSubjectPersistenceEnabled(Pair pair){ - Boolean previous = (Boolean) pair.getValue(); - boolean current = (boolean) props.get(PROPS_NAME_SUBJECT).getValue(); - if (previous != current){ - log.info("[GTW-CONFIG][UPDATE] key: {} | value: {} (previous: {})", PROPS_NAME_SUBJECT, current, previous); - } + return Boolean.parseBoolean( + props.get(PROPS_NAME_ISSUER_CF).getValue() + ); } - private void verifyIsCfOnIssuerAllowed(Pair pair) { - Boolean previous = (Boolean) pair.getValue(); - boolean current = (boolean) props.get(PROPS_NAME_ISSUER_CF).getValue(); - if(previous != current) { - log.info("[GTW-CONFIG][UPDATE] key: {} | value: {} (previous: {})", PROPS_NAME_ISSUER_CF, current, previous); - } + private void refresh(ConfigItemTypeEnum type, String name) { + String previous = props.getOrDefault(name, Pair.of(0L, null)).getValue(); + String prop = client.getProps(type, name, previous); + props.put(name, Pair.of(new Date().getTime(), prop)); } }